From 125cbcd661670ca0e8e31e164e874bd05510163e Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 27 Aug 2017 14:58:23 +0200 Subject: [PATCH] use own reentrant mutex implementation --- src/lib/ebus/filereader.h | 10 +++++----- src/lib/ebus/message.h | 4 ---- src/lib/utils/thread.cpp | 21 +++++++++++++++++++++ src/lib/utils/thread.h | 31 +++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/lib/ebus/filereader.h b/src/lib/ebus/filereader.h index 433eb68d..10c2ff56 100644 --- a/src/lib/ebus/filereader.h +++ b/src/lib/ebus/filereader.h @@ -24,9 +24,9 @@ #include #include #include -#include #include "lib/ebus/symbol.h" #include "lib/ebus/result.h" +#include "lib/utils/thread.h" namespace ebusd { @@ -44,7 +44,6 @@ using std::string; using std::map; using std::ostream; using std::istream; -using std::mutex; /** the separator character used between fields. */ #define FIELD_SEPARATOR ',' @@ -275,6 +274,10 @@ class MappedFileReader : public FileReader { */ static const string combineRow(const map& row); + protected: + /** a @a Mutex for access to defaults. */ + Mutex m_mutex; + private: /** whether this instance supports rows with defaults (starting with a star). */ const bool m_supportsDefaults; @@ -282,9 +285,6 @@ class MappedFileReader : public FileReader { /** the preferred language code (up to 2 characters), or empty. */ const string m_preferLanguage; - /** a @a mutex for access to defaults. */ - mutex m_mutex; - /** the name of each column. */ vector m_columnNames; diff --git a/src/lib/ebus/message.h b/src/lib/ebus/message.h index 7fca7540..7c0e9484 100644 --- a/src/lib/ebus/message.h +++ b/src/lib/ebus/message.h @@ -26,7 +26,6 @@ #include #include #include -#include #include "lib/ebus/data.h" #include "lib/ebus/result.h" #include "lib/ebus/symbol.h" @@ -1556,9 +1555,6 @@ class MessageMap : public MappedFileReader { /** additional attributes by circuit name. */ map m_circuitData; - - /** a @a mutex for locking out changes. */ - mutex m_mutex; }; } // namespace ebusd diff --git a/src/lib/utils/thread.cpp b/src/lib/utils/thread.cpp index bc428d8c..f615eda4 100644 --- a/src/lib/utils/thread.cpp +++ b/src/lib/utils/thread.cpp @@ -105,4 +105,25 @@ bool WaitThread::Wait(int seconds) { return isRunning(); } + +Mutex::Mutex() { + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&m_mutex, &attr); + pthread_mutexattr_destroy(&attr); +} + +Mutex::~Mutex() { + pthread_mutex_destroy(&m_mutex); +} + +void Mutex::lock() { + pthread_mutex_lock(&m_mutex); +} + +void Mutex::unlock() { + pthread_mutex_unlock(&m_mutex); +} + } // namespace ebusd diff --git a/src/lib/utils/thread.h b/src/lib/utils/thread.h index f0f7d56e..ce58eefe 100644 --- a/src/lib/utils/thread.h +++ b/src/lib/utils/thread.h @@ -142,6 +142,37 @@ class WaitThread : public Thread { pthread_cond_t m_cond; }; + +/** + * A simple mutex. + */ +class Mutex { + public: + /** + * Constructor. + */ + Mutex(); + + /** + * Destructor. + */ + virtual ~Mutex(); + + /** + * Lock this mutex. + */ + void lock(); + + /** + * Unlock this mutex. + */ + void unlock(); + + private: + /** the mutex for waiting. */ + pthread_mutex_t m_mutex; +}; + } // namespace ebusd #endif // LIB_UTILS_THREAD_H_