Add Doxygen documentation for API changes from 1.6.0 to 1.6.1
[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                 ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
504                 bt = &lt->backtrace[lt->reentrancy];
505                 ast_reentrancy_unlock(lt);
506                 ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t, bt);
507 #else
508                 ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t);
509 #endif
510         }
511
512 #ifdef DETECT_DEADLOCKS
513         {
514                 time_t seconds = time(NULL);
515                 time_t wait_time, reported_wait = 0;
516                 do {
517 #ifdef  HAVE_MTX_PROFILE
518                         ast_mark(mtx_prof, 1);
519 #endif
520                         res = pthread_mutex_trylock(&t->mutex);
521 #ifdef  HAVE_MTX_PROFILE
522                         ast_mark(mtx_prof, 0);
523 #endif
524                         if (res == EBUSY) {
525                                 wait_time = time(NULL) - seconds;
526                                 if (wait_time > reported_wait && (wait_time % 5) == 0) {
527                                         __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for mutex '%s'?\n",
528                                                            filename, lineno, func, (int) wait_time, mutex_name);
529                                         ast_reentrancy_lock(lt);
530 #ifdef HAVE_BKTR
531                                         __dump_backtrace(&lt->backtrace[lt->reentrancy], canlog);
532 #endif
533                                         __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
534                                                            lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1],
535                                                            lt->func[lt->reentrancy-1], mutex_name);
536 #ifdef HAVE_BKTR
537                                         __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
538 #endif
539                                         ast_reentrancy_unlock(lt);
540                                         reported_wait = wait_time;
541                                 }
542                                 usleep(200);
543                         }
544                 } while (res == EBUSY);
545         }
546 #else
547 #ifdef  HAVE_MTX_PROFILE
548         ast_mark(mtx_prof, 1);
549         res = pthread_mutex_trylock(&t->mutex);
550         ast_mark(mtx_prof, 0);
551         if (res)
552 #endif
553         res = pthread_mutex_lock(&t->mutex);
554 #endif /* DETECT_DEADLOCKS */
555
556         if (!res) {
557                 ast_reentrancy_lock(lt);
558                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
559                         lt->file[lt->reentrancy] = filename;
560                         lt->lineno[lt->reentrancy] = lineno;
561                         lt->func[lt->reentrancy] = func;
562                         lt->thread[lt->reentrancy] = pthread_self();
563                         lt->reentrancy++;
564                 } else {
565                         __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
566                                                            filename, lineno, func, mutex_name);
567                 }
568                 ast_reentrancy_unlock(lt);
569                 if (t->tracking) {
570                         ast_mark_lock_acquired(t);
571                 }
572         } else {
573 #ifdef HAVE_BKTR
574                 if (lt->reentrancy) {
575                         ast_reentrancy_lock(lt);
576                         bt = &lt->backtrace[lt->reentrancy-1];
577                         ast_reentrancy_unlock(lt);
578                 } else {
579                         bt = NULL;
580                 }
581                 if (t->tracking) {
582                         ast_remove_lock_info(t, bt);
583                 }
584 #else
585                 if (t->tracking) {
586                         ast_remove_lock_info(t);
587                 }
588 #endif
589                 __ast_mutex_logger("%s line %d (%s): Error obtaining mutex: %s\n",
590                                    filename, lineno, func, strerror(res));
591                 DO_THREAD_CRASH;
592         }
593
594         return res;
595 }
596
597 static inline int __ast_pthread_mutex_trylock(const char *filename, int lineno, const char *func,
598                                               const char* mutex_name, ast_mutex_t *t)
599 {
600         int res;
601         struct ast_lock_track *lt= &t->track;
602         int canlog = strcmp(filename, "logger.c") & t->tracking;
603 #ifdef HAVE_BKTR
604         struct ast_bt *bt = NULL;
605 #endif
606
607 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
608         if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
609                 /* Don't warn abount uninitialized mutex.
610                  * Simple try to initialize it.
611                  * May be not needed in linux system.
612                  */
613                 res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
614                 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
615                         __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
616                                          filename, lineno, func, mutex_name);
617                         return res;
618                 }
619         }
620 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
621
622         if (t->tracking) {
623 #ifdef HAVE_BKTR
624                 ast_reentrancy_lock(lt);
625                 ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
626                 bt = &lt->backtrace[lt->reentrancy];
627                 ast_reentrancy_unlock(lt);
628                 ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t, bt);
629 #else
630                 ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t);
631 #endif
632         }
633
634         if (!(res = pthread_mutex_trylock(&t->mutex))) {
635                 ast_reentrancy_lock(lt);
636                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
637                         lt->file[lt->reentrancy] = filename;
638                         lt->lineno[lt->reentrancy] = lineno;
639                         lt->func[lt->reentrancy] = func;
640                         lt->thread[lt->reentrancy] = pthread_self();
641                         lt->reentrancy++;
642                 } else {
643                         __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
644                                            filename, lineno, func, mutex_name);
645                 }
646                 ast_reentrancy_unlock(lt);
647                 if (t->tracking) {
648                         ast_mark_lock_acquired(t);
649                 }
650         } else if (t->tracking) {
651                 ast_mark_lock_failed(t);
652         }
653
654         return res;
655 }
656
657 static inline int __ast_pthread_mutex_unlock(const char *filename, int lineno, const char *func,
658                                              const char *mutex_name, ast_mutex_t *t)
659 {
660         int res;
661         struct ast_lock_track *lt = &t->track;
662         int canlog = strcmp(filename, "logger.c") & t->tracking;
663 #ifdef HAVE_BKTR
664         struct ast_bt *bt = NULL;
665 #endif
666
667 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
668         if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
669                 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
670                                    filename, lineno, func, mutex_name);
671                 res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
672                 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
673                         __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
674                                          filename, lineno, func, mutex_name);
675                 }
676                 return res;
677         }
678 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
679
680         ast_reentrancy_lock(lt);
681         if (lt->reentrancy && (lt->thread[lt->reentrancy-1] != pthread_self())) {
682                 __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
683                                    filename, lineno, func, mutex_name);
684                 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
685                                    lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1], lt->func[lt->reentrancy-1], mutex_name);
686 #ifdef HAVE_BKTR
687                 __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
688 #endif
689                 DO_THREAD_CRASH;
690         }
691
692         if (--lt->reentrancy < 0) {
693                 __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
694                                    filename, lineno, func, mutex_name);
695                 lt->reentrancy = 0;
696         }
697
698         if (lt->reentrancy < AST_MAX_REENTRANCY) {
699                 lt->file[lt->reentrancy] = NULL;
700                 lt->lineno[lt->reentrancy] = 0;
701                 lt->func[lt->reentrancy] = NULL;
702                 lt->thread[lt->reentrancy] = 0;
703         }
704
705 #ifdef HAVE_BKTR
706         if (lt->reentrancy) {
707                 bt = &lt->backtrace[lt->reentrancy - 1];
708         }
709 #endif
710         ast_reentrancy_unlock(lt);
711
712         if (t->tracking) {
713 #ifdef HAVE_BKTR
714                 ast_remove_lock_info(t, bt);
715 #else
716                 ast_remove_lock_info(t);
717 #endif
718         }
719
720         if ((res = pthread_mutex_unlock(&t->mutex))) {
721                 __ast_mutex_logger("%s line %d (%s): Error releasing mutex: %s\n",
722                                    filename, lineno, func, strerror(res));
723                 DO_THREAD_CRASH;
724         }
725
726         return res;
727 }
728
729 static inline int __ast_cond_init(const char *filename, int lineno, const char *func,
730                                   const char *cond_name, ast_cond_t *cond, pthread_condattr_t *cond_attr)
731 {
732         return pthread_cond_init(cond, cond_attr);
733 }
734
735 static inline int __ast_cond_signal(const char *filename, int lineno, const char *func,
736                                     const char *cond_name, ast_cond_t *cond)
737 {
738         return pthread_cond_signal(cond);
739 }
740
741 static inline int __ast_cond_broadcast(const char *filename, int lineno, const char *func,
742                                        const char *cond_name, ast_cond_t *cond)
743 {
744         return pthread_cond_broadcast(cond);
745 }
746
747 static inline int __ast_cond_destroy(const char *filename, int lineno, const char *func,
748                                      const char *cond_name, ast_cond_t *cond)
749 {
750         return pthread_cond_destroy(cond);
751 }
752
753 static inline int __ast_cond_wait(const char *filename, int lineno, const char *func,
754                                   const char *cond_name, const char *mutex_name,
755                                   ast_cond_t *cond, ast_mutex_t *t)
756 {
757         int res;
758         struct ast_lock_track *lt= &t->track;
759         int canlog = strcmp(filename, "logger.c") & t->tracking;
760 #ifdef HAVE_BKTR
761         struct ast_bt *bt = NULL;
762 #endif
763
764 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
765         if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
766                 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
767                                    filename, lineno, func, mutex_name);
768                 res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
769                 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
770                         __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
771                                          filename, lineno, func, mutex_name);
772                 }
773                 return res;
774         }
775 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
776
777         ast_reentrancy_lock(lt);
778         if (lt->reentrancy && (lt->thread[lt->reentrancy-1] != pthread_self())) {
779                 __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
780                                    filename, lineno, func, mutex_name);
781                 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
782                                    lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1], lt->func[lt->reentrancy-1], mutex_name);
783 #ifdef HAVE_BKTR
784                 __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
785 #endif
786                 DO_THREAD_CRASH;
787         }
788
789         if (--lt->reentrancy < 0) {
790                 __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
791                                    filename, lineno, func, mutex_name);
792                 lt->reentrancy = 0;
793         }
794
795         if (lt->reentrancy < AST_MAX_REENTRANCY) {
796                 lt->file[lt->reentrancy] = NULL;
797                 lt->lineno[lt->reentrancy] = 0;
798                 lt->func[lt->reentrancy] = NULL;
799                 lt->thread[lt->reentrancy] = 0;
800         }
801
802 #ifdef HAVE_BKTR
803         if (lt->reentrancy) {
804                 bt = &lt->backtrace[lt->reentrancy - 1];
805         }
806 #endif
807         ast_reentrancy_unlock(lt);
808
809         if (t->tracking) {
810 #ifdef HAVE_BKTR
811                 ast_remove_lock_info(t, bt);
812 #else
813                 ast_remove_lock_info(t);
814 #endif
815         }
816
817         if ((res = pthread_cond_wait(cond, &t->mutex))) {
818                 __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n",
819                                    filename, lineno, func, strerror(res));
820                 DO_THREAD_CRASH;
821         } else {
822                 ast_reentrancy_lock(lt);
823                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
824                         lt->file[lt->reentrancy] = filename;
825                         lt->lineno[lt->reentrancy] = lineno;
826                         lt->func[lt->reentrancy] = func;
827                         lt->thread[lt->reentrancy] = pthread_self();
828 #ifdef HAVE_BKTR
829                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
830                         bt = &lt->backtrace[lt->reentrancy];
831 #endif
832                         lt->reentrancy++;
833                 } else {
834                         __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
835                                                            filename, lineno, func, mutex_name);
836                 }
837                 ast_reentrancy_unlock(lt);
838
839                 if (t->tracking) {
840 #ifdef HAVE_BKTR
841                         ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t, bt);
842 #else
843                         ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t);
844 #endif
845                 }
846         }
847
848         return res;
849 }
850
851 static inline int __ast_cond_timedwait(const char *filename, int lineno, const char *func,
852                                        const char *cond_name, const char *mutex_name, ast_cond_t *cond,
853                                        ast_mutex_t *t, const struct timespec *abstime)
854 {
855         int res;
856         struct ast_lock_track *lt = &t->track;
857         int canlog = strcmp(filename, "logger.c") & t->tracking;
858 #ifdef HAVE_BKTR
859         struct ast_bt *bt = NULL;
860 #endif
861
862 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
863         if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
864                 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
865                                    filename, lineno, func, mutex_name);
866                 res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
867                 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
868                         __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
869                                          filename, lineno, func, mutex_name);
870                 }
871                 return res;
872         }
873 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
874
875         ast_reentrancy_lock(lt);
876         if (lt->reentrancy && (lt->thread[lt->reentrancy-1] != pthread_self())) {
877                 __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
878                                    filename, lineno, func, mutex_name);
879                 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
880                                    lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1], lt->func[lt->reentrancy-1], mutex_name);
881 #ifdef HAVE_BKTR
882                 __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
883 #endif
884                 DO_THREAD_CRASH;
885         }
886
887         if (--lt->reentrancy < 0) {
888                 __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
889                                    filename, lineno, func, mutex_name);
890                 lt->reentrancy = 0;
891         }
892
893         if (lt->reentrancy < AST_MAX_REENTRANCY) {
894                 lt->file[lt->reentrancy] = NULL;
895                 lt->lineno[lt->reentrancy] = 0;
896                 lt->func[lt->reentrancy] = NULL;
897                 lt->thread[lt->reentrancy] = 0;
898         }
899 #ifdef HAVE_BKTR
900         if (lt->reentrancy) {
901                 bt = &lt->backtrace[lt->reentrancy - 1];
902         }
903 #endif
904         ast_reentrancy_unlock(lt);
905
906         if (t->tracking) {
907 #ifdef HAVE_BKTR
908                 ast_remove_lock_info(t, bt);
909 #else
910                 ast_remove_lock_info(t);
911 #endif
912         }
913
914         if ((res = pthread_cond_timedwait(cond, &t->mutex, abstime)) && (res != ETIMEDOUT)) {
915                 __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n",
916                                    filename, lineno, func, strerror(res));
917                 DO_THREAD_CRASH;
918         } else {
919                 ast_reentrancy_lock(lt);
920                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
921                         lt->file[lt->reentrancy] = filename;
922                         lt->lineno[lt->reentrancy] = lineno;
923                         lt->func[lt->reentrancy] = func;
924                         lt->thread[lt->reentrancy] = pthread_self();
925 #ifdef HAVE_BKTR
926                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
927                         bt = &lt->backtrace[lt->reentrancy];
928 #endif
929                         lt->reentrancy++;
930                 } else {
931                         __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
932                                                            filename, lineno, func, mutex_name);
933                 }
934                 ast_reentrancy_unlock(lt);
935
936                 if (t->tracking) {
937 #ifdef HAVE_BKTR
938                         ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t, bt);
939 #else
940                         ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t);
941 #endif
942                 }
943         }
944
945         return res;
946 }
947
948 #define ast_mutex_destroy(a)                    __ast_pthread_mutex_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
949 #define ast_mutex_lock(a)                       __ast_pthread_mutex_lock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
950 #define ast_mutex_unlock(a)                     __ast_pthread_mutex_unlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
951 #define ast_mutex_trylock(a)                    __ast_pthread_mutex_trylock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
952 #define ast_cond_init(cond, attr)               __ast_cond_init(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond, attr)
953 #define ast_cond_destroy(cond)                  __ast_cond_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
954 #define ast_cond_signal(cond)                   __ast_cond_signal(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
955 #define ast_cond_broadcast(cond)                __ast_cond_broadcast(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
956 #define ast_cond_wait(cond, mutex)              __ast_cond_wait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex)
957 #define ast_cond_timedwait(cond, mutex, time)   __ast_cond_timedwait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex, time)
958
959 struct ast_rwlock_info {
960         /*! Track which thread holds this lock */
961         struct ast_lock_track track;
962         unsigned int tracking:1;
963         pthread_rwlock_t lock;
964 };
965
966 typedef struct ast_rwlock_info ast_rwlock_t;
967
968 /*!
969  * \brief wrapper for rwlock with tracking enabled
970  * \return 0 on success, non zero for error
971  * \since 1.6.1
972  */
973 #define ast_rwlock_init(rwlock) __ast_rwlock_init(1, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
974
975 /*!
976  * \brief wrapper for ast_rwlock_init with tracking disabled
977  * \return 0 on success, non zero for error
978  * \since 1.6.1
979  */
980 #define ast_rwlock_init_notracking(rwlock) __ast_rwlock_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
981
982 #define ast_rwlock_destroy(rwlock)      __ast_rwlock_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
983 #define ast_rwlock_unlock(a)            _ast_rwlock_unlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
984 #define ast_rwlock_rdlock(a)            _ast_rwlock_rdlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
985 #define ast_rwlock_wrlock(a)            _ast_rwlock_wrlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
986 #define ast_rwlock_tryrdlock(a)         _ast_rwlock_tryrdlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
987 #define ast_rwlock_trywrlock(a) _ast_rwlock_trywrlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
988
989
990 #ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
991 #define __AST_RWLOCK_INIT_VALUE PTHREAD_RWLOCK_INITIALIZER
992 #else  /* HAVE_PTHREAD_RWLOCK_INITIALIZER */
993 #define __AST_RWLOCK_INIT_VALUE {0}
994 #endif /* HAVE_PTHREAD_RWLOCK_INITIALIZER */
995
996 #define AST_RWLOCK_INIT_VALUE \
997         { AST_LOCK_TRACK_INIT_VALUE, 1, __AST_RWLOCK_INIT_VALUE }
998 #define AST_RWLOCK_INIT_VALUE_NOTRACKING \
999         { AST_LOCK_TRACK_INIT_VALUE, 0, __AST_RWLOCK_INIT_VALUE }
1000
1001 static inline int __ast_rwlock_init(int tracking, const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
1002 {
1003         int res;
1004         struct ast_lock_track *lt= &t->track;
1005         pthread_rwlockattr_t attr;
1006
1007 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1008         int canlog = strcmp(filename, "logger.c") & t->tracking;
1009
1010         if (t->lock != ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1011                 __ast_mutex_logger("%s line %d (%s): Warning: rwlock '%s' is already initialized.\n",
1012                                 filename, lineno, func, rwlock_name);
1013                 return 0;
1014         }
1015 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1016
1017         ast_reentrancy_init(lt);
1018         t->tracking = tracking;
1019         pthread_rwlockattr_init(&attr);
1020
1021 #ifdef HAVE_PTHREAD_RWLOCK_PREFER_WRITER_NP
1022         pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP);
1023 #endif
1024
1025         res = pthread_rwlock_init(&t->lock, &attr);
1026         pthread_rwlockattr_destroy(&attr);
1027         return res;
1028 }
1029
1030 static inline int __ast_rwlock_destroy(const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
1031 {
1032         int res;
1033         struct ast_lock_track *lt = &t->track;
1034         int canlog = strcmp(filename, "logger.c") & t->tracking;
1035
1036 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1037         if (t->lock == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1038                 __ast_mutex_logger("%s line %d (%s): Warning: rwlock '%s' is uninitialized.\n",
1039                                    filename, lineno, func, rwlock_name);
1040                 return 0;
1041         }
1042 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1043
1044         if ((res = pthread_rwlock_destroy(&t->lock))) {
1045                 __ast_mutex_logger("%s line %d (%s): Error destroying rwlock %s: %s\n",
1046                                 filename, lineno, func, rwlock_name, strerror(res));
1047         }
1048         ast_reentrancy_lock(lt);
1049         lt->file[0] = filename;
1050         lt->lineno[0] = lineno;
1051         lt->func[0] = func;
1052         lt->reentrancy = 0;
1053         lt->thread[0] = 0;
1054 #ifdef HAVE_BKTR
1055         memset(&lt->backtrace[0], 0, sizeof(lt->backtrace[0]));
1056 #endif
1057         ast_reentrancy_unlock(lt);
1058         delete_reentrancy_cs(lt);
1059
1060         return res;
1061 }
1062
1063 static inline int _ast_rwlock_unlock(ast_rwlock_t *t, const char *name,
1064         const char *filename, int line, const char *func)
1065 {
1066         int res;
1067         struct ast_lock_track *lt = &t->track;
1068         int canlog = strcmp(filename, "logger.c") & t->tracking;
1069 #ifdef HAVE_BKTR
1070         struct ast_bt *bt = NULL;
1071 #endif
1072
1073
1074 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1075         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1076                 __ast_mutex_logger("%s line %d (%s): Warning: rwlock '%s' is uninitialized.\n",
1077                                    filename, line, func, name);
1078                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1079                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1080                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1081                                         filename, line, func, name);
1082                 }
1083                 return res;
1084         }
1085 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1086
1087         ast_reentrancy_lock(lt);
1088         if (lt->reentrancy) {
1089                 int lock_found = 0;
1090                 int i;
1091                 pthread_t self = pthread_self();
1092                 for (i = lt->reentrancy-1; i >= 0; --i) {
1093                         if (lt->thread[i] == self) {
1094                                 lock_found = 1;
1095                                 if (i != lt->reentrancy-1) {
1096                                         lt->file[i] = lt->file[lt->reentrancy-1];
1097                                         lt->lineno[i] = lt->lineno[lt->reentrancy-1];
1098                                         lt->func[i] = lt->func[lt->reentrancy-1];
1099                                         lt->thread[i] = lt->thread[lt->reentrancy-1];
1100                                 }
1101                                 break;
1102                         }
1103                 }
1104                 if (!lock_found) {
1105                         __ast_mutex_logger("%s line %d (%s): attempted unlock rwlock '%s' without owning it!\n",
1106                                                 filename, line, func, name);
1107                         __ast_mutex_logger("%s line %d (%s): '%s' was last locked here.\n",
1108                                         lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1], lt->func[lt->reentrancy-1], name);
1109 #ifdef HAVE_BKTR
1110                         __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
1111 #endif
1112                         DO_THREAD_CRASH;
1113                 }
1114         }
1115
1116         if (--lt->reentrancy < 0) {
1117                 __ast_mutex_logger("%s line %d (%s): rwlock '%s' freed more times than we've locked!\n",
1118                                 filename, line, func, name);
1119                 lt->reentrancy = 0;
1120         }
1121
1122 #ifdef HAVE_BKTR
1123         if (lt->reentrancy) {
1124                 bt = &lt->backtrace[lt->reentrancy - 1];
1125         }
1126 #endif
1127         ast_reentrancy_unlock(lt);
1128
1129         if (t->tracking) {
1130 #ifdef HAVE_BKTR
1131                 ast_remove_lock_info(t, bt);
1132 #else
1133                 ast_remove_lock_info(t);
1134 #endif
1135         }
1136
1137         if ((res = pthread_rwlock_unlock(&t->lock))) {
1138                 __ast_mutex_logger("%s line %d (%s): Error releasing rwlock: %s\n",
1139                                 filename, line, func, strerror(res));
1140                 DO_THREAD_CRASH;
1141         }
1142
1143         return res;
1144 }
1145
1146 static inline int _ast_rwlock_rdlock(ast_rwlock_t *t, const char *name,
1147         const char *filename, int line, const char *func)
1148 {
1149         int res;
1150         struct ast_lock_track *lt = &t->track;
1151         int canlog = strcmp(filename, "logger.c") & t->tracking;
1152 #ifdef HAVE_BKTR
1153         struct ast_bt *bt = NULL;
1154 #endif
1155
1156 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1157         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1158                  /* Don't warn abount uninitialized lock.
1159                   * Simple try to initialize it.
1160                   * May be not needed in linux system.
1161                   */
1162                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1163                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1164                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1165                                         filename, line, func, name);
1166                         return res;
1167                 }
1168         }
1169 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1170
1171         if (t->tracking) {
1172 #ifdef HAVE_BKTR
1173                 ast_reentrancy_lock(lt);
1174                 ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
1175                 bt = &lt->backtrace[lt->reentrancy];
1176                 ast_reentrancy_unlock(lt);
1177                 ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t, bt);
1178 #else
1179                 ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t);
1180 #endif
1181         }
1182
1183 #ifdef DETECT_DEADLOCKS
1184         {
1185                 time_t seconds = time(NULL);
1186                 time_t wait_time, reported_wait = 0;
1187                 do {
1188                         res = pthread_rwlock_tryrdlock(&t->lock);
1189                         if (res == EBUSY) {
1190                                 wait_time = time(NULL) - seconds;
1191                                 if (wait_time > reported_wait && (wait_time % 5) == 0) {
1192                                         __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for readlock '%s'?\n",
1193                                                 filename, line, func, (int)wait_time, name);
1194                                         ast_reentrancy_lock(lt);
1195 #ifdef HAVE_BKTR
1196                                         __dump_backtrace(&lt->backtrace[lt->reentrancy], canlog);
1197 #endif
1198                                         __ast_mutex_logger("%s line %d (%s): '%s' was locked  here.\n",
1199                                                         lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1],
1200                                                         lt->func[lt->reentrancy-1], name);
1201 #ifdef HAVE_BKTR
1202                                         __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
1203 #endif
1204                                         ast_reentrancy_unlock(lt);
1205                                         reported_wait = wait_time;
1206                                 }
1207                                 usleep(200);
1208                         }
1209                 } while (res == EBUSY);
1210         }
1211 #else /* !DETECT_DEADLOCKS */
1212         res = pthread_rwlock_rdlock(&t->lock);
1213 #endif /* !DETECT_DEADLOCKS */
1214
1215         if (!res) {
1216                 ast_reentrancy_lock(lt);
1217                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
1218                         lt->file[lt->reentrancy] = filename;
1219                         lt->lineno[lt->reentrancy] = line;
1220                         lt->func[lt->reentrancy] = func;
1221                         lt->thread[lt->reentrancy] = pthread_self();
1222                         lt->reentrancy++;
1223                 } else {
1224                         __ast_mutex_logger("%s line %d (%s): read lock '%s' really deep reentrancy!\n",
1225                                         filename, line, func, name);
1226                 }
1227                 ast_reentrancy_unlock(lt);
1228                 if (t->tracking) {
1229                         ast_mark_lock_acquired(t);
1230                 }
1231         } else {
1232 #ifdef HAVE_BKTR
1233                 if (lt->reentrancy) {
1234                         ast_reentrancy_lock(lt);
1235                         bt = &lt->backtrace[lt->reentrancy-1];
1236                         ast_reentrancy_unlock(lt);
1237                 } else {
1238                         bt = NULL;
1239                 }
1240                 if (t->tracking) {
1241                         ast_remove_lock_info(t, bt);
1242                 }
1243 #else
1244                 if (t->tracking) {
1245                         ast_remove_lock_info(t);
1246                 }
1247 #endif
1248                 __ast_mutex_logger("%s line %d (%s): Error obtaining read lock: %s\n",
1249                                 filename, line, func, strerror(res));
1250                 DO_THREAD_CRASH;
1251         }
1252         return res;
1253 }
1254
1255 static inline int _ast_rwlock_wrlock(ast_rwlock_t *t, const char *name,
1256         const char *filename, int line, const char *func)
1257 {
1258         int res;
1259         struct ast_lock_track *lt = &t->track;
1260         int canlog = strcmp(filename, "logger.c") & t->tracking;
1261 #ifdef HAVE_BKTR
1262         struct ast_bt *bt = NULL;
1263 #endif
1264
1265 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1266         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1267                  /* Don't warn abount uninitialized lock.
1268                   * Simple try to initialize it.
1269                   * May be not needed in linux system.
1270                   */
1271                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1272                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1273                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1274                                         filename, line, func, name);
1275                         return res;
1276                 }
1277         }
1278 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1279
1280         if (t->tracking) {
1281 #ifdef HAVE_BKTR
1282                 ast_reentrancy_lock(lt);
1283                 ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
1284                 bt = &lt->backtrace[lt->reentrancy];
1285                 ast_reentrancy_unlock(lt);
1286                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
1287 #else
1288                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
1289 #endif
1290         }
1291 #ifdef DETECT_DEADLOCKS
1292         {
1293                 time_t seconds = time(NULL);
1294                 time_t wait_time, reported_wait = 0;
1295                 do {
1296                         res = pthread_rwlock_trywrlock(&t->lock);
1297                         if (res == EBUSY) {
1298                                 wait_time = time(NULL) - seconds;
1299                                 if (wait_time > reported_wait && (wait_time % 5) == 0) {
1300                                         __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for writelock '%s'?\n",
1301                                                 filename, line, func, (int)wait_time, name);
1302                                         ast_reentrancy_lock(lt);
1303 #ifdef HAVE_BKTR
1304                                         __dump_backtrace(&lt->backtrace[lt->reentrancy], canlog);
1305 #endif
1306                                         __ast_mutex_logger("%s line %d (%s): '%s' was locked  here.\n",
1307                                                         lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1],
1308                                                         lt->func[lt->reentrancy-1], name);
1309 #ifdef HAVE_BKTR
1310                                         __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
1311 #endif
1312                                         ast_reentrancy_unlock(lt);
1313                                         reported_wait = wait_time;
1314                                 }
1315                                 usleep(200);
1316                         }
1317                 } while (res == EBUSY);
1318         }
1319 #else /* !DETECT_DEADLOCKS */
1320         res = pthread_rwlock_wrlock(&t->lock);
1321 #endif /* !DETECT_DEADLOCKS */
1322
1323         if (!res) {
1324                 ast_reentrancy_lock(lt);
1325                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
1326                         lt->file[lt->reentrancy] = filename;
1327                         lt->lineno[lt->reentrancy] = line;
1328                         lt->func[lt->reentrancy] = func;
1329                         lt->thread[lt->reentrancy] = pthread_self();
1330                         lt->reentrancy++;
1331                 } else {
1332                         __ast_mutex_logger("%s line %d (%s): write lock '%s' really deep reentrancy!\n",
1333                                         filename, line, func, name);
1334                 }
1335                 ast_reentrancy_unlock(lt);
1336                 if (t->tracking) {
1337                         ast_mark_lock_acquired(t);
1338                 }
1339         } else {
1340 #ifdef HAVE_BKTR
1341                 if (lt->reentrancy) {
1342                         ast_reentrancy_lock(lt);
1343                         bt = &lt->backtrace[lt->reentrancy-1];
1344                 } else {
1345                         bt = NULL;
1346                 }
1347                 if (t->tracking) {
1348                         ast_remove_lock_info(t, bt);
1349                 }
1350 #else
1351                 if (t->tracking) {
1352                         ast_remove_lock_info(t);
1353                 }
1354 #endif
1355                 __ast_mutex_logger("%s line %d (%s): Error obtaining write lock: %s\n",
1356                                 filename, line, func, strerror(res));
1357                 DO_THREAD_CRASH;
1358         }
1359         return res;
1360 }
1361
1362 static inline int _ast_rwlock_tryrdlock(ast_rwlock_t *t, const char *name,
1363         const char *filename, int line, const char *func)
1364 {
1365         int res;
1366         struct ast_lock_track *lt = &t->track;
1367         int canlog = strcmp(filename, "logger.c") & t->tracking;
1368 #ifdef HAVE_BKTR
1369         struct ast_bt *bt = NULL;
1370 #endif
1371
1372
1373 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1374         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1375                  /* Don't warn abount uninitialized lock.
1376                   * Simple try to initialize it.
1377                   * May be not needed in linux system.
1378                   */
1379                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1380                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1381                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1382                                         filename, line, func, name);
1383                         return res;
1384                 }
1385         }
1386 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1387
1388         if (t->tracking) {
1389 #ifdef HAVE_BKTR
1390                 ast_reentrancy_lock(lt);
1391                 ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
1392                 bt = &lt->backtrace[lt->reentrancy];
1393                 ast_reentrancy_unlock(lt);
1394                 ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t, bt);
1395 #else
1396                 ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t);
1397 #endif
1398         }
1399
1400         if (!(res = pthread_rwlock_tryrdlock(&t->lock))) {
1401                 ast_reentrancy_lock(lt);
1402                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
1403                         lt->file[lt->reentrancy] = filename;
1404                         lt->lineno[lt->reentrancy] = line;
1405                         lt->func[lt->reentrancy] = func;
1406                         lt->thread[lt->reentrancy] = pthread_self();
1407                         lt->reentrancy++;
1408                 } else {
1409                         __ast_mutex_logger("%s line %d (%s): read lock '%s' really deep reentrancy!\n",
1410                                         filename, line, func, name);
1411                 }
1412                 ast_reentrancy_unlock(lt);
1413                 if (t->tracking) {
1414                         ast_mark_lock_acquired(t);
1415                 }
1416         } else if (t->tracking) {
1417                 ast_mark_lock_failed(t);
1418         }
1419         return res;
1420 }
1421
1422 static inline int _ast_rwlock_trywrlock(ast_rwlock_t *t, const char *name,
1423         const char *filename, int line, const char *func)
1424 {
1425         int res;
1426         struct ast_lock_track *lt= &t->track;
1427         int canlog = strcmp(filename, "logger.c") & t->tracking;
1428 #ifdef HAVE_BKTR
1429         struct ast_bt *bt = NULL;
1430 #endif
1431
1432
1433 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1434         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1435                  /* Don't warn abount uninitialized lock.
1436                   * Simple try to initialize it.
1437                   * May be not needed in linux system.
1438                   */
1439                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1440                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1441                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1442                                         filename, line, func, name);
1443                         return res;
1444                 }
1445         }
1446 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1447
1448         if (t->tracking) {
1449 #ifdef HAVE_BKTR
1450                 ast_reentrancy_lock(lt);
1451                 ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
1452                 bt = &lt->backtrace[lt->reentrancy];
1453                 ast_reentrancy_unlock(lt);
1454                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
1455 #else
1456                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
1457 #endif
1458         }
1459
1460         if (!(res = pthread_rwlock_trywrlock(&t->lock))) {
1461                 ast_reentrancy_lock(lt);
1462                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
1463                         lt->file[lt->reentrancy] = filename;
1464                         lt->lineno[lt->reentrancy] = line;
1465                         lt->func[lt->reentrancy] = func;
1466                         lt->thread[lt->reentrancy] = pthread_self();
1467                         lt->reentrancy++;
1468                 } else {
1469                         __ast_mutex_logger("%s line %d (%s): write lock '%s' really deep reentrancy!\n",
1470                                         filename, line, func, name);
1471                 }
1472                 ast_reentrancy_unlock(lt);
1473                 if (t->tracking) {
1474                         ast_mark_lock_acquired(t);
1475                 }
1476         } else if (t->tracking) {
1477                 ast_mark_lock_failed(t);
1478         }
1479         return res;
1480 }
1481
1482 #else /* !DEBUG_THREADS */
1483
1484 #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
1485         ast_channel_unlock(chan); \
1486         usleep(1); \
1487         ast_channel_lock(chan);
1488
1489 #define DEADLOCK_AVOIDANCE(lock) \
1490         ast_mutex_unlock(lock); \
1491         usleep(1); \
1492         ast_mutex_lock(lock);
1493
1494 #define DLA_UNLOCK(lock)        ast_mutex_unlock(lock)
1495
1496 #define DLA_LOCK(lock)  ast_mutex_lock(lock)
1497
1498 typedef pthread_mutex_t ast_mutex_t;
1499
1500 #define AST_MUTEX_INIT_VALUE                    ((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
1501 #define AST_MUTEX_INIT_VALUE_NOTRACKING         ((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
1502
1503 #define ast_mutex_init_notracking(m)            ast_mutex_init(m)
1504
1505 static inline int ast_mutex_init(ast_mutex_t *pmutex)
1506 {
1507         int res;
1508         pthread_mutexattr_t attr;
1509
1510         pthread_mutexattr_init(&attr);
1511         pthread_mutexattr_settype(&attr, AST_MUTEX_KIND);
1512
1513         res = pthread_mutex_init(pmutex, &attr);
1514         pthread_mutexattr_destroy(&attr);
1515         return res;
1516 }
1517
1518 #define ast_pthread_mutex_init(pmutex,a) pthread_mutex_init(pmutex,a)
1519
1520 static inline int ast_mutex_unlock(ast_mutex_t *pmutex)
1521 {
1522         return pthread_mutex_unlock(pmutex);
1523 }
1524
1525 static inline int ast_mutex_destroy(ast_mutex_t *pmutex)
1526 {
1527         return pthread_mutex_destroy(pmutex);
1528 }
1529
1530 static inline int ast_mutex_lock(ast_mutex_t *pmutex)
1531 {
1532         __MTX_PROF(pmutex);
1533 }
1534
1535 static inline int ast_mutex_trylock(ast_mutex_t *pmutex)
1536 {
1537         return pthread_mutex_trylock(pmutex);
1538 }
1539
1540 typedef pthread_cond_t ast_cond_t;
1541
1542 static inline int ast_cond_init(ast_cond_t *cond, pthread_condattr_t *cond_attr)
1543 {
1544         return pthread_cond_init(cond, cond_attr);
1545 }
1546
1547 static inline int ast_cond_signal(ast_cond_t *cond)
1548 {
1549         return pthread_cond_signal(cond);
1550 }
1551
1552 static inline int ast_cond_broadcast(ast_cond_t *cond)
1553 {
1554         return pthread_cond_broadcast(cond);
1555 }
1556
1557 static inline int ast_cond_destroy(ast_cond_t *cond)
1558 {
1559         return pthread_cond_destroy(cond);
1560 }
1561
1562 static inline int ast_cond_wait(ast_cond_t *cond, ast_mutex_t *t)
1563 {
1564         return pthread_cond_wait(cond, t);
1565 }
1566
1567 static inline int ast_cond_timedwait(ast_cond_t *cond, ast_mutex_t *t, const struct timespec *abstime)
1568 {
1569         return pthread_cond_timedwait(cond, t, abstime);
1570 }
1571
1572
1573 typedef pthread_rwlock_t ast_rwlock_t;
1574
1575 #ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
1576 #define AST_RWLOCK_INIT_VALUE PTHREAD_RWLOCK_INITIALIZER
1577 #else
1578 #define AST_RWLOCK_INIT_VALUE { 0 }
1579 #endif
1580
1581 #define ast_rwlock_init_notracking(a) ast_rwlock_init(a)
1582
1583 static inline int ast_rwlock_init(ast_rwlock_t *prwlock)
1584 {
1585         int res;
1586         pthread_rwlockattr_t attr;
1587
1588         pthread_rwlockattr_init(&attr);
1589
1590 #ifdef HAVE_PTHREAD_RWLOCK_PREFER_WRITER_NP
1591         pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP);
1592 #endif
1593
1594         res = pthread_rwlock_init(prwlock, &attr);
1595         pthread_rwlockattr_destroy(&attr);
1596         return res;
1597 }
1598
1599 static inline int ast_rwlock_destroy(ast_rwlock_t *prwlock)
1600 {
1601         return pthread_rwlock_destroy(prwlock);
1602 }
1603
1604 static inline int ast_rwlock_unlock(ast_rwlock_t *prwlock)
1605 {
1606         return pthread_rwlock_unlock(prwlock);
1607 }
1608
1609 static inline int ast_rwlock_rdlock(ast_rwlock_t *prwlock)
1610 {
1611         return pthread_rwlock_rdlock(prwlock);
1612 }
1613
1614 static inline int ast_rwlock_tryrdlock(ast_rwlock_t *prwlock)
1615 {
1616         return pthread_rwlock_tryrdlock(prwlock);
1617 }
1618
1619 static inline int ast_rwlock_wrlock(ast_rwlock_t *prwlock)
1620 {
1621         return pthread_rwlock_wrlock(prwlock);
1622 }
1623
1624 static inline int ast_rwlock_trywrlock(ast_rwlock_t *prwlock)
1625 {
1626         return pthread_rwlock_trywrlock(prwlock);
1627 }
1628
1629 #endif /* !DEBUG_THREADS */
1630
1631 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
1632 /*
1633  * If AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope constructors
1634  * and destructors to create/destroy global mutexes.
1635  */
1636 #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track)       \
1637         scope ast_mutex_t mutex = init_val;                     \
1638 static void  __attribute__((constructor)) init_##mutex(void)    \
1639 {                                                               \
1640         if (track)                                              \
1641                 ast_mutex_init(&mutex);                         \
1642         else                                                    \
1643                 ast_mutex_init_notracking(&mutex);              \
1644 }                                                               \
1645                                                                 \
1646 static void  __attribute__((destructor)) fini_##mutex(void)     \
1647 {                                                               \
1648         ast_mutex_destroy(&mutex);                              \
1649 }
1650 #else /* !AST_MUTEX_INIT_W_CONSTRUCTORS */
1651 /* By default, use static initialization of mutexes. */
1652 #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track)       scope ast_mutex_t mutex = init_val
1653 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1654
1655 #ifndef __CYGWIN__      /* temporary disabled for cygwin */
1656 #define pthread_mutex_t         use_ast_mutex_t_instead_of_pthread_mutex_t
1657 #define pthread_cond_t          use_ast_cond_t_instead_of_pthread_cond_t
1658 #endif
1659 #define pthread_mutex_lock      use_ast_mutex_lock_instead_of_pthread_mutex_lock
1660 #define pthread_mutex_unlock    use_ast_mutex_unlock_instead_of_pthread_mutex_unlock
1661 #define pthread_mutex_trylock   use_ast_mutex_trylock_instead_of_pthread_mutex_trylock
1662 #define pthread_mutex_init      use_ast_mutex_init_instead_of_pthread_mutex_init
1663 #define pthread_mutex_destroy   use_ast_mutex_destroy_instead_of_pthread_mutex_destroy
1664 #define pthread_cond_init       use_ast_cond_init_instead_of_pthread_cond_init
1665 #define pthread_cond_destroy    use_ast_cond_destroy_instead_of_pthread_cond_destroy
1666 #define pthread_cond_signal     use_ast_cond_signal_instead_of_pthread_cond_signal
1667 #define pthread_cond_broadcast  use_ast_cond_broadcast_instead_of_pthread_cond_broadcast
1668 #define pthread_cond_wait       use_ast_cond_wait_instead_of_pthread_cond_wait
1669 #define pthread_cond_timedwait  use_ast_cond_timedwait_instead_of_pthread_cond_timedwait
1670
1671 #define AST_MUTEX_DEFINE_STATIC(mutex)                  __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE, 1)
1672 #define AST_MUTEX_DEFINE_STATIC_NOTRACKING(mutex)       __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE_NOTRACKING, 0)
1673
1674 #define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__
1675
1676 #define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
1677
1678 #ifndef __linux__
1679 #define pthread_create __use_ast_pthread_create_instead__
1680 #endif
1681
1682 /* Statically declared read/write locks */
1683
1684 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1685 #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) \
1686         scope ast_rwlock_t rwlock = init_val; \
1687 static void  __attribute__((constructor)) init_##rwlock(void) \
1688 { \
1689         if (track) \
1690                 ast_rwlock_init(&rwlock); \
1691         else \
1692                 ast_rwlock_init_notracking(&rwlock); \
1693 } \
1694 static void  __attribute__((destructor)) fini_##rwlock(void) \
1695 { \
1696         ast_rwlock_destroy(&rwlock); \
1697 }
1698 #else
1699 #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) \
1700         scope ast_rwlock_t rwlock = init_val
1701 #endif
1702
1703 #define AST_RWLOCK_DEFINE_STATIC(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE, 1)
1704 #define AST_RWLOCK_DEFINE_STATIC_NOTRACKING(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE_NOTRACKING, 0)
1705
1706 /*
1707  * Support for atomic instructions.
1708  * For platforms that have it, use the native cpu instruction to
1709  * implement them. For other platforms, resort to a 'slow' version
1710  * (defined in utils.c) that protects the atomic instruction with
1711  * a single lock.
1712  * The slow versions is always available, for testing purposes,
1713  * as ast_atomic_fetchadd_int_slow()
1714  */
1715
1716 int ast_atomic_fetchadd_int_slow(volatile int *p, int v);
1717
1718 #include "asterisk/inline_api.h"
1719
1720 #if defined(HAVE_OSX_ATOMICS)
1721 #include "libkern/OSAtomic.h"
1722 #endif
1723
1724 /*! \brief Atomically add v to *p and return * the previous value of *p.
1725  * This can be used to handle reference counts, and the return value
1726  * can be used to generate unique identifiers.
1727  */
1728
1729 #if defined(HAVE_GCC_ATOMICS)
1730 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1731 {
1732         return __sync_fetch_and_add(p, v);
1733 })
1734 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
1735 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1736 {
1737         return OSAtomicAdd32(v, (int32_t *) p) - v;
1738 })
1739 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
1740 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1741 {
1742         return OSAtomicAdd64(v, (int64_t *) p) - v;
1743 #elif defined (__i386__) || defined(__x86_64__)
1744 #ifdef sun
1745 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1746 {
1747         __asm __volatile (
1748         "       lock;  xaddl   %0, %1 ;        "
1749         : "+r" (v),                     /* 0 (result) */
1750           "=m" (*p)                     /* 1 */
1751         : "m" (*p));                    /* 2 */
1752         return (v);
1753 })
1754 #else /* ifndef sun */
1755 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1756 {
1757         __asm __volatile (
1758         "       lock   xaddl   %0, %1 ;        "
1759         : "+r" (v),                     /* 0 (result) */
1760           "=m" (*p)                     /* 1 */
1761         : "m" (*p));                    /* 2 */
1762         return (v);
1763 })
1764 #endif
1765 #else   /* low performance version in utils.c */
1766 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1767 {
1768         return ast_atomic_fetchadd_int_slow(p, v);
1769 })
1770 #endif
1771
1772 /*! \brief decrement *p by 1 and return true if the variable has reached 0.
1773  * Useful e.g. to check if a refcount has reached 0.
1774  */
1775 #if defined(HAVE_GCC_ATOMICS)
1776 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
1777 {
1778         return __sync_sub_and_fetch(p, 1) == 0;
1779 })
1780 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
1781 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
1782 {
1783         return OSAtomicAdd32( -1, (int32_t *) p) == 0;
1784 })
1785 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
1786 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
1787 {
1788         return OSAtomicAdd64( -1, (int64_t *) p) == 0;
1789 #else
1790 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
1791 {
1792         int a = ast_atomic_fetchadd_int(p, -1);
1793         return a == 1; /* true if the value is 0 now (so it was 1 previously) */
1794 })
1795 #endif
1796
1797 #ifndef DEBUG_CHANNEL_LOCKS
1798 /*! \brief Lock a channel. If DEBUG_CHANNEL_LOCKS is defined
1799         in the Makefile, print relevant output for debugging */
1800 #define ast_channel_lock(x)             ast_mutex_lock(&x->lock_dont_use)
1801 /*! \brief Unlock a channel. If DEBUG_CHANNEL_LOCKS is defined
1802         in the Makefile, print relevant output for debugging */
1803 #define ast_channel_unlock(x)           ast_mutex_unlock(&x->lock_dont_use)
1804 /*! \brief Try locking a channel. If DEBUG_CHANNEL_LOCKS is defined
1805         in the Makefile, print relevant output for debugging */
1806 #define ast_channel_trylock(x)          ast_mutex_trylock(&x->lock_dont_use)
1807 #else
1808
1809 #define ast_channel_lock(a) __ast_channel_lock(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1810 /*! \brief Lock AST channel (and print debugging output)
1811 \note You need to enable DEBUG_CHANNEL_LOCKS for this function */
1812 int __ast_channel_lock(struct ast_channel *chan, const char *file, int lineno, const char *func);
1813
1814 #define ast_channel_unlock(a) __ast_channel_unlock(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1815 /*! \brief Unlock AST channel (and print debugging output)
1816 \note You need to enable DEBUG_CHANNEL_LOCKS for this function
1817 */
1818 int __ast_channel_unlock(struct ast_channel *chan, const char *file, int lineno, const char *func);
1819
1820 #define ast_channel_trylock(a) __ast_channel_trylock(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1821 /*! \brief Lock AST channel (and print debugging output)
1822 \note   You need to enable DEBUG_CHANNEL_LOCKS for this function */
1823 int __ast_channel_trylock(struct ast_channel *chan, const char *file, int lineno, const char *func);
1824 #endif
1825
1826 #endif /* _ASTERISK_LOCK_H */