Bump copyright year
[idzebra-moved-to-github.git] / util / zebra-lock.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2010 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 #include <assert.h>
23 #include <stdio.h>
24
25 #include <zebra-lock.h>
26
27
28 int zebra_mutex_init (Zebra_mutex *p)
29 {
30     p->state = 1;
31 #if YAZ_POSIX_THREADS
32     pthread_mutex_init (&p->mutex, 0);
33 #endif
34 #ifdef WIN32
35     InitializeCriticalSection (&p->mutex);
36 #endif
37     return 0;
38 }
39
40 int zebra_mutex_destroy (Zebra_mutex *p)
41 {
42     --(p->state);
43     if (p->state != 0)
44     {
45         fprintf (stderr, "zebra_mutex_destroy. state = %d\n", p->state);
46     } 
47 #if YAZ_POSIX_THREADS
48     pthread_mutex_destroy (&p->mutex);
49 #endif
50 #ifdef WIN32
51     DeleteCriticalSection (&p->mutex);
52 #endif
53     return 0;
54 }
55
56 int zebra_mutex_lock (Zebra_mutex *p)
57 {
58     if (p->state != 1)
59     {
60         fprintf (stderr, "zebra_mutex_lock. state = %d\n", p->state);
61     }
62 #if YAZ_POSIX_THREADS
63     pthread_mutex_lock (&p->mutex);
64 #endif
65 #ifdef WIN32
66     EnterCriticalSection (&p->mutex);
67 #endif
68     return 0;
69 }
70
71 int zebra_mutex_unlock (Zebra_mutex *p)
72 {
73     if (p->state != 1)
74     {
75         fprintf (stderr, "zebra_mutex_unlock. state = %d\n", p->state);
76     }
77 #if YAZ_POSIX_THREADS
78     pthread_mutex_unlock (&p->mutex);
79 #endif
80 #ifdef WIN32
81     LeaveCriticalSection (&p->mutex);
82 #endif
83     return 0;
84 }
85
86 int zebra_lock_rdwr_init (Zebra_lock_rdwr *p)
87 {
88     p->readers_reading = 0;
89     p->writers_writing = 0;
90 #if YAZ_POSIX_THREADS
91     pthread_mutex_init (&p->mutex, 0);
92     pthread_cond_init (&p->lock_free, 0);
93 #endif
94     return 0;
95 }
96
97 int zebra_lock_rdwr_destroy (Zebra_lock_rdwr *p)
98 {
99     assert (p->readers_reading == 0);
100     assert (p->writers_writing == 0);
101 #if YAZ_POSIX_THREADS
102     pthread_mutex_destroy (&p->mutex);
103     pthread_cond_destroy (&p->lock_free);
104 #endif
105     return 0;
106 }
107
108 int zebra_lock_rdwr_rlock (Zebra_lock_rdwr *p)
109 {
110 #if YAZ_POSIX_THREADS
111     pthread_mutex_lock (& p->mutex);
112     while (p->writers_writing)
113         pthread_cond_wait (&p->lock_free, &p->mutex);
114     p->readers_reading++;
115     pthread_mutex_unlock(&p->mutex);
116 #endif
117     return 0;
118 }
119
120 int zebra_lock_rdwr_wlock (Zebra_lock_rdwr *p)
121 {
122 #if YAZ_POSIX_THREADS
123     pthread_mutex_lock (&p->mutex);
124     while (p->writers_writing || p->readers_reading)
125         pthread_cond_wait (&p->lock_free, &p->mutex);
126     p->writers_writing++;
127     pthread_mutex_unlock (&p->mutex);
128 #endif
129     return 0;
130 }
131
132 int zebra_lock_rdwr_runlock (Zebra_lock_rdwr *p)
133 {
134 #if YAZ_POSIX_THREADS
135     pthread_mutex_lock (&p->mutex);
136     if (p->readers_reading == 0)
137     {
138         pthread_mutex_unlock (&p->mutex);
139         return -1;
140     } 
141     else
142     {
143         p->readers_reading--;
144         if (p->readers_reading == 0)
145             pthread_cond_signal (&p->lock_free);
146         pthread_mutex_unlock (&p->mutex);
147     }
148 #endif
149     return 0;
150 }
151
152 int zebra_lock_rdwr_wunlock (Zebra_lock_rdwr *p)
153 {
154 #if YAZ_POSIX_THREADS
155     pthread_mutex_lock (&p->mutex);
156     if (p->writers_writing == 0)
157     {
158         pthread_mutex_unlock (&p->mutex);
159         return -1;
160     }
161     else
162     {
163         p->writers_writing--;
164         pthread_cond_broadcast(&p->lock_free);
165         pthread_mutex_unlock(&p->mutex);
166     }
167 #endif
168     return 0;
169 }
170
171 int zebra_mutex_cond_init (Zebra_mutex_cond *p)
172 {
173 #if YAZ_POSIX_THREADS
174     pthread_cond_init (&p->cond, 0);
175     pthread_mutex_init (&p->mutex, 0);
176 #endif
177     return 0;
178 }
179
180 int zebra_mutex_cond_destroy (Zebra_mutex_cond *p)
181 {
182 #if YAZ_POSIX_THREADS
183     pthread_cond_destroy (&p->cond);
184     pthread_mutex_destroy (&p->mutex);
185 #endif
186     return 0;
187 }
188
189 int zebra_mutex_cond_lock (Zebra_mutex_cond *p)
190 {
191 #if YAZ_POSIX_THREADS
192     return pthread_mutex_lock (&p->mutex);
193 #else
194     return 0;
195 #endif
196 }
197
198 int zebra_mutex_cond_unlock (Zebra_mutex_cond *p)
199 {
200 #if YAZ_POSIX_THREADS
201     return pthread_mutex_unlock (&p->mutex);
202 #else
203     return 0;
204 #endif
205 }
206
207 int zebra_mutex_cond_wait (Zebra_mutex_cond *p)
208 {
209 #if YAZ_POSIX_THREADS
210     return pthread_cond_wait (&p->cond, &p->mutex);
211 #else
212     return 0;
213 #endif
214 }
215
216 int zebra_mutex_cond_signal (Zebra_mutex_cond *p)
217 {
218 #if YAZ_POSIX_THREADS
219     return pthread_cond_signal (&p->cond);
220 #else
221     return 0;
222 #endif
223 }
224 /*
225  * Local variables:
226  * c-basic-offset: 4
227  * c-file-style: "Stroustrup"
228  * indent-tabs-mode: nil
229  * End:
230  * vim: shiftwidth=4 tabstop=8 expandtab
231  */
232