On Unix, zebra/z'mbol uses automake.
[idzebra-moved-to-github.git] / util / zebra-lock.c
1
2 #include <assert.h>
3
4 #include <zebra-lock.h>
5
6 int zebra_mutex_init (Zebra_mutex *p)
7 {
8 #if HAVE_PTHREAD_H
9     pthread_mutex_init (&p->mutex, 0);
10 #endif
11     return 0;
12 }
13
14 int zebra_mutex_destroy (Zebra_mutex *p)
15 {
16 #if HAVE_PTHREAD_H
17     pthread_mutex_destroy (&p->mutex);
18 #endif
19     return 0;
20 }
21
22 int zebra_mutex_lock (Zebra_mutex *p)
23 {
24 #if HAVE_PTHREAD_H
25     pthread_mutex_lock (&p->mutex);
26 #endif
27     return 0;
28 }
29
30 int zebra_mutex_unlock (Zebra_mutex *p)
31 {
32 #if HAVE_PTHREAD_H
33     pthread_mutex_unlock (&p->mutex);
34 #endif
35     return 0;
36 }
37
38 int zebra_lock_rdwr_init (Zebra_lock_rdwr *p)
39 {
40     p->readers_reading = 0;
41     p->writers_writing = 0;
42 #if HAVE_PTHREAD_H
43     pthread_mutex_init (&p->mutex, 0);
44     pthread_cond_init (&p->lock_free, 0);
45 #endif
46     return 0;
47 }
48
49 int zebra_lock_rdwr_destroy (Zebra_lock_rdwr *p)
50 {
51     assert (p->readers_reading == 0);
52     assert (p->writers_writing == 0);
53 #if HAVE_PTHREAD_H
54     pthread_mutex_destroy (&p->mutex);
55     pthread_cond_destroy (&p->lock_free);
56 #endif
57     return 0;
58 }
59
60 int zebra_lock_rdwr_rlock (Zebra_lock_rdwr *p)
61 {
62 #if HAVE_PTHREAD_H
63     pthread_mutex_lock (& p->mutex);
64     while (p->writers_writing)
65         pthread_cond_wait (&p->lock_free, &p->mutex);
66     p->readers_reading++;
67     pthread_mutex_unlock(&p->mutex);
68 #endif
69     return 0;
70 }
71
72 int zebra_lock_rdwr_wlock (Zebra_lock_rdwr *p)
73 {
74 #if HAVE_PTHREAD_H
75     pthread_mutex_lock (&p->mutex);
76     while (p->writers_writing || p->readers_reading)
77         pthread_cond_wait (&p->lock_free, &p->mutex);
78     p->writers_writing++;
79     pthread_mutex_unlock (&p->mutex);
80 #endif
81     return 0;
82 }
83
84 int zebra_lock_rdwr_runlock (Zebra_lock_rdwr *p)
85 {
86 #if HAVE_PTHREAD_H
87     pthread_mutex_lock (&p->mutex);
88     if (p->readers_reading == 0)
89     {
90         pthread_mutex_unlock (&p->mutex);
91         return -1;
92     } 
93     else
94     {
95         p->readers_reading--;
96         if (p->readers_reading == 0)
97             pthread_cond_signal (&p->lock_free);
98         pthread_mutex_unlock (&p->mutex);
99     }
100 #endif
101     return 0;
102 }
103
104 int zebra_lock_rdwr_wunlock (Zebra_lock_rdwr *p)
105 {
106 #if HAVE_PTHREAD_H
107     pthread_mutex_lock (&p->mutex);
108     if (p->writers_writing == 0)
109     {
110         pthread_mutex_unlock (&p->mutex);
111         return -1;
112     }
113     else
114     {
115         p->writers_writing--;
116         pthread_cond_broadcast(&p->lock_free);
117         pthread_mutex_unlock(&p->mutex);
118     }
119 #endif
120     return 0;
121 }
122
123 int zebra_mutex_cond_init (Zebra_mutex_cond *p)
124 {
125 #if HAVE_PTHREAD_H
126     pthread_cond_init (&p->cond, 0);
127     pthread_mutex_init (&p->mutex, 0);
128 #endif
129     return 0;
130 }
131
132 int zebra_mutex_cond_destroy (Zebra_mutex_cond *p)
133 {
134 #if HAVE_PTHREAD_H
135     pthread_cond_destroy (&p->cond);
136     pthread_mutex_destroy (&p->mutex);
137 #endif
138     return 0;
139 }
140
141 int zebra_mutex_cond_lock (Zebra_mutex_cond *p)
142 {
143 #if HAVE_PTHREAD_H
144     return pthread_mutex_lock (&p->mutex);
145 #else
146     return 0;
147 #endif
148 }
149
150 int zebra_mutex_cond_unlock (Zebra_mutex_cond *p)
151 {
152 #if HAVE_PTHREAD_H
153     return pthread_mutex_unlock (&p->mutex);
154 #else
155     return 0;
156 #endif
157 }
158
159 int zebra_mutex_cond_wait (Zebra_mutex_cond *p)
160 {
161 #if HAVE_PTHREAD_H
162     return pthread_cond_wait (&p->cond, &p->mutex);
163 #else
164     return 0;
165 #endif
166 }
167
168 int zebra_mutex_cond_signal (Zebra_mutex_cond *p)
169 {
170 #if HAVE_PTHREAD_H
171     return pthread_cond_signal (&p->cond);
172 #else
173     return 0;
174 #endif
175 }