1 /* $Id: zebra-lock.c,v 1.8 2005-01-15 19:38:42 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 #include <zebra-lock.h>
31 int zebra_mutex_init (Zebra_mutex *p)
35 pthread_mutex_init (&p->mutex, 0);
38 InitializeCriticalSection (&p->mutex);
43 int zebra_mutex_destroy (Zebra_mutex *p)
48 fprintf (stderr, "zebra_mutex_destroy. state = %d\n", p->state);
51 pthread_mutex_destroy (&p->mutex);
54 DeleteCriticalSection (&p->mutex);
59 int zebra_mutex_lock (Zebra_mutex *p)
63 fprintf (stderr, "zebra_mutex_lock. state = %d\n", p->state);
66 pthread_mutex_lock (&p->mutex);
69 EnterCriticalSection (&p->mutex);
74 int zebra_mutex_unlock (Zebra_mutex *p)
78 fprintf (stderr, "zebra_mutex_unlock. state = %d\n", p->state);
81 pthread_mutex_unlock (&p->mutex);
84 LeaveCriticalSection (&p->mutex);
89 int zebra_lock_rdwr_init (Zebra_lock_rdwr *p)
91 p->readers_reading = 0;
92 p->writers_writing = 0;
94 pthread_mutex_init (&p->mutex, 0);
95 pthread_cond_init (&p->lock_free, 0);
100 int zebra_lock_rdwr_destroy (Zebra_lock_rdwr *p)
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);
111 int zebra_lock_rdwr_rlock (Zebra_lock_rdwr *p)
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);
123 int zebra_lock_rdwr_wlock (Zebra_lock_rdwr *p)
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);
135 int zebra_lock_rdwr_runlock (Zebra_lock_rdwr *p)
137 #if YAZ_POSIX_THREADS
138 pthread_mutex_lock (&p->mutex);
139 if (p->readers_reading == 0)
141 pthread_mutex_unlock (&p->mutex);
146 p->readers_reading--;
147 if (p->readers_reading == 0)
148 pthread_cond_signal (&p->lock_free);
149 pthread_mutex_unlock (&p->mutex);
155 int zebra_lock_rdwr_wunlock (Zebra_lock_rdwr *p)
157 #if YAZ_POSIX_THREADS
158 pthread_mutex_lock (&p->mutex);
159 if (p->writers_writing == 0)
161 pthread_mutex_unlock (&p->mutex);
166 p->writers_writing--;
167 pthread_cond_broadcast(&p->lock_free);
168 pthread_mutex_unlock(&p->mutex);
174 int zebra_mutex_cond_init (Zebra_mutex_cond *p)
176 #if YAZ_POSIX_THREADS
177 pthread_cond_init (&p->cond, 0);
178 pthread_mutex_init (&p->mutex, 0);
183 int zebra_mutex_cond_destroy (Zebra_mutex_cond *p)
185 #if YAZ_POSIX_THREADS
186 pthread_cond_destroy (&p->cond);
187 pthread_mutex_destroy (&p->mutex);
192 int zebra_mutex_cond_lock (Zebra_mutex_cond *p)
194 #if YAZ_POSIX_THREADS
195 return pthread_mutex_lock (&p->mutex);
201 int zebra_mutex_cond_unlock (Zebra_mutex_cond *p)
203 #if YAZ_POSIX_THREADS
204 return pthread_mutex_unlock (&p->mutex);
210 int zebra_mutex_cond_wait (Zebra_mutex_cond *p)
212 #if YAZ_POSIX_THREADS
213 return pthread_cond_wait (&p->cond, &p->mutex);
219 int zebra_mutex_cond_signal (Zebra_mutex_cond *p)
221 #if YAZ_POSIX_THREADS
222 return pthread_cond_signal (&p->cond);