2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
20 * \brief General Asterisk channel locking definitions.
25 /* \page LockDef Asterisk thread locking models
27 * This file provides different implementation of the functions,
28 * depending on the platform, the use of DEBUG_THREADS, and the way
29 * module-level mutexes are initialized.
31 * - \b static: the mutex is assigned the value AST_MUTEX_INIT_VALUE
32 * this is done at compile time, and is the way used on Linux.
33 * This method is not applicable to all platforms e.g. when the
34 * initialization needs that some code is run.
36 * - \b through constructors: for each mutex, a constructor function is
37 * defined, which then runs when the program (or the module)
38 * starts. The problem with this approach is that there is a
39 * lot of code duplication (a new block of code is created for
40 * each mutex). Also, it does not prevent a user from declaring
41 * a global mutex without going through the wrapper macros,
42 * so sane programming practices are still required.
45 #ifndef _ASTERISK_LOCK_H
46 #define _ASTERISK_LOCK_H
51 #include <sys/param.h>
53 #include "asterisk/logger.h"
55 /* internal macro to profile mutexes. Only computes the delay on
58 #ifndef HAVE_MTX_PROFILE
59 #define __MTX_PROF(a) return pthread_mutex_lock((a))
61 #define __MTX_PROF(a) do { \
63 /* profile only non-blocking events */ \
64 ast_mark(mtx_prof, 1); \
65 i = pthread_mutex_trylock((a)); \
66 ast_mark(mtx_prof, 0); \
70 return pthread_mutex_lock((a)); \
72 #endif /* HAVE_MTX_PROFILE */
74 #define AST_PTHREADT_NULL (pthread_t) -1
75 #define AST_PTHREADT_STOP (pthread_t) -2
77 #if defined(SOLARIS) || defined(BSD)
78 #define AST_MUTEX_INIT_W_CONSTRUCTORS
79 #endif /* SOLARIS || BSD */
81 /* Asterisk REQUIRES recursive (not error checking) mutexes
82 and will not run without them. */
83 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
84 #define PTHREAD_MUTEX_INIT_VALUE PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
85 #define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE_NP
87 #define PTHREAD_MUTEX_INIT_VALUE PTHREAD_MUTEX_INITIALIZER
88 #define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE
89 #endif /* PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP */
93 #define __ast_mutex_logger(...) do { if (canlog) ast_log(LOG_ERROR, __VA_ARGS__); else fprintf(stderr, __VA_ARGS__); } while (0)
96 #define DO_THREAD_CRASH do { *((int *)(0)) = 1; } while(0)
98 #define DO_THREAD_CRASH do { } while (0)
106 #define AST_MUTEX_INIT_VALUE { PTHREAD_MUTEX_INIT_VALUE, { NULL }, { 0 }, 0, { NULL }, { 0 } }
108 #define AST_MAX_REENTRANCY 10
110 struct ast_mutex_info {
111 pthread_mutex_t mutex;
112 const char *file[AST_MAX_REENTRANCY];
113 int lineno[AST_MAX_REENTRANCY];
115 const char *func[AST_MAX_REENTRANCY];
116 pthread_t thread[AST_MAX_REENTRANCY];
119 typedef struct ast_mutex_info ast_mutex_t;
121 typedef pthread_cond_t ast_cond_t;
123 static pthread_mutex_t empty_mutex;
125 static void __attribute__((constructor)) init_empty_mutex(void)
127 memset(&empty_mutex, 0, sizeof(empty_mutex));
130 static inline int __ast_pthread_mutex_init_attr(const char *filename, int lineno, const char *func,
131 const char *mutex_name, ast_mutex_t *t,
132 pthread_mutexattr_t *attr)
134 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
135 int canlog = strcmp(filename, "logger.c");
137 if ((t->mutex) != ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
138 if ((t->mutex) != (empty_mutex)) {
139 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is already initialized.\n",
140 filename, lineno, func, mutex_name);
141 __ast_mutex_logger("%s line %d (%s): Error: previously initialization of mutex '%s'.\n",
142 t->file[0], t->lineno[0], t->func[0], mutex_name);
149 t->file[0] = filename;
150 t->lineno[0] = lineno;
155 return pthread_mutex_init(&t->mutex, attr);
158 static inline int __ast_pthread_mutex_init(const char *filename, int lineno, const char *func,
159 const char *mutex_name, ast_mutex_t *t)
161 static pthread_mutexattr_t attr;
163 pthread_mutexattr_init(&attr);
164 pthread_mutexattr_settype(&attr, AST_MUTEX_KIND);
166 return __ast_pthread_mutex_init_attr(filename, lineno, func, mutex_name, t, &attr);
168 #define ast_mutex_init(pmutex) __ast_pthread_mutex_init(__FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex)
170 static inline int __ast_pthread_mutex_destroy(const char *filename, int lineno, const char *func,
171 const char *mutex_name, ast_mutex_t *t)
174 int canlog = strcmp(filename, "logger.c");
176 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
177 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
178 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
179 filename, lineno, func, mutex_name);
183 res = pthread_mutex_trylock(&t->mutex);
186 pthread_mutex_unlock(&t->mutex);
189 __ast_mutex_logger("%s line %d (%s): Error: attempt to destroy invalid mutex '%s'.\n",
190 filename, lineno, func, mutex_name);
193 __ast_mutex_logger("%s line %d (%s): Error: attempt to destroy locked mutex '%s'.\n",
194 filename, lineno, func, mutex_name);
195 __ast_mutex_logger("%s line %d (%s): Error: '%s' was locked here.\n",
196 t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
200 if ((res = pthread_mutex_destroy(&t->mutex)))
201 __ast_mutex_logger("%s line %d (%s): Error destroying mutex: %s\n",
202 filename, lineno, func, strerror(res));
203 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
205 t->mutex = PTHREAD_MUTEX_INIT_VALUE;
207 t->file[0] = filename;
208 t->lineno[0] = lineno;
214 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
215 /* If AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope
216 constructors/destructors to create/destroy mutexes. */
217 #define __AST_MUTEX_DEFINE(scope, mutex) \
218 scope ast_mutex_t mutex = AST_MUTEX_INIT_VALUE; \
219 static void __attribute__ ((constructor)) init_##mutex(void) \
221 ast_mutex_init(&mutex); \
223 static void __attribute__ ((destructor)) fini_##mutex(void) \
225 ast_mutex_destroy(&mutex); \
227 #else /* !AST_MUTEX_INIT_W_CONSTRUCTORS */
228 /* By default, use static initialization of mutexes. */
229 #define __AST_MUTEX_DEFINE(scope, mutex) \
230 scope ast_mutex_t mutex = AST_MUTEX_INIT_VALUE
231 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
233 static inline int __ast_pthread_mutex_lock(const char *filename, int lineno, const char *func,
234 const char* mutex_name, ast_mutex_t *t)
237 int canlog = strcmp(filename, "logger.c");
239 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
240 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
241 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
242 filename, lineno, func, mutex_name);
245 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
247 #ifdef DETECT_DEADLOCKS
249 time_t seconds = time(NULL);
252 #ifdef HAVE_MTX_PROFILE
253 ast_mark(mtx_prof, 1);
255 res = pthread_mutex_trylock(&t->mutex);
256 #ifdef HAVE_MTX_PROFILE
257 ast_mark(mtx_prof, 0);
260 current = time(NULL);
261 if ((current - seconds) && (!((current - seconds) % 5))) {
262 __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for mutex '%s'?\n",
263 filename, lineno, func, (int)(current - seconds), mutex_name);
264 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
265 t->file[t->reentrancy-1], t->lineno[t->reentrancy-1],
266 t->func[t->reentrancy-1], mutex_name);
270 } while (res == EBUSY);
273 #ifdef HAVE_MTX_PROFILE
274 ast_mark(mtx_prof, 1);
275 res = pthread_mutex_trylock(&t->mutex);
276 ast_mark(mtx_prof, 0);
279 res = pthread_mutex_lock(&t->mutex);
280 #endif /* DETECT_DEADLOCKS */
283 if (t->reentrancy < AST_MAX_REENTRANCY) {
284 t->file[t->reentrancy] = filename;
285 t->lineno[t->reentrancy] = lineno;
286 t->func[t->reentrancy] = func;
287 t->thread[t->reentrancy] = pthread_self();
290 __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
291 filename, lineno, func, mutex_name);
294 __ast_mutex_logger("%s line %d (%s): Error obtaining mutex: %s\n",
295 filename, lineno, func, strerror(errno));
302 static inline int __ast_pthread_mutex_trylock(const char *filename, int lineno, const char *func,
303 const char* mutex_name, ast_mutex_t *t)
306 int canlog = strcmp(filename, "logger.c");
308 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
309 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
310 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
311 filename, lineno, func, mutex_name);
314 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
316 if (!(res = pthread_mutex_trylock(&t->mutex))) {
317 if (t->reentrancy < AST_MAX_REENTRANCY) {
318 t->file[t->reentrancy] = filename;
319 t->lineno[t->reentrancy] = lineno;
320 t->func[t->reentrancy] = func;
321 t->thread[t->reentrancy] = pthread_self();
324 __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
325 filename, lineno, func, mutex_name);
332 static inline int __ast_pthread_mutex_unlock(const char *filename, int lineno, const char *func,
333 const char *mutex_name, ast_mutex_t *t)
336 int canlog = strcmp(filename, "logger.c");
338 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
339 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
340 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
341 filename, lineno, func, mutex_name);
345 if (t->reentrancy && (t->thread[t->reentrancy-1] != pthread_self())) {
346 __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
347 filename, lineno, func, mutex_name);
348 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
349 t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
353 if (--t->reentrancy < 0) {
354 __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
355 filename, lineno, func, mutex_name);
359 if (t->reentrancy < AST_MAX_REENTRANCY) {
360 t->file[t->reentrancy] = NULL;
361 t->lineno[t->reentrancy] = 0;
362 t->func[t->reentrancy] = NULL;
363 t->thread[t->reentrancy] = 0;
366 if ((res = pthread_mutex_unlock(&t->mutex))) {
367 __ast_mutex_logger("%s line %d (%s): Error releasing mutex: %s\n",
368 filename, lineno, func, strerror(res));
375 static inline int __ast_cond_init(const char *filename, int lineno, const char *func,
376 const char *cond_name, ast_cond_t *cond, pthread_condattr_t *cond_attr)
378 return pthread_cond_init(cond, cond_attr);
381 static inline int __ast_cond_signal(const char *filename, int lineno, const char *func,
382 const char *cond_name, ast_cond_t *cond)
384 return pthread_cond_signal(cond);
387 static inline int __ast_cond_broadcast(const char *filename, int lineno, const char *func,
388 const char *cond_name, ast_cond_t *cond)
390 return pthread_cond_broadcast(cond);
393 static inline int __ast_cond_destroy(const char *filename, int lineno, const char *func,
394 const char *cond_name, ast_cond_t *cond)
396 return pthread_cond_destroy(cond);
399 static inline int __ast_cond_wait(const char *filename, int lineno, const char *func,
400 const char *cond_name, const char *mutex_name,
401 ast_cond_t *cond, ast_mutex_t *t)
404 int canlog = strcmp(filename, "logger.c");
406 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
407 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
408 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
409 filename, lineno, func, mutex_name);
413 if (t->reentrancy && (t->thread[t->reentrancy-1] != pthread_self())) {
414 __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
415 filename, lineno, func, mutex_name);
416 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
417 t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
421 if (--t->reentrancy < 0) {
422 __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
423 filename, lineno, func, mutex_name);
427 if (t->reentrancy < AST_MAX_REENTRANCY) {
428 t->file[t->reentrancy] = NULL;
429 t->lineno[t->reentrancy] = 0;
430 t->func[t->reentrancy] = NULL;
431 t->thread[t->reentrancy] = 0;
434 if ((res = pthread_cond_wait(cond, &t->mutex))) {
435 __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n",
436 filename, lineno, func, strerror(res));
439 if (t->reentrancy < AST_MAX_REENTRANCY) {
440 t->file[t->reentrancy] = filename;
441 t->lineno[t->reentrancy] = lineno;
442 t->func[t->reentrancy] = func;
443 t->thread[t->reentrancy] = pthread_self();
446 __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
447 filename, lineno, func, mutex_name);
454 static inline int __ast_cond_timedwait(const char *filename, int lineno, const char *func,
455 const char *cond_name, const char *mutex_name, ast_cond_t *cond,
456 ast_mutex_t *t, const struct timespec *abstime)
459 int canlog = strcmp(filename, "logger.c");
461 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
462 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
463 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
464 filename, lineno, func, mutex_name);
468 if (t->reentrancy && (t->thread[t->reentrancy-1] != pthread_self())) {
469 __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
470 filename, lineno, func, mutex_name);
471 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
472 t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
476 if (--t->reentrancy < 0) {
477 __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
478 filename, lineno, func, mutex_name);
482 if (t->reentrancy < AST_MAX_REENTRANCY) {
483 t->file[t->reentrancy] = NULL;
484 t->lineno[t->reentrancy] = 0;
485 t->func[t->reentrancy] = NULL;
486 t->thread[t->reentrancy] = 0;
489 if ((res = pthread_cond_timedwait(cond, &t->mutex, abstime)) && (res != ETIMEDOUT)) {
490 __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n",
491 filename, lineno, func, strerror(res));
494 if (t->reentrancy < AST_MAX_REENTRANCY) {
495 t->file[t->reentrancy] = filename;
496 t->lineno[t->reentrancy] = lineno;
497 t->func[t->reentrancy] = func;
498 t->thread[t->reentrancy] = pthread_self();
501 __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
502 filename, lineno, func, mutex_name);
509 #define ast_mutex_destroy(a) __ast_pthread_mutex_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
510 #define ast_mutex_lock(a) __ast_pthread_mutex_lock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
511 #define ast_mutex_unlock(a) __ast_pthread_mutex_unlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
512 #define ast_mutex_trylock(a) __ast_pthread_mutex_trylock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
513 #define ast_cond_init(cond, attr) __ast_cond_init(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond, attr)
514 #define ast_cond_destroy(cond) __ast_cond_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
515 #define ast_cond_signal(cond) __ast_cond_signal(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
516 #define ast_cond_broadcast(cond) __ast_cond_broadcast(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
517 #define ast_cond_wait(cond, mutex) __ast_cond_wait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex)
518 #define ast_cond_timedwait(cond, mutex, time) __ast_cond_timedwait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex, time)
520 #else /* !DEBUG_THREADS */
523 typedef pthread_mutex_t ast_mutex_t;
525 #define AST_MUTEX_INIT_VALUE ((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
527 static inline int ast_mutex_init(ast_mutex_t *pmutex)
529 pthread_mutexattr_t attr;
531 pthread_mutexattr_init(&attr);
532 pthread_mutexattr_settype(&attr, AST_MUTEX_KIND);
534 return pthread_mutex_init(pmutex, &attr);
537 #define ast_pthread_mutex_init(pmutex,a) pthread_mutex_init(pmutex,a)
539 static inline int ast_mutex_unlock(ast_mutex_t *pmutex)
541 return pthread_mutex_unlock(pmutex);
544 static inline int ast_mutex_destroy(ast_mutex_t *pmutex)
546 return pthread_mutex_destroy(pmutex);
549 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
550 /* if AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope
551 constructors/destructors to create/destroy mutexes. */
552 #define __AST_MUTEX_DEFINE(scope,mutex) \
553 scope ast_mutex_t mutex = AST_MUTEX_INIT_VALUE; \
554 static void __attribute__ ((constructor)) init_##mutex(void) \
556 ast_mutex_init(&mutex); \
558 static void __attribute__ ((destructor)) fini_##mutex(void) \
560 ast_mutex_destroy(&mutex); \
563 static inline int ast_mutex_lock(ast_mutex_t *pmutex)
568 static inline int ast_mutex_trylock(ast_mutex_t *pmutex)
570 return pthread_mutex_trylock(pmutex);
574 /* By default, use static initialization of mutexes.*/
575 #define __AST_MUTEX_DEFINE(scope, mutex) \
576 scope ast_mutex_t mutex = AST_MUTEX_INIT_VALUE
578 static inline int ast_mutex_lock(ast_mutex_t *pmutex)
583 static inline int ast_mutex_trylock(ast_mutex_t *pmutex)
585 return pthread_mutex_trylock(pmutex);
588 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
590 typedef pthread_cond_t ast_cond_t;
592 static inline int ast_cond_init(ast_cond_t *cond, pthread_condattr_t *cond_attr)
594 return pthread_cond_init(cond, cond_attr);
597 static inline int ast_cond_signal(ast_cond_t *cond)
599 return pthread_cond_signal(cond);
602 static inline int ast_cond_broadcast(ast_cond_t *cond)
604 return pthread_cond_broadcast(cond);
607 static inline int ast_cond_destroy(ast_cond_t *cond)
609 return pthread_cond_destroy(cond);
612 static inline int ast_cond_wait(ast_cond_t *cond, ast_mutex_t *t)
614 return pthread_cond_wait(cond, t);
617 static inline int ast_cond_timedwait(ast_cond_t *cond, ast_mutex_t *t, const struct timespec *abstime)
619 return pthread_cond_timedwait(cond, t, abstime);
622 #endif /* !DEBUG_THREADS */
624 #define pthread_mutex_t use_ast_mutex_t_instead_of_pthread_mutex_t
625 #define pthread_mutex_lock use_ast_mutex_lock_instead_of_pthread_mutex_lock
626 #define pthread_mutex_unlock use_ast_mutex_unlock_instead_of_pthread_mutex_unlock
627 #define pthread_mutex_trylock use_ast_mutex_trylock_instead_of_pthread_mutex_trylock
628 #define pthread_mutex_init use_ast_mutex_init_instead_of_pthread_mutex_init
629 #define pthread_mutex_destroy use_ast_mutex_destroy_instead_of_pthread_mutex_destroy
630 #define pthread_cond_t use_ast_cond_t_instead_of_pthread_cond_t
631 #define pthread_cond_init use_ast_cond_init_instead_of_pthread_cond_init
632 #define pthread_cond_destroy use_ast_cond_destroy_instead_of_pthread_cond_destroy
633 #define pthread_cond_signal use_ast_cond_signal_instead_of_pthread_cond_signal
634 #define pthread_cond_broadcast use_ast_cond_broadcast_instead_of_pthread_cond_broadcast
635 #define pthread_cond_wait use_ast_cond_wait_instead_of_pthread_cond_wait
636 #define pthread_cond_timedwait use_ast_cond_timedwait_instead_of_pthread_cond_timedwait
638 #define AST_MUTEX_DEFINE_STATIC(mutex) __AST_MUTEX_DEFINE(static, mutex)
640 #define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__
642 #define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
645 #define pthread_create __use_ast_pthread_create_instead__
649 * Initial support for atomic instructions.
650 * For platforms that have it, use the native cpu instruction to
651 * implement them. For other platforms, resort to a 'slow' version
652 * (defined in utils.c) that protects the atomic instruction with
654 * The slow versions is always available, for testing purposes,
655 * as ast_atomic_fetchadd_int_slow()
658 int ast_atomic_fetchadd_int_slow(volatile int *p, int v);
660 #include "asterisk/inline_api.h"
662 #if defined(HAVE_OSX_ATOMICS)
663 #include "libkern/OSAtomic.h"
666 /*! \brief Atomically add v to *p and return * the previous value of *p.
667 * This can be used to handle reference counts, and the return value
668 * can be used to generate unique identifiers.
671 #if defined(HAVE_GCC_ATOMICS)
672 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
674 return __sync_fetch_and_add(p, v);
676 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
677 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
679 return OSAtomicAdd32(v, (int32_t *) p);
681 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
682 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
684 return OSAtomicAdd64(v, (int64_t *) p);
685 #elif defined (__i386__)
686 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
689 " lock xaddl %0, %1 ; "
690 : "+r" (v), /* 0 (result) */
695 #else /* low performance version in utils.c */
696 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
698 return ast_atomic_fetchadd_int_slow(p, v);
702 /*! \brief decrement *p by 1 and return true if the variable has reached 0.
703 * Useful e.g. to check if a refcount has reached 0.
705 #if defined(HAVE_GCC_ATOMICS)
706 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
708 return __sync_sub_and_fetch(p, 1) == 0;
710 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
711 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
713 return OSAtomicAdd32( -1, (int32_t *) p) == 0;
715 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
716 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
718 return OSAtomicAdd64( -1, (int64_t *) p) == 0;
720 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
722 int a = ast_atomic_fetchadd_int(p, -1);
723 return a == 1; /* true if the value is 0 now (so it was 1 previously) */
727 #ifndef DEBUG_CHANNEL_LOCKS
728 /*! \brief Lock a channel. If DEBUG_CHANNEL_LOCKS is defined
729 in the Makefile, print relevant output for debugging */
730 #define ast_channel_lock(x) ast_mutex_lock(&x->lock)
731 /*! \brief Unlock a channel. If DEBUG_CHANNEL_LOCKS is defined
732 in the Makefile, print relevant output for debugging */
733 #define ast_channel_unlock(x) ast_mutex_unlock(&x->lock)
734 /*! \brief Try locking a channel. If DEBUG_CHANNEL_LOCKS is defined
735 in the Makefile, print relevant output for debugging */
736 #define ast_channel_trylock(x) ast_mutex_trylock(&x->lock)
739 /*! \brief Lock AST channel (and print debugging output)
740 \note You need to enable DEBUG_CHANNEL_LOCKS for this function */
741 int ast_channel_lock(struct ast_channel *chan);
743 /*! \brief Unlock AST channel (and print debugging output)
744 \note You need to enable DEBUG_CHANNEL_LOCKS for this function
746 int ast_channel_unlock(struct ast_channel *chan);
748 /*! \brief Lock AST channel (and print debugging output)
749 \note You need to enable DEBUG_CHANNEL_LOCKS for this function */
750 int ast_channel_trylock(struct ast_channel *chan);
753 #endif /* _ASTERISK_LOCK_H */