48 #ifndef _ASTERISK_LOCK_H
49 #define _ASTERISK_LOCK_H
53 #include <sys/param.h>
58 #ifndef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
65 #define AST_PTHREADT_NULL (pthread_t) -1
66 #define AST_PTHREADT_STOP (pthread_t) -2
68 #if (defined(SOLARIS) || defined(BSD))
69 #define AST_MUTEX_INIT_W_CONSTRUCTORS
74 #if defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) && defined(HAVE_PTHREAD_MUTEX_RECURSIVE_NP)
75 #define PTHREAD_MUTEX_INIT_VALUE PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
76 #define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE_NP
78 #define PTHREAD_MUTEX_INIT_VALUE PTHREAD_MUTEX_INITIALIZER
79 #define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE
82 #ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
83 #define __AST_RWLOCK_INIT_VALUE PTHREAD_RWLOCK_INITIALIZER
85 #define __AST_RWLOCK_INIT_VALUE {0}
89 #define AST_LOCK_TRACK_INIT_VALUE { { NULL }, { 0 }, 0, { NULL }, { 0 }, {{{ 0 }}}, PTHREAD_MUTEX_INIT_VALUE }
91 #define AST_LOCK_TRACK_INIT_VALUE { { NULL }, { 0 }, 0, { NULL }, { 0 }, PTHREAD_MUTEX_INIT_VALUE }
94 #define AST_MUTEX_INIT_VALUE { PTHREAD_MUTEX_INIT_VALUE, NULL, 1 }
95 #define AST_MUTEX_INIT_VALUE_NOTRACKING { PTHREAD_MUTEX_INIT_VALUE, NULL, 0 }
97 #define AST_RWLOCK_INIT_VALUE { __AST_RWLOCK_INIT_VALUE, NULL, 1 }
98 #define AST_RWLOCK_INIT_VALUE_NOTRACKING { __AST_RWLOCK_INIT_VALUE, NULL, 0 }
100 #define AST_MAX_REENTRANCY 10
152 #define ast_mutex_init(pmutex) __ast_pthread_mutex_init(1, __FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex)
153 #define ast_mutex_init_notracking(pmutex) __ast_pthread_mutex_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex)
154 #define ast_mutex_destroy(a) __ast_pthread_mutex_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
155 #define ast_mutex_lock(a) __ast_pthread_mutex_lock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
156 #define ast_mutex_unlock(a) __ast_pthread_mutex_unlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
157 #define ast_mutex_trylock(a) __ast_pthread_mutex_trylock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
160 int __ast_cond_init(
const char *filename,
int lineno,
const char *func,
const char *cond_name,
ast_cond_t *
cond, pthread_condattr_t *cond_attr);
167 #define ast_cond_init(cond, attr) __ast_cond_init(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond, attr)
168 #define ast_cond_destroy(cond) __ast_cond_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
169 #define ast_cond_signal(cond) __ast_cond_signal(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
170 #define ast_cond_broadcast(cond) __ast_cond_broadcast(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
171 #define ast_cond_wait(cond, mutex) __ast_cond_wait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex)
172 #define ast_cond_timedwait(cond, mutex, time) __ast_cond_timedwait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex, time)
190 #define ast_rwlock_init(rwlock) __ast_rwlock_init(1, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
197 #define ast_rwlock_init_notracking(rwlock) __ast_rwlock_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
199 #define ast_rwlock_destroy(rwlock) __ast_rwlock_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
200 #define ast_rwlock_unlock(a) __ast_rwlock_unlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
201 #define ast_rwlock_rdlock(a) __ast_rwlock_rdlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
202 #define ast_rwlock_wrlock(a) __ast_rwlock_wrlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
203 #define ast_rwlock_tryrdlock(a) __ast_rwlock_tryrdlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
204 #define ast_rwlock_trywrlock(a) __ast_rwlock_trywrlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
205 #define ast_rwlock_timedrdlock(a, b) __ast_rwlock_timedrdlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a, b)
206 #define ast_rwlock_timedwrlock(a, b) __ast_rwlock_timedwrlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a, b)
208 #define ROFFSET ((lt->reentrancy > 0) ? (lt->reentrancy-1) : 0)
212 #define __ast_mutex_logger(...) do { if (canlog) ast_log(LOG_ERROR, __VA_ARGS__); else fprintf(stderr, __VA_ARGS__); } while (0)
215 #define DO_THREAD_CRASH do { *((int *)(0)) = 1; } while(0)
217 #define DO_THREAD_CRASH do { } while (0)
236 #if !defined(LOW_MEMORY)
239 int line_num,
const char *func,
const char *lock_name,
void *lock_addr,
struct ast_bt *bt);
242 int line_num,
const char *func,
const char *lock_name,
void *lock_addr);
248 #define ast_store_lock_info(I,DONT,CARE,ABOUT,THE,PARAMETERS,BUD)
250 #define ast_store_lock_info(I,DONT,CARE,ABOUT,THE,PARAMETERS)
257 #if !defined(LOW_MEMORY)
258 void ast_mark_lock_acquired(
void *lock_addr);
260 #define ast_mark_lock_acquired(ignore)
266 #if !defined(LOW_MEMORY)
267 void ast_mark_lock_failed(
void *lock_addr);
269 #define ast_mark_lock_failed(ignore)
278 #if !defined(LOW_MEMORY)
280 void ast_remove_lock_info(
void *lock_addr,
struct ast_bt *bt);
282 void ast_remove_lock_info(
void *lock_addr);
284 void ast_suspend_lock_info(
void *lock_addr);
285 void ast_restore_lock_info(
void *lock_addr);
288 #define ast_remove_lock_info(ignore,me)
290 #define ast_remove_lock_info(ignore)
292 #define ast_suspend_lock_info(ignore);
293 #define ast_restore_lock_info(ignore);
306 void log_show_lock(
void *this_lock_addr);
314 #if !defined(LOW_MEMORY)
315 int ast_find_lock_info(
void *lock_addr,
char *filename,
size_t filename_size,
int *lineno,
char *func,
size_t func_size,
char *mutex_name,
size_t mutex_name_size);
317 #define ast_find_lock_info(a,b,c,d,e,f,g,h) -1
326 #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
328 char __filename[80], __func[80], __mutex_name[80]; \
330 int __res = ast_find_lock_info(ao2_object_get_lockaddr(chan), __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
331 int __res2 = ast_channel_unlock(chan); \
335 ast_log(LOG_WARNING, "Could not unlock channel '%s': %s and no lock info found! I will NOT try to relock.\n", #chan, strerror(__res2)); \
337 ast_channel_lock(chan); \
341 ast_log(LOG_WARNING, "Could not unlock channel '%s': %s. {{{Originally locked at %s line %d: (%s) '%s'}}} I will NOT try to relock.\n", #chan, strerror(__res2), __filename, __lineno, __func, __mutex_name); \
343 __ao2_lock(chan, __filename, __func, __lineno, __mutex_name); \
348 #define DEADLOCK_AVOIDANCE(lock) \
350 char __filename[80], __func[80], __mutex_name[80]; \
352 int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
353 int __res2 = ast_mutex_unlock(lock); \
357 ast_mutex_lock(lock); \
359 ast_log(LOG_WARNING, "Could not unlock mutex '%s': %s and no lock info found! I will NOT try to relock.\n", #lock, strerror(__res2)); \
363 __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
365 ast_log(LOG_WARNING, "Could not unlock mutex '%s': %s. {{{Originally locked at %s line %d: (%s) '%s'}}} I will NOT try to relock.\n", #lock, strerror(__res2), __filename, __lineno, __func, __mutex_name); \
382 #define DLA_UNLOCK(lock) \
384 char __filename[80], __func[80], __mutex_name[80]; \
386 int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
387 int __res2 = ast_mutex_unlock(lock);
401 #define DLA_LOCK(lock) \
404 ast_log(LOG_WARNING, "Could not unlock mutex '%s': %s and no lock info found! I will NOT try to relock.\n", #lock, strerror(__res2)); \
406 ast_mutex_lock(lock); \
410 ast_log(LOG_WARNING, "Could not unlock mutex '%s': %s. {{{Originally locked at %s line %d: (%s) '%s'}}} I will NOT try to relock.\n", #lock, strerror(__res2), __filename, __lineno, __func, __mutex_name); \
412 __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
421 fprintf(stderr,
"ast_reentrancy_lock failed: '%s' (%d)\n", strerror(res), res);
422 #if defined(DO_CRASH) || defined(THREAD_CRASH)
428 static inline void ast_reentrancy_unlock(
struct ast_lock_track *lt)
432 fprintf(stderr,
"ast_reentrancy_unlock failed: '%s' (%d)\n", strerror(res), res);
433 #if defined(DO_CRASH) || defined(THREAD_CRASH)
439 static inline void ast_reentrancy_init(
struct ast_lock_track **plt)
442 pthread_mutexattr_t reentr_attr;
461 pthread_mutexattr_init(&reentr_attr);
464 pthread_mutexattr_destroy(&reentr_attr);
467 static inline void delete_reentrancy_cs(
struct ast_lock_track **plt)
480 #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
481 ast_channel_unlock(chan); \
483 ast_channel_lock(chan);
485 #define DEADLOCK_AVOIDANCE(lock) \
488 if (!(__res = ast_mutex_unlock(lock))) { \
490 ast_mutex_lock(lock); \
492 ast_log(LOG_WARNING, "Failed to unlock mutex '%s' (%s). I will NOT try to relock. {{{ THIS IS A BUG. }}}\n", #lock, strerror(__res)); \
496 #define DLA_UNLOCK(lock) ast_mutex_unlock(lock)
498 #define DLA_LOCK(lock) ast_mutex_lock(lock)
502 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
507 #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track) \
508 scope ast_mutex_t mutex = init_val; \
509 static void __attribute__((constructor)) init_##mutex(void) \
512 ast_mutex_init(&mutex); \
514 ast_mutex_init_notracking(&mutex); \
517 static void __attribute__((destructor)) fini_##mutex(void) \
519 ast_mutex_destroy(&mutex); \
523 #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track) scope ast_mutex_t mutex = init_val
526 #define AST_MUTEX_DEFINE_STATIC(mutex) __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE, 1)
527 #define AST_MUTEX_DEFINE_STATIC_NOTRACKING(mutex) __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE_NOTRACKING, 0)
531 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
532 #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) \
533 scope ast_rwlock_t rwlock = init_val; \
534 static void __attribute__((constructor)) init_##rwlock(void) \
537 ast_rwlock_init(&rwlock); \
539 ast_rwlock_init_notracking(&rwlock); \
541 static void __attribute__((destructor)) fini_##rwlock(void) \
543 ast_rwlock_destroy(&rwlock); \
546 #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) scope ast_rwlock_t rwlock = init_val
549 #define AST_RWLOCK_DEFINE_STATIC(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE, 1)
550 #define AST_RWLOCK_DEFINE_STATIC_NOTRACKING(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE_NOTRACKING, 0)
553 #define pthread_mutex_t use_ast_mutex_t_instead_of_pthread_mutex_t
554 #define pthread_cond_t use_ast_cond_t_instead_of_pthread_cond_t
556 #define pthread_mutex_lock use_ast_mutex_lock_instead_of_pthread_mutex_lock
557 #define pthread_mutex_unlock use_ast_mutex_unlock_instead_of_pthread_mutex_unlock
558 #define pthread_mutex_trylock use_ast_mutex_trylock_instead_of_pthread_mutex_trylock
559 #define pthread_mutex_init use_ast_mutex_init_instead_of_pthread_mutex_init
560 #define pthread_mutex_destroy use_ast_mutex_destroy_instead_of_pthread_mutex_destroy
561 #define pthread_cond_init use_ast_cond_init_instead_of_pthread_cond_init
562 #define pthread_cond_destroy use_ast_cond_destroy_instead_of_pthread_cond_destroy
563 #define pthread_cond_signal use_ast_cond_signal_instead_of_pthread_cond_signal
564 #define pthread_cond_broadcast use_ast_cond_broadcast_instead_of_pthread_cond_broadcast
565 #define pthread_cond_wait use_ast_cond_wait_instead_of_pthread_cond_wait
566 #define pthread_cond_timedwait use_ast_cond_timedwait_instead_of_pthread_cond_timedwait
568 #define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__
570 #define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
573 #define pthread_create __use_ast_pthread_create_instead__
590 #if defined(HAVE_OSX_ATOMICS)
591 #include "libkern/OSAtomic.h"
599 #if defined(HAVE_GCC_ATOMICS)
602 return __sync_fetch_and_add(p, v);
604 #elif defined(HAVE_OSX_ATOMICS) && (
SIZEOF_INT == 4)
607 return OSAtomicAdd32(v, (int32_t *) p) - v;
609 #elif defined(HAVE_OSX_ATOMICS) && (
SIZEOF_INT == 8)
612 return OSAtomicAdd64(v, (int64_t *) p) - v;
613 #elif defined (__i386__) || defined(__x86_64__)
618 " lock; xaddl %0, %1 ; "
628 " lock xaddl %0, %1 ; "
648 return __sync_sub_and_fetch(p, 1) == 0;
650 #elif defined(HAVE_OSX_ATOMICS) && (
SIZEOF_INT == 4)
653 return OSAtomicAdd32( -1, (int32_t *) p) == 0;
655 #elif defined(HAVE_OSX_ATOMICS) && (
SIZEOF_INT == 8)
658 return OSAtomicAdd64( -1, (int64_t *) p) == 0;
int __ast_cond_init(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond, pthread_condattr_t *cond_attr)
#define pthread_mutex_init
pthread_t thread[AST_MAX_REENTRANCY]
Main Channel structure associated with a channel.
int __ast_rwlock_timedrdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name, const struct timespec *abs_timeout)
int __ast_rwlock_trywrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
const char * file[AST_MAX_REENTRANCY]
int __ast_pthread_mutex_destroy(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
Time-related functions and macros.
int __ast_rwlock_rdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
int __ast_rwlock_timedwrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name, const struct timespec *abs_timeout)
int __ast_pthread_mutex_init(int tracking, const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
int __ast_rwlock_unlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
int lineno[AST_MAX_REENTRANCY]
struct ast_bt backtrace[AST_MAX_REENTRANCY]
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return * the previous value of *p. This can be used to handle reference co...
struct ast_lock_track * track
pthread_cond_t ast_cond_t
Compiler-specific macros and other items.
#define pthread_mutex_destroy
int __ast_cond_timedwait(const char *filename, int lineno, const char *func, const char *cond_name, const char *mutex_name, ast_cond_t *cond, ast_mutex_t *t, const struct timespec *abstime)
int __ast_cond_wait(const char *filename, int lineno, const char *func, const char *cond_name, const char *mutex_name, ast_cond_t *cond, ast_mutex_t *t)
#define pthread_mutex_lock
Inlinable API function macro.
int ast_atomic_dec_and_test(volatile int *p)
decrement *p by 1 and return true if the variable has reached 0. Useful e.g. to check if a refcount h...
int __ast_pthread_mutex_unlock(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
int __ast_cond_broadcast(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond)
const char * func[AST_MAX_REENTRANCY]
int __ast_cond_destroy(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond)
int __ast_rwlock_destroy(const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
int __ast_rwlock_wrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
#define AST_MAX_REENTRANCY
Support for logging to various files, console and syslog Configuration in file logger.conf.
Structure for rwlock and tracking information.
int __ast_pthread_mutex_lock(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
int __ast_rwlock_init(int tracking, const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
int __ast_pthread_mutex_trylock(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
pthread_mutex_t reentr_mutex
int __ast_cond_signal(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond)
#define pthread_mutex_unlock
int __ast_rwlock_tryrdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
static enum AST_LOCK_TYPE ast_lock_type
Structure for mutex and tracking information.
struct ast_lock_track * track
#define AST_INLINE_API(hdr, body)