1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 Index Data
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
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
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
25 #include <zebra-lock.h>
28 int zebra_mutex_init (Zebra_mutex *p)
32 pthread_mutex_init (&p->mutex, 0);
35 InitializeCriticalSection (&p->mutex);
40 int zebra_mutex_destroy (Zebra_mutex *p)
45 fprintf (stderr, "zebra_mutex_destroy. state = %d\n", p->state);
48 pthread_mutex_destroy (&p->mutex);
51 DeleteCriticalSection (&p->mutex);
56 int zebra_mutex_lock (Zebra_mutex *p)
60 fprintf (stderr, "zebra_mutex_lock. state = %d\n", p->state);
63 pthread_mutex_lock (&p->mutex);
66 EnterCriticalSection (&p->mutex);
71 int zebra_mutex_unlock (Zebra_mutex *p)
75 fprintf (stderr, "zebra_mutex_unlock. state = %d\n", p->state);
78 pthread_mutex_unlock (&p->mutex);
81 LeaveCriticalSection (&p->mutex);
86 int zebra_lock_rdwr_init (Zebra_lock_rdwr *p)
88 p->readers_reading = 0;
89 p->writers_writing = 0;
91 pthread_mutex_init (&p->mutex, 0);
92 pthread_cond_init (&p->lock_free, 0);
97 int zebra_lock_rdwr_destroy (Zebra_lock_rdwr *p)
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);
108 int zebra_lock_rdwr_rlock (Zebra_lock_rdwr *p)
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);
120 int zebra_lock_rdwr_wlock (Zebra_lock_rdwr *p)
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);
132 int zebra_lock_rdwr_runlock (Zebra_lock_rdwr *p)
134 #if YAZ_POSIX_THREADS
135 pthread_mutex_lock (&p->mutex);
136 if (p->readers_reading == 0)
138 pthread_mutex_unlock (&p->mutex);
143 p->readers_reading--;
144 if (p->readers_reading == 0)
145 pthread_cond_signal (&p->lock_free);
146 pthread_mutex_unlock (&p->mutex);
152 int zebra_lock_rdwr_wunlock (Zebra_lock_rdwr *p)
154 #if YAZ_POSIX_THREADS
155 pthread_mutex_lock (&p->mutex);
156 if (p->writers_writing == 0)
158 pthread_mutex_unlock (&p->mutex);
163 p->writers_writing--;
164 pthread_cond_broadcast(&p->lock_free);
165 pthread_mutex_unlock(&p->mutex);
171 int zebra_mutex_cond_init (Zebra_mutex_cond *p)
173 #if YAZ_POSIX_THREADS
174 pthread_cond_init (&p->cond, 0);
175 pthread_mutex_init (&p->mutex, 0);
180 int zebra_mutex_cond_destroy (Zebra_mutex_cond *p)
182 #if YAZ_POSIX_THREADS
183 pthread_cond_destroy (&p->cond);
184 pthread_mutex_destroy (&p->mutex);
189 int zebra_mutex_cond_lock (Zebra_mutex_cond *p)
191 #if YAZ_POSIX_THREADS
192 return pthread_mutex_lock (&p->mutex);
198 int zebra_mutex_cond_unlock (Zebra_mutex_cond *p)
200 #if YAZ_POSIX_THREADS
201 return pthread_mutex_unlock (&p->mutex);
207 int zebra_mutex_cond_wait (Zebra_mutex_cond *p)
209 #if YAZ_POSIX_THREADS
210 return pthread_cond_wait (&p->cond, &p->mutex);
216 int zebra_mutex_cond_signal (Zebra_mutex_cond *p)
218 #if YAZ_POSIX_THREADS
219 return pthread_cond_signal (&p->cond);
227 * c-file-style: "Stroustrup"
228 * indent-tabs-mode: nil
230 * vim: shiftwidth=4 tabstop=8 expandtab