Asterisk - The Open Source Telephony Project GIT-master-c7a8271
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
lock.h
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2010, 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_mutex_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 <time.h>
53#include <sys/param.h>
54#ifdef HAVE_BKTR
55#include <execinfo.h>
56#endif
57#ifdef DEBUG_THREADS
58#include <string.h>
59#endif
60
61#ifndef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
62#include "asterisk/time.h"
63#endif
64
65#include "asterisk/backtrace.h"
66#include "asterisk/logger.h"
67#include "asterisk/compiler.h"
68
69#if defined(__cplusplus) || defined(c_plusplus)
70extern "C" {
71#endif
72
73#define AST_PTHREADT_NULL (pthread_t) -1
74#define AST_PTHREADT_STOP (pthread_t) -2
75
76#if (defined(SOLARIS) || defined(BSD))
77#define AST_MUTEX_INIT_W_CONSTRUCTORS
78#endif /* SOLARIS || BSD */
79
80/* Asterisk REQUIRES recursive (not error checking) mutexes
81 and will not run without them. */
82#if defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) && defined(HAVE_PTHREAD_MUTEX_RECURSIVE_NP)
83#define PTHREAD_MUTEX_INIT_VALUE PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
84#define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE_NP
85#else
86#define PTHREAD_MUTEX_INIT_VALUE PTHREAD_MUTEX_INITIALIZER
87#define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE
88#endif /* PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP */
89
90#ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
91#define __AST_RWLOCK_INIT_VALUE PTHREAD_RWLOCK_INITIALIZER
92#else /* HAVE_PTHREAD_RWLOCK_INITIALIZER */
93#define __AST_RWLOCK_INIT_VALUE {0}
94#endif /* HAVE_PTHREAD_RWLOCK_INITIALIZER */
95
96#ifdef HAVE_BKTR
97#define AST_LOCK_TRACK_INIT_VALUE { { NULL }, { 0 }, 0, { NULL }, { 0 }, {{{ 0 }}}, PTHREAD_MUTEX_INIT_VALUE }
98#else
99#define AST_LOCK_TRACK_INIT_VALUE { { NULL }, { 0 }, 0, { NULL }, { 0 }, PTHREAD_MUTEX_INIT_VALUE }
100#endif
101
102#define AST_MUTEX_INIT_VALUE { PTHREAD_MUTEX_INIT_VALUE, NULL, {1, 0} }
103#define AST_MUTEX_INIT_VALUE_NOTRACKING { PTHREAD_MUTEX_INIT_VALUE, NULL, {0, 0} }
104
105#define AST_RWLOCK_INIT_VALUE { __AST_RWLOCK_INIT_VALUE, NULL, {1, 0} }
106#define AST_RWLOCK_INIT_VALUE_NOTRACKING { __AST_RWLOCK_INIT_VALUE, NULL, {0, 0} }
107
108#define AST_MAX_REENTRANCY 10
109
110struct ast_channel;
111
112/*!
113 * \brief Lock tracking information.
114 *
115 * \note Any changes to this struct MUST be reflected in the
116 * lock.c:restore_lock_tracking() function.
117 */
124#ifdef HAVE_BKTR
126#endif
128};
129
131 /*! non-zero if lock tracking is enabled */
132 unsigned int tracking:1;
133 /*! non-zero if track is setup */
134 volatile unsigned int setup:1;
135};
136
137/*! \brief Structure for mutex and tracking information.
138 *
139 * We have tracking information in this structure regardless of DEBUG_THREADS being enabled.
140 * The information will just be ignored in the core if a module does not request it..
141 */
144#if !defined(DEBUG_THREADS) && !defined(DEBUG_THREADS_LOOSE_ABI) && \
145 !defined(DETECT_DEADLOCKS)
146 /*!
147 * These fields are renamed to ensure they are never used when
148 * DEBUG_THREADS is not defined.
149 */
152#elif defined(DEBUG_THREADS) || defined(DETECT_DEADLOCKS)
153 /*! Track which thread holds this mutex. */
154 struct ast_lock_track *track;
155 struct ast_lock_track_flags flags;
156#endif
157};
158
159/*! \brief Structure for rwlock and tracking information.
160 *
161 * We have tracking information in this structure regardless of DEBUG_THREADS being enabled.
162 * The information will just be ignored in the core if a module does not request it..
163 */
165 pthread_rwlock_t lock;
166#if !defined(DEBUG_THREADS) && !defined(DEBUG_THREADS_LOOSE_ABI) && \
167 !defined(DETECT_DEADLOCKS)
168 /*!
169 * These fields are renamed to ensure they are never used when
170 * DEBUG_THREADS is not defined.
171 */
174#elif defined(DEBUG_THREADS) || defined(DETECT_DEADLOCKS)
175 /*! Track which thread holds this lock */
176 struct ast_lock_track *track;
177 struct ast_lock_track_flags flags;
178#endif
179};
180
182
184
186
187int __ast_pthread_mutex_init(int tracking, const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t);
188int __ast_pthread_mutex_destroy(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t);
189int __ast_pthread_mutex_lock(const char *filename, int lineno, const char *func, const char* mutex_name, ast_mutex_t *t);
190int __ast_pthread_mutex_trylock(const char *filename, int lineno, const char *func, const char* mutex_name, ast_mutex_t *t);
191int __ast_pthread_mutex_unlock(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t);
192
193#define ast_mutex_init(pmutex) __ast_pthread_mutex_init(1, __FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex)
194#define ast_mutex_init_notracking(pmutex) __ast_pthread_mutex_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex)
195#define ast_mutex_destroy(a) __ast_pthread_mutex_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
196#define ast_mutex_lock(a) __ast_pthread_mutex_lock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
197#define ast_mutex_unlock(a) __ast_pthread_mutex_unlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
198#define ast_mutex_trylock(a) __ast_pthread_mutex_trylock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
199
200
201int __ast_cond_init(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond, pthread_condattr_t *cond_attr);
202int __ast_cond_signal(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond);
203int __ast_cond_broadcast(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond);
204int __ast_cond_destroy(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond);
205int __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);
206int __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);
207
208#define ast_cond_init(cond, attr) __ast_cond_init(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond, attr)
209#define ast_cond_destroy(cond) __ast_cond_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
210#define ast_cond_signal(cond) __ast_cond_signal(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
211#define ast_cond_broadcast(cond) __ast_cond_broadcast(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
212#define ast_cond_wait(cond, mutex) __ast_cond_wait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex)
213#define ast_cond_timedwait(cond, mutex, time) __ast_cond_timedwait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex, time)
214
215
216int __ast_rwlock_init(int tracking, const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t);
217int __ast_rwlock_destroy(const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t);
218int __ast_rwlock_unlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
219int __ast_rwlock_rdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
220int __ast_rwlock_wrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
221int __ast_rwlock_timedrdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name, const struct timespec *abs_timeout);
222int __ast_rwlock_timedwrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name, const struct timespec *abs_timeout);
223int __ast_rwlock_tryrdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
224int __ast_rwlock_trywrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
225
226/*!
227 * \brief wrapper for rwlock with tracking enabled
228 * \return 0 on success, non zero for error
229 * \since 1.6.1
230 */
231#define ast_rwlock_init(rwlock) __ast_rwlock_init(1, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
232
233/*!
234 * \brief wrapper for ast_rwlock_init with tracking disabled
235 * \return 0 on success, non zero for error
236 * \since 1.6.1
237 */
238#define ast_rwlock_init_notracking(rwlock) __ast_rwlock_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
239
240#define ast_rwlock_destroy(rwlock) __ast_rwlock_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
241#define ast_rwlock_unlock(a) __ast_rwlock_unlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
242#define ast_rwlock_rdlock(a) __ast_rwlock_rdlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
243#define ast_rwlock_wrlock(a) __ast_rwlock_wrlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
244#define ast_rwlock_tryrdlock(a) __ast_rwlock_tryrdlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
245#define ast_rwlock_trywrlock(a) __ast_rwlock_trywrlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
246#define ast_rwlock_timedrdlock(a, b) __ast_rwlock_timedrdlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a, b)
247#define ast_rwlock_timedwrlock(a, b) __ast_rwlock_timedwrlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a, b)
248
249#define ROFFSET ((lt->reentrancy > 0) ? (lt->reentrancy-1) : 0)
250
251#ifdef DEBUG_THREADS
252
253#ifdef THREAD_CRASH
254#define DO_THREAD_CRASH do { *((int *)(0)) = 1; } while(0)
255#else
256#define DO_THREAD_CRASH do { } while (0)
257#endif
258
259#include <errno.h>
260
261enum ast_lock_type {
262 AST_MUTEX,
265};
266
267/*!
268 * \brief Store lock info for the current thread
269 *
270 * This function gets called in ast_mutex_lock() and ast_mutex_trylock() so
271 * that information about this lock can be stored in this thread's
272 * lock info struct. The lock is marked as pending as the thread is waiting
273 * on the lock. ast_mark_lock_acquired() will mark it as held by this thread.
274 */
275void ast_store_lock_info(enum ast_lock_type type, const char *filename,
276 int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt);
277
278/*!
279 * \brief Mark the last lock as acquired
280 */
281void ast_mark_lock_acquired(void *lock_addr);
282
283/*!
284 * \brief Mark the last lock as failed (trylock)
285 */
286void ast_mark_lock_failed(void *lock_addr);
287
288/*!
289 * \brief remove lock info for the current thread
290 *
291 * this gets called by ast_mutex_unlock so that information on the lock can
292 * be removed from the current thread's lock info struct.
293 */
294void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt);
295void ast_suspend_lock_info(void *lock_addr);
296void ast_restore_lock_info(void *lock_addr);
297
298/*!
299 * \brief log info for the current lock with ast_log().
300 *
301 * this function would be mostly for debug. If you come across a lock
302 * that is unexpectedly but momentarily locked, and you wonder who
303 * are fighting with for the lock, this routine could be called, IF
304 * you have the thread debugging stuff turned on.
305 * \param this_lock_addr lock address to return lock information
306 * \since 1.6.1
307 */
308void ast_log_show_lock(void *this_lock_addr);
309
310/*!
311 * \brief Generate a lock dump equivalent to "core show locks".
312 *
313 * The lock dump generated is generally too large to be output by a
314 * single ast_verbose/log/debug/etc. call. Only ast_cli() handles it
315 * properly without changing BUFSIZ in logger.c.
316 *
317 * Note: This must be ast_free()d when you're done with it.
318 *
319 * \retval An ast_str containing the lock dump
320 * \retval NULL on error
321 * \since 12
322 */
323struct ast_str *ast_dump_locks(void);
324
325/*!
326 * \brief retrieve lock info for the specified mutex
327 *
328 * this gets called during deadlock avoidance, so that the information may
329 * be preserved as to what location originally acquired the lock.
330 */
331int 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);
332
333/*!
334 * \brief Unlock a lock briefly
335 *
336 * used during deadlock avoidance, to preserve the original location where
337 * a lock was originally acquired.
338 */
339#define AO2_DEADLOCK_AVOIDANCE(obj) \
340 do { \
341 char __filename[80], __func[80], __mutex_name[80]; \
342 int __lineno; \
343 int __res = ast_find_lock_info(ao2_object_get_lockaddr(obj), __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
344 int __res2 = ao2_unlock(obj); \
345 usleep(1); \
346 if (__res < 0) { /* Could happen if the ao2 object does not have a mutex. */ \
347 if (__res2) { \
348 ast_log(LOG_WARNING, "Could not unlock ao2 object '%s': %s and no lock info found! I will NOT try to relock.\n", #obj, strerror(__res2)); \
349 } else { \
350 ao2_lock(obj); \
351 } \
352 } else { \
353 if (__res2) { \
354 ast_log(LOG_WARNING, "Could not unlock ao2 object '%s': %s. {{{Originally locked at %s line %d: (%s) '%s'}}} I will NOT try to relock.\n", #obj, strerror(__res2), __filename, __lineno, __func, __mutex_name); \
355 } else { \
356 __ao2_lock(obj, AO2_LOCK_REQ_MUTEX, __filename, __func, __lineno, __mutex_name); \
357 } \
358 } \
359 } while (0)
360
361#define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
362 do { \
363 char __filename[80], __func[80], __mutex_name[80]; \
364 int __lineno; \
365 int __res = ast_find_lock_info(ao2_object_get_lockaddr(chan), __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
366 int __res2 = ast_channel_unlock(chan); \
367 usleep(1); \
368 if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
369 if (__res2) { \
370 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)); \
371 } else { \
372 ast_channel_lock(chan); \
373 } \
374 } else { \
375 if (__res2) { \
376 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); \
377 } else { \
378 __ao2_lock(chan, AO2_LOCK_REQ_MUTEX, __filename, __func, __lineno, __mutex_name); \
379 } \
380 } \
381 } while (0)
382
383#define DEADLOCK_AVOIDANCE(lock) \
384 do { \
385 char __filename[80], __func[80], __mutex_name[80]; \
386 int __lineno; \
387 int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
388 int __res2 = ast_mutex_unlock(lock); \
389 usleep(1); \
390 if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
391 if (__res2 == 0) { \
392 ast_mutex_lock(lock); \
393 } else { \
394 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)); \
395 } \
396 } else { \
397 if (__res2 == 0) { \
398 __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
399 } else { \
400 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); \
401 } \
402 } \
403 } while (0)
404
405/*!
406 * \brief Deadlock avoidance unlock
407 *
408 * In certain deadlock avoidance scenarios, there is more than one lock to be
409 * unlocked and relocked. Therefore, this pair of macros is provided for that
410 * purpose. Note that every DLA_UNLOCK _MUST_ be paired with a matching
411 * DLA_LOCK. The intent of this pair of macros is to be used around another
412 * set of deadlock avoidance code, mainly CHANNEL_DEADLOCK_AVOIDANCE, as the
413 * locking order specifies that we may safely lock a channel, followed by its
414 * pvt, with no worries about a deadlock. In any other scenario, this macro
415 * may not be safe to use.
416 */
417#define DLA_UNLOCK(lock) \
418 do { \
419 char __filename[80], __func[80], __mutex_name[80]; \
420 int __lineno; \
421 int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
422 int __res2 = ast_mutex_unlock(lock);
423
424/*!
425 * \brief Deadlock avoidance lock
426 *
427 * In certain deadlock avoidance scenarios, there is more than one lock to be
428 * unlocked and relocked. Therefore, this pair of macros is provided for that
429 * purpose. Note that every DLA_UNLOCK _MUST_ be paired with a matching
430 * DLA_LOCK. The intent of this pair of macros is to be used around another
431 * set of deadlock avoidance code, mainly CHANNEL_DEADLOCK_AVOIDANCE, as the
432 * locking order specifies that we may safely lock a channel, followed by its
433 * pvt, with no worries about a deadlock. In any other scenario, this macro
434 * may not be safe to use.
435 */
436#define DLA_LOCK(lock) \
437 if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
438 if (__res2) { \
439 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)); \
440 } else { \
441 ast_mutex_lock(lock); \
442 } \
443 } else { \
444 if (__res2) { \
445 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); \
446 } else { \
447 __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
448 } \
449 } \
450 } while (0)
451
452static inline void ast_reentrancy_lock(struct ast_lock_track *lt)
453{
454 int res;
455 if ((res = pthread_mutex_lock(&lt->reentr_mutex))) {
456 fprintf(stderr, "ast_reentrancy_lock failed: '%s' (%d)\n", strerror(res), res);
457#if defined(DO_CRASH) || defined(THREAD_CRASH)
458 abort();
459#endif
460 }
461}
462
463static inline void ast_reentrancy_unlock(struct ast_lock_track *lt)
464{
465 int res;
466 if ((res = pthread_mutex_unlock(&lt->reentr_mutex))) {
467 fprintf(stderr, "ast_reentrancy_unlock failed: '%s' (%d)\n", strerror(res), res);
468#if defined(DO_CRASH) || defined(THREAD_CRASH)
469 abort();
470#endif
471 }
472}
473
474#else /* !DEBUG_THREADS */
475
476#define AO2_DEADLOCK_AVOIDANCE(obj) \
477 ao2_unlock(obj); \
478 usleep(1); \
479 ao2_lock(obj);
480
481#define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
482 ast_channel_unlock(chan); \
483 usleep(1); \
484 ast_channel_lock(chan);
485
486#define DEADLOCK_AVOIDANCE(lock) \
487 do { \
488 int __res; \
489 if (!(__res = ast_mutex_unlock(lock))) { \
490 usleep(1); \
491 ast_mutex_lock(lock); \
492 } else { \
493 ast_log(LOG_WARNING, "Failed to unlock mutex '%s' (%s). I will NOT try to relock. {{{ THIS IS A BUG. }}}\n", #lock, strerror(__res)); \
494 } \
495 } while (0)
496
497#define DLA_UNLOCK(lock) ast_mutex_unlock(lock)
498
499#define DLA_LOCK(lock) ast_mutex_lock(lock)
500
501#endif /* !DEBUG_THREADS */
502
503#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
504/*
505 * If AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope constructors
506 * and destructors to create/destroy global mutexes.
507 */
508#define __AST_MUTEX_DEFINE(scope, mutex, init_val, track) \
509 scope ast_mutex_t mutex = init_val; \
510static void __attribute__((constructor)) init_##mutex(void) \
511{ \
512 if (track) \
513 ast_mutex_init(&mutex); \
514 else \
515 ast_mutex_init_notracking(&mutex); \
516} \
517 \
518static void __attribute__((destructor)) fini_##mutex(void) \
519{ \
520 ast_mutex_destroy(&mutex); \
521}
522#else /* !AST_MUTEX_INIT_W_CONSTRUCTORS */
523/* By default, use static initialization of mutexes. */
524#define __AST_MUTEX_DEFINE(scope, mutex, init_val, track) scope ast_mutex_t mutex = init_val
525#endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
526
527#define AST_MUTEX_DEFINE_STATIC(mutex) __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE, 1)
528#define AST_MUTEX_DEFINE_STATIC_NOTRACKING(mutex) __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE_NOTRACKING, 0)
529
530
531/* Statically declared read/write locks */
532#ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
533#define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) \
534 scope ast_rwlock_t rwlock = init_val; \
535static void __attribute__((constructor)) init_##rwlock(void) \
536{ \
537 if (track) \
538 ast_rwlock_init(&rwlock); \
539 else \
540 ast_rwlock_init_notracking(&rwlock); \
541} \
542static void __attribute__((destructor)) fini_##rwlock(void) \
543{ \
544 ast_rwlock_destroy(&rwlock); \
545}
546#else
547#define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) scope ast_rwlock_t rwlock = init_val
548#endif
549
550#define AST_RWLOCK_DEFINE_STATIC(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE, 1)
551#define AST_RWLOCK_DEFINE_STATIC_NOTRACKING(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE_NOTRACKING, 0)
552
553/*!
554 * \brief Scoped Locks
555 *
556 * Scoped locks provide a way to use RAII locks. In other words,
557 * declaration of a scoped lock will automatically define and lock
558 * the lock. When the lock goes out of scope, it will automatically
559 * be unlocked.
560 *
561 * \code
562 * int some_function(struct ast_channel *chan)
563 * {
564 * SCOPED_LOCK(lock, chan, ast_channel_lock, ast_channel_unlock);
565 *
566 * if (!strcmp(ast_channel_name(chan, "foo")) {
567 * return 0;
568 * }
569 *
570 * return -1;
571 * }
572 * \endcode
573 *
574 * In the above example, neither return path requires explicit unlocking
575 * of the channel.
576 *
577 * \note
578 * Care should be taken when using SCOPED_LOCKS in conjunction with ao2 objects.
579 * ao2 objects should be unlocked before they are unreffed. Since SCOPED_LOCK runs
580 * once the variable goes out of scope, this can easily lead to situations where the
581 * variable gets unlocked after it is unreffed.
582 *
583 * \param varname The unique name to give to the scoped lock. You are not likely to reference
584 * this outside of the SCOPED_LOCK invocation.
585 * \param lock The variable to lock. This can be anything that can be passed to a locking
586 * or unlocking function.
587 * \param lockfunc The function to call to lock the lock
588 * \param unlockfunc The function to call to unlock the lock
589 */
590#define SCOPED_LOCK(varname, lock, lockfunc, unlockfunc) \
591 RAII_VAR(typeof((lock)), varname, ({lockfunc((lock)); (lock); }), unlockfunc)
592
593/*!
594 * \brief scoped lock specialization for mutexes
595 */
596#define SCOPED_MUTEX(varname, lock) SCOPED_LOCK(varname, (lock), ast_mutex_lock, ast_mutex_unlock)
597
598/*!
599 * \brief scoped lock specialization for read locks
600 */
601#define SCOPED_RDLOCK(varname, lock) SCOPED_LOCK(varname, (lock), ast_rwlock_rdlock, ast_rwlock_unlock)
602
603/*!
604 * \brief scoped lock specialization for write locks
605 */
606#define SCOPED_WRLOCK(varname, lock) SCOPED_LOCK(varname, (lock), ast_rwlock_wrlock, ast_rwlock_unlock)
607
608/*!
609 * \brief scoped lock specialization for ao2 mutexes.
610 */
611#define SCOPED_AO2LOCK(varname, obj) SCOPED_LOCK(varname, (obj), ao2_lock, ao2_unlock)
612
613/*!
614 * \brief scoped lock specialization for ao2 read locks.
615 */
616#define SCOPED_AO2RDLOCK(varname, obj) SCOPED_LOCK(varname, (obj), ao2_rdlock, ao2_unlock)
617
618/*!
619 * \brief scoped lock specialization for ao2 write locks.
620 */
621#define SCOPED_AO2WRLOCK(varname, obj) SCOPED_LOCK(varname, (obj), ao2_wrlock, ao2_unlock)
622
623/*!
624 * \brief scoped lock specialization for channels.
625 */
626#define SCOPED_CHANNELLOCK(varname, chan) SCOPED_LOCK(varname, (chan), ast_channel_lock, ast_channel_unlock)
627
628#ifndef __CYGWIN__ /* temporary disabled for cygwin */
629#define pthread_mutex_t use_ast_mutex_t_instead_of_pthread_mutex_t
630#define pthread_cond_t use_ast_cond_t_instead_of_pthread_cond_t
631#endif
632#define pthread_mutex_lock use_ast_mutex_lock_instead_of_pthread_mutex_lock
633#define pthread_mutex_unlock use_ast_mutex_unlock_instead_of_pthread_mutex_unlock
634#define pthread_mutex_trylock use_ast_mutex_trylock_instead_of_pthread_mutex_trylock
635#define pthread_mutex_init use_ast_mutex_init_instead_of_pthread_mutex_init
636#define pthread_mutex_destroy use_ast_mutex_destroy_instead_of_pthread_mutex_destroy
637#define pthread_cond_init use_ast_cond_init_instead_of_pthread_cond_init
638#define pthread_cond_destroy use_ast_cond_destroy_instead_of_pthread_cond_destroy
639#define pthread_cond_signal use_ast_cond_signal_instead_of_pthread_cond_signal
640#define pthread_cond_broadcast use_ast_cond_broadcast_instead_of_pthread_cond_broadcast
641#define pthread_cond_wait use_ast_cond_wait_instead_of_pthread_cond_wait
642#define pthread_cond_timedwait use_ast_cond_timedwait_instead_of_pthread_cond_timedwait
643
644#define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__
645
646#define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
647
648#ifndef __linux__
649#define pthread_create __use_ast_pthread_create_instead__
650#endif
651
652/*!
653 * \brief Support for atomic instructions.
654 *
655 * These macros implement a uniform interface to use built-in atomic functionality.
656 * If available __atomic built-ins are prefered. Legacy __sync built-ins are used
657 * as a fallback for older compilers.
658 *
659 * Detailed documentation can be found in the GCC manual, all API's are modeled after
660 * the __atomic interfaces but using the namespace ast_atomic.
661 *
662 * The memorder argument is always ignored by legacy __sync functions. Invalid
663 * memorder arguments do not produce errors unless __atomic functions are supported
664 * as the argument is erased by the preprocessor.
665 *
666 * \note ast_atomic_fetch_nand and ast_atomic_nand_fetch purposely do not exist.
667 * It's implementation was broken prior to gcc-4.4.
668 *
669 * @{
670 */
671
672#include "asterisk/inline_api.h"
673
674#if defined(HAVE_C_ATOMICS)
675/*! Atomic += */
676#define ast_atomic_fetch_add(ptr, val, memorder) __atomic_fetch_add((ptr), (val), (memorder))
677#define ast_atomic_add_fetch(ptr, val, memorder) __atomic_add_fetch((ptr), (val), (memorder))
678
679/*! Atomic -= */
680#define ast_atomic_fetch_sub(ptr, val, memorder) __atomic_fetch_sub((ptr), (val), (memorder))
681#define ast_atomic_sub_fetch(ptr, val, memorder) __atomic_sub_fetch((ptr), (val), (memorder))
682
683/*! Atomic &= */
684#define ast_atomic_fetch_and(ptr, val, memorder) __atomic_fetch_and((ptr), (val), (memorder))
685#define ast_atomic_and_fetch(ptr, val, memorder) __atomic_and_fetch((ptr), (val), (memorder))
686
687/*! Atomic |= */
688#define ast_atomic_fetch_or(ptr, val, memorder) __atomic_fetch_or((ptr), (val), (memorder))
689#define ast_atomic_or_fetch(ptr, val, memorder) __atomic_or_fetch((ptr), (val), (memorder))
690
691/*! Atomic xor = */
692#define ast_atomic_fetch_xor(ptr, val, memorder) __atomic_fetch_xor((ptr), (val), (memorder))
693#define ast_atomic_xor_fetch(ptr, val, memorder) __atomic_xor_fetch((ptr), (val), (memorder))
694
695#if 0
696/* Atomic compare and swap
697 *
698 * See comments near the __atomic implementation for why this is disabled.
699 */
700#define ast_atomic_compare_exchange_n(ptr, expected, desired, success_memorder, failure_memorder) \
701 __atomic_compare_exchange_n((ptr), (expected), (desired), 0, success_memorder, failure_memorder)
702
703#define ast_atomic_compare_exchange(ptr, expected, desired, success_memorder, failure_memorder) \
704 __atomic_compare_exchange((ptr), (expected), (desired), 0, success_memorder, failure_memorder)
705#endif
706
707#elif defined(HAVE_GCC_ATOMICS)
708/*! Atomic += */
709#define ast_atomic_fetch_add(ptr, val, memorder) __sync_fetch_and_add((ptr), (val))
710#define ast_atomic_add_fetch(ptr, val, memorder) __sync_add_and_fetch((ptr), (val))
711
712/*! Atomic -= */
713#define ast_atomic_fetch_sub(ptr, val, memorder) __sync_fetch_and_sub((ptr), (val))
714#define ast_atomic_sub_fetch(ptr, val, memorder) __sync_sub_and_fetch((ptr), (val))
715
716/*! Atomic &= */
717#define ast_atomic_fetch_and(ptr, val, memorder) __sync_fetch_and_and((ptr), (val))
718#define ast_atomic_and_fetch(ptr, val, memorder) __sync_and_and_fetch((ptr), (val))
719
720/*! Atomic |= */
721#define ast_atomic_fetch_or(ptr, val, memorder) __sync_fetch_and_or((ptr), (val))
722#define ast_atomic_or_fetch(ptr, val, memorder) __sync_or_and_fetch((ptr), (val))
723
724/*! Atomic xor = */
725#define ast_atomic_fetch_xor(ptr, val, memorder) __sync_fetch_and_xor((ptr), (val))
726#define ast_atomic_xor_fetch(ptr, val, memorder) __sync_xor_and_fetch((ptr), (val))
727
728#if 0
729/* Atomic compare and swap
730 *
731 * The \a expected argument is a pointer, I'm guessing __atomic built-ins
732 * perform all memory reads/writes in a single atomic operation. I don't
733 * believe this is possible to exactly replicate using __sync built-ins.
734 * Will need to determine potential use cases of this feature and write a
735 * wrapper which provides consistant behavior between __sync and __atomic
736 * implementations.
737 */
738#define ast_atomic_compare_exchange_n(ptr, expected, desired, success_memorder, failure_memorder) \
739 __sync_bool_compare_and_swap((ptr), *(expected), (desired))
740
741#define ast_atomic_compare_exchange(ptr, expected, desired, success_memorder, failure_memorder) \
742 __sync_bool_compare_and_swap((ptr), *(expected), *(desired))
743#endif
744
745#else
746#error "Atomics not available."
747#endif
748
749/*! Atomic flag set */
750#define ast_atomic_flag_set(ptr, val, memorder) ast_atomic_fetch_or((ptr), (val), (memorder))
751
752/*! Atomic flag clear */
753#define ast_atomic_flag_clear(ptr, val, memorder) ast_atomic_fetch_and((ptr), ~(val), (memorder))
754
755/*!
756 * \brief Atomically add v to *p and return the previous value of *p.
757 *
758 * This can be used to handle reference counts, and the return value
759 * can be used to generate unique identifiers.
760 */
761AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
762{
763 return ast_atomic_fetch_add(p, v, __ATOMIC_RELAXED);
765
766/*!
767 * \brief decrement *p by 1 and return true if the variable has reached 0.
768 *
769 * Useful e.g. to check if a refcount has reached 0.
770 */
771AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
772{
773 return ast_atomic_sub_fetch(p, 1, __ATOMIC_RELAXED) == 0;
775
776#if defined(__cplusplus) || defined(c_plusplus)
777}
778#endif
779
780/*! @} */
781
782#endif /* _ASTERISK_LOCK_H */
ast_cond_t cond
Definition: app_sla.c:336
Asterisk backtrace generation.
static const char type[]
Definition: chan_ooh323.c:109
ast_lock_type
Definition: check_expr.c:35
@ AST_RDLOCK
Definition: check_expr.c:37
@ AST_WRLOCK
Definition: check_expr.c:38
@ AST_MUTEX
Definition: check_expr.c:36
Compiler-specific macros and other items.
void ast_mark_lock_failed(void *lock_addr)
Definition: extconf.c:2313
static const char name[]
Definition: format_mp3.c:68
Support for logging to various files, console and syslog Configuration in file logger....
Inlinable API function macro.
#define AST_INLINE_API(hdr, body)
Definition: inline_api.h:54
#define AST_MAX_REENTRANCY
Definition: lock.h:108
int __ast_rwlock_wrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
Definition: lock.c:957
int __ast_rwlock_destroy(const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
Definition: lock.c:735
#define pthread_cond_t
Definition: lock.h:630
int __ast_cond_broadcast(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond)
Definition: lock.c:536
int __ast_rwlock_trywrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
Definition: lock.c:1274
int __ast_rwlock_unlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
Definition: lock.c:782
int __ast_rwlock_init(int tracking, const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
Definition: lock.c:701
int __ast_rwlock_timedrdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name, const struct timespec *abs_timeout)
Definition: lock.c:1060
#define pthread_mutex_lock
Definition: lock.h:632
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)
Definition: lock.c:524
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)
Definition: lock.c:571
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:764
#define pthread_mutex_t
Definition: lock.h:629
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)
Definition: lock.c:636
int __ast_cond_signal(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond)
Definition: lock.c:530
int __ast_pthread_mutex_lock(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
Definition: lock.c:255
int __ast_pthread_mutex_init(int tracking, const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
Definition: lock.c:145
int __ast_rwlock_timedwrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name, const struct timespec *abs_timeout)
Definition: lock.c:1142
#define ast_atomic_sub_fetch(ptr, val, memorder)
Definition: lock.h:681
pthread_cond_t ast_cond_t
Definition: lock.h:185
int __ast_pthread_mutex_trylock(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
Definition: lock.c:398
#define pthread_mutex_unlock
Definition: lock.h:633
int __ast_cond_destroy(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond)
Definition: lock.c:542
int ast_atomic_dec_and_test(volatile int *p)
decrement *p by 1 and return true if the variable has reached 0.
Definition: lock.h:774
int __ast_rwlock_tryrdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
Definition: lock.c:1224
int __ast_pthread_mutex_destroy(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
Definition: lock.c:177
int __ast_pthread_mutex_unlock(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
Definition: lock.c:453
int __ast_rwlock_rdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
Definition: lock.c:853
#define ast_atomic_fetch_add(ptr, val, memorder)
Support for atomic instructions.
Definition: lock.h:676
A structure to hold backtrace information. This structure provides an easy means to store backtrace i...
Definition: backtrace.h:50
Main Channel structure associated with a channel.
volatile unsigned int setup
Definition: lock.h:134
unsigned int tracking
Definition: lock.h:132
Lock tracking information.
Definition: lock.h:118
int reentrancy
Definition: lock.h:121
struct ast_bt backtrace[AST_MAX_REENTRANCY]
Definition: lock.h:125
pthread_t thread_id[AST_MAX_REENTRANCY]
Definition: lock.h:123
int lineno[AST_MAX_REENTRANCY]
Definition: lock.h:120
pthread_mutex_t reentr_mutex
Definition: lock.h:127
const char * file[AST_MAX_REENTRANCY]
Definition: lock.h:119
const char * func[AST_MAX_REENTRANCY]
Definition: lock.h:122
Structure for mutex and tracking information.
Definition: lock.h:142
struct ast_lock_track * _track
Definition: lock.h:150
pthread_mutex_t mutex
Definition: lock.h:143
struct ast_lock_track_flags _flags
Definition: lock.h:151
Structure for rwlock and tracking information.
Definition: lock.h:164
struct ast_lock_track * _track
Definition: lock.h:172
pthread_rwlock_t lock
Definition: lock.h:165
struct ast_lock_track_flags _flags
Definition: lock.h:173
Support for dynamic strings.
Definition: strings.h:623
Time-related functions and macros.