add slovene language, thanks to matej
[enigma2.git] / lib / base / elock.cpp
1 #include <lib/base/elock.h>
2 #include <unistd.h>
3
4 void eLock::lock(int res)
5 {
6         if (res>max)
7                 res=max;
8         pthread_mutex_lock(&mutex);
9         while ((counter+res)>max)
10                 pthread_cond_wait(&cond, &mutex);
11         counter+=res;
12         pthread_mutex_unlock(&mutex);
13 }
14
15 void eLock::unlock(int res)
16 {
17         if (res>max)
18                 res=max;
19         pthread_mutex_lock(&mutex);
20         counter-=res;
21         pthread_mutex_unlock(&mutex);
22         pthread_cond_signal(&cond);
23 }
24
25 eLock::eLock(int max): max(max)
26 {
27         pthread_mutex_init(&mutex, 0);
28         pthread_cond_init(&cond, 0);
29         counter=0;
30         pid=-1;
31 }
32
33 eLock::~eLock()
34 {
35         pthread_mutex_destroy(&mutex);
36         pthread_cond_destroy(&cond);
37 }
38
39 eLocker::eLocker(eLock &lock, int res): lock(lock), res(res)
40 {
41         lock.lock(res);
42 }
43
44 eLocker::~eLocker()
45 {
46         lock.unlock(res);
47 }
48
49 eSemaphore::eSemaphore()
50 {
51         v=1;
52         pthread_mutex_init(&mutex, 0);
53         pthread_cond_init(&cond, 0);
54 }
55
56 eSemaphore::~eSemaphore()
57 {
58         pthread_mutex_destroy(&mutex);
59         pthread_cond_destroy(&cond);
60 }
61
62 int eSemaphore::down()
63 {
64         int value_after_op;
65         pthread_mutex_lock(&mutex);
66         while (v<=0)
67                 pthread_cond_wait(&cond, &mutex);
68         v--;
69         value_after_op=v;
70         pthread_mutex_unlock(&mutex);
71         return value_after_op;
72 }
73
74 int eSemaphore::decrement()
75 {
76         int value_after_op;
77         pthread_mutex_lock(&mutex);
78         v--;
79         value_after_op=v;
80         pthread_mutex_unlock(&mutex);
81         pthread_cond_signal(&cond);
82         return value_after_op;
83 }
84
85 int eSemaphore::up()
86 {
87         int value_after_op;
88         pthread_mutex_lock(&mutex);
89         v++;
90         value_after_op=v;
91         pthread_mutex_unlock(&mutex);
92         pthread_cond_signal(&cond);
93         return value_after_op;
94 }
95
96 int eSemaphore::value()
97 {
98         int value_after_op;
99         pthread_mutex_lock(&mutex);
100         value_after_op=v;
101         pthread_mutex_unlock(&mutex);
102         return value_after_op;
103 }
104