5 #include <zebra-lock.h>
8 int zebra_mutex_init (Zebra_mutex *p)
12 pthread_mutex_init (&p->mutex, 0);
15 InitializeCriticalSection (&p->mutex);
20 int zebra_mutex_destroy (Zebra_mutex *p)
25 fprintf (stderr, "zebra_mutex_destroy. state = %d\n", p->state);
28 pthread_mutex_destroy (&p->mutex);
31 DeleteCriticalSection (&p->mutex);
36 int zebra_mutex_lock (Zebra_mutex *p)
40 fprintf (stderr, "zebra_mutex_lock. state = %d\n", p->state);
43 pthread_mutex_lock (&p->mutex);
46 EnterCriticalSection (&p->mutex);
51 int zebra_mutex_unlock (Zebra_mutex *p)
55 fprintf (stderr, "zebra_mutex_unlock. state = %d\n", p->state);
58 pthread_mutex_unlock (&p->mutex);
61 LeaveCriticalSection (&p->mutex);
66 int zebra_lock_rdwr_init (Zebra_lock_rdwr *p)
68 p->readers_reading = 0;
69 p->writers_writing = 0;
71 pthread_mutex_init (&p->mutex, 0);
72 pthread_cond_init (&p->lock_free, 0);
77 int zebra_lock_rdwr_destroy (Zebra_lock_rdwr *p)
79 assert (p->readers_reading == 0);
80 assert (p->writers_writing == 0);
82 pthread_mutex_destroy (&p->mutex);
83 pthread_cond_destroy (&p->lock_free);
88 int zebra_lock_rdwr_rlock (Zebra_lock_rdwr *p)
91 pthread_mutex_lock (& p->mutex);
92 while (p->writers_writing)
93 pthread_cond_wait (&p->lock_free, &p->mutex);
95 pthread_mutex_unlock(&p->mutex);
100 int zebra_lock_rdwr_wlock (Zebra_lock_rdwr *p)
102 #if YAZ_POSIX_THREADS
103 pthread_mutex_lock (&p->mutex);
104 while (p->writers_writing || p->readers_reading)
105 pthread_cond_wait (&p->lock_free, &p->mutex);
106 p->writers_writing++;
107 pthread_mutex_unlock (&p->mutex);
112 int zebra_lock_rdwr_runlock (Zebra_lock_rdwr *p)
114 #if YAZ_POSIX_THREADS
115 pthread_mutex_lock (&p->mutex);
116 if (p->readers_reading == 0)
118 pthread_mutex_unlock (&p->mutex);
123 p->readers_reading--;
124 if (p->readers_reading == 0)
125 pthread_cond_signal (&p->lock_free);
126 pthread_mutex_unlock (&p->mutex);
132 int zebra_lock_rdwr_wunlock (Zebra_lock_rdwr *p)
134 #if YAZ_POSIX_THREADS
135 pthread_mutex_lock (&p->mutex);
136 if (p->writers_writing == 0)
138 pthread_mutex_unlock (&p->mutex);
143 p->writers_writing--;
144 pthread_cond_broadcast(&p->lock_free);
145 pthread_mutex_unlock(&p->mutex);
151 int zebra_mutex_cond_init (Zebra_mutex_cond *p)
153 #if YAZ_POSIX_THREADS
154 pthread_cond_init (&p->cond, 0);
155 pthread_mutex_init (&p->mutex, 0);
160 int zebra_mutex_cond_destroy (Zebra_mutex_cond *p)
162 #if YAZ_POSIX_THREADS
163 pthread_cond_destroy (&p->cond);
164 pthread_mutex_destroy (&p->mutex);
169 int zebra_mutex_cond_lock (Zebra_mutex_cond *p)
171 #if YAZ_POSIX_THREADS
172 return pthread_mutex_lock (&p->mutex);
178 int zebra_mutex_cond_unlock (Zebra_mutex_cond *p)
180 #if YAZ_POSIX_THREADS
181 return pthread_mutex_unlock (&p->mutex);
187 int zebra_mutex_cond_wait (Zebra_mutex_cond *p)
189 #if YAZ_POSIX_THREADS
190 return pthread_cond_wait (&p->cond, &p->mutex);
196 int zebra_mutex_cond_signal (Zebra_mutex_cond *p)
198 #if YAZ_POSIX_THREADS
199 return pthread_cond_signal (&p->cond);