elock.h: add possibility to create recursive mutexes
[enigma2.git] / lib / base / elock.h
index 6a47f8c..0175718 100644 (file)
@@ -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;