nmake: align with pazpar2 WRT icu/libxslt
[idzebra-moved-to-github.git] / util / zebra-lock.c
1 /* This file is part of the Zebra server.
2    Copyright (C) Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20
21
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <assert.h>
26 #include <stdio.h>
27
28 #include <zebra-lock.h>
29
30
31 int zebra_mutex_init (Zebra_mutex *p)
32 {
33     p->state = 1;
34 #if YAZ_POSIX_THREADS
35     pthread_mutex_init (&p->mutex, 0);
36 #endif
37 #ifdef WIN32
38     InitializeCriticalSection (&p->mutex);
39 #endif
40     return 0;
41 }
42
43 int zebra_mutex_destroy (Zebra_mutex *p)
44 {
45     --(p->state);
46     if (p->state != 0)
47     {
48         fprintf (stderr, "zebra_mutex_destroy. state = %d\n", p->state);
49     }
50 #if YAZ_POSIX_THREADS
51     pthread_mutex_destroy (&p->mutex);
52 #endif
53 #ifdef WIN32
54     DeleteCriticalSection (&p->mutex);
55 #endif
56     return 0;
57 }
58
59 int zebra_mutex_lock (Zebra_mutex *p)
60 {
61     if (p->state != 1)
62     {
63         fprintf (stderr, "zebra_mutex_lock. state = %d\n", p->state);
64     }
65 #if YAZ_POSIX_THREADS
66     pthread_mutex_lock (&p->mutex);
67 #endif
68 #ifdef WIN32
69     EnterCriticalSection (&p->mutex);
70 #endif
71     return 0;
72 }
73
74 int zebra_mutex_unlock (Zebra_mutex *p)
75 {
76     if (p->state != 1)
77     {
78         fprintf (stderr, "zebra_mutex_unlock. state = %d\n", p->state);
79     }
80 #if YAZ_POSIX_THREADS
81     pthread_mutex_unlock (&p->mutex);
82 #endif
83 #ifdef WIN32
84     LeaveCriticalSection (&p->mutex);
85 #endif
86     return 0;
87 }
88
89 int zebra_lock_rdwr_init (Zebra_lock_rdwr *p)
90 {
91     p->readers_reading = 0;
92     p->writers_writing = 0;
93 #if YAZ_POSIX_THREADS
94     pthread_mutex_init (&p->mutex, 0);
95     pthread_cond_init (&p->lock_free, 0);
96 #endif
97     return 0;
98 }
99
100 int zebra_lock_rdwr_destroy (Zebra_lock_rdwr *p)
101 {
102     assert (p->readers_reading == 0);
103     assert (p->writers_writing == 0);
104 #if YAZ_POSIX_THREADS
105     pthread_mutex_destroy (&p->mutex);
106     pthread_cond_destroy (&p->lock_free);
107 #endif
108     return 0;
109 }
110
111 int zebra_lock_rdwr_rlock (Zebra_lock_rdwr *p)
112 {
113 #if YAZ_POSIX_THREADS
114     pthread_mutex_lock (& p->mutex);
115     while (p->writers_writing)
116         pthread_cond_wait (&p->lock_free, &p->mutex);
117     p->readers_reading++;
118     pthread_mutex_unlock(&p->mutex);
119 #endif
120     return 0;
121 }
122
123 int zebra_lock_rdwr_wlock (Zebra_lock_rdwr *p)
124 {
125 #if YAZ_POSIX_THREADS
126     pthread_mutex_lock (&p->mutex);
127     while (p->writers_writing || p->readers_reading)
128         pthread_cond_wait (&p->lock_free, &p->mutex);
129     p->writers_writing++;
130     pthread_mutex_unlock (&p->mutex);
131 #endif
132     return 0;
133 }
134
135 int zebra_lock_rdwr_runlock (Zebra_lock_rdwr *p)
136 {
137 #if YAZ_POSIX_THREADS
138     pthread_mutex_lock (&p->mutex);
139     if (p->readers_reading == 0)
140     {
141         pthread_mutex_unlock (&p->mutex);
142         return -1;
143     }
144     else
145     {
146         p->readers_reading--;
147         if (p->readers_reading == 0)
148             pthread_cond_signal (&p->lock_free);
149         pthread_mutex_unlock (&p->mutex);
150     }
151 #endif
152     return 0;
153 }
154
155 int zebra_lock_rdwr_wunlock (Zebra_lock_rdwr *p)
156 {
157 #if YAZ_POSIX_THREADS
158     pthread_mutex_lock (&p->mutex);
159     if (p->writers_writing == 0)
160     {
161         pthread_mutex_unlock (&p->mutex);
162         return -1;
163     }
164     else
165     {
166         p->writers_writing--;
167         pthread_cond_broadcast(&p->lock_free);
168         pthread_mutex_unlock(&p->mutex);
169     }
170 #endif
171     return 0;
172 }
173
174 int zebra_mutex_cond_init (Zebra_mutex_cond *p)
175 {
176 #if YAZ_POSIX_THREADS
177     pthread_cond_init (&p->cond, 0);
178     pthread_mutex_init (&p->mutex, 0);
179 #endif
180     return 0;
181 }
182
183 int zebra_mutex_cond_destroy (Zebra_mutex_cond *p)
184 {
185 #if YAZ_POSIX_THREADS
186     pthread_cond_destroy (&p->cond);
187     pthread_mutex_destroy (&p->mutex);
188 #endif
189     return 0;
190 }
191
192 int zebra_mutex_cond_lock (Zebra_mutex_cond *p)
193 {
194 #if YAZ_POSIX_THREADS
195     return pthread_mutex_lock (&p->mutex);
196 #else
197     return 0;
198 #endif
199 }
200
201 int zebra_mutex_cond_unlock (Zebra_mutex_cond *p)
202 {
203 #if YAZ_POSIX_THREADS
204     return pthread_mutex_unlock (&p->mutex);
205 #else
206     return 0;
207 #endif
208 }
209
210 int zebra_mutex_cond_wait (Zebra_mutex_cond *p)
211 {
212 #if YAZ_POSIX_THREADS
213     return pthread_cond_wait (&p->cond, &p->mutex);
214 #else
215     return 0;
216 #endif
217 }
218
219 int zebra_mutex_cond_signal (Zebra_mutex_cond *p)
220 {
221 #if YAZ_POSIX_THREADS
222     return pthread_cond_signal (&p->cond);
223 #else
224     return 0;
225 #endif
226 }
227 /*
228  * Local variables:
229  * c-basic-offset: 4
230  * c-file-style: "Stroustrup"
231  * indent-tabs-mode: nil
232  * End:
233  * vim: shiftwidth=4 tabstop=8 expandtab
234  */
235