diff options
author | Albert Astals Cid <aacid@kde.org> | 2013-04-06 17:01:08 +0200 |
---|---|---|
committer | Albert Astals Cid <aacid@kde.org> | 2013-04-06 17:01:08 +0200 |
commit | 837e3704e76ea857b3de45503840e9b855096fef (patch) | |
tree | 8249d907e9edd208509b01e1d6521c7eb2b793f9 /goo | |
parent | 0a243c8c14d09a40f25338999c3ca06273277b45 (diff) |
Make our mutexes recursive
Fixes a deadlock problem found with a pdf i can't share (411klaralv.pdf)
Reviewed by Thomas and Adam on the mailing list
Diffstat (limited to 'goo')
-rw-r--r-- | goo/GooMutex.h | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/goo/GooMutex.h b/goo/GooMutex.h index 591a7d44..4d425a29 100644 --- a/goo/GooMutex.h +++ b/goo/GooMutex.h @@ -53,28 +53,36 @@ typedef CRITICAL_SECTION GooMutex; #include <pthread.h> -typedef pthread_mutex_t GooMutex; - -#define gInitMutex(m) pthread_mutex_init(m, NULL) -#define gDestroyMutex(m) pthread_mutex_destroy(m) -#define gLockMutex(m) pthread_mutex_lock(m) -#define gUnlockMutex(m) pthread_mutex_unlock(m) +typedef struct { + pthread_mutexattr_t attributes; + pthread_mutex_t mutex; +} GooMutex; + +inline void gInitMutex(GooMutex *m) { + pthread_mutexattr_init(&m->attributes); + pthread_mutexattr_settype(&m->attributes, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&m->mutex, &m->attributes); +} +inline void gDestroyMutex(GooMutex *m) { + pthread_mutex_destroy(&m->mutex); + pthread_mutexattr_destroy(&m->attributes); +} +inline void gLockMutex(GooMutex *m) { + pthread_mutex_lock(&m->mutex); +} +inline void gUnlockMutex(GooMutex *m) { + pthread_mutex_unlock(&m->mutex); +} #endif -enum MutexLockMode { - DoNotLockMutex, // for conditional locks: do not lock - DoLockMutex // for conditional locks: do lock -}; - class MutexLocker { public: - MutexLocker(GooMutex *mutexA, MutexLockMode modeA = DoLockMutex) : mutex(mutexA), mode(modeA) { if (mode == DoLockMutex) gLockMutex(mutex); } - ~MutexLocker() { if (mode == DoLockMutex) gUnlockMutex(mutex); } + MutexLocker(GooMutex *mutexA) : mutex(mutexA) { gLockMutex(mutex); } + ~MutexLocker() { gUnlockMutex(mutex); } private: GooMutex *mutex; - const MutexLockMode mode; }; #endif |