Fixed locking for metafiles.
[idzebra-moved-to-github.git] / util / zebra-lock.c
1
2 #include <assert.h>
3 #include <stdio.h>
4
5 #include <zebra-lock.h>
6
7
8 int zebra_mutex_init (Zebra_mutex *p)
9 {
10     if (p->state == 1)
11     {
12         fprintf (stderr, "zebra_mutex_init. state=%d\n", p->state);
13     }
14     p->state = 1;
15 #if HAVE_PTHREAD_H
16     pthread_mutex_init (&p->mutex, 0);
17 #endif
18 #ifdef WIN32
19     InitializeCriticalSection (&p->mutex);
20 #endif
21     return 0;
22 }
23
24 int zebra_mutex_destroy (Zebra_mutex *p)
25 {
26     --(p->state);
27     if (p->state != 0)
28     {
29         fprintf (stderr, "zebra_mutex_destroy. state = %d\n", p->state);
30     } 
31 #if HAVE_PTHREAD_H
32     pthread_mutex_destroy (&p->mutex);
33 #endif
34 #ifdef WIN32
35     DeleteCriticalSection (&p->mutex);
36 #endif
37     return 0;
38 }
39
40 int zebra_mutex_lock (Zebra_mutex *p)
41 {
42     if (p->state != 1)
43     {
44         fprintf (stderr, "zebra_mutex_lock. state = %d\n", p->state);
45     }
46 #if HAVE_PTHREAD_H
47     pthread_mutex_lock (&p->mutex);
48 #endif
49 #ifdef WIN32
50     EnterCriticalSection (&p->mutex);
51 #endif
52     return 0;
53 }
54
55 int zebra_mutex_unlock (Zebra_mutex *p)
56 {
57     if (p->state != 1)
58     {
59         fprintf (stderr, "zebra_mutex_unlock. state = %d\n", p->state);
60     }
61 #if HAVE_PTHREAD_H
62     pthread_mutex_unlock (&p->mutex);
63 #endif
64 #ifdef WIN32
65     LeaveCriticalSection (&p->mutex);
66 #endif
67     return 0;
68 }
69
70 int zebra_lock_rdwr_init (Zebra_lock_rdwr *p)
71 {
72     p->readers_reading = 0;
73     p->writers_writing = 0;
74 #if HAVE_PTHREAD_H
75     pthread_mutex_init (&p->mutex, 0);
76     pthread_cond_init (&p->lock_free, 0);
77 #endif
78     return 0;
79 }
80
81 int zebra_lock_rdwr_destroy (Zebra_lock_rdwr *p)
82 {
83     assert (p->readers_reading == 0);
84     assert (p->writers_writing == 0);
85 #if HAVE_PTHREAD_H
86     pthread_mutex_destroy (&p->mutex);
87     pthread_cond_destroy (&p->lock_free);
88 #endif
89     return 0;
90 }
91
92 int zebra_lock_rdwr_rlock (Zebra_lock_rdwr *p)
93 {
94 #if HAVE_PTHREAD_H
95     pthread_mutex_lock (& p->mutex);
96     while (p->writers_writing)
97         pthread_cond_wait (&p->lock_free, &p->mutex);
98     p->readers_reading++;
99     pthread_mutex_unlock(&p->mutex);
100 #endif
101     return 0;
102 }
103
104 int zebra_lock_rdwr_wlock (Zebra_lock_rdwr *p)
105 {
106 #if HAVE_PTHREAD_H
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);
112 #endif
113     return 0;
114 }
115
116 int zebra_lock_rdwr_runlock (Zebra_lock_rdwr *p)
117 {
118 #if HAVE_PTHREAD_H
119     pthread_mutex_lock (&p->mutex);
120     if (p->readers_reading == 0)
121     {
122         pthread_mutex_unlock (&p->mutex);
123         return -1;
124     } 
125     else
126     {
127         p->readers_reading--;
128         if (p->readers_reading == 0)
129             pthread_cond_signal (&p->lock_free);
130         pthread_mutex_unlock (&p->mutex);
131     }
132 #endif
133     return 0;
134 }
135
136 int zebra_lock_rdwr_wunlock (Zebra_lock_rdwr *p)
137 {
138 #if HAVE_PTHREAD_H
139     pthread_mutex_lock (&p->mutex);
140     if (p->writers_writing == 0)
141     {
142         pthread_mutex_unlock (&p->mutex);
143         return -1;
144     }
145     else
146     {
147         p->writers_writing--;
148         pthread_cond_broadcast(&p->lock_free);
149         pthread_mutex_unlock(&p->mutex);
150     }
151 #endif
152     return 0;
153 }
154
155 int zebra_mutex_cond_init (Zebra_mutex_cond *p)
156 {
157 #if HAVE_PTHREAD_H
158     pthread_cond_init (&p->cond, 0);
159     pthread_mutex_init (&p->mutex, 0);
160 #endif
161     return 0;
162 }
163
164 int zebra_mutex_cond_destroy (Zebra_mutex_cond *p)
165 {
166 #if HAVE_PTHREAD_H
167     pthread_cond_destroy (&p->cond);
168     pthread_mutex_destroy (&p->mutex);
169 #endif
170     return 0;
171 }
172
173 int zebra_mutex_cond_lock (Zebra_mutex_cond *p)
174 {
175 #if HAVE_PTHREAD_H
176     return pthread_mutex_lock (&p->mutex);
177 #else
178     return 0;
179 #endif
180 }
181
182 int zebra_mutex_cond_unlock (Zebra_mutex_cond *p)
183 {
184 #if HAVE_PTHREAD_H
185     return pthread_mutex_unlock (&p->mutex);
186 #else
187     return 0;
188 #endif
189 }
190
191 int zebra_mutex_cond_wait (Zebra_mutex_cond *p)
192 {
193 #if HAVE_PTHREAD_H
194     return pthread_cond_wait (&p->cond, &p->mutex);
195 #else
196     return 0;
197 #endif
198 }
199
200 int zebra_mutex_cond_signal (Zebra_mutex_cond *p)
201 {
202 #if HAVE_PTHREAD_H
203     return pthread_cond_signal (&p->cond);
204 #else
205     return 0;
206 #endif
207 }