WIN32 update (this version is known not to work on Windows).
[idzebra-moved-to-github.git] / util / zebra-lock.c
1
2 #include <assert.h>
3
4 #include <zebra-lock.h>
5
6 int zebra_mutex_init (Zebra_mutex *p)
7 {
8 #if HAVE_PTHREAD_H
9     pthread_mutex_init (&p->mutex, 0);
10 #endif
11 #ifdef WIN32
12     InitializeCriticalSection (&p->mutex);
13 #endif
14     return 0;
15 }
16
17 int zebra_mutex_destroy (Zebra_mutex *p)
18 {
19 #if HAVE_PTHREAD_H
20     pthread_mutex_destroy (&p->mutex);
21 #endif
22 #ifdef WIN32
23     DeleteCriticalSection (&p->mutex);
24 #endif
25     return 0;
26 }
27
28 int zebra_mutex_lock (Zebra_mutex *p)
29 {
30 #if HAVE_PTHREAD_H
31     pthread_mutex_lock (&p->mutex);
32 #endif
33 #ifdef WIN32
34     EnterCriticalSection (&p->mutex);
35 #endif
36     return 0;
37 }
38
39 int zebra_mutex_unlock (Zebra_mutex *p)
40 {
41 #if HAVE_PTHREAD_H
42     pthread_mutex_unlock (&p->mutex);
43 #endif
44 #ifdef WIN32
45     LeaveCriticalSection (&p->mutex);
46 #endif
47     return 0;
48 }
49
50 int zebra_lock_rdwr_init (Zebra_lock_rdwr *p)
51 {
52     p->readers_reading = 0;
53     p->writers_writing = 0;
54 #if HAVE_PTHREAD_H
55     pthread_mutex_init (&p->mutex, 0);
56     pthread_cond_init (&p->lock_free, 0);
57 #endif
58     return 0;
59 }
60
61 int zebra_lock_rdwr_destroy (Zebra_lock_rdwr *p)
62 {
63     assert (p->readers_reading == 0);
64     assert (p->writers_writing == 0);
65 #if HAVE_PTHREAD_H
66     pthread_mutex_destroy (&p->mutex);
67     pthread_cond_destroy (&p->lock_free);
68 #endif
69     return 0;
70 }
71
72 int zebra_lock_rdwr_rlock (Zebra_lock_rdwr *p)
73 {
74 #if HAVE_PTHREAD_H
75     pthread_mutex_lock (& p->mutex);
76     while (p->writers_writing)
77         pthread_cond_wait (&p->lock_free, &p->mutex);
78     p->readers_reading++;
79     pthread_mutex_unlock(&p->mutex);
80 #endif
81     return 0;
82 }
83
84 int zebra_lock_rdwr_wlock (Zebra_lock_rdwr *p)
85 {
86 #if HAVE_PTHREAD_H
87     pthread_mutex_lock (&p->mutex);
88     while (p->writers_writing || p->readers_reading)
89         pthread_cond_wait (&p->lock_free, &p->mutex);
90     p->writers_writing++;
91     pthread_mutex_unlock (&p->mutex);
92 #endif
93     return 0;
94 }
95
96 int zebra_lock_rdwr_runlock (Zebra_lock_rdwr *p)
97 {
98 #if HAVE_PTHREAD_H
99     pthread_mutex_lock (&p->mutex);
100     if (p->readers_reading == 0)
101     {
102         pthread_mutex_unlock (&p->mutex);
103         return -1;
104     } 
105     else
106     {
107         p->readers_reading--;
108         if (p->readers_reading == 0)
109             pthread_cond_signal (&p->lock_free);
110         pthread_mutex_unlock (&p->mutex);
111     }
112 #endif
113     return 0;
114 }
115
116 int zebra_lock_rdwr_wunlock (Zebra_lock_rdwr *p)
117 {
118 #if HAVE_PTHREAD_H
119     pthread_mutex_lock (&p->mutex);
120     if (p->writers_writing == 0)
121     {
122         pthread_mutex_unlock (&p->mutex);
123         return -1;
124     }
125     else
126     {
127         p->writers_writing--;
128         pthread_cond_broadcast(&p->lock_free);
129         pthread_mutex_unlock(&p->mutex);
130     }
131 #endif
132     return 0;
133 }
134
135 int zebra_mutex_cond_init (Zebra_mutex_cond *p)
136 {
137 #if HAVE_PTHREAD_H
138     pthread_cond_init (&p->cond, 0);
139     pthread_mutex_init (&p->mutex, 0);
140 #endif
141     return 0;
142 }
143
144 int zebra_mutex_cond_destroy (Zebra_mutex_cond *p)
145 {
146 #if HAVE_PTHREAD_H
147     pthread_cond_destroy (&p->cond);
148     pthread_mutex_destroy (&p->mutex);
149 #endif
150     return 0;
151 }
152
153 int zebra_mutex_cond_lock (Zebra_mutex_cond *p)
154 {
155 #if HAVE_PTHREAD_H
156     return pthread_mutex_lock (&p->mutex);
157 #else
158     return 0;
159 #endif
160 }
161
162 int zebra_mutex_cond_unlock (Zebra_mutex_cond *p)
163 {
164 #if HAVE_PTHREAD_H
165     return pthread_mutex_unlock (&p->mutex);
166 #else
167     return 0;
168 #endif
169 }
170
171 int zebra_mutex_cond_wait (Zebra_mutex_cond *p)
172 {
173 #if HAVE_PTHREAD_H
174     return pthread_cond_wait (&p->cond, &p->mutex);
175 #else
176     return 0;
177 #endif
178 }
179
180 int zebra_mutex_cond_signal (Zebra_mutex_cond *p)
181 {
182 #if HAVE_PTHREAD_H
183     return pthread_cond_signal (&p->cond);
184 #else
185     return 0;
186 #endif
187 }