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