X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/d63d2c3c6cbbd574dda4f8b00ebe6c661735edd5..5e05afa7dbc5acfc92a158dcc88b0be12c52af62:/lib/base/elock.h diff --git a/lib/base/elock.h b/lib/base/elock.h index 6a47f8c9..01757182 100644 --- a/lib/base/elock.h +++ b/lib/base/elock.h @@ -7,7 +7,7 @@ class singleLock { pthread_mutex_t &lock; public: - singleLock( pthread_mutex_t &m ) + singleLock(pthread_mutex_t &m ) :lock(m) { pthread_mutex_lock(&lock); @@ -18,6 +18,105 @@ public: } }; +class eRdWrLock +{ + friend class eRdLocker; + friend class eWrLocker; + pthread_rwlock_t m_lock; + eRdWrLock(eRdWrLock &); +public: + eRdWrLock() + { + pthread_rwlock_init(&m_lock, 0); + } + ~eRdWrLock() + { + pthread_rwlock_destroy(&m_lock); + } + void RdLock() + { + pthread_rwlock_rdlock(&m_lock); + } + void WrLock() + { + pthread_rwlock_wrlock(&m_lock); + } + void Unlock() + { + pthread_rwlock_unlock(&m_lock); + } +}; + +class eRdLocker +{ + eRdWrLock &m_lock; +public: + eRdLocker(eRdWrLock &m) + : m_lock(m) + { + pthread_rwlock_rdlock(&m_lock.m_lock); + } + ~eRdLocker() + { + pthread_rwlock_unlock(&m_lock.m_lock); + } +}; + +class eWrLocker +{ + eRdWrLock &m_lock; +public: + eWrLocker(eRdWrLock &m) + : m_lock(m) + { + pthread_rwlock_wrlock(&m_lock.m_lock); + } + ~eWrLocker() + { + pthread_rwlock_unlock(&m_lock.m_lock); + } +}; + +class eSingleLock +{ + friend class eSingleLocker; + pthread_mutex_t m_lock; + eSingleLock(eSingleLock &); +public: + eSingleLock(bool recursive=false) + { + if (recursive) + { + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&m_lock, &attr); + pthread_mutexattr_destroy(&attr); + } + else + pthread_mutex_init(&m_lock, 0); + } + ~eSingleLock() + { + pthread_mutex_destroy(&m_lock); + } +}; + +class eSingleLocker +{ + eSingleLock &m_lock; +public: + eSingleLocker(eSingleLock &m) + : m_lock(m) + { + pthread_mutex_lock(&m_lock.m_lock); + } + ~eSingleLocker() + { + pthread_mutex_unlock(&m_lock.m_lock); + } +}; + class eLock { pthread_mutex_t mutex;