2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2010, 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 Asterisk locking-related definitions:
21 * - ast_mutext_t, ast_rwlock_t and related functions;
22 * - atomic arithmetic instructions;
23 * - wrappers for channel locking.
28 /*! \page LockDef Asterisk thread locking models
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.
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.
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.
48 #ifndef _ASTERISK_LOCK_H
49 #define _ASTERISK_LOCK_H
53 #include <sys/param.h>
58 #ifndef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
59 #include "asterisk/time.h"
62 #include "asterisk/backtrace.h"
63 #include "asterisk/logger.h"
64 #include "asterisk/compiler.h"
66 #define AST_PTHREADT_NULL (pthread_t) -1
67 #define AST_PTHREADT_STOP (pthread_t) -2
69 #if (defined(SOLARIS) || defined(BSD))
70 #define AST_MUTEX_INIT_W_CONSTRUCTORS
71 #endif /* SOLARIS || BSD */
73 /* Asterisk REQUIRES recursive (not error checking) mutexes
74 and will not run without them. */
75 #if defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) && defined(HAVE_PTHREAD_MUTEX_RECURSIVE_NP)
76 #define PTHREAD_MUTEX_INIT_VALUE PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
77 #define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE_NP
79 #define PTHREAD_MUTEX_INIT_VALUE PTHREAD_MUTEX_INITIALIZER
80 #define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE
81 #endif /* PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP */
83 #ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
84 #define __AST_RWLOCK_INIT_VALUE PTHREAD_RWLOCK_INITIALIZER
85 #else /* HAVE_PTHREAD_RWLOCK_INITIALIZER */
86 #define __AST_RWLOCK_INIT_VALUE {0}
87 #endif /* HAVE_PTHREAD_RWLOCK_INITIALIZER */
90 #define AST_LOCK_TRACK_INIT_VALUE { { NULL }, { 0 }, 0, { NULL }, { 0 }, {{{ 0 }}}, PTHREAD_MUTEX_INIT_VALUE }
92 #define AST_LOCK_TRACK_INIT_VALUE { { NULL }, { 0 }, 0, { NULL }, { 0 }, PTHREAD_MUTEX_INIT_VALUE }
95 #define AST_MUTEX_INIT_VALUE { PTHREAD_MUTEX_INIT_VALUE, NULL, 1 }
96 #define AST_MUTEX_INIT_VALUE_NOTRACKING { PTHREAD_MUTEX_INIT_VALUE, NULL, 0 }
98 #define AST_RWLOCK_INIT_VALUE { __AST_RWLOCK_INIT_VALUE, NULL, 1 }
99 #define AST_RWLOCK_INIT_VALUE_NOTRACKING { __AST_RWLOCK_INIT_VALUE, NULL, 0 }
101 #define AST_MAX_REENTRANCY 10
105 struct ast_lock_track {
106 const char *file[AST_MAX_REENTRANCY];
107 int lineno[AST_MAX_REENTRANCY];
109 const char *func[AST_MAX_REENTRANCY];
110 pthread_t thread[AST_MAX_REENTRANCY];
112 struct ast_bt backtrace[AST_MAX_REENTRANCY];
114 pthread_mutex_t reentr_mutex;
117 /*! \brief Structure for mutex and tracking information.
119 * We have tracking information in this structure regardless of DEBUG_THREADS being enabled.
120 * The information will just be ignored in the core if a module does not request it..
122 struct ast_mutex_info {
123 pthread_mutex_t mutex;
124 /*! Track which thread holds this mutex */
125 struct ast_lock_track *track;
126 unsigned int tracking:1;
129 /*! \brief Structure for rwlock and tracking information.
131 * We have tracking information in this structure regardless of DEBUG_THREADS being enabled.
132 * The information will just be ignored in the core if a module does not request it..
134 struct ast_rwlock_info {
135 pthread_rwlock_t lock;
136 /*! Track which thread holds this lock */
137 struct ast_lock_track *track;
138 unsigned int tracking:1;
141 typedef struct ast_mutex_info ast_mutex_t;
143 typedef struct ast_rwlock_info ast_rwlock_t;
145 typedef pthread_cond_t ast_cond_t;
147 int __ast_pthread_mutex_init(int tracking, const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t);
148 int __ast_pthread_mutex_destroy(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t);
149 int __ast_pthread_mutex_lock(const char *filename, int lineno, const char *func, const char* mutex_name, ast_mutex_t *t);
150 int __ast_pthread_mutex_trylock(const char *filename, int lineno, const char *func, const char* mutex_name, ast_mutex_t *t);
151 int __ast_pthread_mutex_unlock(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t);
153 #define ast_mutex_init(pmutex) __ast_pthread_mutex_init(1, __FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex)
154 #define ast_mutex_init_notracking(pmutex) __ast_pthread_mutex_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex)
155 #define ast_mutex_destroy(a) __ast_pthread_mutex_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
156 #define ast_mutex_lock(a) __ast_pthread_mutex_lock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
157 #define ast_mutex_unlock(a) __ast_pthread_mutex_unlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
158 #define ast_mutex_trylock(a) __ast_pthread_mutex_trylock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
161 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);
162 int __ast_cond_signal(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond);
163 int __ast_cond_broadcast(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond);
164 int __ast_cond_destroy(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond);
165 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);
166 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);
168 #define ast_cond_init(cond, attr) __ast_cond_init(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond, attr)
169 #define ast_cond_destroy(cond) __ast_cond_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
170 #define ast_cond_signal(cond) __ast_cond_signal(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
171 #define ast_cond_broadcast(cond) __ast_cond_broadcast(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
172 #define ast_cond_wait(cond, mutex) __ast_cond_wait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex)
173 #define ast_cond_timedwait(cond, mutex, time) __ast_cond_timedwait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex, time)
176 int __ast_rwlock_init(int tracking, const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t);
177 int __ast_rwlock_destroy(const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t);
178 int __ast_rwlock_unlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
179 int __ast_rwlock_rdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
180 int __ast_rwlock_wrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
181 int __ast_rwlock_timedrdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name, const struct timespec *abs_timeout);
182 int __ast_rwlock_timedwrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name, const struct timespec *abs_timeout);
183 int __ast_rwlock_tryrdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
184 int __ast_rwlock_trywrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
187 * \brief wrapper for rwlock with tracking enabled
188 * \return 0 on success, non zero for error
191 #define ast_rwlock_init(rwlock) __ast_rwlock_init(1, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
194 * \brief wrapper for ast_rwlock_init with tracking disabled
195 * \return 0 on success, non zero for error
198 #define ast_rwlock_init_notracking(rwlock) __ast_rwlock_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
200 #define ast_rwlock_destroy(rwlock) __ast_rwlock_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
201 #define ast_rwlock_unlock(a) __ast_rwlock_unlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
202 #define ast_rwlock_rdlock(a) __ast_rwlock_rdlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
203 #define ast_rwlock_wrlock(a) __ast_rwlock_wrlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
204 #define ast_rwlock_tryrdlock(a) __ast_rwlock_tryrdlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
205 #define ast_rwlock_trywrlock(a) __ast_rwlock_trywrlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
206 #define ast_rwlock_timedrdlock(a, b) __ast_rwlock_timedrdlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a, b)
207 #define ast_rwlock_timedwrlock(a, b) __ast_rwlock_timedwrlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a, b)
209 #define ROFFSET ((lt->reentrancy > 0) ? (lt->reentrancy-1) : 0)
213 #define __ast_mutex_logger(...) do { if (canlog) ast_log(LOG_ERROR, __VA_ARGS__); else fprintf(stderr, __VA_ARGS__); } while (0)
216 #define DO_THREAD_CRASH do { *((int *)(0)) = 1; } while(0)
218 #define DO_THREAD_CRASH do { } while (0)
230 * \brief Store lock info for the current thread
232 * This function gets called in ast_mutex_lock() and ast_mutex_trylock() so
233 * that information about this lock can be stored in this thread's
234 * lock info struct. The lock is marked as pending as the thread is waiting
235 * on the lock. ast_mark_lock_acquired() will mark it as held by this thread.
237 #if !defined(LOW_MEMORY)
239 void ast_store_lock_info(enum ast_lock_type type, const char *filename,
240 int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt);
242 void ast_store_lock_info(enum ast_lock_type type, const char *filename,
243 int line_num, const char *func, const char *lock_name, void *lock_addr);
244 #endif /* HAVE_BKTR */
249 #define ast_store_lock_info(I,DONT,CARE,ABOUT,THE,PARAMETERS,BUD)
251 #define ast_store_lock_info(I,DONT,CARE,ABOUT,THE,PARAMETERS)
252 #endif /* HAVE_BKTR */
253 #endif /* !defined(LOW_MEMORY) */
256 * \brief Mark the last lock as acquired
258 #if !defined(LOW_MEMORY)
259 void ast_mark_lock_acquired(void *lock_addr);
261 #define ast_mark_lock_acquired(ignore)
265 * \brief Mark the last lock as failed (trylock)
267 #if !defined(LOW_MEMORY)
268 void ast_mark_lock_failed(void *lock_addr);
270 #define ast_mark_lock_failed(ignore)
274 * \brief remove lock info for the current thread
276 * this gets called by ast_mutex_unlock so that information on the lock can
277 * be removed from the current thread's lock info struct.
279 #if !defined(LOW_MEMORY)
281 void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt);
283 void ast_remove_lock_info(void *lock_addr);
284 #endif /* HAVE_BKTR */
287 #define ast_remove_lock_info(ignore,me)
289 #define ast_remove_lock_info(ignore)
290 #endif /* HAVE_BKTR */
291 #endif /* !defined(LOW_MEMORY) */
294 static inline void __dump_backtrace(struct ast_bt *bt, int canlog)
300 strings = backtrace_symbols(bt->addresses, bt->num_frames);
302 for (i = 0; i < bt->num_frames; i++)
303 __ast_mutex_logger("%s\n", strings[i]);
310 * \brief log info for the current lock with ast_log().
312 * this function would be mostly for debug. If you come across a lock
313 * that is unexpectedly but momentarily locked, and you wonder who
314 * are fighting with for the lock, this routine could be called, IF
315 * you have the thread debugging stuff turned on.
316 * \param this_lock_addr lock address to return lock information
319 void log_show_lock(void *this_lock_addr);
322 * \brief retrieve lock info for the specified mutex
324 * this gets called during deadlock avoidance, so that the information may
325 * be preserved as to what location originally acquired the lock.
327 #if !defined(LOW_MEMORY)
328 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);
330 #define ast_find_lock_info(a,b,c,d,e,f,g,h) -1
334 * \brief Unlock a lock briefly
336 * used during deadlock avoidance, to preserve the original location where
337 * a lock was originally acquired.
339 #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
341 char __filename[80], __func[80], __mutex_name[80]; \
343 int __res = ast_find_lock_info(ao2_object_get_lockaddr(chan), __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
344 int __res2 = ast_channel_unlock(chan); \
346 if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
348 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)); \
350 ast_channel_lock(chan); \
354 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); \
356 __ao2_lock(chan, AO2_LOCK_REQ_MUTEX, __filename, __func, __lineno, __mutex_name); \
361 #define DEADLOCK_AVOIDANCE(lock) \
363 char __filename[80], __func[80], __mutex_name[80]; \
365 int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
366 int __res2 = ast_mutex_unlock(lock); \
368 if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
370 ast_mutex_lock(lock); \
372 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)); \
376 __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
378 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); \
384 * \brief Deadlock avoidance unlock
386 * In certain deadlock avoidance scenarios, there is more than one lock to be
387 * unlocked and relocked. Therefore, this pair of macros is provided for that
388 * purpose. Note that every DLA_UNLOCK _MUST_ be paired with a matching
389 * DLA_LOCK. The intent of this pair of macros is to be used around another
390 * set of deadlock avoidance code, mainly CHANNEL_DEADLOCK_AVOIDANCE, as the
391 * locking order specifies that we may safely lock a channel, followed by its
392 * pvt, with no worries about a deadlock. In any other scenario, this macro
393 * may not be safe to use.
395 #define DLA_UNLOCK(lock) \
397 char __filename[80], __func[80], __mutex_name[80]; \
399 int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
400 int __res2 = ast_mutex_unlock(lock);
403 * \brief Deadlock avoidance lock
405 * In certain deadlock avoidance scenarios, there is more than one lock to be
406 * unlocked and relocked. Therefore, this pair of macros is provided for that
407 * purpose. Note that every DLA_UNLOCK _MUST_ be paired with a matching
408 * DLA_LOCK. The intent of this pair of macros is to be used around another
409 * set of deadlock avoidance code, mainly CHANNEL_DEADLOCK_AVOIDANCE, as the
410 * locking order specifies that we may safely lock a channel, followed by its
411 * pvt, with no worries about a deadlock. In any other scenario, this macro
412 * may not be safe to use.
414 #define DLA_LOCK(lock) \
415 if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
417 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)); \
419 ast_mutex_lock(lock); \
423 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); \
425 __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
430 static inline void ast_reentrancy_lock(struct ast_lock_track *lt)
433 if ((res = pthread_mutex_lock(<->reentr_mutex))) {
434 fprintf(stderr, "ast_reentrancy_lock failed: '%s' (%d)\n", strerror(res), res);
435 #if defined(DO_CRASH) || defined(THREAD_CRASH)
441 static inline void ast_reentrancy_unlock(struct ast_lock_track *lt)
444 if ((res = pthread_mutex_unlock(<->reentr_mutex))) {
445 fprintf(stderr, "ast_reentrancy_unlock failed: '%s' (%d)\n", strerror(res), res);
446 #if defined(DO_CRASH) || defined(THREAD_CRASH)
452 static inline void ast_reentrancy_init(struct ast_lock_track **plt)
455 pthread_mutexattr_t reentr_attr;
456 struct ast_lock_track *lt = *plt;
459 lt = *plt = (struct ast_lock_track *) calloc(1, sizeof(*lt));
462 for (i = 0; i < AST_MAX_REENTRANCY; i++) {
468 memset(<->backtrace[i], 0, sizeof(lt->backtrace[i]));
474 pthread_mutexattr_init(&reentr_attr);
475 pthread_mutexattr_settype(&reentr_attr, AST_MUTEX_KIND);
476 pthread_mutex_init(<->reentr_mutex, &reentr_attr);
477 pthread_mutexattr_destroy(&reentr_attr);
480 static inline void delete_reentrancy_cs(struct ast_lock_track **plt)
482 struct ast_lock_track *lt;
485 pthread_mutex_destroy(<->reentr_mutex);
491 #else /* !DEBUG_THREADS */
493 #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
494 ast_channel_unlock(chan); \
496 ast_channel_lock(chan);
498 #define DEADLOCK_AVOIDANCE(lock) \
501 if (!(__res = ast_mutex_unlock(lock))) { \
503 ast_mutex_lock(lock); \
505 ast_log(LOG_WARNING, "Failed to unlock mutex '%s' (%s). I will NOT try to relock. {{{ THIS IS A BUG. }}}\n", #lock, strerror(__res)); \
509 #define DLA_UNLOCK(lock) ast_mutex_unlock(lock)
511 #define DLA_LOCK(lock) ast_mutex_lock(lock)
513 #endif /* !DEBUG_THREADS */
515 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
517 * If AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope constructors
518 * and destructors to create/destroy global mutexes.
520 #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track) \
521 scope ast_mutex_t mutex = init_val; \
522 static void __attribute__((constructor)) init_##mutex(void) \
525 ast_mutex_init(&mutex); \
527 ast_mutex_init_notracking(&mutex); \
530 static void __attribute__((destructor)) fini_##mutex(void) \
532 ast_mutex_destroy(&mutex); \
534 #else /* !AST_MUTEX_INIT_W_CONSTRUCTORS */
535 /* By default, use static initialization of mutexes. */
536 #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track) scope ast_mutex_t mutex = init_val
537 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
539 #define AST_MUTEX_DEFINE_STATIC(mutex) __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE, 1)
540 #define AST_MUTEX_DEFINE_STATIC_NOTRACKING(mutex) __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE_NOTRACKING, 0)
543 /* Statically declared read/write locks */
544 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
545 #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) \
546 scope ast_rwlock_t rwlock = init_val; \
547 static void __attribute__((constructor)) init_##rwlock(void) \
550 ast_rwlock_init(&rwlock); \
552 ast_rwlock_init_notracking(&rwlock); \
554 static void __attribute__((destructor)) fini_##rwlock(void) \
556 ast_rwlock_destroy(&rwlock); \
559 #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) scope ast_rwlock_t rwlock = init_val
562 #define AST_RWLOCK_DEFINE_STATIC(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE, 1)
563 #define AST_RWLOCK_DEFINE_STATIC_NOTRACKING(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE_NOTRACKING, 0)
566 * \brief Scoped Locks
568 * Scoped locks provide a way to use RAII locks. In other words,
569 * declaration of a scoped lock will automatically define and lock
570 * the lock. When the lock goes out of scope, it will automatically
574 * int some_function(struct ast_channel *chan)
576 * SCOPED_LOCK(lock, chan, ast_channel_lock, ast_channel_unlock);
578 * if (!strcmp(ast_channel_name(chan, "foo")) {
586 * In the above example, neither return path requires explicit unlocking
590 * Care should be taken when using SCOPED_LOCKS in conjunction with ao2 objects.
591 * ao2 objects should be unlocked before they are unreffed. Since SCOPED_LOCK runs
592 * once the variable goes out of scope, this can easily lead to situations where the
593 * variable gets unlocked after it is unreffed.
595 * \param varname The unique name to give to the scoped lock. You are not likely to reference
596 * this outside of the SCOPED_LOCK invocation.
597 * \param lock The variable to lock. This can be anything that can be passed to a locking
598 * or unlocking function.
599 * \param lockfunc The function to call to lock the lock
600 * \param unlockfunc The function to call to unlock the lock
602 #define SCOPED_LOCK(varname, lock, lockfunc, unlockfunc) \
603 RAII_VAR(typeof((lock)), varname, ({lockfunc((lock)); (lock); }), unlockfunc)
606 * \brief scoped lock specialization for mutexes
608 #define SCOPED_MUTEX(varname, lock) SCOPED_LOCK(varname, (lock), ast_mutex_lock, ast_mutex_unlock)
611 * \brief scoped lock specialization for read locks
613 #define SCOPED_RDLOCK(varname, lock) SCOPED_LOCK(varname, (lock), ast_rwlock_rdlock, ast_rwlock_unlock)
616 * \brief scoped lock specialization for write locks
618 #define SCOPED_WRLOCK(varname, lock) SCOPED_LOCK(varname, (lock), ast_rwlock_wrlock, ast_rwlock_unlock)
621 * \brief scoped lock specialization for ao2 mutexes.
623 #define SCOPED_AO2LOCK(varname, obj) SCOPED_LOCK(varname, (obj), ao2_lock, ao2_unlock)
626 * \brief scoped lock specialization for ao2 read locks.
628 #define SCOPED_AO2RDLOCK(varname, obj) SCOPED_LOCK(varname, (obj), ao2_rdlock, ao2_unlock)
631 * \brief scoped lock specialization for ao2 write locks.
633 #define SCOPED_AO2WRLOCK(varname, obj) SCOPED_LOCK(varname, (obj), ao2_wrlock, ao2_unlock)
636 * \brief scoped lock specialization for channels.
638 #define SCOPED_CHANNELLOCK(varname, chan) SCOPED_LOCK(varname, (chan), ast_channel_lock, ast_channel_unlock)
640 #ifndef __CYGWIN__ /* temporary disabled for cygwin */
641 #define pthread_mutex_t use_ast_mutex_t_instead_of_pthread_mutex_t
642 #define pthread_cond_t use_ast_cond_t_instead_of_pthread_cond_t
644 #define pthread_mutex_lock use_ast_mutex_lock_instead_of_pthread_mutex_lock
645 #define pthread_mutex_unlock use_ast_mutex_unlock_instead_of_pthread_mutex_unlock
646 #define pthread_mutex_trylock use_ast_mutex_trylock_instead_of_pthread_mutex_trylock
647 #define pthread_mutex_init use_ast_mutex_init_instead_of_pthread_mutex_init
648 #define pthread_mutex_destroy use_ast_mutex_destroy_instead_of_pthread_mutex_destroy
649 #define pthread_cond_init use_ast_cond_init_instead_of_pthread_cond_init
650 #define pthread_cond_destroy use_ast_cond_destroy_instead_of_pthread_cond_destroy
651 #define pthread_cond_signal use_ast_cond_signal_instead_of_pthread_cond_signal
652 #define pthread_cond_broadcast use_ast_cond_broadcast_instead_of_pthread_cond_broadcast
653 #define pthread_cond_wait use_ast_cond_wait_instead_of_pthread_cond_wait
654 #define pthread_cond_timedwait use_ast_cond_timedwait_instead_of_pthread_cond_timedwait
656 #define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__
658 #define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
661 #define pthread_create __use_ast_pthread_create_instead__
665 * Support for atomic instructions.
666 * For platforms that have it, use the native cpu instruction to
667 * implement them. For other platforms, resort to a 'slow' version
668 * (defined in utils.c) that protects the atomic instruction with
670 * The slow versions is always available, for testing purposes,
671 * as ast_atomic_fetchadd_int_slow()
674 int ast_atomic_fetchadd_int_slow(volatile int *p, int v);
676 #include "asterisk/inline_api.h"
678 #if defined(HAVE_OSX_ATOMICS)
679 #include "libkern/OSAtomic.h"
682 /*! \brief Atomically add v to *p and return * the previous value of *p.
683 * This can be used to handle reference counts, and the return value
684 * can be used to generate unique identifiers.
687 #if defined(HAVE_GCC_ATOMICS)
688 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
690 return __sync_fetch_and_add(p, v);
692 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
693 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
695 return OSAtomicAdd32(v, (int32_t *) p) - v;
697 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
698 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
700 return OSAtomicAdd64(v, (int64_t *) p) - v;
701 #elif defined (__i386__) || defined(__x86_64__)
703 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
706 " lock; xaddl %0, %1 ; "
707 : "+r" (v), /* 0 (result) */
712 #else /* ifndef sun */
713 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
716 " lock xaddl %0, %1 ; "
717 : "+r" (v), /* 0 (result) */
723 #else /* low performance version in utils.c */
724 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
726 return ast_atomic_fetchadd_int_slow(p, v);
730 /*! \brief decrement *p by 1 and return true if the variable has reached 0.
731 * Useful e.g. to check if a refcount has reached 0.
733 #if defined(HAVE_GCC_ATOMICS)
734 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
736 return __sync_sub_and_fetch(p, 1) == 0;
738 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
739 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
741 return OSAtomicAdd32( -1, (int32_t *) p) == 0;
743 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
744 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
746 return OSAtomicAdd64( -1, (int64_t *) p) == 0;
748 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
750 int a = ast_atomic_fetchadd_int(p, -1);
751 return a == 1; /* true if the value is 0 now (so it was 1 previously) */
755 #endif /* _ASTERISK_LOCK_H */