1 : /*
2 : * debug.h - libteredo transparent threading debugging
3 : * $Id: debug.h 1808 2006-10-26 19:34:40Z remi $
4 : */
5 :
6 : /***********************************************************************
7 : * Copyright © 2006 Rémi Denis-Courmont. *
8 : * This program is free software; you can redistribute and/or modify *
9 : * it under the terms of the GNU General Public License as published *
10 : * by the Free Software Foundation; version 2 of the license. *
11 : * *
12 : * This program is distributed in the hope that it will be useful, *
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
15 : * See the GNU General Public License for more details. *
16 : * *
17 : * You should have received a copy of the GNU General Public License *
18 : * along with this program; if not, you can get it from: *
19 : * http://www.gnu.org/copyleft/gpl.html *
20 : ***********************************************************************/
21 :
22 : #ifndef LIBTEREDO_COMMON_H
23 : # define LIBTEREDO_COMMON_H
24 :
25 : # ifdef __GNUC__
26 : # define LIBTEREDO_NORETURN __attribute__ ((noreturn))
27 : # else
28 : # define LIBTEREDO_NORETURN
29 : # endif
30 :
31 : # if defined (__linux__) && !defined NDEBUG
32 : # include <errno.h>
33 : # include <assert.h>
34 : # undef PTHREAD_MUTEX_INITIALIZER
35 : # define PTHREAD_MUTEX_INITIALIZER PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
36 :
37 : static inline int
38 : d_pthread_mutex_init (pthread_mutex_t *mutex, pthread_mutexattr_t *pattr)
39 14 : {
40 : pthread_mutexattr_t attr;
41 :
42 14 : if (pattr == NULL)
43 : {
44 14 : pattr = &attr;
45 14 : pthread_mutexattr_init (&attr);
46 : }
47 :
48 14 : pthread_mutexattr_settype (pattr, PTHREAD_MUTEX_ERRORCHECK_NP);
49 14 : int res = pthread_mutex_init (mutex, pattr);
50 :
51 14 : if (pattr == &attr)
52 14 : pthread_mutexattr_destroy (&attr);
53 14 : return res;
54 : }
55 : # define pthread_mutex_init(m, a) d_pthread_mutex_init (m, a)
56 :
57 : static inline int d_pthread_mutex_lock (pthread_mutex_t *mutex)
58 611224 : {
59 611224 : int err = pthread_mutex_lock (mutex);
60 611224 : assert (err != EDEADLK);
61 611224 : assert (err == 0);
62 611224 : return 0;
63 : }
64 : # define pthread_mutex_lock(m) d_pthread_mutex_lock (m)
65 :
66 : static inline int d_pthread_mutex_unlock (pthread_mutex_t *mutex)
67 611224 : {
68 611224 : int err = pthread_mutex_unlock (mutex);
69 611224 : assert (err != EPERM);
70 611224 : assert (err == 0);
71 611224 : return 0;
72 : }
73 : # define pthread_mutex_unlock(m) d_pthread_mutex_unlock (m)
74 : # endif
75 :
76 : #endif /* ifndef LIBTEREDO_COMMON_H */
|