renamed some conflicting function names
[strongswan.git] / src / libstrongswan / utils / mutex.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #define _GNU_SOURCE
18 #include <pthread.h>
19 #include <sys/time.h>
20 #include <stdint.h>
21 #include <time.h>
22 #include <errno.h>
23
24 #include "mutex.h"
25
26 #include <library.h>
27 #include <debug.h>
28
29 typedef struct private_mutex_t private_mutex_t;
30 typedef struct private_r_mutex_t private_r_mutex_t;
31 typedef struct private_condvar_t private_condvar_t;
32 typedef struct private_rwlock_t private_rwlock_t;
33
34 #ifdef LOCK_PROFILER
35
36 /**
37 * Do not report mutexes with an overall waiting time smaller than this (in us)
38 */
39 #define PROFILE_TRESHHOLD 1000
40
41 #include <utils/backtrace.h>
42
43 typedef struct lock_profile_t lock_profile_t;
44
45 struct lock_profile_t {
46
47 /**
48 * how long threads have waited for the lock in this mutex so far
49 */
50 struct timeval waited;
51
52 /**
53 * backtrace where mutex has been created
54 */
55 backtrace_t *backtrace;
56 };
57
58 /**
59 * Print and cleanup mutex profiler
60 */
61 static void profiler_cleanup(lock_profile_t *profile)
62 {
63 if (profile->waited.tv_sec > 0 ||
64 profile->waited.tv_usec > PROFILE_TRESHHOLD)
65 {
66 fprintf(stderr, "%d.%06ds in lock created at:",
67 profile->waited.tv_sec, profile->waited.tv_usec);
68 profile->backtrace->log(profile->backtrace, stderr);
69 }
70 profile->backtrace->destroy(profile->backtrace);
71 }
72
73 /**
74 * Initialize mutex profiler
75 */
76 static void profiler_init(lock_profile_t *profile)
77 {
78 profile->backtrace = backtrace_create(2);
79 timerclear(&profile->waited);
80 }
81
82 #define profiler_start(profile) { \
83 struct timeval _start, _end, _diff; \
84 gettimeofday(&_start, NULL);
85
86 #define profiler_end(profile) \
87 gettimeofday(&_end, NULL); \
88 timersub(&_end, &_start, &_diff); \
89 timeradd(&(profile)->waited, &_diff, &(profile)->waited); }
90
91 #else /* !LOCK_PROFILER */
92
93 #define lock_profile_t struct {}
94 #define profiler_cleanup(...) {}
95 #define profiler_init(...) {}
96 #define profiler_start(...) {}
97 #define profiler_end(...) {}
98
99 #endif /* LOCK_PROFILER */
100
101 /**
102 * private data of mutex
103 */
104 struct private_mutex_t {
105
106 /**
107 * public functions
108 */
109 mutex_t public;
110
111 /**
112 * wrapped pthread mutex
113 */
114 pthread_mutex_t mutex;
115
116 /**
117 * is this a recursiv emutex, implementing private_r_mutex_t?
118 */
119 bool recursive;
120
121 /**
122 * profiling info, if enabled
123 */
124 lock_profile_t profile;
125 };
126
127 /**
128 * private data of mutex, extended by recursive locking information
129 */
130 struct private_r_mutex_t {
131
132 /**
133 * Extends private_mutex_t
134 */
135 private_mutex_t generic;
136
137 /**
138 * thread which currently owns mutex
139 */
140 pthread_t thread;
141
142 /**
143 * times we have locked the lock, stored per thread
144 */
145 pthread_key_t times;
146 };
147
148 /**
149 * private data of condvar
150 */
151 struct private_condvar_t {
152
153 /**
154 * public functions
155 */
156 condvar_t public;
157
158 /**
159 * wrapped pthread condvar
160 */
161 pthread_cond_t condvar;
162 };
163
164 /**
165 * private data of rwlock
166 */
167 struct private_rwlock_t {
168
169 /**
170 * public functions
171 */
172 rwlock_t public;
173
174 /**
175 * wrapped pthread rwlock
176 */
177 pthread_rwlock_t rwlock;
178
179 /**
180 * profiling info, if enabled
181 */
182 lock_profile_t profile;
183 };
184
185 /**
186 * Implementation of mutex_t.lock.
187 */
188 static void lock(private_mutex_t *this)
189 {
190 profiler_start(&this->profile);
191 if (pthread_mutex_lock(&this->mutex))
192 {
193 DBG1("!!!! MUTEX %sLOCK ERROR, your code is buggy !!!", "");
194 }
195 profiler_end(&this->profile);
196 }
197
198 /**
199 * Implementation of mutex_t.unlock.
200 */
201 static void unlock(private_mutex_t *this)
202 {
203 if (pthread_mutex_unlock(&this->mutex))
204 {
205 DBG1("!!!! MUTEX %sLOCK ERROR, your code is buggy !!!", "UN");
206 }
207 }
208
209 /**
210 * Implementation of mutex_t.lock.
211 */
212 static void lock_r(private_r_mutex_t *this)
213 {
214 pthread_t self = pthread_self();
215
216 if (this->thread == self)
217 {
218 uintptr_t times;
219
220 /* times++ */
221 times = (uintptr_t)pthread_getspecific(this->times);
222 pthread_setspecific(this->times, (void*)times + 1);
223 }
224 else
225 {
226 lock(&this->generic);
227 this->thread = self;
228 /* times = 1 */
229 pthread_setspecific(this->times, (void*)1);
230 }
231 }
232
233 /**
234 * Implementation of mutex_t.unlock.
235 */
236 static void unlock_r(private_r_mutex_t *this)
237 {
238 uintptr_t times;
239
240 /* times-- */
241 times = (uintptr_t)pthread_getspecific(this->times);
242 pthread_setspecific(this->times, (void*)--times);
243
244 if (times == 0)
245 {
246 this->thread = 0;
247 unlock(&this->generic);
248 }
249 }
250
251 /**
252 * Implementation of mutex_t.destroy
253 */
254 static void mutex_destroy(private_mutex_t *this)
255 {
256 profiler_cleanup(&this->profile);
257 pthread_mutex_destroy(&this->mutex);
258 free(this);
259 }
260
261 /**
262 * Implementation of mutex_t.destroy for recursive mutex'
263 */
264 static void mutex_destroy_r(private_r_mutex_t *this)
265 {
266 profiler_cleanup(&this->generic.profile);
267 pthread_mutex_destroy(&this->generic.mutex);
268 pthread_key_delete(this->times);
269 free(this);
270 }
271
272 /*
273 * see header file
274 */
275 mutex_t *mutex_create(mutex_type_t type)
276 {
277 switch (type)
278 {
279 case MUTEX_RECURSIVE:
280 {
281 private_r_mutex_t *this = malloc_thing(private_r_mutex_t);
282
283 this->generic.public.lock = (void(*)(mutex_t*))lock_r;
284 this->generic.public.unlock = (void(*)(mutex_t*))unlock_r;
285 this->generic.public.destroy = (void(*)(mutex_t*))mutex_destroy_r;
286
287 pthread_mutex_init(&this->generic.mutex, NULL);
288 pthread_key_create(&this->times, NULL);
289 this->generic.recursive = TRUE;
290 profiler_init(&this->generic.profile);
291 this->thread = 0;
292
293 return &this->generic.public;
294 }
295 case MUTEX_DEFAULT:
296 default:
297 {
298 private_mutex_t *this = malloc_thing(private_mutex_t);
299
300 this->public.lock = (void(*)(mutex_t*))lock;
301 this->public.unlock = (void(*)(mutex_t*))unlock;
302 this->public.destroy = (void(*)(mutex_t*))mutex_destroy;
303
304 pthread_mutex_init(&this->mutex, NULL);
305 this->recursive = FALSE;
306 profiler_init(&this->profile);
307
308 return &this->public;
309 }
310 }
311 }
312
313 /**
314 * Implementation of condvar_t.wait.
315 */
316 static void _wait(private_condvar_t *this, private_mutex_t *mutex)
317 {
318 if (mutex->recursive)
319 {
320 private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
321
322 /* mutex owner gets cleared during condvar wait */
323 recursive->thread = 0;
324 pthread_cond_wait(&this->condvar, &mutex->mutex);
325 recursive->thread = pthread_self();
326 }
327 else
328 {
329 pthread_cond_wait(&this->condvar, &mutex->mutex);
330 }
331 }
332
333 /**
334 * Implementation of condvar_t.timed_wait_abs.
335 */
336 static bool timed_wait_abs(private_condvar_t *this, private_mutex_t *mutex,
337 timeval_t time)
338 {
339 struct timespec ts;
340 bool timed_out;
341
342 ts.tv_sec = time.tv_sec;
343 ts.tv_nsec = time.tv_usec * 1000;
344
345 if (mutex->recursive)
346 {
347 private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
348
349 recursive->thread = 0;
350 timed_out = pthread_cond_timedwait(&this->condvar, &mutex->mutex,
351 &ts) == ETIMEDOUT;
352 recursive->thread = pthread_self();
353 }
354 else
355 {
356 timed_out = pthread_cond_timedwait(&this->condvar, &mutex->mutex,
357 &ts) == ETIMEDOUT;
358 }
359 return timed_out;
360 }
361
362 /**
363 * Implementation of condvar_t.timed_wait.
364 */
365 static bool timed_wait(private_condvar_t *this, private_mutex_t *mutex,
366 u_int timeout)
367 {
368 timeval_t tv;
369 u_int s, ms;
370
371 gettimeofday(&tv, NULL);
372
373 s = timeout / 1000;
374 ms = timeout % 1000;
375
376 tv.tv_sec += s;
377 tv.tv_usec += ms * 1000;
378
379 if (tv.tv_usec > 1000000 /* 1s */)
380 {
381 tv.tv_usec -= 1000000;
382 tv.tv_sec++;
383 }
384 return timed_wait_abs(this, mutex, tv);
385 }
386
387 /**
388 * Implementation of condvar_t.signal.
389 */
390 static void _signal(private_condvar_t *this)
391 {
392 pthread_cond_signal(&this->condvar);
393 }
394
395 /**
396 * Implementation of condvar_t.broadcast.
397 */
398 static void broadcast(private_condvar_t *this)
399 {
400 pthread_cond_broadcast(&this->condvar);
401 }
402
403 /**
404 * Implementation of condvar_t.destroy
405 */
406 static void condvar_destroy(private_condvar_t *this)
407 {
408 pthread_cond_destroy(&this->condvar);
409 free(this);
410 }
411
412 /*
413 * see header file
414 */
415 condvar_t *condvar_create(condvar_type_t type)
416 {
417 switch (type)
418 {
419 case CONDVAR_DEFAULT:
420 default:
421 {
422 private_condvar_t *this = malloc_thing(private_condvar_t);
423
424 this->public.wait = (void(*)(condvar_t*, mutex_t *mutex))_wait;
425 this->public.timed_wait = (bool(*)(condvar_t*, mutex_t *mutex, u_int timeout))timed_wait;
426 this->public.timed_wait_abs = (bool(*)(condvar_t*, mutex_t *mutex, timeval_t time))timed_wait_abs;
427 this->public.signal = (void(*)(condvar_t*))_signal;
428 this->public.broadcast = (void(*)(condvar_t*))broadcast;
429 this->public.destroy = (void(*)(condvar_t*))condvar_destroy;
430
431 pthread_cond_init(&this->condvar, NULL);
432
433 return &this->public;
434 }
435 }
436 }
437
438 /**
439 * Implementation of rwlock_t.read_lock
440 */
441 static void read_lock(private_rwlock_t *this)
442 {
443 profiler_start(&this->profile);
444 pthread_rwlock_rdlock(&this->rwlock);
445 profiler_end(&this->profile);
446 }
447
448 /**
449 * Implementation of rwlock_t.write_lock
450 */
451 static void write_lock(private_rwlock_t *this)
452 {
453 profiler_start(&this->profile);
454 pthread_rwlock_wrlock(&this->rwlock);
455 profiler_end(&this->profile);
456 }
457
458 /**
459 * Implementation of rwlock_t.try_write_lock
460 */
461 static bool try_write_lock(private_rwlock_t *this)
462 {
463 return pthread_rwlock_trywrlock(&this->rwlock) == 0;
464 }
465
466 /**
467 * Implementation of rwlock_t.unlock
468 */
469 static void rw_unlock(private_rwlock_t *this)
470 {
471 pthread_rwlock_unlock(&this->rwlock);
472 }
473
474 /**
475 * Implementation of rwlock_t.destroy
476 */
477 static void rw_destroy(private_rwlock_t *this)
478 {
479 pthread_rwlock_destroy(&this->rwlock);
480 profiler_cleanup(&this->profile);
481 free(this);
482 }
483
484 /*
485 * see header file
486 */
487 rwlock_t *rwlock_create(rwlock_type_t type)
488 {
489 switch (type)
490 {
491 case RWLOCK_DEFAULT:
492 default:
493 {
494 private_rwlock_t *this = malloc_thing(private_rwlock_t);
495
496 this->public.read_lock = (void(*)(rwlock_t*))read_lock;
497 this->public.write_lock = (void(*)(rwlock_t*))write_lock;
498 this->public.try_write_lock = (bool(*)(rwlock_t*))try_write_lock;
499 this->public.unlock = (void(*)(rwlock_t*))rw_unlock;
500 this->public.destroy = (void(*)(rwlock_t*))rw_destroy;
501
502 pthread_rwlock_init(&this->rwlock, NULL);
503 profiler_init(&this->profile);
504
505 return &this->public;
506 }
507 }
508 }
509