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 static inline int __ast_pthread_mutex_lock(const char *filename, int lineno, const char *func,
215 const char* mutex_name, ast_mutex_t *t)
218 int canlog = strcmp(filename, "logger.c");
220 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
221 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
222 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
223 filename, lineno, func, mutex_name);
226 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
228 #ifdef DETECT_DEADLOCKS
230 time_t seconds = time(NULL);
233 #ifdef HAVE_MTX_PROFILE
234 ast_mark(mtx_prof, 1);
236 res = pthread_mutex_trylock(&t->mutex);
237 #ifdef HAVE_MTX_PROFILE
238 ast_mark(mtx_prof, 0);
241 current = time(NULL);
242 if ((current - seconds) && (!((current - seconds) % 5))) {
243 __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for mutex '%s'?\n",
244 filename, lineno, func, (int)(current - seconds), mutex_name);
245 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
246 t->file[t->reentrancy-1], t->lineno[t->reentrancy-1],
247 t->func[t->reentrancy-1], mutex_name);
251 } while (res == EBUSY);
254 #ifdef HAVE_MTX_PROFILE
255 ast_mark(mtx_prof, 1);
256 res = pthread_mutex_trylock(&t->mutex);
257 ast_mark(mtx_prof, 0);
260 res = pthread_mutex_lock(&t->mutex);
261 #endif /* DETECT_DEADLOCKS */
264 if (t->reentrancy < AST_MAX_REENTRANCY) {
265 t->file[t->reentrancy] = filename;
266 t->lineno[t->reentrancy] = lineno;
267 t->func[t->reentrancy] = func;
268 t->thread[t->reentrancy] = pthread_self();
271 __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
272 filename, lineno, func, mutex_name);
275 __ast_mutex_logger("%s line %d (%s): Error obtaining mutex: %s\n",
276 filename, lineno, func, strerror(errno));
283 static inline int __ast_pthread_mutex_trylock(const char *filename, int lineno, const char *func,
284 const char* mutex_name, ast_mutex_t *t)
287 int canlog = strcmp(filename, "logger.c");
289 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
290 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
291 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
292 filename, lineno, func, mutex_name);
295 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
297 if (!(res = pthread_mutex_trylock(&t->mutex))) {
298 if (t->reentrancy < AST_MAX_REENTRANCY) {
299 t->file[t->reentrancy] = filename;
300 t->lineno[t->reentrancy] = lineno;
301 t->func[t->reentrancy] = func;
302 t->thread[t->reentrancy] = pthread_self();
305 __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
306 filename, lineno, func, mutex_name);
313 static inline int __ast_pthread_mutex_unlock(const char *filename, int lineno, const char *func,
314 const char *mutex_name, ast_mutex_t *t)
317 int canlog = strcmp(filename, "logger.c");
319 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
320 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
321 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
322 filename, lineno, func, mutex_name);
326 if (t->reentrancy && (t->thread[t->reentrancy-1] != pthread_self())) {
327 __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
328 filename, lineno, func, mutex_name);
329 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
330 t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
334 if (--t->reentrancy < 0) {
335 __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
336 filename, lineno, func, mutex_name);
340 if (t->reentrancy < AST_MAX_REENTRANCY) {
341 t->file[t->reentrancy] = NULL;
342 t->lineno[t->reentrancy] = 0;
343 t->func[t->reentrancy] = NULL;
344 t->thread[t->reentrancy] = 0;
347 if ((res = pthread_mutex_unlock(&t->mutex))) {
348 __ast_mutex_logger("%s line %d (%s): Error releasing mutex: %s\n",
349 filename, lineno, func, strerror(res));
356 static inline int __ast_cond_init(const char *filename, int lineno, const char *func,
357 const char *cond_name, ast_cond_t *cond, pthread_condattr_t *cond_attr)
359 return pthread_cond_init(cond, cond_attr);
362 static inline int __ast_cond_signal(const char *filename, int lineno, const char *func,
363 const char *cond_name, ast_cond_t *cond)
365 return pthread_cond_signal(cond);
368 static inline int __ast_cond_broadcast(const char *filename, int lineno, const char *func,
369 const char *cond_name, ast_cond_t *cond)
371 return pthread_cond_broadcast(cond);
374 static inline int __ast_cond_destroy(const char *filename, int lineno, const char *func,
375 const char *cond_name, ast_cond_t *cond)
377 return pthread_cond_destroy(cond);
380 static inline int __ast_cond_wait(const char *filename, int lineno, const char *func,
381 const char *cond_name, const char *mutex_name,
382 ast_cond_t *cond, ast_mutex_t *t)
385 int canlog = strcmp(filename, "logger.c");
387 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
388 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
389 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
390 filename, lineno, func, mutex_name);
394 if (t->reentrancy && (t->thread[t->reentrancy-1] != pthread_self())) {
395 __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
396 filename, lineno, func, mutex_name);
397 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
398 t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
402 if (--t->reentrancy < 0) {
403 __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
404 filename, lineno, func, mutex_name);
408 if (t->reentrancy < AST_MAX_REENTRANCY) {
409 t->file[t->reentrancy] = NULL;
410 t->lineno[t->reentrancy] = 0;
411 t->func[t->reentrancy] = NULL;
412 t->thread[t->reentrancy] = 0;
415 if ((res = pthread_cond_wait(cond, &t->mutex))) {
416 __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n",
417 filename, lineno, func, strerror(res));
420 if (t->reentrancy < AST_MAX_REENTRANCY) {
421 t->file[t->reentrancy] = filename;
422 t->lineno[t->reentrancy] = lineno;
423 t->func[t->reentrancy] = func;
424 t->thread[t->reentrancy] = pthread_self();
427 __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
428 filename, lineno, func, mutex_name);
435 static inline int __ast_cond_timedwait(const char *filename, int lineno, const char *func,
436 const char *cond_name, const char *mutex_name, ast_cond_t *cond,
437 ast_mutex_t *t, const struct timespec *abstime)
440 int canlog = strcmp(filename, "logger.c");
442 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
443 if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
444 __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
445 filename, lineno, func, mutex_name);
449 if (t->reentrancy && (t->thread[t->reentrancy-1] != pthread_self())) {
450 __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
451 filename, lineno, func, mutex_name);
452 __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
453 t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
457 if (--t->reentrancy < 0) {
458 __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
459 filename, lineno, func, mutex_name);
463 if (t->reentrancy < AST_MAX_REENTRANCY) {
464 t->file[t->reentrancy] = NULL;
465 t->lineno[t->reentrancy] = 0;
466 t->func[t->reentrancy] = NULL;
467 t->thread[t->reentrancy] = 0;
470 if ((res = pthread_cond_timedwait(cond, &t->mutex, abstime)) && (res != ETIMEDOUT)) {
471 __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n",
472 filename, lineno, func, strerror(res));
475 if (t->reentrancy < AST_MAX_REENTRANCY) {
476 t->file[t->reentrancy] = filename;
477 t->lineno[t->reentrancy] = lineno;
478 t->func[t->reentrancy] = func;
479 t->thread[t->reentrancy] = pthread_self();
482 __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
483 filename, lineno, func, mutex_name);
490 #define ast_mutex_destroy(a) __ast_pthread_mutex_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
491 #define ast_mutex_lock(a) __ast_pthread_mutex_lock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
492 #define ast_mutex_unlock(a) __ast_pthread_mutex_unlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
493 #define ast_mutex_trylock(a) __ast_pthread_mutex_trylock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
494 #define ast_cond_init(cond, attr) __ast_cond_init(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond, attr)
495 #define ast_cond_destroy(cond) __ast_cond_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
496 #define ast_cond_signal(cond) __ast_cond_signal(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
497 #define ast_cond_broadcast(cond) __ast_cond_broadcast(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
498 #define ast_cond_wait(cond, mutex) __ast_cond_wait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex)
499 #define ast_cond_timedwait(cond, mutex, time) __ast_cond_timedwait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex, time)
501 #else /* !DEBUG_THREADS */
504 typedef pthread_mutex_t ast_mutex_t;
506 #define AST_MUTEX_INIT_VALUE ((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
508 static inline int ast_mutex_init(ast_mutex_t *pmutex)
510 pthread_mutexattr_t attr;
512 pthread_mutexattr_init(&attr);
513 pthread_mutexattr_settype(&attr, AST_MUTEX_KIND);
515 return pthread_mutex_init(pmutex, &attr);
518 #define ast_pthread_mutex_init(pmutex,a) pthread_mutex_init(pmutex,a)
520 static inline int ast_mutex_unlock(ast_mutex_t *pmutex)
522 return pthread_mutex_unlock(pmutex);
525 static inline int ast_mutex_destroy(ast_mutex_t *pmutex)
527 return pthread_mutex_destroy(pmutex);
530 static inline int ast_mutex_lock(ast_mutex_t *pmutex)
535 static inline int ast_mutex_trylock(ast_mutex_t *pmutex)
537 return pthread_mutex_trylock(pmutex);
540 typedef pthread_cond_t ast_cond_t;
542 static inline int ast_cond_init(ast_cond_t *cond, pthread_condattr_t *cond_attr)
544 return pthread_cond_init(cond, cond_attr);
547 static inline int ast_cond_signal(ast_cond_t *cond)
549 return pthread_cond_signal(cond);
552 static inline int ast_cond_broadcast(ast_cond_t *cond)
554 return pthread_cond_broadcast(cond);
557 static inline int ast_cond_destroy(ast_cond_t *cond)
559 return pthread_cond_destroy(cond);
562 static inline int ast_cond_wait(ast_cond_t *cond, ast_mutex_t *t)
564 return pthread_cond_wait(cond, t);
567 static inline int ast_cond_timedwait(ast_cond_t *cond, ast_mutex_t *t, const struct timespec *abstime)
569 return pthread_cond_timedwait(cond, t, abstime);
572 #endif /* !DEBUG_THREADS */
574 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
575 /* If AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope
576 constructors/destructors to create/destroy mutexes. */
577 #define __AST_MUTEX_DEFINE(scope, mutex) \
578 scope ast_mutex_t mutex = AST_MUTEX_INIT_VALUE; \
579 static void __attribute__ ((constructor)) init_##mutex(void) \
581 ast_mutex_init(&mutex); \
583 static void __attribute__ ((destructor)) fini_##mutex(void) \
585 ast_mutex_destroy(&mutex); \
587 #else /* !AST_MUTEX_INIT_W_CONSTRUCTORS */
588 /* By default, use static initialization of mutexes. */
589 #define __AST_MUTEX_DEFINE(scope, mutex) \
590 scope ast_mutex_t mutex = AST_MUTEX_INIT_VALUE
591 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
593 #define pthread_mutex_t use_ast_mutex_t_instead_of_pthread_mutex_t
594 #define pthread_mutex_lock use_ast_mutex_lock_instead_of_pthread_mutex_lock
595 #define pthread_mutex_unlock use_ast_mutex_unlock_instead_of_pthread_mutex_unlock
596 #define pthread_mutex_trylock use_ast_mutex_trylock_instead_of_pthread_mutex_trylock
597 #define pthread_mutex_init use_ast_mutex_init_instead_of_pthread_mutex_init
598 #define pthread_mutex_destroy use_ast_mutex_destroy_instead_of_pthread_mutex_destroy
599 #define pthread_cond_t use_ast_cond_t_instead_of_pthread_cond_t
600 #define pthread_cond_init use_ast_cond_init_instead_of_pthread_cond_init
601 #define pthread_cond_destroy use_ast_cond_destroy_instead_of_pthread_cond_destroy
602 #define pthread_cond_signal use_ast_cond_signal_instead_of_pthread_cond_signal
603 #define pthread_cond_broadcast use_ast_cond_broadcast_instead_of_pthread_cond_broadcast
604 #define pthread_cond_wait use_ast_cond_wait_instead_of_pthread_cond_wait
605 #define pthread_cond_timedwait use_ast_cond_timedwait_instead_of_pthread_cond_timedwait
607 #define AST_MUTEX_DEFINE_STATIC(mutex) __AST_MUTEX_DEFINE(static, mutex)
609 #define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__
611 #define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
614 #define pthread_create __use_ast_pthread_create_instead__
618 * Initial support for atomic instructions.
619 * For platforms that have it, use the native cpu instruction to
620 * implement them. For other platforms, resort to a 'slow' version
621 * (defined in utils.c) that protects the atomic instruction with
623 * The slow versions is always available, for testing purposes,
624 * as ast_atomic_fetchadd_int_slow()
627 int ast_atomic_fetchadd_int_slow(volatile int *p, int v);
629 #include "asterisk/inline_api.h"
631 #if defined(HAVE_OSX_ATOMICS)
632 #include "libkern/OSAtomic.h"
635 /*! \brief Atomically add v to *p and return * the previous value of *p.
636 * This can be used to handle reference counts, and the return value
637 * can be used to generate unique identifiers.
640 #if defined(HAVE_GCC_ATOMICS)
641 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
643 return __sync_fetch_and_add(p, v);
645 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
646 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
648 return OSAtomicAdd32(v, (int32_t *) p);
650 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
651 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
653 return OSAtomicAdd64(v, (int64_t *) p);
654 #elif defined (__i386__)
655 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
658 " lock xaddl %0, %1 ; "
659 : "+r" (v), /* 0 (result) */
664 #else /* low performance version in utils.c */
665 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
667 return ast_atomic_fetchadd_int_slow(p, v);
671 /*! \brief decrement *p by 1 and return true if the variable has reached 0.
672 * Useful e.g. to check if a refcount has reached 0.
674 #if defined(HAVE_GCC_ATOMICS)
675 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
677 return __sync_sub_and_fetch(p, 1) == 0;
679 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
680 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
682 return OSAtomicAdd32( -1, (int32_t *) p) == 0;
684 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
685 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
687 return OSAtomicAdd64( -1, (int64_t *) p) == 0;
689 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
691 int a = ast_atomic_fetchadd_int(p, -1);
692 return a == 1; /* true if the value is 0 now (so it was 1 previously) */
696 #ifndef DEBUG_CHANNEL_LOCKS
697 /*! \brief Lock a channel. If DEBUG_CHANNEL_LOCKS is defined
698 in the Makefile, print relevant output for debugging */
699 #define ast_channel_lock(x) ast_mutex_lock(&x->lock)
700 /*! \brief Unlock a channel. If DEBUG_CHANNEL_LOCKS is defined
701 in the Makefile, print relevant output for debugging */
702 #define ast_channel_unlock(x) ast_mutex_unlock(&x->lock)
703 /*! \brief Try locking a channel. If DEBUG_CHANNEL_LOCKS is defined
704 in the Makefile, print relevant output for debugging */
705 #define ast_channel_trylock(x) ast_mutex_trylock(&x->lock)
710 /*! \brief Lock AST channel (and print debugging output)
711 \note You need to enable DEBUG_CHANNEL_LOCKS for this function */
712 int ast_channel_lock(struct ast_channel *chan);
714 /*! \brief Unlock AST channel (and print debugging output)
715 \note You need to enable DEBUG_CHANNEL_LOCKS for this function
717 int ast_channel_unlock(struct ast_channel *chan);
719 /*! \brief Lock AST channel (and print debugging output)
720 \note You need to enable DEBUG_CHANNEL_LOCKS for this function */
721 int ast_channel_trylock(struct ast_channel *chan);
724 #endif /* _ASTERISK_LOCK_H */