From 3d126cd9a7f9029e199e9220e2ffa08ac4e23227 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Bri=C3=A8re?= Date: Wed, 2 Oct 2019 22:13:56 -0400 Subject: Create t_rwmutex_guard base class (Doing this ahead of time to simplify the next commit a bit.) --- src/threads/mutex.cpp | 11 +++++++++-- src/threads/mutex.h | 15 ++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/threads/mutex.cpp b/src/threads/mutex.cpp index 9c6a762..7eaa329 100644 --- a/src/threads/mutex.cpp +++ b/src/threads/mutex.cpp @@ -129,7 +129,13 @@ void t_rwmutex::unlock() // t_rwmutex_guard /////////////////////////// -t_rwmutex_reader::t_rwmutex_reader(t_rwmutex& mutex) : _mutex(mutex) +t_rwmutex_guard::t_rwmutex_guard(t_rwmutex& mutex) : + _mutex(mutex) +{ +} + +t_rwmutex_reader::t_rwmutex_reader(t_rwmutex& mutex) : + t_rwmutex_guard(mutex) { // std::cout << "mtx rd lock " << (void*)&_mutex << std::endl; _mutex.lockRead(); @@ -141,7 +147,8 @@ t_rwmutex_reader::~t_rwmutex_reader() _mutex.unlock(); } -t_rwmutex_writer::t_rwmutex_writer(t_rwmutex& mutex) : _mutex(mutex) +t_rwmutex_writer::t_rwmutex_writer(t_rwmutex& mutex) : + t_rwmutex_guard(mutex) { // std::cout << "mtx wr lock " << (void*)&_mutex << std::endl; _mutex.lockWrite(); diff --git a/src/threads/mutex.h b/src/threads/mutex.h index 15d5181..d757d61 100644 --- a/src/threads/mutex.h +++ b/src/threads/mutex.h @@ -95,17 +95,22 @@ public: void unlock(); }; -class t_rwmutex_reader { -private: +// Base (abstract) class +class t_rwmutex_guard { +protected: t_rwmutex& _mutex; + + // A protected constructor to keep this class abstract + t_rwmutex_guard(t_rwmutex& mutex); +}; + +class t_rwmutex_reader : public t_rwmutex_guard { public: t_rwmutex_reader(t_rwmutex& mutex); ~t_rwmutex_reader(); }; -class t_rwmutex_writer { -private: - t_rwmutex& _mutex; +class t_rwmutex_writer : public t_rwmutex_guard { public: t_rwmutex_writer(t_rwmutex& mutex); ~t_rwmutex_writer(); -- cgit v1.2.3