Fix some issues with rwlock corruption that caused deadlock like symptoms.
[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                 } else {
1339                         bt = NULL;
1340                 }
1341                 if (t->tracking) {
1342                         ast_remove_lock_info(t, bt);
1343                 }
1344 #else
1345                 if (t->tracking) {
1346                         ast_remove_lock_info(t);
1347                 }
1348 #endif
1349                 __ast_mutex_logger("%s line %d (%s): Error obtaining write lock: %s\n",
1350                                 filename, line, func, strerror(res));
1351                 DO_THREAD_CRASH;
1352         }
1353         return res;
1354 }
1355
1356 static inline int _ast_rwlock_tryrdlock(ast_rwlock_t *t, const char *name,
1357         const char *filename, int line, const char *func)
1358 {
1359         int res;
1360         struct ast_lock_track *lt = &t->track;
1361 #ifdef HAVE_BKTR
1362         struct ast_bt *bt = NULL;
1363 #endif
1364
1365
1366 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1367         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1368                  /* Don't warn abount uninitialized lock.
1369                   * Simple try to initialize it.
1370                   * May be not needed in linux system.
1371                   */
1372                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1373                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1374                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1375                                         filename, line, func, name);
1376                         return res;
1377                 }
1378         }
1379 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1380
1381         if (t->tracking) {
1382 #ifdef HAVE_BKTR
1383                 ast_reentrancy_lock(lt);
1384                 if (lt->reentrancy != AST_MAX_REENTRANCY) {
1385                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
1386                         bt = &lt->backtrace[lt->reentrancy];
1387                 }
1388                 ast_reentrancy_unlock(lt);
1389                 ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t, bt);
1390 #else
1391                 ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t);
1392 #endif
1393         }
1394
1395         if (!(res = pthread_rwlock_tryrdlock(&t->lock))) {
1396                 ast_reentrancy_lock(lt);
1397                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
1398                         lt->file[lt->reentrancy] = filename;
1399                         lt->lineno[lt->reentrancy] = line;
1400                         lt->func[lt->reentrancy] = func;
1401                         lt->thread[lt->reentrancy] = pthread_self();
1402                         lt->reentrancy++;
1403                 }
1404                 ast_reentrancy_unlock(lt);
1405                 if (t->tracking) {
1406                         ast_mark_lock_acquired(t);
1407                 }
1408         } else if (t->tracking) {
1409                 ast_mark_lock_failed(t);
1410         }
1411         return res;
1412 }
1413
1414 static inline int _ast_rwlock_trywrlock(ast_rwlock_t *t, const char *name,
1415         const char *filename, int line, const char *func)
1416 {
1417         int res;
1418         struct ast_lock_track *lt= &t->track;
1419 #ifdef HAVE_BKTR
1420         struct ast_bt *bt = NULL;
1421 #endif
1422
1423
1424 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1425         if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1426                  /* Don't warn abount uninitialized lock.
1427                   * Simple try to initialize it.
1428                   * May be not needed in linux system.
1429                   */
1430                 res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
1431                 if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
1432                         __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
1433                                         filename, line, func, name);
1434                         return res;
1435                 }
1436         }
1437 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1438
1439         if (t->tracking) {
1440 #ifdef HAVE_BKTR
1441                 ast_reentrancy_lock(lt);
1442                 if (lt->reentrancy != AST_MAX_REENTRANCY) {
1443                         ast_bt_get_addresses(&lt->backtrace[lt->reentrancy]);
1444                         bt = &lt->backtrace[lt->reentrancy];
1445                 }
1446                 ast_reentrancy_unlock(lt);
1447                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
1448 #else
1449                 ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
1450 #endif
1451         }
1452
1453         if (!(res = pthread_rwlock_trywrlock(&t->lock))) {
1454                 ast_reentrancy_lock(lt);
1455                 if (lt->reentrancy < AST_MAX_REENTRANCY) {
1456                         lt->file[lt->reentrancy] = filename;
1457                         lt->lineno[lt->reentrancy] = line;
1458                         lt->func[lt->reentrancy] = func;
1459                         lt->thread[lt->reentrancy] = pthread_self();
1460                         lt->reentrancy++;
1461                 }
1462                 ast_reentrancy_unlock(lt);
1463                 if (t->tracking) {
1464                         ast_mark_lock_acquired(t);
1465                 }
1466         } else if (t->tracking) {
1467                 ast_mark_lock_failed(t);
1468         }
1469         return res;
1470 }
1471
1472 #else /* !DEBUG_THREADS */
1473
1474 #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
1475         ast_channel_unlock(chan); \
1476         usleep(1); \
1477         ast_channel_lock(chan);
1478
1479 #define DEADLOCK_AVOIDANCE(lock) \
1480         ast_mutex_unlock(lock); \
1481         usleep(1); \
1482         ast_mutex_lock(lock);
1483
1484 #define DLA_UNLOCK(lock)        ast_mutex_unlock(lock)
1485
1486 #define DLA_LOCK(lock)  ast_mutex_lock(lock)
1487
1488 typedef pthread_mutex_t ast_mutex_t;
1489
1490 #define AST_MUTEX_INIT_VALUE                    ((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
1491 #define AST_MUTEX_INIT_VALUE_NOTRACKING         ((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
1492
1493 #define ast_mutex_init_notracking(m)            ast_mutex_init(m)
1494
1495 static inline int ast_mutex_init(ast_mutex_t *pmutex)
1496 {
1497         int res;
1498         pthread_mutexattr_t attr;
1499
1500         pthread_mutexattr_init(&attr);
1501         pthread_mutexattr_settype(&attr, AST_MUTEX_KIND);
1502
1503         res = pthread_mutex_init(pmutex, &attr);
1504         pthread_mutexattr_destroy(&attr);
1505         return res;
1506 }
1507
1508 #define ast_pthread_mutex_init(pmutex,a) pthread_mutex_init(pmutex,a)
1509
1510 static inline int ast_mutex_unlock(ast_mutex_t *pmutex)
1511 {
1512         return pthread_mutex_unlock(pmutex);
1513 }
1514
1515 static inline int ast_mutex_destroy(ast_mutex_t *pmutex)
1516 {
1517         return pthread_mutex_destroy(pmutex);
1518 }
1519
1520 static inline int ast_mutex_lock(ast_mutex_t *pmutex)
1521 {
1522         __MTX_PROF(pmutex);
1523 }
1524
1525 static inline int ast_mutex_trylock(ast_mutex_t *pmutex)
1526 {
1527         return pthread_mutex_trylock(pmutex);
1528 }
1529
1530 typedef pthread_cond_t ast_cond_t;
1531
1532 static inline int ast_cond_init(ast_cond_t *cond, pthread_condattr_t *cond_attr)
1533 {
1534         return pthread_cond_init(cond, cond_attr);
1535 }
1536
1537 static inline int ast_cond_signal(ast_cond_t *cond)
1538 {
1539         return pthread_cond_signal(cond);
1540 }
1541
1542 static inline int ast_cond_broadcast(ast_cond_t *cond)
1543 {
1544         return pthread_cond_broadcast(cond);
1545 }
1546
1547 static inline int ast_cond_destroy(ast_cond_t *cond)
1548 {
1549         return pthread_cond_destroy(cond);
1550 }
1551
1552 static inline int ast_cond_wait(ast_cond_t *cond, ast_mutex_t *t)
1553 {
1554         return pthread_cond_wait(cond, t);
1555 }
1556
1557 static inline int ast_cond_timedwait(ast_cond_t *cond, ast_mutex_t *t, const struct timespec *abstime)
1558 {
1559         return pthread_cond_timedwait(cond, t, abstime);
1560 }
1561
1562
1563 typedef pthread_rwlock_t ast_rwlock_t;
1564
1565 #ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
1566 #define AST_RWLOCK_INIT_VALUE PTHREAD_RWLOCK_INITIALIZER
1567 #else
1568 #define AST_RWLOCK_INIT_VALUE { 0 }
1569 #endif
1570
1571 #define ast_rwlock_init_notracking(a) ast_rwlock_init(a)
1572
1573 static inline int ast_rwlock_init(ast_rwlock_t *prwlock)
1574 {
1575         int res;
1576         pthread_rwlockattr_t attr;
1577
1578         pthread_rwlockattr_init(&attr);
1579
1580 #ifdef HAVE_PTHREAD_RWLOCK_PREFER_WRITER_NP
1581         pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP);
1582 #endif
1583
1584         res = pthread_rwlock_init(prwlock, &attr);
1585         pthread_rwlockattr_destroy(&attr);
1586         return res;
1587 }
1588
1589 static inline int ast_rwlock_destroy(ast_rwlock_t *prwlock)
1590 {
1591         return pthread_rwlock_destroy(prwlock);
1592 }
1593
1594 static inline int ast_rwlock_unlock(ast_rwlock_t *prwlock)
1595 {
1596         return pthread_rwlock_unlock(prwlock);
1597 }
1598
1599 static inline int ast_rwlock_rdlock(ast_rwlock_t *prwlock)
1600 {
1601         return pthread_rwlock_rdlock(prwlock);
1602 }
1603
1604 static inline int ast_rwlock_tryrdlock(ast_rwlock_t *prwlock)
1605 {
1606         return pthread_rwlock_tryrdlock(prwlock);
1607 }
1608
1609 static inline int ast_rwlock_wrlock(ast_rwlock_t *prwlock)
1610 {
1611         return pthread_rwlock_wrlock(prwlock);
1612 }
1613
1614 static inline int ast_rwlock_trywrlock(ast_rwlock_t *prwlock)
1615 {
1616         return pthread_rwlock_trywrlock(prwlock);
1617 }
1618
1619 #endif /* !DEBUG_THREADS */
1620
1621 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
1622 /*
1623  * If AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope constructors
1624  * and destructors to create/destroy global mutexes.
1625  */
1626 #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track)       \
1627         scope ast_mutex_t mutex = init_val;                     \
1628 static void  __attribute__((constructor)) init_##mutex(void)    \
1629 {                                                               \
1630         if (track)                                              \
1631                 ast_mutex_init(&mutex);                         \
1632         else                                                    \
1633                 ast_mutex_init_notracking(&mutex);              \
1634 }                                                               \
1635                                                                 \
1636 static void  __attribute__((destructor)) fini_##mutex(void)     \
1637 {                                                               \
1638         ast_mutex_destroy(&mutex);                              \
1639 }
1640 #else /* !AST_MUTEX_INIT_W_CONSTRUCTORS */
1641 /* By default, use static initialization of mutexes. */
1642 #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track)       scope ast_mutex_t mutex = init_val
1643 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
1644
1645 #ifndef __CYGWIN__      /* temporary disabled for cygwin */
1646 #define pthread_mutex_t         use_ast_mutex_t_instead_of_pthread_mutex_t
1647 #define pthread_cond_t          use_ast_cond_t_instead_of_pthread_cond_t
1648 #endif
1649 #define pthread_mutex_lock      use_ast_mutex_lock_instead_of_pthread_mutex_lock
1650 #define pthread_mutex_unlock    use_ast_mutex_unlock_instead_of_pthread_mutex_unlock
1651 #define pthread_mutex_trylock   use_ast_mutex_trylock_instead_of_pthread_mutex_trylock
1652 #define pthread_mutex_init      use_ast_mutex_init_instead_of_pthread_mutex_init
1653 #define pthread_mutex_destroy   use_ast_mutex_destroy_instead_of_pthread_mutex_destroy
1654 #define pthread_cond_init       use_ast_cond_init_instead_of_pthread_cond_init
1655 #define pthread_cond_destroy    use_ast_cond_destroy_instead_of_pthread_cond_destroy
1656 #define pthread_cond_signal     use_ast_cond_signal_instead_of_pthread_cond_signal
1657 #define pthread_cond_broadcast  use_ast_cond_broadcast_instead_of_pthread_cond_broadcast
1658 #define pthread_cond_wait       use_ast_cond_wait_instead_of_pthread_cond_wait
1659 #define pthread_cond_timedwait  use_ast_cond_timedwait_instead_of_pthread_cond_timedwait
1660
1661 #define AST_MUTEX_DEFINE_STATIC(mutex)                  __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE, 1)
1662 #define AST_MUTEX_DEFINE_STATIC_NOTRACKING(mutex)       __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE_NOTRACKING, 0)
1663
1664 #define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__
1665
1666 #define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
1667
1668 #ifndef __linux__
1669 #define pthread_create __use_ast_pthread_create_instead__
1670 #endif
1671
1672 /* Statically declared read/write locks */
1673
1674 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
1675 #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) \
1676         scope ast_rwlock_t rwlock = init_val; \
1677 static void  __attribute__((constructor)) init_##rwlock(void) \
1678 { \
1679         if (track) \
1680                 ast_rwlock_init(&rwlock); \
1681         else \
1682                 ast_rwlock_init_notracking(&rwlock); \
1683 } \
1684 static void  __attribute__((destructor)) fini_##rwlock(void) \
1685 { \
1686         ast_rwlock_destroy(&rwlock); \
1687 }
1688 #else
1689 #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) \
1690         scope ast_rwlock_t rwlock = init_val
1691 #endif
1692
1693 #define AST_RWLOCK_DEFINE_STATIC(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE, 1)
1694 #define AST_RWLOCK_DEFINE_STATIC_NOTRACKING(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE_NOTRACKING, 0)
1695
1696 /*
1697  * Support for atomic instructions.
1698  * For platforms that have it, use the native cpu instruction to
1699  * implement them. For other platforms, resort to a 'slow' version
1700  * (defined in utils.c) that protects the atomic instruction with
1701  * a single lock.
1702  * The slow versions is always available, for testing purposes,
1703  * as ast_atomic_fetchadd_int_slow()
1704  */
1705
1706 int ast_atomic_fetchadd_int_slow(volatile int *p, int v);
1707
1708 #include "asterisk/inline_api.h"
1709
1710 #if defined(HAVE_OSX_ATOMICS)
1711 #include "libkern/OSAtomic.h"
1712 #endif
1713
1714 /*! \brief Atomically add v to *p and return * the previous value of *p.
1715  * This can be used to handle reference counts, and the return value
1716  * can be used to generate unique identifiers.
1717  */
1718
1719 #if defined(HAVE_GCC_ATOMICS)
1720 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1721 {
1722         return __sync_fetch_and_add(p, v);
1723 })
1724 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
1725 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1726 {
1727         return OSAtomicAdd32(v, (int32_t *) p) - v;
1728 })
1729 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
1730 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1731 {
1732         return OSAtomicAdd64(v, (int64_t *) p) - v;
1733 #elif defined (__i386__) || defined(__x86_64__)
1734 #ifdef sun
1735 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1736 {
1737         __asm __volatile (
1738         "       lock;  xaddl   %0, %1 ;        "
1739         : "+r" (v),                     /* 0 (result) */
1740           "=m" (*p)                     /* 1 */
1741         : "m" (*p));                    /* 2 */
1742         return (v);
1743 })
1744 #else /* ifndef 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 #endif
1755 #else   /* low performance version in utils.c */
1756 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
1757 {
1758         return ast_atomic_fetchadd_int_slow(p, v);
1759 })
1760 #endif
1761
1762 /*! \brief decrement *p by 1 and return true if the variable has reached 0.
1763  * Useful e.g. to check if a refcount has reached 0.
1764  */
1765 #if defined(HAVE_GCC_ATOMICS)
1766 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
1767 {
1768         return __sync_sub_and_fetch(p, 1) == 0;
1769 })
1770 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
1771 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
1772 {
1773         return OSAtomicAdd32( -1, (int32_t *) p) == 0;
1774 })
1775 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
1776 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
1777 {
1778         return OSAtomicAdd64( -1, (int64_t *) p) == 0;
1779 #else
1780 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
1781 {
1782         int a = ast_atomic_fetchadd_int(p, -1);
1783         return a == 1; /* true if the value is 0 now (so it was 1 previously) */
1784 })
1785 #endif
1786
1787 #ifndef DEBUG_CHANNEL_LOCKS
1788 /*! \brief Lock a channel. If DEBUG_CHANNEL_LOCKS is defined
1789         in the Makefile, print relevant output for debugging */
1790 #define ast_channel_lock(x)             ast_mutex_lock(&x->lock_dont_use)
1791 /*! \brief Unlock a channel. If DEBUG_CHANNEL_LOCKS is defined
1792         in the Makefile, print relevant output for debugging */
1793 #define ast_channel_unlock(x)           ast_mutex_unlock(&x->lock_dont_use)
1794 /*! \brief Try locking a channel. If DEBUG_CHANNEL_LOCKS is defined
1795         in the Makefile, print relevant output for debugging */
1796 #define ast_channel_trylock(x)          ast_mutex_trylock(&x->lock_dont_use)
1797 #else
1798
1799 #define ast_channel_lock(a) __ast_channel_lock(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1800 /*! \brief Lock AST channel (and print debugging output)
1801 \note You need to enable DEBUG_CHANNEL_LOCKS for this function */
1802 int __ast_channel_lock(struct ast_channel *chan, const char *file, int lineno, const char *func);
1803
1804 #define ast_channel_unlock(a) __ast_channel_unlock(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1805 /*! \brief Unlock AST channel (and print debugging output)
1806 \note You need to enable DEBUG_CHANNEL_LOCKS for this function
1807 */
1808 int __ast_channel_unlock(struct ast_channel *chan, const char *file, int lineno, const char *func);
1809
1810 #define ast_channel_trylock(a) __ast_channel_trylock(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1811 /*! \brief Lock AST channel (and print debugging output)
1812 \note   You need to enable DEBUG_CHANNEL_LOCKS for this function */
1813 int __ast_channel_trylock(struct ast_channel *chan, const char *file, int lineno, const char *func);
1814 #endif
1815
1816 #endif /* _ASTERISK_LOCK_H */