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