clamp progress to 0..end, add possibility to receive whole lines only in processOutpu...
[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()
87         {
88                 pthread_mutex_init(&m_lock, 0);
89         }
90         ~eSingleLock()
91         {
92                 pthread_mutex_destroy(&m_lock);
93         }
94 };
95
96 class eSingleLocker
97 {
98         eSingleLock &m_lock;
99 public:
100         eSingleLocker(eSingleLock &m)
101                 : m_lock(m)
102         {
103                 pthread_mutex_lock(&m_lock.m_lock);
104         }
105         ~eSingleLocker()
106         {
107                 pthread_mutex_unlock(&m_lock.m_lock);
108         }
109 };
110
111 class eLock
112 {
113         pthread_mutex_t mutex;
114         pthread_cond_t cond;
115
116         int pid;
117         int counter, max;
118 public:
119         void lock(int res=100);
120         void unlock(int res=100);
121
122         eLock(int max=100);
123         ~eLock();
124 };
125
126 class eLocker
127 {
128         eLock &lock;
129         int res;
130 public:
131         eLocker(eLock &lock, int res=100);
132         ~eLocker();
133 };
134
135 class eSemaphore
136 {
137         int v;
138         pthread_mutex_t mutex;
139         pthread_cond_t cond;
140 public:
141         eSemaphore();
142         ~eSemaphore();
143         
144         int down();
145         int decrement();
146         int up();
147         int value();
148 };
149
150 #endif