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