4.2.7r1
[enigma2.git] / usr / include / enigma2 / lib / base / elock.h
1 #ifndef __elock_h
2 #define __elock_h
3
4 #include <pthread.h>
5
6 class singleLock
7 {
8         pthread_mutex_t &lock;
9 public:
10         singleLock(pthread_mutex_t &m ) __attribute__ ((always_inline))
11                 :lock(m)
12         {
13                 pthread_mutex_lock(&lock);
14         }
15         ~singleLock() __attribute__ ((always_inline))
16         {
17                 pthread_mutex_unlock(&lock);
18         }
19 };
20
21 class eRdWrLock
22 {
23         friend class eRdLocker;
24         friend class eWrLocker;
25         pthread_rwlock_t m_lock;
26         eRdWrLock(eRdWrLock &);
27 public:
28         eRdWrLock() __attribute__ ((always_inline))
29         {
30                 pthread_rwlock_init(&m_lock, 0);
31         }
32         ~eRdWrLock() __attribute__ ((always_inline))
33         {
34                 pthread_rwlock_destroy(&m_lock);
35         }
36         void RdLock() __attribute__ ((always_inline))
37         {
38                 pthread_rwlock_rdlock(&m_lock);
39         }
40         void WrLock() __attribute__ ((always_inline))
41         {
42                 pthread_rwlock_wrlock(&m_lock);
43         }
44         void Unlock() __attribute__ ((always_inline))
45         {
46                 pthread_rwlock_unlock(&m_lock);
47         }
48 };
49
50 class eRdLocker
51 {
52         eRdWrLock &m_lock;
53 public:
54         eRdLocker(eRdWrLock &m) __attribute__ ((always_inline))
55                 : m_lock(m)
56         {
57                 pthread_rwlock_rdlock(&m_lock.m_lock);
58         }
59         ~eRdLocker() __attribute__ ((always_inline))
60         {
61                 pthread_rwlock_unlock(&m_lock.m_lock);
62         }
63 };
64
65 class eWrLocker
66 {
67         eRdWrLock &m_lock;
68 public:
69         eWrLocker(eRdWrLock &m) __attribute__ ((always_inline))
70                 : m_lock(m)
71         {
72                 pthread_rwlock_wrlock(&m_lock.m_lock);
73         }
74         ~eWrLocker() __attribute__ ((always_inline))
75         {
76                 pthread_rwlock_unlock(&m_lock.m_lock);
77         }
78 };
79
80 /* FIXME: rename to eMutex */
81 class eSingleLock
82 {
83         pthread_mutex_t m_lock;
84 public:
85         eSingleLock(bool recursive=false)
86         {
87                 if (recursive)
88                 {
89                         pthread_mutexattr_t attr;
90                         pthread_mutexattr_init(&attr);
91                         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
92                         pthread_mutex_init(&m_lock, &attr);
93                         pthread_mutexattr_destroy(&attr);
94                 }
95                 else
96                         pthread_mutex_init(&m_lock, 0);
97         }
98         ~eSingleLock() __attribute__ ((always_inline))
99         {
100                 pthread_mutex_destroy(&m_lock);
101         }
102
103         void lock() __attribute__ ((always_inline))
104         {
105                 pthread_mutex_lock(&m_lock);
106         }
107         bool tryLock() __attribute__ ((always_inline))
108         {
109                 return pthread_mutex_trylock(&m_lock) == 0;
110         }
111         void unlock() __attribute__ ((always_inline))
112         {
113                 pthread_mutex_unlock(&m_lock);
114         }
115 };
116
117 class eSingleLocker
118 {
119         eSingleLock &m_lock;
120 public:
121         eSingleLocker(eSingleLock &m) __attribute__ ((always_inline))
122                 : m_lock(m)
123         {
124                 m_lock.lock();
125         }
126         ~eSingleLocker() __attribute__ ((always_inline))
127         {
128                 m_lock.unlock();
129         }
130 };
131
132 class eLock
133 {
134         pthread_mutex_t m_mutex;
135         pthread_cond_t m_cond;
136
137         int m_counter, m_max;
138 public:
139         void lock(int res);
140         void unlock(int res);
141         int lock_count();
142         int trylock(int res, bool force=false);
143         int counter() const { return m_counter; }
144         int max() const { return m_max; }
145
146         eLock(int max);
147         ~eLock();
148 };
149
150 class eSemaphore
151 {
152         int v;
153         pthread_mutex_t mutex;
154         pthread_cond_t cond;
155 public:
156         eSemaphore();
157         ~eSemaphore();
158         
159         int down();
160         int decrement();
161         int up();
162         int value();
163 };
164
165 #endif