bba20c85df4f728e0cca9d45118553b360acdabc
[asterisk/asterisk.git] / include / asterisk / lock.h
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  * \brief Asterisk locking-related definitions:
21  * - ast_mutext_t, ast_rwlock_t and related functions;
22  * - atomic arithmetic instructions;
23  * - wrappers for channel locking.
24  *
25  * - See \ref LockDef
26  */
27
28 /*! \page LockDef Asterisk thread locking models
29  *
30  * This file provides different implementation of the functions,
31  * depending on the platform, the use of DEBUG_THREADS, and the way
32  * module-level mutexes are initialized.
33  *
34  *  - \b static: the mutex is assigned the value AST_MUTEX_INIT_VALUE
35  *        this is done at compile time, and is the way used on Linux.
36  *        This method is not applicable to all platforms e.g. when the
37  *        initialization needs that some code is run.
38  *
39  *  - \b through constructors: for each mutex, a constructor function is
40  *        defined, which then runs when the program (or the module)
41  *        starts. The problem with this approach is that there is a
42  *        lot of code duplication (a new block of code is created for
43  *        each mutex). Also, it does not prevent a user from declaring
44  *        a global mutex without going through the wrapper macros,
45  *        so sane programming practices are still required.
46  */
47
48 #ifndef _ASTERISK_LOCK_H
49 #define _ASTERISK_LOCK_H
50
51 #include <pthread.h>
52 #include <sys/param.h>
53 #ifdef HAVE_BKTR
54 #include <execinfo.h>
55 #endif
56 #include "asterisk/logger.h"
57
58 /* internal macro to profile mutexes. Only computes the delay on
59  * non-blocking calls.
60  */
61 #ifndef HAVE_MTX_PROFILE
62 #define __MTX_PROF(a)   return pthread_mutex_lock((a))
63 #else
64 #define __MTX_PROF(a)   do {                    \
65         int i;                                  \
66         /* profile only non-blocking events */  \
67         ast_mark(mtx_prof, 1);                  \
68         i = pthread_mutex_trylock((a));         \
69         ast_mark(mtx_prof, 0);                  \
70         if (!i)                                 \
71                 return i;                       \
72         else                                    \
73                 return pthread_mutex_lock((a)); \
74         } while (0)
75 #endif  /* HAVE_MTX_PROFILE */
76
77 #define AST_PTHREADT_NULL (pthread_t) -1
78 #define AST_PTHREADT_STOP (pthread_t) -2
79
80 #if defined(SOLARIS) || defined(BSD)
81 #define AST_MUTEX_INIT_W_CONSTRUCTORS
82 #endif /* SOLARIS || BSD */
83
84 /* Asterisk REQUIRES recursive (not error checking) mutexes
85    and will not run without them. */
86 #if defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) && defined(HAVE_PTHREAD_MUTEX_RECURSIVE_NP)
87 #define PTHREAD_MUTEX_INIT_VALUE        PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
88 #define AST_MUTEX_KIND                  PTHREAD_MUTEX_RECURSIVE_NP
89 #else
90 #define PTHREAD_MUTEX_INIT_VALUE        PTHREAD_MUTEX_INITIALIZER
91 #define AST_MUTEX_KIND                  PTHREAD_MUTEX_RECURSIVE
92 #endif /* PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP */
93
94 /*
95  * Definition of ast_mutex_t, ast_cont_d and related functions with/without debugging
96  * (search for DEBUG_THREADS to find the start/end of the sections).
97  *
98  * The non-debug code contains just wrappers for the corresponding pthread functions.
99  * The debug code tracks usage and tries to identify deadlock situations.
100  */
101 #ifdef DEBUG_THREADS
102
103 #define __ast_mutex_logger(...)  do { if (canlog) ast_log(LOG_ERROR, __VA_ARGS__); else fprintf(stderr, __VA_ARGS__); } while (0)
104
105 #ifdef THREAD_CRASH
106 #define DO_THREAD_CRASH do { *((int *)(0)) = 1; } while(0)
107 #else
108 #define DO_THREAD_CRASH do { } while (0)
109 #endif
110
111 #include <errno.h>
112
113 #ifdef HAVE_BKTR
114 #define AST_LOCK_TRACK_INIT_VALUE { { NULL }, { 0 }, 0, { NULL }, { 0 }, {{{ 0 }}}, PTHREAD_MUTEX_INIT_VALUE }
115
116 #else
117 #define AST_LOCK_TRACK_INIT_VALUE { { NULL }, { 0 }, 0, { NULL }, { 0 }, PTHREAD_MUTEX_INIT_VALUE }
118 #endif
119
120 #define AST_MUTEX_INIT_VALUE { AST_LOCK_TRACK_INIT_VALUE, 1, PTHREAD_MUTEX_INIT_VALUE }
121 #define AST_MUTEX_INIT_VALUE_NOTRACKING { AST_LOCK_TRACK_INIT_VALUE, 0, PTHREAD_MUTEX_INIT_VALUE }
122
123 #define AST_MAX_REENTRANCY 10
124
125 struct ast_channel;
126
127 struct ast_lock_track {
128         const char *file[AST_MAX_REENTRANCY];
129         int lineno[AST_MAX_REENTRANCY];
130         int reentrancy;
131         const char *func[AST_MAX_REENTRANCY];
132         pthread_t thread[AST_MAX_REENTRANCY];
133 #ifdef HAVE_BKTR
134         struct ast_bt backtrace[AST_MAX_REENTRANCY];
135 #endif
136         pthread_mutex_t reentr_mutex;
137 };
138
139 struct ast_mutex_info {
140         /*! Track which thread holds this mutex */
141         struct ast_lock_track track;
142         unsigned int tracking:1;
143         pthread_mutex_t mutex;
144 };
145
146 typedef struct ast_mutex_info ast_mutex_t;
147
148 typedef pthread_cond_t ast_cond_t;
149
150 enum ast_lock_type {
151         AST_MUTEX,
152         AST_RDLOCK,
153         AST_WRLOCK,
154 };
155
156 /*!
157  * \brief Store lock info for the current thread
158  *
159  * This function gets called in ast_mutex_lock() and ast_mutex_trylock() so
160  * that information about this lock can be stored in this thread's
161  * lock info struct.  The lock is marked as pending as the thread is waiting
162  * on the lock.  ast_mark_lock_acquired() will mark it as held by this thread.
163  */
164 #if !defined(LOW_MEMORY)
165 #ifdef HAVE_BKTR
166 void ast_store_lock_info(enum ast_lock_type type, const char *filename,
167         int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt);
168 #else
169 void ast_store_lock_info(enum ast_lock_type type, const char *filename,
170         int line_num, const char *func, const char *lock_name, void *lock_addr);
171 #endif /* HAVE_BKTR */
172
173 #else
174
175 #ifdef HAVE_BKTR
176 #define ast_store_lock_info(I,DONT,CARE,ABOUT,THE,PARAMETERS,BUD)
177 #else
178 #define ast_store_lock_info(I,DONT,CARE,ABOUT,THE,PARAMETERS)
179 #endif /* HAVE_BKTR */
180 #endif /* !defined(LOW_MEMORY) */
181
182 /*!
183  * \brief Mark the last lock as acquired
184  */
185 #if !defined(LOW_MEMORY)
186 void ast_mark_lock_acquired(void *lock_addr);
187 #else
188 #define ast_mark_lock_acquired(ignore)
189 #endif
190
191 /*!
192  * \brief Mark the last lock as failed (trylock)
193  */
194 #if !defined(LOW_MEMORY)
195 void ast_mark_lock_failed(void *lock_addr);
196 #else
197 #define ast_mark_lock_failed(ignore)
198 #endif
199
200 /*!
201  * \brief remove lock info for the current thread
202  *
203  * this gets called by ast_mutex_unlock so that information on the lock can
204  * be removed from the current thread's lock info struct.
205  */
206 #if !defined(LOW_MEMORY)
207 #ifdef HAVE_BKTR
208 void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt);
209 #else
210 void ast_remove_lock_info(void *lock_addr);
211 #endif /* HAVE_BKTR */
212 #else
213 #ifdef HAVE_BKTR
214 #define ast_remove_lock_info(ignore,me)
215 #else
216 #define ast_remove_lock_info(ignore)
217 #endif /* HAVE_BKTR */
218 #endif /* !defined(LOW_MEMORY) */
219
220 #ifdef HAVE_BKTR
221 static inline void __dump_backtrace(struct ast_bt *bt, int canlog)
222 {
223         char **strings;
224
225         ssize_t i;
226
227         strings = backtrace_symbols(bt->addresses, bt->num_frames);
228
229         for (i = 0; i < bt->num_frames; i++)
230                 __ast_mutex_logger("%s\n", strings[i]);
231
232         free(strings);
233 }
234 #endif
235
236 /*!
237  * \brief log info for the current lock with ast_log().
238  *
239  * this function would be mostly for debug. If you come across a lock
240  * that is unexpectedly but momentarily locked, and you wonder who
241  * are fighting with for the lock, this routine could be called, IF
242  * you have the thread debugging stuff turned on.
243  * \param this_lock_addr lock address to return lock information
244  * \since 1.6.1
245  */
246 void log_show_lock(void *this_lock_addr);
247
248 /*!
249  * \brief retrieve lock info for the specified mutex
250  *
251  * this gets called during deadlock avoidance, so that the information may
252  * be preserved as to what location originally acquired the lock.
253  */
254 #if !defined(LOW_MEMORY)
255 int ast_find_lock_info(void *lock_addr, char *filename, size_t filename_size, int *lineno, char *func, size_t func_size, char *mutex_name, size_t mutex_name_size);
256 #else
257 #define ast_find_lock_info(a,b,c,d,e,f,g,h) -1
258 #endif
259
260 /*!
261  * \brief Unlock a lock briefly
262  *
263  * used during deadlock avoidance, to preserve the original location where
264  * a lock was originally acquired.
265  */
266 #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
267         do { \
268                 char __filename[80], __func[80], __mutex_name[80]; \
269                 int __lineno; \
270                 int __res = ast_find_lock_info(&chan->lock_dont_use, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
271                 ast_channel_unlock(chan); \
272                 usleep(1); \
273                 if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
274                         ast_channel_lock(chan); \
275                 } else { \
276                         __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, &chan->lock_dont_use); \
277                 } \
278         } while (0)
279
280 #define DEADLOCK_AVOIDANCE(lock) \
281         do { \
282                 char __filename[80], __func[80], __mutex_name[80]; \
283                 int __lineno; \
284                 int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
285                 ast_mutex_unlock(lock); \
286                 usleep(1); \
287                 if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
288                         ast_mutex_lock(lock); \
289                 } else { \
290                         __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
291                 } \
292         } while (0)
293
294 /*!
295  * \brief Deadlock avoidance unlock
296  *
297  * In certain deadlock avoidance scenarios, there is more than one lock to be
298  * unlocked and relocked.  Therefore, this pair of macros is provided for that
299  * purpose.  Note that every DLA_UNLOCK _MUST_ be paired with a matching
300  * DLA_LOCK.  The intent of this pair of macros is to be used around another
301  * set of deadlock avoidance code, mainly CHANNEL_DEADLOCK_AVOIDANCE, as the
302  * locking order specifies that we may safely lock a channel, followed by its
303  * pvt, with no worries about a deadlock.  In any other scenario, this macro
304  * may not be safe to use.
305  */
306 #define DLA_UNLOCK(lock) \
307         do { \
308                 char __filename[80], __func[80], __mutex_name[80]; \
309                 int __lineno; \
310                 int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
311                 ast_mutex_unlock(lock);
312
313 /*!
314  * \brief Deadlock avoidance lock
315  *
316  * In certain deadlock avoidance scenarios, there is more than one lock to be
317  * unlocked and relocked.  Therefore, this pair of macros is provided for that
318  * purpose.  Note that every DLA_UNLOCK _MUST_ be paired with a matching
319  * DLA_LOCK.  The intent of this pair of macros is to be used around another
320  * set of deadlock avoidance code, mainly CHANNEL_DEADLOCK_AVOIDANCE, as the
321  * locking order specifies that we may safely lock a channel, followed by its
322  * pvt, with no worries about a deadlock.  In any other scenario, this macro
323  * may not be safe to use.
324  */
325 #define DLA_LOCK(lock) \
326                 if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
327                         ast_mutex_lock(lock); \
328                 } else { \
329                         __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
330                 } \
331         } while (0)
332
333 static inline void ast_reentrancy_lock(struct ast_lock_track *lt)
334 {
335         pthread_mutex_lock(&lt->reentr_mutex);
336 }
337
338 static inline void ast_reentrancy_unlock(struct ast_lock_track *lt)
339 {
340         pthread_mutex_unlock(&lt->reentr_mutex);
341 }
342
343 static inline void ast_reentrancy_init(struct ast_lock_track *lt)
344 {
345         int i;
346         pthread_mutexattr_t reentr_attr;
347
348         for (i = 0; i < AST_MAX_REENTRANCY; i++) {
349                 lt->file[i] = NULL;
350                 lt->lineno[i] = 0;
351                 lt->func[i] = NULL;
352                 lt->thread[i] = 0;
353 #ifdef HAVE_BKTR
354                 memset(&lt->backtrace[i], 0, sizeof(lt->backtrace[i]));
355 #endif
356         }
357
358         lt->reentrancy = 0;
359
360         pthread_mutexattr_init(&reentr_attr);
361         pthread_mutexattr_settype(&reentr_attr, AST_MUTEX_KIND);
362         pthread_mutex_init(&lt->reentr_mutex, &reentr_attr);
363         pthread_mutexattr_destroy(&reentr_attr);
364 }
365
366 static inline void delete_reentrancy_cs(struct ast_lock_track *lt)
367 {
368         pthread_mutex_destroy(&lt->reentr_mutex);
369 }
370
371 static inline int __ast_pthread_mutex_init(int tracking, const char *filename, int lineno, const char *func,
372                                                 const char *mutex_name, ast_mutex_t *t)
373 {
374         int res;
375         pthread_mutexattr_t  attr;
376
377 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
378
379         if ((t->mutex) != ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
380 /*
381                 int canlog = strcmp(filename, "logger.c") & track;
382                 __ast_mutex_logger("%s line %d (%s): NOTICE: mutex '%s' is already initialized.\n",
383                                    filename, lineno, func, mutex_name);
384                 DO_THREAD_CRASH;
385 */
386                 return 0;
387         }
388
389 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
390
391         ast_reentrancy_init(&t->track);
392         t->tracking = tracking;
393
394         pthread_mutexattr_init(&attr);
395         pthread_mutexattr_settype(&attr, AST_MUTEX_KIND);
396
397         res = pthread_mutex_init(&t->mutex, &attr);
398         pthread_mutexattr_destroy(&attr);
399         return res;
400 }
401
402 #define ast_mutex_init(pmutex) __ast_pthread_mutex_init(1, __FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex)
403 #define ast_mutex_init_notracking(pmutex) \
404         __ast_pthread_mutex_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex)
405
406 static inline int __ast_pthread_mutex_destroy(const char *filename, int lineno, const char *func,
407                                                 const char *mutex_name, ast_mutex_t *t)
408 {
409         int res;
410         struct ast_lock_track *lt;
411         int canlog = strcmp(filename, "logger.c") & t->tracking;
412
413 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
414         if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
415                 /* Don't try to uninitialize non initialized mutex
416                  * This may no effect on linux
417                  * And always ganerate core on *BSD with
418                  * linked libpthread
419                  * This not error condition if the mutex created on the fly.
420                  */
421                 __ast_mutex_logger("%s line %d (%s): NOTICE: mutex '%s' is uninitialized.\n",
422                                    filename, lineno, func, mutex_name);
423                 return 0;
424         }
425 #endif
426
427         lt = &t->track;
428
429         res = pthread_mutex_trylock(&t->mutex);
430         switch (res) {
431         case 0:
432                 pthread_mutex_unlock(&t->mutex);
433                 break;
434         case EINVAL:
435                 __ast_mutex_logger("%s line %d (%s): Error: attempt to destroy invalid mutex '%s'.\n",
436                                   filename, lineno, func, mutex_name);
437                 break;
438         case EBUSY:
439                 __ast_mutex_logger("%s line %d (%s): Error: attempt to destroy locked mutex '%s'.\n",
440                                    filename, lineno, func, mutex_name);
441                 ast_reentrancy_lock(lt);
442                 __ast_mutex_logger("%s line %d (%s): Error: '%s' was locked here.\n",
443                             lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1], lt->func[lt->reentrancy-1], mutex_name);
444 #ifdef HAVE_BKTR
445                 __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
446 #endif
447                 ast_reentrancy_unlock(lt);
448                 break;
449         }
450
451
452         if ((res = pthread_mutex_destroy(&t->mutex))) {
453                 __ast_mutex_logger("%s line %d (%s): Error destroying mutex %s: %s\n",
454                                    filename, lineno, func, mutex_name, strerror(res));
455         }
456 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
457         else
458                 t->mutex = PTHREAD_MUTEX_INIT_VALUE;
459 #endif
460         ast_reentrancy_lock(lt);
461         lt->file[0] = filename;
462         lt->lineno[0] = lineno;
463         lt->func[0] = func;
464         lt->reentrancy = 0;
465         lt->thread[0] = 0;
466 #ifdef HAVE_BKTR
467         memset(&lt->backtrace[0], 0, sizeof(lt->backtrace[0]));
468 #endif
469         ast_reentrancy_unlock(lt);
470         delete_reentrancy_cs(lt);
471
472         return res;
473 }
474
475 static inline int __ast_pthread_mutex_lock(const char *filename, int lineno, const char *func,
476                                            const char* mutex_name, ast_mutex_t *t)
477 {
478         int res;
479         struct ast_lock_track *lt = &t->track;
480         int canlog = strcmp(filename, "logger.c") & t->tracking;
481 #ifdef HAVE_BKTR
482         struct ast_bt *bt = NULL;
483 #endif
484
485 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
486         if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
487                 /* Don't warn abount uninitialized mutex.
488                  * Simple try to initialize it.
489                  * May be not needed in linux system.
490                  */
491                 res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
492                 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
493                         __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
494                                          filename, lineno, func, mutex_name);
495                         return res;
496                 }
497         }
498 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
499
500         if (t->tracking) {
501 #ifdef HAVE_BKTR
502                 ast_reentrancy_lock(lt);
503                 if (lt->reentrancy != AST_MAX_REENTRANCY) {
504                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
505                         bt = &lt->backtrace[lt->reentrancy];
506                 }
507                 ast_reentrancy_unlock(lt);
508                 ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t, bt);
509 #else
510                 ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t);
511 #endif
512         }
513
514 #ifdef DETECT_DEADLOCKS
515         {
516                 time_t seconds = time(NULL);
517                 time_t wait_time, reported_wait = 0;
518                 do {
519 #ifdef  HAVE_MTX_PROFILE
520                         ast_mark(mtx_prof, 1);
521 #endif
522                         res = pthread_mutex_trylock(&t->mutex);
523 #ifdef  HAVE_MTX_PROFILE
524                         ast_mark(mtx_prof, 0);
525 #endif
526                         if (res == EBUSY) {
527                                 wait_time = time(NULL) - seconds;
528                                 if (wait_time > reported_wait && (wait_time % 5) == 0) {
529                                         __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for mutex '%s'?\n",
530                                                            filename, lineno, func, (int) wait_time, mutex_name);
531                                         ast_reentrancy_lock(lt);
532 #ifdef HAVE_BKTR
533                                         __dump_backtrace(&lt->backtrace[lt->reentrancy], canlog);
534 #endif
535                                         __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
536                                                            lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1],
537                                                            lt->func[lt->reentrancy-1], mutex_name);
538 #ifdef HAVE_BKTR
539                                         __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
540 #endif
541                                         ast_reentrancy_unlock(lt);
542                                         reported_wait = wait_time;
543                                 }
544                                 usleep(200);
545                         }
546                 } while (res == EBUSY);
547         }
548 #else
549 #ifdef  HAVE_MTX_PROFILE
550         ast_mark(mtx_prof, 1);
551         res = pthread_mutex_trylock(&t->mutex);
552         ast_mark(mtx_prof, 0);
553         if (res)
554 #endif
555         res = pthread_mutex_lock(&t->mutex);
556 #endif /* DETECT_DEADLOCKS */
557
558         if (!res) {
559                 ast_reentrancy_lock(lt);
560                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
561                         lt->file[lt->reentrancy] = filename;
562                         lt->lineno[lt->reentrancy] = lineno;
563                         lt->func[lt->reentrancy] = func;
564                         lt->thread[lt->reentrancy] = pthread_self();
565                         lt->reentrancy++;
566                 } else {
567                         __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
568                                                            filename, lineno, func, mutex_name);
569                 }
570                 ast_reentrancy_unlock(lt);
571                 if (t->tracking) {
572                         ast_mark_lock_acquired(t);
573                 }
574         } else {
575 #ifdef HAVE_BKTR
576                 if (lt->reentrancy) {
577                         ast_reentrancy_lock(lt);
578                         bt = &lt->backtrace[lt->reentrancy-1];
579                         ast_reentrancy_unlock(lt);
580                 } else {
581                         bt = NULL;
582                 }
583                 if (t->tracking) {
584                         ast_remove_lock_info(t, bt);
585                 }
586 #else
587                 if (t->tracking) {
588                         ast_remove_lock_info(t);
589                 }
590 #endif
591                 __ast_mutex_logger("%s line %d (%s): Error obtaining mutex: %s\n",
592                                    filename, lineno, func, strerror(res));
593                 DO_THREAD_CRASH;
594         }
595
596         return res;
597 }
598
599 static inline int __ast_pthread_mutex_trylock(const char *filename, int lineno, const char *func,
600                                               const char* mutex_name, ast_mutex_t *t)
601 {
602         int res;
603         struct ast_lock_track *lt= &t->track;
604         int canlog = strcmp(filename, "logger.c") & t->tracking;
605 #ifdef HAVE_BKTR
606         struct ast_bt *bt = NULL;
607 #endif
608
609 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
610         if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
611                 /* Don't warn abount uninitialized mutex.
612                  * Simple try to initialize it.
613                  * May be not needed in linux system.
614                  */
615                 res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
616                 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
617                         __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
618                                          filename, lineno, func, mutex_name);
619                         return res;
620                 }
621         }
622 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
623
624         if (t->tracking) {
625 #ifdef HAVE_BKTR
626                 ast_reentrancy_lock(lt);
627                 if (lt->reentrancy != AST_MAX_REENTRANCY) {
628                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
629                         bt = &lt->backtrace[lt->reentrancy];
630                 }
631                 ast_reentrancy_unlock(lt);
632                 ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t, bt);
633 #else
634                 ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t);
635 #endif
636         }
637
638         if (!(res = pthread_mutex_trylock(&t->mutex))) {
639                 ast_reentrancy_lock(lt);
640                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
641                         lt->file[lt->reentrancy] = filename;
642                         lt->lineno[lt->reentrancy] = lineno;
643                         lt->func[lt->reentrancy] = func;
644                         lt->thread[lt->reentrancy] = pthread_self();
645                         lt->reentrancy++;
646                 } else {
647                         __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
648                                            filename, lineno, func, mutex_name);
649                 }
650                 ast_reentrancy_unlock(lt);
651                 if (t->tracking) {
652                         ast_mark_lock_acquired(t);
653                 }
654         } else if (t->tracking) {
655                 ast_mark_lock_failed(t);
656         }
657
658         return res;
659 }
660
661 static inline int __ast_pthread_mutex_unlock(const char *filename, int lineno, const char *func,
662                                              const char *mutex_name, ast_mutex_t *t)
663 {
664         int res;
665         struct ast_lock_track *lt = &t->track;
666         int canlog = strcmp(filename, "logger.c") & t->tracking;
667 #ifdef HAVE_BKTR
668         struct ast_bt *bt = NULL;
669 #endif
670
671 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
672         if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
673                 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
674                                    filename, lineno, func, mutex_name);
675                 res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
676                 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
677                         __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
678                                          filename, lineno, func, mutex_name);
679                 }
680                 return res;
681         }
682 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
683
684         ast_reentrancy_lock(lt);
685         if (lt->reentrancy && (lt->thread[lt->reentrancy-1] != pthread_self())) {
686                 __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
687                                    filename, lineno, func, mutex_name);
688                 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
689                                    lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1], lt->func[lt->reentrancy-1], mutex_name);
690 #ifdef HAVE_BKTR
691                 __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
692 #endif
693                 DO_THREAD_CRASH;
694         }
695
696         if (--lt->reentrancy < 0) {
697                 __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
698                                    filename, lineno, func, mutex_name);
699                 lt->reentrancy = 0;
700         }
701
702         if (lt->reentrancy < AST_MAX_REENTRANCY) {
703                 lt->file[lt->reentrancy] = NULL;
704                 lt->lineno[lt->reentrancy] = 0;
705                 lt->func[lt->reentrancy] = NULL;
706                 lt->thread[lt->reentrancy] = 0;
707         }
708
709 #ifdef HAVE_BKTR
710         if (lt->reentrancy) {
711                 bt = &lt->backtrace[lt->reentrancy - 1];
712         }
713 #endif
714         ast_reentrancy_unlock(lt);
715
716         if (t->tracking) {
717 #ifdef HAVE_BKTR
718                 ast_remove_lock_info(t, bt);
719 #else
720                 ast_remove_lock_info(t);
721 #endif
722         }
723
724         if ((res = pthread_mutex_unlock(&t->mutex))) {
725                 __ast_mutex_logger("%s line %d (%s): Error releasing mutex: %s\n",
726                                    filename, lineno, func, strerror(res));
727                 DO_THREAD_CRASH;
728         }
729
730         return res;
731 }
732
733 static inline int __ast_cond_init(const char *filename, int lineno, const char *func,
734                                   const char *cond_name, ast_cond_t *cond, pthread_condattr_t *cond_attr)
735 {
736         return pthread_cond_init(cond, cond_attr);
737 }
738
739 static inline int __ast_cond_signal(const char *filename, int lineno, const char *func,
740                                     const char *cond_name, ast_cond_t *cond)
741 {
742         return pthread_cond_signal(cond);
743 }
744
745 static inline int __ast_cond_broadcast(const char *filename, int lineno, const char *func,
746                                        const char *cond_name, ast_cond_t *cond)
747 {
748         return pthread_cond_broadcast(cond);
749 }
750
751 static inline int __ast_cond_destroy(const char *filename, int lineno, const char *func,
752                                      const char *cond_name, ast_cond_t *cond)
753 {
754         return pthread_cond_destroy(cond);
755 }
756
757 static inline int __ast_cond_wait(const char *filename, int lineno, const char *func,
758                                   const char *cond_name, const char *mutex_name,
759                                   ast_cond_t *cond, ast_mutex_t *t)
760 {
761         int res;
762         struct ast_lock_track *lt= &t->track;
763         int canlog = strcmp(filename, "logger.c") & t->tracking;
764 #ifdef HAVE_BKTR
765         struct ast_bt *bt = NULL;
766 #endif
767
768 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
769         if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
770                 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
771                                    filename, lineno, func, mutex_name);
772                 res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
773                 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
774                         __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
775                                          filename, lineno, func, mutex_name);
776                 }
777                 return res;
778         }
779 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
780
781         ast_reentrancy_lock(lt);
782         if (lt->reentrancy && (lt->thread[lt->reentrancy-1] != pthread_self())) {
783                 __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
784                                    filename, lineno, func, mutex_name);
785                 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
786                                    lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1], lt->func[lt->reentrancy-1], mutex_name);
787 #ifdef HAVE_BKTR
788                 __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
789 #endif
790                 DO_THREAD_CRASH;
791         }
792
793         if (--lt->reentrancy < 0) {
794                 __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
795                                    filename, lineno, func, mutex_name);
796                 lt->reentrancy = 0;
797         }
798
799         if (lt->reentrancy < AST_MAX_REENTRANCY) {
800                 lt->file[lt->reentrancy] = NULL;
801                 lt->lineno[lt->reentrancy] = 0;
802                 lt->func[lt->reentrancy] = NULL;
803                 lt->thread[lt->reentrancy] = 0;
804         }
805
806 #ifdef HAVE_BKTR
807         if (lt->reentrancy) {
808                 bt = &lt->backtrace[lt->reentrancy - 1];
809         }
810 #endif
811         ast_reentrancy_unlock(lt);
812
813         if (t->tracking) {
814 #ifdef HAVE_BKTR
815                 ast_remove_lock_info(t, bt);
816 #else
817                 ast_remove_lock_info(t);
818 #endif
819         }
820
821         if ((res = pthread_cond_wait(cond, &t->mutex))) {
822                 __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n",
823                                    filename, lineno, func, strerror(res));
824                 DO_THREAD_CRASH;
825         } else {
826                 ast_reentrancy_lock(lt);
827                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
828                         lt->file[lt->reentrancy] = filename;
829                         lt->lineno[lt->reentrancy] = lineno;
830                         lt->func[lt->reentrancy] = func;
831                         lt->thread[lt->reentrancy] = pthread_self();
832 #ifdef HAVE_BKTR
833                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
834                         bt = &lt->backtrace[lt->reentrancy];
835 #endif
836                         lt->reentrancy++;
837                 } else {
838                         __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
839                                                            filename, lineno, func, mutex_name);
840                 }
841                 ast_reentrancy_unlock(lt);
842
843                 if (t->tracking) {
844 #ifdef HAVE_BKTR
845                         ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t, bt);
846 #else
847                         ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t);
848 #endif
849                 }
850         }
851
852         return res;
853 }
854
855 static inline int __ast_cond_timedwait(const char *filename, int lineno, const char *func,
856                                        const char *cond_name, const char *mutex_name, ast_cond_t *cond,
857                                        ast_mutex_t *t, const struct timespec *abstime)
858 {
859         int res;
860         struct ast_lock_track *lt = &t->track;
861         int canlog = strcmp(filename, "logger.c") & t->tracking;
862 #ifdef HAVE_BKTR
863         struct ast_bt *bt = NULL;
864 #endif
865
866 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
867         if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
868                 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
869                                    filename, lineno, func, mutex_name);
870                 res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
871                 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
872                         __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
873                                          filename, lineno, func, mutex_name);
874                 }
875                 return res;
876         }
877 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
878
879         ast_reentrancy_lock(lt);
880         if (lt->reentrancy && (lt->thread[lt->reentrancy-1] != pthread_self())) {
881                 __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
882                                    filename, lineno, func, mutex_name);
883                 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
884                                    lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1], lt->func[lt->reentrancy-1], mutex_name);
885 #ifdef HAVE_BKTR
886                 __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
887 #endif
888                 DO_THREAD_CRASH;
889         }
890
891         if (--lt->reentrancy < 0) {
892                 __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
893                                    filename, lineno, func, mutex_name);
894                 lt->reentrancy = 0;
895         }
896
897         if (lt->reentrancy < AST_MAX_REENTRANCY) {
898                 lt->file[lt->reentrancy] = NULL;
899                 lt->lineno[lt->reentrancy] = 0;
900                 lt->func[lt->reentrancy] = NULL;
901                 lt->thread[lt->reentrancy] = 0;
902         }
903 #ifdef HAVE_BKTR
904         if (lt->reentrancy) {
905                 bt = &lt->backtrace[lt->reentrancy - 1];
906         }
907 #endif
908         ast_reentrancy_unlock(lt);
909
910         if (t->tracking) {
911 #ifdef HAVE_BKTR
912                 ast_remove_lock_info(t, bt);
913 #else
914                 ast_remove_lock_info(t);
915 #endif
916         }
917
918         if ((res = pthread_cond_timedwait(cond, &t->mutex, abstime)) && (res != ETIMEDOUT)) {
919                 __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n",
920                                    filename, lineno, func, strerror(res));
921                 DO_THREAD_CRASH;
922         } else {
923                 ast_reentrancy_lock(lt);
924                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
925                         lt->file[lt->reentrancy] = filename;
926                         lt->lineno[lt->reentrancy] = lineno;
927                         lt->func[lt->reentrancy] = func;
928                         lt->thread[lt->reentrancy] = pthread_self();
929 #ifdef HAVE_BKTR
930                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
931                         bt = &lt->backtrace[lt->reentrancy];
932 #endif
933                         lt->reentrancy++;
934                 } else {
935                         __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
936                                                            filename, lineno, func, mutex_name);
937                 }
938                 ast_reentrancy_unlock(lt);
939
940                 if (t->tracking) {
941 #ifdef HAVE_BKTR
942                         ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t, bt);
943 #else
944                         ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t);
945 #endif
946                 }
947         }
948
949         return res;
950 }
951
952 #define ast_mutex_destroy(a)                    __ast_pthread_mutex_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
953 #define ast_mutex_lock(a)                       __ast_pthread_mutex_lock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
954 #define ast_mutex_unlock(a)                     __ast_pthread_mutex_unlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
955 #define ast_mutex_trylock(a)                    __ast_pthread_mutex_trylock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
956 #define ast_cond_init(cond, attr)               __ast_cond_init(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond, attr)
957 #define ast_cond_destroy(cond)                  __ast_cond_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
958 #define ast_cond_signal(cond)                   __ast_cond_signal(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
959 #define ast_cond_broadcast(cond)                __ast_cond_broadcast(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
960 #define ast_cond_wait(cond, mutex)              __ast_cond_wait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex)
961 #define ast_cond_timedwait(cond, mutex, time)   __ast_cond_timedwait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex, time)
962
963 struct ast_rwlock_info {
964         /*! Track which thread holds this lock */
965         struct ast_lock_track track;
966         unsigned int tracking:1;
967         pthread_rwlock_t lock;
968 };
969
970 typedef struct ast_rwlock_info ast_rwlock_t;
971
972 /*!
973  * \brief wrapper for rwlock with tracking enabled
974  * \return 0 on success, non zero for error
975  * \since 1.6.1
976  */
977 #define ast_rwlock_init(rwlock) __ast_rwlock_init(1, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
978
979 /*!
980  * \brief wrapper for ast_rwlock_init with tracking disabled
981  * \return 0 on success, non zero for error
982  * \since 1.6.1
983  */
984 #define ast_rwlock_init_notracking(rwlock) __ast_rwlock_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
985
986 #define ast_rwlock_destroy(rwlock)      __ast_rwlock_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
987 #define ast_rwlock_unlock(a)            _ast_rwlock_unlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
988 #define ast_rwlock_rdlock(a)            _ast_rwlock_rdlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
989 #define ast_rwlock_wrlock(a)            _ast_rwlock_wrlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
990 #define ast_rwlock_tryrdlock(a)         _ast_rwlock_tryrdlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
991 #define ast_rwlock_trywrlock(a) _ast_rwlock_trywrlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
992
993
994 #ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
995 #define __AST_RWLOCK_INIT_VALUE PTHREAD_RWLOCK_INITIALIZER
996 #else  /* HAVE_PTHREAD_RWLOCK_INITIALIZER */
997 #define __AST_RWLOCK_INIT_VALUE {0}
998 #endif /* HAVE_PTHREAD_RWLOCK_INITIALIZER */
999
1000 #define AST_RWLOCK_INIT_VALUE \
1001         { AST_LOCK_TRACK_INIT_VALUE, 1, __AST_RWLOCK_INIT_VALUE }
1002 #define AST_RWLOCK_INIT_VALUE_NOTRACKING \
1003         { AST_LOCK_TRACK_INIT_VALUE, 0, __AST_RWLOCK_INIT_VALUE }
1004
1005 static inline int __ast_rwlock_init(int tracking, const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
1006 {
1007         int res;
1008         struct ast_lock_track *lt= &t->track;
1009         pthread_rwlockattr_t attr;
1010
1011 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1012         int canlog = strcmp(filename, "logger.c") & t->tracking;
1013
1014         if (t->lock != ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1015                 __ast_mutex_logger("%s line %d (%s): Warning: rwlock '%s' is already initialized.\n",
1016                                 filename, lineno, func, rwlock_name);
1017                 return 0;
1018         }
1019 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1020
1021         ast_reentrancy_init(lt);
1022         t->tracking = tracking;
1023         pthread_rwlockattr_init(&attr);
1024
1025 #ifdef HAVE_PTHREAD_RWLOCK_PREFER_WRITER_NP
1026         pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP);
1027 #endif
1028
1029         res = pthread_rwlock_init(&t->lock, &attr);
1030         pthread_rwlockattr_destroy(&attr);
1031         return res;
1032 }
1033
1034 static inline int __ast_rwlock_destroy(const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
1035 {
1036         int res;
1037         struct ast_lock_track *lt = &t->track;
1038         int canlog = strcmp(filename, "logger.c") & t->tracking;
1039
1040 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1041         if (t->lock == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1042                 __ast_mutex_logger("%s line %d (%s): Warning: rwlock '%s' is uninitialized.\n",
1043                                    filename, lineno, func, rwlock_name);
1044                 return 0;
1045         }
1046 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1047
1048         if ((res = pthread_rwlock_destroy(&t->lock))) {
1049                 __ast_mutex_logger("%s line %d (%s): Error destroying rwlock %s: %s\n",
1050                                 filename, lineno, func, rwlock_name, strerror(res));
1051         }
1052         ast_reentrancy_lock(lt);
1053         lt->file[0] = filename;
1054         lt->lineno[0] = lineno;
1055         lt->func[0] = func;
1056         lt->reentrancy = 0;
1057         lt->thread[0] = 0;
1058 #ifdef HAVE_BKTR
1059         memset(&lt->backtrace[0], 0, sizeof(lt->backtrace[0]));
1060 #endif
1061         ast_reentrancy_unlock(lt);
1062         delete_reentrancy_cs(lt);
1063
1064         return res;
1065 }
1066
1067 static inline int _ast_rwlock_unlock(ast_rwlock_t *t, const char *name,
1068         const char *filename, int line, const char *func)
1069 {
1070         int res;
1071         struct ast_lock_track *lt = &t->track;
1072         int canlog = strcmp(filename, "logger.c") & t->tracking;
1073 #ifdef HAVE_BKTR
1074         struct ast_bt *bt = NULL;
1075 #endif
1076         int lock_found = 0;
1077
1078
1079 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1080         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1081                 __ast_mutex_logger("%s line %d (%s): Warning: rwlock '%s' is uninitialized.\n",
1082                                    filename, line, func, name);
1083                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1084                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1085                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1086                                         filename, line, func, name);
1087                 }
1088                 return res;
1089         }
1090 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1091
1092         ast_reentrancy_lock(lt);
1093         if (lt->reentrancy) {
1094                 int i;
1095                 pthread_t self = pthread_self();
1096                 for (i = lt->reentrancy - 1; i >= 0; --i) {
1097                         if (lt->thread[i] == self) {
1098                                 lock_found = 1;
1099                                 if (i != lt->reentrancy - 1) {
1100                                         lt->file[i] = lt->file[lt->reentrancy - 1];
1101                                         lt->lineno[i] = lt->lineno[lt->reentrancy - 1];
1102                                         lt->func[i] = lt->func[lt->reentrancy - 1];
1103                                         lt->thread[i] = lt->thread[lt->reentrancy - 1];
1104                                 }
1105 #ifdef HAVE_BKTR
1106                                 bt = &lt->backtrace[i];
1107 #endif
1108                                 lt->file[lt->reentrancy - 1] = NULL;
1109                                 lt->lineno[lt->reentrancy - 1] = 0;
1110                                 lt->func[lt->reentrancy - 1] = NULL;
1111                                 lt->thread[lt->reentrancy - 1] = AST_PTHREADT_NULL;
1112                                 break;
1113                         }
1114                 }
1115         }
1116
1117         if (lock_found && --lt->reentrancy < 0) {
1118                 __ast_mutex_logger("%s line %d (%s): rwlock '%s' freed more times than we've locked!\n",
1119                                 filename, line, func, name);
1120                 lt->reentrancy = 0;
1121         }
1122
1123         ast_reentrancy_unlock(lt);
1124
1125         if (t->tracking) {
1126 #ifdef HAVE_BKTR
1127                 ast_remove_lock_info(t, bt);
1128 #else
1129                 ast_remove_lock_info(t);
1130 #endif
1131         }
1132
1133         if ((res = pthread_rwlock_unlock(&t->lock))) {
1134                 __ast_mutex_logger("%s line %d (%s): Error releasing rwlock: %s\n",
1135                                 filename, line, func, strerror(res));
1136                 DO_THREAD_CRASH;
1137         }
1138
1139         return res;
1140 }
1141
1142 static inline int _ast_rwlock_rdlock(ast_rwlock_t *t, const char *name,
1143         const char *filename, int line, const char *func)
1144 {
1145         int res;
1146         struct ast_lock_track *lt = &t->track;
1147         int canlog = strcmp(filename, "logger.c") & t->tracking;
1148 #ifdef HAVE_BKTR
1149         struct ast_bt *bt = NULL;
1150 #endif
1151
1152 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1153         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1154                  /* Don't warn abount uninitialized lock.
1155                   * Simple try to initialize it.
1156                   * May be not needed in linux system.
1157                   */
1158                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1159                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1160                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1161                                         filename, line, func, name);
1162                         return res;
1163                 }
1164         }
1165 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1166
1167         if (t->tracking) {
1168 #ifdef HAVE_BKTR
1169                 ast_reentrancy_lock(lt);
1170                 if (lt->reentrancy != AST_MAX_REENTRANCY) {
1171                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
1172                         bt = &lt->backtrace[lt->reentrancy];
1173                 }
1174                 ast_reentrancy_unlock(lt);
1175                 ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t, bt);
1176 #else
1177                 ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t);
1178 #endif
1179         }
1180
1181 #ifdef DETECT_DEADLOCKS
1182         {
1183                 time_t seconds = time(NULL);
1184                 time_t wait_time, reported_wait = 0;
1185                 do {
1186                         res = pthread_rwlock_tryrdlock(&t->lock);
1187                         if (res == EBUSY) {
1188                                 wait_time = time(NULL) - seconds;
1189                                 if (wait_time > reported_wait && (wait_time % 5) == 0) {
1190                                         __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for readlock '%s'?\n",
1191                                                 filename, line, func, (int)wait_time, name);
1192                                         ast_reentrancy_lock(lt);
1193 #ifdef HAVE_BKTR
1194                                         __dump_backtrace(&lt->backtrace[lt->reentrancy], canlog);
1195 #endif
1196                                         __ast_mutex_logger("%s line %d (%s): '%s' was locked  here.\n",
1197                                                         lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1],
1198                                                         lt->func[lt->reentrancy-1], name);
1199 #ifdef HAVE_BKTR
1200                                         __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
1201 #endif
1202                                         ast_reentrancy_unlock(lt);
1203                                         reported_wait = wait_time;
1204                                 }
1205                                 usleep(200);
1206                         }
1207                 } while (res == EBUSY);
1208         }
1209 #else /* !DETECT_DEADLOCKS */
1210         res = pthread_rwlock_rdlock(&t->lock);
1211 #endif /* !DETECT_DEADLOCKS */
1212
1213         if (!res) {
1214                 ast_reentrancy_lock(lt);
1215                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
1216                         lt->file[lt->reentrancy] = filename;
1217                         lt->lineno[lt->reentrancy] = line;
1218                         lt->func[lt->reentrancy] = func;
1219                         lt->thread[lt->reentrancy] = pthread_self();
1220                         lt->reentrancy++;
1221                 }
1222                 ast_reentrancy_unlock(lt);
1223                 if (t->tracking) {
1224                         ast_mark_lock_acquired(t);
1225                 }
1226         } else {
1227 #ifdef HAVE_BKTR
1228                 if (lt->reentrancy) {
1229                         ast_reentrancy_lock(lt);
1230                         bt = &lt->backtrace[lt->reentrancy-1];
1231                         ast_reentrancy_unlock(lt);
1232                 } else {
1233                         bt = NULL;
1234                 }
1235                 if (t->tracking) {
1236                         ast_remove_lock_info(t, bt);
1237                 }
1238 #else
1239                 if (t->tracking) {
1240                         ast_remove_lock_info(t);
1241                 }
1242 #endif
1243                 __ast_mutex_logger("%s line %d (%s): Error obtaining read lock: %s\n",
1244                                 filename, line, func, strerror(res));
1245                 DO_THREAD_CRASH;
1246         }
1247         return res;
1248 }
1249
1250 static inline int _ast_rwlock_wrlock(ast_rwlock_t *t, const char *name,
1251         const char *filename, int line, const char *func)
1252 {
1253         int res;
1254         struct ast_lock_track *lt = &t->track;
1255         int canlog = strcmp(filename, "logger.c") & t->tracking;
1256 #ifdef HAVE_BKTR
1257         struct ast_bt *bt = NULL;
1258 #endif
1259
1260 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1261         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1262                  /* Don't warn abount uninitialized lock.
1263                   * Simple try to initialize it.
1264                   * May be not needed in linux system.
1265                   */
1266                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1267                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1268                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1269                                         filename, line, func, name);
1270                         return res;
1271                 }
1272         }
1273 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1274
1275         if (t->tracking) {
1276 #ifdef HAVE_BKTR
1277                 ast_reentrancy_lock(lt);
1278                 if (lt->reentrancy != AST_MAX_REENTRANCY) {
1279                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
1280                         bt = &lt->backtrace[lt->reentrancy];
1281                 }
1282                 ast_reentrancy_unlock(lt);
1283                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
1284 #else
1285                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
1286 #endif
1287         }
1288 #ifdef DETECT_DEADLOCKS
1289         {
1290                 time_t seconds = time(NULL);
1291                 time_t wait_time, reported_wait = 0;
1292                 do {
1293                         res = pthread_rwlock_trywrlock(&t->lock);
1294                         if (res == EBUSY) {
1295                                 wait_time = time(NULL) - seconds;
1296                                 if (wait_time > reported_wait && (wait_time % 5) == 0) {
1297                                         __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for writelock '%s'?\n",
1298                                                 filename, line, func, (int)wait_time, name);
1299                                         ast_reentrancy_lock(lt);
1300 #ifdef HAVE_BKTR
1301                                         __dump_backtrace(&lt->backtrace[lt->reentrancy], canlog);
1302 #endif
1303                                         __ast_mutex_logger("%s line %d (%s): '%s' was locked  here.\n",
1304                                                         lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1],
1305                                                         lt->func[lt->reentrancy-1], name);
1306 #ifdef HAVE_BKTR
1307                                         __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
1308 #endif
1309                                         ast_reentrancy_unlock(lt);
1310                                         reported_wait = wait_time;
1311                                 }
1312                                 usleep(200);
1313                         }
1314                 } while (res == EBUSY);
1315         }
1316 #else /* !DETECT_DEADLOCKS */
1317         res = pthread_rwlock_wrlock(&t->lock);
1318 #endif /* !DETECT_DEADLOCKS */
1319
1320         if (!res) {
1321                 ast_reentrancy_lock(lt);
1322                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
1323                         lt->file[lt->reentrancy] = filename;
1324                         lt->lineno[lt->reentrancy] = line;
1325                         lt->func[lt->reentrancy] = func;
1326                         lt->thread[lt->reentrancy] = pthread_self();
1327                         lt->reentrancy++;
1328                 }
1329                 ast_reentrancy_unlock(lt);
1330                 if (t->tracking) {
1331                         ast_mark_lock_acquired(t);
1332                 }
1333         } else {
1334 #ifdef HAVE_BKTR
1335                 if (lt->reentrancy) {
1336                         ast_reentrancy_lock(lt);
1337                         bt = &lt->backtrace[lt->reentrancy-1];
1338                         ast_reentrancy_unlock(lt);
1339                 } else {
1340                         bt = NULL;
1341                 }
1342                 if (t->tracking) {
1343                         ast_remove_lock_info(t, bt);
1344                 }
1345 #else
1346                 if (t->tracking) {
1347                         ast_remove_lock_info(t);
1348                 }
1349 #endif
1350                 __ast_mutex_logger("%s line %d (%s): Error obtaining write lock: %s\n",
1351                                 filename, line, func, strerror(res));
1352                 DO_THREAD_CRASH;
1353         }
1354         return res;
1355 }
1356
1357 #define ast_rwlock_timedrdlock(a, b) \
1358         _ast_rwlock_timedrdlock(a, # a, b, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1359
1360 static inline int _ast_rwlock_timedrdlock(ast_rwlock_t *t, const char *name,
1361         const struct timespec *abs_timeout, const char *filename, int line, const char *func)
1362 {
1363         int res;
1364         struct ast_lock_track *lt = &t->track;
1365         int canlog = strcmp(filename, "logger.c") & t->tracking;
1366 #ifdef HAVE_BKTR
1367         struct ast_bt *bt = NULL;
1368 #endif
1369
1370 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1371         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1372                  /* Don't warn abount uninitialized lock.
1373                   * Simple try to initialize it.
1374                   * May be not needed in linux system.
1375                   */
1376                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1377                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1378                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1379                                         filename, line, func, name);
1380                         return res;
1381                 }
1382         }
1383 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1384
1385         if (t->tracking) {
1386 #ifdef HAVE_BKTR
1387                 ast_reentrancy_lock(lt);
1388                 if (lt->reentrancy != AST_MAX_REENTRANCY) {
1389                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
1390                         bt = &lt->backtrace[lt->reentrancy];
1391                 }
1392                 ast_reentrancy_unlock(lt);
1393                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
1394 #else
1395                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
1396 #endif
1397         }
1398         res = pthread_rwlock_timedrdlock(&t->lock, abs_timeout);
1399         if (!res) {
1400                 ast_reentrancy_lock(lt);
1401                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
1402                         lt->file[lt->reentrancy] = filename;
1403                         lt->lineno[lt->reentrancy] = line;
1404                         lt->func[lt->reentrancy] = func;
1405                         lt->thread[lt->reentrancy] = pthread_self();
1406                         lt->reentrancy++;
1407                 }
1408                 ast_reentrancy_unlock(lt);
1409                 if (t->tracking) {
1410                         ast_mark_lock_acquired(t);
1411                 }
1412         } else {
1413 #ifdef HAVE_BKTR
1414                 if (lt->reentrancy) {
1415                         ast_reentrancy_lock(lt);
1416                         bt = &lt->backtrace[lt->reentrancy-1];
1417                         ast_reentrancy_unlock(lt);
1418                 } else {
1419                         bt = NULL;
1420                 }
1421                 if (t->tracking) {
1422                         ast_remove_lock_info(t, bt);
1423                 }
1424 #else
1425                 if (t->tracking) {
1426                         ast_remove_lock_info(t);
1427                 }
1428 #endif
1429                 __ast_mutex_logger("%s line %d (%s): Error obtaining read lock: %s\n",
1430                                 filename, line, func, strerror(res));
1431                 DO_THREAD_CRASH;
1432         }
1433         return res;
1434 }
1435
1436 #define ast_rwlock_timedwrlock(a, b) \
1437         _ast_rwlock_timedwrlock(a, # a, b, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1438
1439 static inline int _ast_rwlock_timedwrlock(ast_rwlock_t *t, const char *name,
1440         const struct timespec *abs_timeout, const char *filename, int line, const char *func)
1441 {
1442         int res;
1443         struct ast_lock_track *lt = &t->track;
1444         int canlog = strcmp(filename, "logger.c") & t->tracking;
1445 #ifdef HAVE_BKTR
1446         struct ast_bt *bt = NULL;
1447 #endif
1448
1449 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1450         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1451                  /* Don't warn abount uninitialized lock.
1452                   * Simple try to initialize it.
1453                   * May be not needed in linux system.
1454                   */
1455                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1456                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1457                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1458                                         filename, line, func, name);
1459                         return res;
1460                 }
1461         }
1462 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1463
1464         if (t->tracking) {
1465 #ifdef HAVE_BKTR
1466                 ast_reentrancy_lock(lt);
1467                 if (lt->reentrancy != AST_MAX_REENTRANCY) {
1468                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
1469                         bt = &lt->backtrace[lt->reentrancy];
1470                 }
1471                 ast_reentrancy_unlock(lt);
1472                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
1473 #else
1474                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
1475 #endif
1476         }
1477         res = pthread_rwlock_timedwrlock(&t->lock, abs_timeout);
1478         if (!res) {
1479                 ast_reentrancy_lock(lt);
1480                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
1481                         lt->file[lt->reentrancy] = filename;
1482                         lt->lineno[lt->reentrancy] = line;
1483                         lt->func[lt->reentrancy] = func;
1484                         lt->thread[lt->reentrancy] = pthread_self();
1485                         lt->reentrancy++;
1486                 }
1487                 ast_reentrancy_unlock(lt);
1488                 if (t->tracking) {
1489                         ast_mark_lock_acquired(t);
1490                 }
1491         } else {
1492 #ifdef HAVE_BKTR
1493                 if (lt->reentrancy) {
1494                         ast_reentrancy_lock(lt);
1495                         bt = &lt->backtrace[lt->reentrancy-1];
1496                         ast_reentrancy_unlock(lt);
1497                 } else {
1498                         bt = NULL;
1499                 }
1500                 if (t->tracking) {
1501                         ast_remove_lock_info(t, bt);
1502                 }
1503 #else
1504                 if (t->tracking) {
1505                         ast_remove_lock_info(t);
1506                 }
1507 #endif
1508                 __ast_mutex_logger("%s line %d (%s): Error obtaining read lock: %s\n",
1509                                 filename, line, func, strerror(res));
1510                 DO_THREAD_CRASH;
1511         }
1512         return res;
1513 }
1514
1515 static inline int _ast_rwlock_tryrdlock(ast_rwlock_t *t, const char *name,
1516         const char *filename, int line, const char *func)
1517 {
1518         int res;
1519         struct ast_lock_track *lt = &t->track;
1520 #ifdef HAVE_BKTR
1521         struct ast_bt *bt = NULL;
1522 #endif
1523
1524
1525 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1526         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1527                  /* Don't warn abount uninitialized lock.
1528                   * Simple try to initialize it.
1529                   * May be not needed in linux system.
1530                   */
1531                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1532                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1533                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1534                                         filename, line, func, name);
1535                         return res;
1536                 }
1537         }
1538 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1539
1540         if (t->tracking) {
1541 #ifdef HAVE_BKTR
1542                 ast_reentrancy_lock(lt);
1543                 if (lt->reentrancy != AST_MAX_REENTRANCY) {
1544                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
1545                         bt = &lt->backtrace[lt->reentrancy];
1546                 }
1547                 ast_reentrancy_unlock(lt);
1548                 ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t, bt);
1549 #else
1550                 ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t);
1551 #endif
1552         }
1553
1554         if (!(res = pthread_rwlock_tryrdlock(&t->lock))) {
1555                 ast_reentrancy_lock(lt);
1556                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
1557                         lt->file[lt->reentrancy] = filename;
1558                         lt->lineno[lt->reentrancy] = line;
1559                         lt->func[lt->reentrancy] = func;
1560                         lt->thread[lt->reentrancy] = pthread_self();
1561                         lt->reentrancy++;
1562                 }
1563                 ast_reentrancy_unlock(lt);
1564                 if (t->tracking) {
1565                         ast_mark_lock_acquired(t);
1566                 }
1567         } else if (t->tracking) {
1568                 ast_mark_lock_failed(t);
1569         }
1570         return res;
1571 }
1572
1573 static inline int _ast_rwlock_trywrlock(ast_rwlock_t *t, const char *name,
1574         const char *filename, int line, const char *func)
1575 {
1576         int res;
1577         struct ast_lock_track *lt= &t->track;
1578 #ifdef HAVE_BKTR
1579         struct ast_bt *bt = NULL;
1580 #endif
1581
1582
1583 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1584         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1585                  /* Don't warn abount uninitialized lock.
1586                   * Simple try to initialize it.
1587                   * May be not needed in linux system.
1588                   */
1589                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1590                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1591                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1592                                         filename, line, func, name);
1593                         return res;
1594                 }
1595         }
1596 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1597
1598         if (t->tracking) {
1599 #ifdef HAVE_BKTR
1600                 ast_reentrancy_lock(lt);
1601                 if (lt->reentrancy != AST_MAX_REENTRANCY) {
1602                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
1603                         bt = &lt->backtrace[lt->reentrancy];
1604                 }
1605                 ast_reentrancy_unlock(lt);
1606                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
1607 #else
1608                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
1609 #endif
1610         }
1611
1612         if (!(res = pthread_rwlock_trywrlock(&t->lock))) {
1613                 ast_reentrancy_lock(lt);
1614                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
1615                         lt->file[lt->reentrancy] = filename;
1616                         lt->lineno[lt->reentrancy] = line;
1617                         lt->func[lt->reentrancy] = func;
1618                         lt->thread[lt->reentrancy] = pthread_self();
1619                         lt->reentrancy++;
1620                 }
1621                 ast_reentrancy_unlock(lt);
1622                 if (t->tracking) {
1623                         ast_mark_lock_acquired(t);
1624                 }
1625         } else if (t->tracking) {
1626                 ast_mark_lock_failed(t);
1627         }
1628         return res;
1629 }
1630
1631 #else /* !DEBUG_THREADS */
1632
1633 #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
1634         ast_channel_unlock(chan); \
1635         usleep(1); \
1636         ast_channel_lock(chan);
1637
1638 #define DEADLOCK_AVOIDANCE(lock) \
1639         ast_mutex_unlock(lock); \
1640         usleep(1); \
1641         ast_mutex_lock(lock);
1642
1643 #define DLA_UNLOCK(lock)        ast_mutex_unlock(lock)
1644
1645 #define DLA_LOCK(lock)  ast_mutex_lock(lock)
1646
1647 typedef pthread_mutex_t ast_mutex_t;
1648
1649 #define AST_MUTEX_INIT_VALUE                    ((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
1650 #define AST_MUTEX_INIT_VALUE_NOTRACKING         ((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
1651
1652 #define ast_mutex_init_notracking(m)            ast_mutex_init(m)
1653
1654 static inline int ast_mutex_init(ast_mutex_t *pmutex)
1655 {
1656         int res;
1657         pthread_mutexattr_t attr;
1658
1659         pthread_mutexattr_init(&attr);
1660         pthread_mutexattr_settype(&attr, AST_MUTEX_KIND);
1661
1662         res = pthread_mutex_init(pmutex, &attr);
1663         pthread_mutexattr_destroy(&attr);
1664         return res;
1665 }
1666
1667 #define ast_pthread_mutex_init(pmutex,a) pthread_mutex_init(pmutex,a)
1668
1669 static inline int ast_mutex_unlock(ast_mutex_t *pmutex)
1670 {
1671         return pthread_mutex_unlock(pmutex);
1672 }
1673
1674 static inline int ast_mutex_destroy(ast_mutex_t *pmutex)
1675 {
1676         return pthread_mutex_destroy(pmutex);
1677 }
1678
1679 static inline int ast_mutex_lock(ast_mutex_t *pmutex)
1680 {
1681         __MTX_PROF(pmutex);
1682 }
1683
1684 static inline int ast_mutex_trylock(ast_mutex_t *pmutex)
1685 {
1686         return pthread_mutex_trylock(pmutex);
1687 }
1688
1689 typedef pthread_cond_t ast_cond_t;
1690
1691 static inline int ast_cond_init(ast_cond_t *cond, pthread_condattr_t *cond_attr)
1692 {
1693         return pthread_cond_init(cond, cond_attr);
1694 }
1695
1696 static inline int ast_cond_signal(ast_cond_t *cond)
1697 {
1698         return pthread_cond_signal(cond);
1699 }
1700
1701 static inline int ast_cond_broadcast(ast_cond_t *cond)
1702 {
1703         return pthread_cond_broadcast(cond);
1704 }
1705
1706 static inline int ast_cond_destroy(ast_cond_t *cond)
1707 {
1708         return pthread_cond_destroy(cond);
1709 }
1710
1711 static inline int ast_cond_wait(ast_cond_t *cond, ast_mutex_t *t)
1712 {
1713         return pthread_cond_wait(cond, t);
1714 }
1715
1716 static inline int ast_cond_timedwait(ast_cond_t *cond, ast_mutex_t *t, const struct timespec *abstime)
1717 {
1718         return pthread_cond_timedwait(cond, t, abstime);
1719 }
1720
1721
1722 typedef pthread_rwlock_t ast_rwlock_t;
1723
1724 #ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
1725 #define AST_RWLOCK_INIT_VALUE PTHREAD_RWLOCK_INITIALIZER
1726 #else
1727 #define AST_RWLOCK_INIT_VALUE { 0 }
1728 #endif
1729
1730 #define ast_rwlock_init_notracking(a) ast_rwlock_init(a)
1731
1732 static inline int ast_rwlock_init(ast_rwlock_t *prwlock)
1733 {
1734         int res;
1735         pthread_rwlockattr_t attr;
1736
1737         pthread_rwlockattr_init(&attr);
1738
1739 #ifdef HAVE_PTHREAD_RWLOCK_PREFER_WRITER_NP
1740         pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP);
1741 #endif
1742
1743         res = pthread_rwlock_init(prwlock, &attr);
1744         pthread_rwlockattr_destroy(&attr);
1745         return res;
1746 }
1747
1748 static inline int ast_rwlock_destroy(ast_rwlock_t *prwlock)
1749 {
1750         return pthread_rwlock_destroy(prwlock);
1751 }
1752
1753 static inline int ast_rwlock_unlock(ast_rwlock_t *prwlock)
1754 {
1755         return pthread_rwlock_unlock(prwlock);
1756 }
1757
1758 static inline int ast_rwlock_rdlock(ast_rwlock_t *prwlock)
1759 {
1760         return pthread_rwlock_rdlock(prwlock);
1761 }
1762
1763 static inline int ast_rwlock_timedrdlock(ast_rwlock_t *prwlock, const struct timespec *abs_timeout)
1764 {
1765         return pthread_rwlock_timedrdlock(prwlock, abs_timeout);
1766 }
1767
1768 static inline int ast_rwlock_tryrdlock(ast_rwlock_t *prwlock)
1769 {
1770         return pthread_rwlock_tryrdlock(prwlock);
1771 }
1772
1773 static inline int ast_rwlock_wrlock(ast_rwlock_t *prwlock)
1774 {
1775         return pthread_rwlock_wrlock(prwlock);
1776 }
1777
1778 static inline int ast_rwlock_timedwrlock(ast_rwlock_t *prwlock, const struct timespec *abs_timeout)
1779 {
1780         return pthread_rwlock_timedwrlock(prwlock, abs_timeout);
1781 }
1782
1783 static inline int ast_rwlock_trywrlock(ast_rwlock_t *prwlock)
1784 {
1785         return pthread_rwlock_trywrlock(prwlock);
1786 }
1787
1788 #endif /* !DEBUG_THREADS */
1789
1790 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
1791 /*
1792  * If AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope constructors
1793  * and destructors to create/destroy global mutexes.
1794  */
1795 #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track)       \
1796         scope ast_mutex_t mutex = init_val;                     \
1797 static void  __attribute__((constructor)) init_##mutex(void)    \
1798 {                                                               \
1799         if (track)                                              \
1800                 ast_mutex_init(&mutex);                         \
1801         else                                                    \
1802                 ast_mutex_init_notracking(&mutex);              \
1803 }                                                               \
1804                                                                 \
1805 static void  __attribute__((destructor)) fini_##mutex(void)     \
1806 {                                                               \
1807         ast_mutex_destroy(&mutex);                              \
1808 }
1809 #else /* !AST_MUTEX_INIT_W_CONSTRUCTORS */
1810 /* By default, use static initialization of mutexes. */
1811 #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track)       scope ast_mutex_t mutex = init_val
1812 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1813
1814 #ifndef __CYGWIN__      /* temporary disabled for cygwin */
1815 #define pthread_mutex_t         use_ast_mutex_t_instead_of_pthread_mutex_t
1816 #define pthread_cond_t          use_ast_cond_t_instead_of_pthread_cond_t
1817 #endif
1818 #define pthread_mutex_lock      use_ast_mutex_lock_instead_of_pthread_mutex_lock
1819 #define pthread_mutex_unlock    use_ast_mutex_unlock_instead_of_pthread_mutex_unlock
1820 #define pthread_mutex_trylock   use_ast_mutex_trylock_instead_of_pthread_mutex_trylock
1821 #define pthread_mutex_init      use_ast_mutex_init_instead_of_pthread_mutex_init
1822 #define pthread_mutex_destroy   use_ast_mutex_destroy_instead_of_pthread_mutex_destroy
1823 #define pthread_cond_init       use_ast_cond_init_instead_of_pthread_cond_init
1824 #define pthread_cond_destroy    use_ast_cond_destroy_instead_of_pthread_cond_destroy
1825 #define pthread_cond_signal     use_ast_cond_signal_instead_of_pthread_cond_signal
1826 #define pthread_cond_broadcast  use_ast_cond_broadcast_instead_of_pthread_cond_broadcast
1827 #define pthread_cond_wait       use_ast_cond_wait_instead_of_pthread_cond_wait
1828 #define pthread_cond_timedwait  use_ast_cond_timedwait_instead_of_pthread_cond_timedwait
1829
1830 #define AST_MUTEX_DEFINE_STATIC(mutex)                  __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE, 1)
1831 #define AST_MUTEX_DEFINE_STATIC_NOTRACKING(mutex)       __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE_NOTRACKING, 0)
1832
1833 #define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__
1834
1835 #define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
1836
1837 #ifndef __linux__
1838 #define pthread_create __use_ast_pthread_create_instead__
1839 #endif
1840
1841 /* Statically declared read/write locks */
1842
1843 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1844 #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) \
1845         scope ast_rwlock_t rwlock = init_val; \
1846 static void  __attribute__((constructor)) init_##rwlock(void) \
1847 { \
1848         if (track) \
1849                 ast_rwlock_init(&rwlock); \
1850         else \
1851                 ast_rwlock_init_notracking(&rwlock); \
1852 } \
1853 static void  __attribute__((destructor)) fini_##rwlock(void) \
1854 { \
1855         ast_rwlock_destroy(&rwlock); \
1856 }
1857 #else
1858 #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) \
1859         scope ast_rwlock_t rwlock = init_val
1860 #endif
1861
1862 #define AST_RWLOCK_DEFINE_STATIC(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE, 1)
1863 #define AST_RWLOCK_DEFINE_STATIC_NOTRACKING(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE_NOTRACKING, 0)
1864
1865 /*
1866  * Support for atomic instructions.
1867  * For platforms that have it, use the native cpu instruction to
1868  * implement them. For other platforms, resort to a 'slow' version
1869  * (defined in utils.c) that protects the atomic instruction with
1870  * a single lock.
1871  * The slow versions is always available, for testing purposes,
1872  * as ast_atomic_fetchadd_int_slow()
1873  */
1874
1875 int ast_atomic_fetchadd_int_slow(volatile int *p, int v);
1876
1877 #include "asterisk/inline_api.h"
1878
1879 #if defined(HAVE_OSX_ATOMICS)
1880 #include "libkern/OSAtomic.h"
1881 #endif
1882
1883 /*! \brief Atomically add v to *p and return * the previous value of *p.
1884  * This can be used to handle reference counts, and the return value
1885  * can be used to generate unique identifiers.
1886  */
1887
1888 #if defined(HAVE_GCC_ATOMICS)
1889 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1890 {
1891         return __sync_fetch_and_add(p, v);
1892 })
1893 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
1894 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1895 {
1896         return OSAtomicAdd32(v, (int32_t *) p) - v;
1897 })
1898 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
1899 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1900 {
1901         return OSAtomicAdd64(v, (int64_t *) p) - v;
1902 #elif defined (__i386__) || defined(__x86_64__)
1903 #ifdef sun
1904 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1905 {
1906         __asm __volatile (
1907         "       lock;  xaddl   %0, %1 ;        "
1908         : "+r" (v),                     /* 0 (result) */
1909           "=m" (*p)                     /* 1 */
1910         : "m" (*p));                    /* 2 */
1911         return (v);
1912 })
1913 #else /* ifndef sun */
1914 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1915 {
1916         __asm __volatile (
1917         "       lock   xaddl   %0, %1 ;        "
1918         : "+r" (v),                     /* 0 (result) */
1919           "=m" (*p)                     /* 1 */
1920         : "m" (*p));                    /* 2 */
1921         return (v);
1922 })
1923 #endif
1924 #else   /* low performance version in utils.c */
1925 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1926 {
1927         return ast_atomic_fetchadd_int_slow(p, v);
1928 })
1929 #endif
1930
1931 /*! \brief decrement *p by 1 and return true if the variable has reached 0.
1932  * Useful e.g. to check if a refcount has reached 0.
1933  */
1934 #if defined(HAVE_GCC_ATOMICS)
1935 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
1936 {
1937         return __sync_sub_and_fetch(p, 1) == 0;
1938 })
1939 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
1940 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
1941 {
1942         return OSAtomicAdd32( -1, (int32_t *) p) == 0;
1943 })
1944 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
1945 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
1946 {
1947         return OSAtomicAdd64( -1, (int64_t *) p) == 0;
1948 #else
1949 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
1950 {
1951         int a = ast_atomic_fetchadd_int(p, -1);
1952         return a == 1; /* true if the value is 0 now (so it was 1 previously) */
1953 })
1954 #endif
1955
1956 #ifndef DEBUG_CHANNEL_LOCKS
1957 /*! \brief Lock a channel. If DEBUG_CHANNEL_LOCKS is defined
1958         in the Makefile, print relevant output for debugging */
1959 #define ast_channel_lock(x)             ast_mutex_lock(&x->lock_dont_use)
1960 /*! \brief Unlock a channel. If DEBUG_CHANNEL_LOCKS is defined
1961         in the Makefile, print relevant output for debugging */
1962 #define ast_channel_unlock(x)           ast_mutex_unlock(&x->lock_dont_use)
1963 /*! \brief Try locking a channel. If DEBUG_CHANNEL_LOCKS is defined
1964         in the Makefile, print relevant output for debugging */
1965 #define ast_channel_trylock(x)          ast_mutex_trylock(&x->lock_dont_use)
1966 #else
1967
1968 #define ast_channel_lock(a) __ast_channel_lock(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1969 /*! \brief Lock AST channel (and print debugging output)
1970 \note You need to enable DEBUG_CHANNEL_LOCKS for this function */
1971 int __ast_channel_lock(struct ast_channel *chan, const char *file, int lineno, const char *func);
1972
1973 #define ast_channel_unlock(a) __ast_channel_unlock(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1974 /*! \brief Unlock AST channel (and print debugging output)
1975 \note You need to enable DEBUG_CHANNEL_LOCKS for this function
1976 */
1977 int __ast_channel_unlock(struct ast_channel *chan, const char *file, int lineno, const char *func);
1978
1979 #define ast_channel_trylock(a) __ast_channel_trylock(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1980 /*! \brief Lock AST channel (and print debugging output)
1981 \note   You need to enable DEBUG_CHANNEL_LOCKS for this function */
1982 int __ast_channel_trylock(struct ast_channel *chan, const char *file, int lineno, const char *func);
1983 #endif
1984
1985 #endif /* _ASTERISK_LOCK_H */