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