5 #include <zebra-lock.h>
8 int zebra_mutex_init (Zebra_mutex *p)
12 fprintf (stderr, "zebra_mutex_init. state=%d\n", p->state);
16 pthread_mutex_init (&p->mutex, 0);
19 InitializeCriticalSection (&p->mutex);
24 int zebra_mutex_destroy (Zebra_mutex *p)
29 fprintf (stderr, "zebra_mutex_destroy. state = %d\n", p->state);
32 pthread_mutex_destroy (&p->mutex);
35 DeleteCriticalSection (&p->mutex);
40 int zebra_mutex_lock (Zebra_mutex *p)
44 fprintf (stderr, "zebra_mutex_lock. state = %d\n", p->state);
47 pthread_mutex_lock (&p->mutex);
50 EnterCriticalSection (&p->mutex);
55 int zebra_mutex_unlock (Zebra_mutex *p)
59 fprintf (stderr, "zebra_mutex_unlock. state = %d\n", p->state);
62 pthread_mutex_unlock (&p->mutex);
65 LeaveCriticalSection (&p->mutex);
70 int zebra_lock_rdwr_init (Zebra_lock_rdwr *p)
72 p->readers_reading = 0;
73 p->writers_writing = 0;
75 pthread_mutex_init (&p->mutex, 0);
76 pthread_cond_init (&p->lock_free, 0);
81 int zebra_lock_rdwr_destroy (Zebra_lock_rdwr *p)
83 assert (p->readers_reading == 0);
84 assert (p->writers_writing == 0);
86 pthread_mutex_destroy (&p->mutex);
87 pthread_cond_destroy (&p->lock_free);
92 int zebra_lock_rdwr_rlock (Zebra_lock_rdwr *p)
95 pthread_mutex_lock (& p->mutex);
96 while (p->writers_writing)
97 pthread_cond_wait (&p->lock_free, &p->mutex);
99 pthread_mutex_unlock(&p->mutex);
104 int zebra_lock_rdwr_wlock (Zebra_lock_rdwr *p)
107 pthread_mutex_lock (&p->mutex);
108 while (p->writers_writing || p->readers_reading)
109 pthread_cond_wait (&p->lock_free, &p->mutex);
110 p->writers_writing++;
111 pthread_mutex_unlock (&p->mutex);
116 int zebra_lock_rdwr_runlock (Zebra_lock_rdwr *p)
119 pthread_mutex_lock (&p->mutex);
120 if (p->readers_reading == 0)
122 pthread_mutex_unlock (&p->mutex);
127 p->readers_reading--;
128 if (p->readers_reading == 0)
129 pthread_cond_signal (&p->lock_free);
130 pthread_mutex_unlock (&p->mutex);
136 int zebra_lock_rdwr_wunlock (Zebra_lock_rdwr *p)
139 pthread_mutex_lock (&p->mutex);
140 if (p->writers_writing == 0)
142 pthread_mutex_unlock (&p->mutex);
147 p->writers_writing--;
148 pthread_cond_broadcast(&p->lock_free);
149 pthread_mutex_unlock(&p->mutex);
155 int zebra_mutex_cond_init (Zebra_mutex_cond *p)
158 pthread_cond_init (&p->cond, 0);
159 pthread_mutex_init (&p->mutex, 0);
164 int zebra_mutex_cond_destroy (Zebra_mutex_cond *p)
167 pthread_cond_destroy (&p->cond);
168 pthread_mutex_destroy (&p->mutex);
173 int zebra_mutex_cond_lock (Zebra_mutex_cond *p)
176 return pthread_mutex_lock (&p->mutex);
182 int zebra_mutex_cond_unlock (Zebra_mutex_cond *p)
185 return pthread_mutex_unlock (&p->mutex);
191 int zebra_mutex_cond_wait (Zebra_mutex_cond *p)
194 return pthread_cond_wait (&p->cond, &p->mutex);
200 int zebra_mutex_cond_signal (Zebra_mutex_cond *p)
203 return pthread_cond_signal (&p->cond);