fixes bug #467
[enigma2.git] / 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 )
11                 :lock(m)
12         {
13                 pthread_mutex_lock(&lock);
14         }
15         ~singleLock()
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()
29         {
30                 pthread_rwlock_init(&m_lock, 0);
31         }
32         ~eRdWrLock()
33         {
34                 pthread_rwlock_destroy(&m_lock);
35         }
36         void RdLock()
37         {
38                 pthread_rwlock_rdlock(&m_lock);
39         }
40         void WrLock()
41         {
42                 pthread_rwlock_wrlock(&m_lock);
43         }
44         void Unlock()
45         {
46                 pthread_rwlock_unlock(&m_lock);
47         }
48 };
49
50 class eRdLocker
51 {
52         eRdWrLock &m_lock;
53 public:
54         eRdLocker(eRdWrLock &m)
55                 : m_lock(m)
56         {
57                 pthread_rwlock_rdlock(&m_lock.m_lock);
58         }
59         ~eRdLocker()
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)
70                 : m_lock(m)
71         {
72                 pthread_rwlock_wrlock(&m_lock.m_lock);
73         }
74         ~eWrLocker()
75         {
76                 pthread_rwlock_unlock(&m_lock.m_lock);
77         }
78 };
79
80 class eSingleLock
81 {
82         friend class eSingleLocker;
83         pthread_mutex_t m_lock;
84         eSingleLock(eSingleLock &);
85 public:
86         eSingleLock(bool recursive=false)
87         {
88                 if (recursive)
89                 {
90                         pthread_mutexattr_t attr;
91                         pthread_mutexattr_init(&attr);
92                         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
93                         pthread_mutex_init(&m_lock, &attr);
94                         pthread_mutexattr_destroy(&attr);
95                 }
96                 else
97                         pthread_mutex_init(&m_lock, 0);
98         }
99         ~eSingleLock()
100         {
101                 pthread_mutex_destroy(&m_lock);
102         }
103 };
104
105 class eSingleLocker
106 {
107         eSingleLock &m_lock;
108 public:
109         eSingleLocker(eSingleLock &m)
110                 : m_lock(m)
111         {
112                 pthread_mutex_lock(&m_lock.m_lock);
113         }
114         ~eSingleLocker()
115         {
116                 pthread_mutex_unlock(&m_lock.m_lock);
117         }
118 };
119
120 class eLock
121 {
122         pthread_mutex_t mutex;
123         pthread_cond_t cond;
124
125         int pid;
126         int counter, max;
127 public:
128         void lock(int res=100);
129         void unlock(int res=100);
130
131         eLock(int max=100);
132         ~eLock();
133 };
134
135 class eLocker
136 {
137         eLock &lock;
138         int res;
139 public:
140         eLocker(eLock &lock, int res=100);
141         ~eLocker();
142 };
143
144 class eSemaphore
145 {
146         int v;
147         pthread_mutex_t mutex;
148         pthread_cond_t cond;
149 public:
150         eSemaphore();
151         ~eSemaphore();
152         
153         int down();
154         int decrement();
155         int up();
156         int value();
157 };
158
159 #endif