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