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