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