condvar->wait() can handle recursive mutex
[strongswan.git] / src / libstrongswan / utils / mutex.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include "mutex.h"
19
20 #include <library.h>
21 #include <debug.h>
22
23 #include <pthread.h>
24 #include <sys/time.h>
25 #include <stdint.h>
26 #include <time.h>
27 #include <errno.h>
28
29
30 typedef struct private_mutex_t private_mutex_t;
31 typedef struct private_r_mutex_t private_r_mutex_t;
32 typedef struct private_condvar_t private_condvar_t;
33
34 /**
35 * private data of mutex
36 */
37 struct private_mutex_t {
38
39 /**
40 * public functions
41 */
42 mutex_t public;
43
44 /**
45 * wrapped pthread mutex
46 */
47 pthread_mutex_t mutex;
48
49 /**
50 * is this a recursiv emutex, implementing private_r_mutex_t?
51 */
52 bool recursive;
53 };
54
55 /**
56 * private data of mutex, extended by recursive locking information
57 */
58 struct private_r_mutex_t {
59
60 /**
61 * Extends private_mutex_t
62 */
63 private_mutex_t generic;
64
65 /**
66 * thread which currently owns mutex
67 */
68 pthread_t thread;
69
70 /**
71 * times we have locked the lock, stored per thread
72 */
73 pthread_key_t times;
74 };
75
76 /**
77 * private data of condvar
78 */
79 struct private_condvar_t {
80
81 /**
82 * public functions
83 */
84 condvar_t public;
85
86 /**
87 * wrapped pthread condvar
88 */
89 pthread_cond_t condvar;
90 };
91
92 /**
93 * Implementation of mutex_t.lock.
94 */
95 static void lock(private_mutex_t *this)
96 {
97 if (pthread_mutex_lock(&this->mutex))
98 {
99 DBG1("!!!! MUTEX %sLOCK ERROR, your code is buggy !!!", "");
100 }
101 }
102
103 /**
104 * Implementation of mutex_t.unlock.
105 */
106 static void unlock(private_mutex_t *this)
107 {
108 if (pthread_mutex_unlock(&this->mutex))
109 {
110 DBG1("!!!! MUTEX %sLOCK ERROR, your code is buggy !!!", "UN");
111 }
112 }
113
114 /**
115 * Implementation of mutex_t.lock.
116 */
117 static void lock_r(private_r_mutex_t *this)
118 {
119 pthread_t self = pthread_self();
120
121 if (this->thread == self)
122 {
123 uintptr_t times;
124
125 /* times++ */
126 times = (uintptr_t)pthread_getspecific(this->times);
127 pthread_setspecific(this->times, (void*)times + 1);
128 }
129 else
130 {
131 lock(&this->generic);
132 this->thread = self;
133 /* times = 1 */
134 pthread_setspecific(this->times, (void*)1);
135 }
136 }
137
138 /**
139 * Implementation of mutex_t.unlock.
140 */
141 static void unlock_r(private_r_mutex_t *this)
142 {
143 uintptr_t times;
144
145 /* times-- */
146 times = (uintptr_t)pthread_getspecific(this->times);
147 pthread_setspecific(this->times, (void*)--times);
148
149 if (times == 0)
150 {
151 this->thread = 0;
152 unlock(&this->generic);
153 }
154 }
155
156 /**
157 * Implementation of mutex_t.destroy
158 */
159 static void mutex_destroy(private_mutex_t *this)
160 {
161 pthread_mutex_destroy(&this->mutex);
162 free(this);
163 }
164
165 /**
166 * Implementation of mutex_t.destroy for recursive mutex'
167 */
168 static void mutex_destroy_r(private_r_mutex_t *this)
169 {
170 pthread_mutex_destroy(&this->generic.mutex);
171 pthread_key_delete(this->times);
172 free(this);
173 }
174
175 /*
176 * see header file
177 */
178 mutex_t *mutex_create(mutex_type_t type)
179 {
180 switch (type)
181 {
182 case MUTEX_RECURSIVE:
183 {
184 private_r_mutex_t *this = malloc_thing(private_r_mutex_t);
185
186 this->generic.public.lock = (void(*)(mutex_t*))lock_r;
187 this->generic.public.unlock = (void(*)(mutex_t*))unlock_r;
188 this->generic.public.destroy = (void(*)(mutex_t*))mutex_destroy_r;
189
190 pthread_mutex_init(&this->generic.mutex, NULL);
191 pthread_key_create(&this->times, NULL);
192 this->generic.recursive = TRUE;
193 this->thread = 0;
194
195 return &this->generic.public;
196 }
197 case MUTEX_DEFAULT:
198 default:
199 {
200 private_mutex_t *this = malloc_thing(private_mutex_t);
201
202 this->public.lock = (void(*)(mutex_t*))lock;
203 this->public.unlock = (void(*)(mutex_t*))unlock;
204 this->public.destroy = (void(*)(mutex_t*))mutex_destroy;
205
206 pthread_mutex_init(&this->mutex, NULL);
207 this->recursive = FALSE;
208
209 return &this->public;
210 }
211 }
212 }
213
214 /**
215 * Implementation of condvar_t.wait.
216 */
217 static void wait(private_condvar_t *this, private_mutex_t *mutex)
218 {
219 if (mutex->recursive)
220 {
221 private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
222
223 /* mutex owner gets cleared during condvar wait */
224 recursive->thread = 0;
225 pthread_cond_wait(&this->condvar, &mutex->mutex);
226 recursive->thread = pthread_self();
227 }
228 else
229 {
230 pthread_cond_wait(&this->condvar, &mutex->mutex);
231 }
232 }
233
234 /**
235 * Implementation of condvar_t.timed_wait.
236 */
237 static bool timed_wait(private_condvar_t *this, private_mutex_t *mutex,
238 u_int timeout)
239 {
240 struct timespec ts;
241 struct timeval tv;
242 u_int s, ms;
243 bool timed_out;
244
245 gettimeofday(&tv, NULL);
246
247 s = timeout / 1000;
248 ms = timeout % 1000;
249
250 ts.tv_sec = tv.tv_sec + s;
251 ts.tv_nsec = tv.tv_usec * 1000 + ms * 1000000;
252 if (ts.tv_nsec > 1000000000 /* 1s */)
253 {
254 ts.tv_nsec -= 1000000000;
255 ts.tv_sec++;
256 }
257 if (mutex->recursive)
258 {
259 private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
260
261 recursive->thread = 0;
262 timed_out = pthread_cond_timedwait(&this->condvar, &mutex->mutex,
263 &ts) == ETIMEDOUT;
264 recursive->thread = pthread_self();
265 }
266 else
267 {
268 timed_out = pthread_cond_timedwait(&this->condvar, &mutex->mutex,
269 &ts) == ETIMEDOUT;
270 }
271 return timed_out;
272 }
273
274 /**
275 * Implementation of condvar_t.signal.
276 */
277 static void signal(private_condvar_t *this)
278 {
279 pthread_cond_signal(&this->condvar);
280 }
281
282 /**
283 * Implementation of condvar_t.broadcast.
284 */
285 static void broadcast(private_condvar_t *this)
286 {
287 pthread_cond_broadcast(&this->condvar);
288 }
289
290 /**
291 * Implementation of condvar_t.destroy
292 */
293 static void condvar_destroy(private_condvar_t *this)
294 {
295 pthread_cond_destroy(&this->condvar);
296 free(this);
297 }
298
299 /*
300 * see header file
301 */
302 condvar_t *condvar_create(condvar_type_t type)
303 {
304 switch (type)
305 {
306 case CONDVAR_DEFAULT:
307 default:
308 {
309 private_condvar_t *this = malloc_thing(private_condvar_t);
310
311 this->public.wait = (void(*)(condvar_t*, mutex_t *mutex))wait;
312 this->public.timed_wait = (bool(*)(condvar_t*, mutex_t *mutex, u_int timeout))timed_wait;
313 this->public.signal = (void(*)(condvar_t*))signal;
314 this->public.broadcast = (void(*)(condvar_t*))broadcast;
315 this->public.destroy = (void(*)(condvar_t*))condvar_destroy;
316
317 pthread_cond_init(&this->condvar, NULL);
318
319 return &this->public;
320 }
321 }
322 }
323