Wed Jan 8 2020 09:49:48

Asterisk developer's documentation


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_mutext_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/logger.h"
63 #include "asterisk/compiler.h"
64 
65 #define AST_PTHREADT_NULL (pthread_t) -1
66 #define AST_PTHREADT_STOP (pthread_t) -2
67 
68 #if (defined(SOLARIS) || defined(BSD))
69 #define AST_MUTEX_INIT_W_CONSTRUCTORS
70 #endif /* SOLARIS || BSD */
71 
72 /* Asterisk REQUIRES recursive (not error checking) mutexes
73  and will not run without them. */
74 #if defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) && defined(HAVE_PTHREAD_MUTEX_RECURSIVE_NP)
75 #define PTHREAD_MUTEX_INIT_VALUE PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
76 #define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE_NP
77 #else
78 #define PTHREAD_MUTEX_INIT_VALUE PTHREAD_MUTEX_INITIALIZER
79 #define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE
80 #endif /* PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP */
81 
82 #ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
83 #define __AST_RWLOCK_INIT_VALUE PTHREAD_RWLOCK_INITIALIZER
84 #else /* HAVE_PTHREAD_RWLOCK_INITIALIZER */
85 #define __AST_RWLOCK_INIT_VALUE {0}
86 #endif /* HAVE_PTHREAD_RWLOCK_INITIALIZER */
87 
88 #ifdef HAVE_BKTR
89 #define AST_LOCK_TRACK_INIT_VALUE { { NULL }, { 0 }, 0, { NULL }, { 0 }, {{{ 0 }}}, PTHREAD_MUTEX_INIT_VALUE }
90 #else
91 #define AST_LOCK_TRACK_INIT_VALUE { { NULL }, { 0 }, 0, { NULL }, { 0 }, PTHREAD_MUTEX_INIT_VALUE }
92 #endif
93 
94 #define AST_MUTEX_INIT_VALUE { PTHREAD_MUTEX_INIT_VALUE, NULL, 1 }
95 #define AST_MUTEX_INIT_VALUE_NOTRACKING { PTHREAD_MUTEX_INIT_VALUE, NULL, 0 }
96 
97 #define AST_RWLOCK_INIT_VALUE { __AST_RWLOCK_INIT_VALUE, NULL, 1 }
98 #define AST_RWLOCK_INIT_VALUE_NOTRACKING { __AST_RWLOCK_INIT_VALUE, NULL, 0 }
99 
100 #define AST_MAX_REENTRANCY 10
101 
102 struct ast_channel;
103 
105  const char *file[AST_MAX_REENTRANCY];
108  const char *func[AST_MAX_REENTRANCY];
110 #ifdef HAVE_BKTR
112 #endif
114 };
115 
116 /*! \brief Structure for mutex and tracking information.
117  *
118  * We have tracking information in this structure regardless of DEBUG_THREADS being enabled.
119  * The information will just be ignored in the core if a module does not request it..
120  */
123  /*! Track which thread holds this mutex */
125  unsigned int tracking:1;
126 };
127 
128 /*! \brief Structure for rwlock and tracking information.
129  *
130  * We have tracking information in this structure regardless of DEBUG_THREADS being enabled.
131  * The information will just be ignored in the core if a module does not request it..
132  */
134  pthread_rwlock_t lock;
135  /*! Track which thread holds this lock */
137  unsigned int tracking:1;
138 };
139 
141 
143 
145 
146 int __ast_pthread_mutex_init(int tracking, const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t);
147 int __ast_pthread_mutex_destroy(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t);
148 int __ast_pthread_mutex_lock(const char *filename, int lineno, const char *func, const char* mutex_name, ast_mutex_t *t);
149 int __ast_pthread_mutex_trylock(const char *filename, int lineno, const char *func, const char* mutex_name, ast_mutex_t *t);
150 int __ast_pthread_mutex_unlock(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t);
151 
152 #define ast_mutex_init(pmutex) __ast_pthread_mutex_init(1, __FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex)
153 #define ast_mutex_init_notracking(pmutex) __ast_pthread_mutex_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex)
154 #define ast_mutex_destroy(a) __ast_pthread_mutex_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
155 #define ast_mutex_lock(a) __ast_pthread_mutex_lock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
156 #define ast_mutex_unlock(a) __ast_pthread_mutex_unlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
157 #define ast_mutex_trylock(a) __ast_pthread_mutex_trylock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
158 
159 
160 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);
161 int __ast_cond_signal(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond);
162 int __ast_cond_broadcast(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond);
163 int __ast_cond_destroy(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond);
164 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);
165 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);
166 
167 #define ast_cond_init(cond, attr) __ast_cond_init(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond, attr)
168 #define ast_cond_destroy(cond) __ast_cond_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
169 #define ast_cond_signal(cond) __ast_cond_signal(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
170 #define ast_cond_broadcast(cond) __ast_cond_broadcast(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
171 #define ast_cond_wait(cond, mutex) __ast_cond_wait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex)
172 #define ast_cond_timedwait(cond, mutex, time) __ast_cond_timedwait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex, time)
173 
174 
175 int __ast_rwlock_init(int tracking, const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t);
176 int __ast_rwlock_destroy(const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t);
177 int __ast_rwlock_unlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
178 int __ast_rwlock_rdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
179 int __ast_rwlock_wrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
180 int __ast_rwlock_timedrdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name, const struct timespec *abs_timeout);
181 int __ast_rwlock_timedwrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name, const struct timespec *abs_timeout);
182 int __ast_rwlock_tryrdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
183 int __ast_rwlock_trywrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
184 
185 /*!
186  * \brief wrapper for rwlock with tracking enabled
187  * \return 0 on success, non zero for error
188  * \since 1.6.1
189  */
190 #define ast_rwlock_init(rwlock) __ast_rwlock_init(1, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
191 
192 /*!
193  * \brief wrapper for ast_rwlock_init with tracking disabled
194  * \return 0 on success, non zero for error
195  * \since 1.6.1
196  */
197 #define ast_rwlock_init_notracking(rwlock) __ast_rwlock_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
198 
199 #define ast_rwlock_destroy(rwlock) __ast_rwlock_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
200 #define ast_rwlock_unlock(a) __ast_rwlock_unlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
201 #define ast_rwlock_rdlock(a) __ast_rwlock_rdlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
202 #define ast_rwlock_wrlock(a) __ast_rwlock_wrlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
203 #define ast_rwlock_tryrdlock(a) __ast_rwlock_tryrdlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
204 #define ast_rwlock_trywrlock(a) __ast_rwlock_trywrlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a)
205 #define ast_rwlock_timedrdlock(a, b) __ast_rwlock_timedrdlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a, b)
206 #define ast_rwlock_timedwrlock(a, b) __ast_rwlock_timedwrlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, #a, b)
207 
208 #define ROFFSET ((lt->reentrancy > 0) ? (lt->reentrancy-1) : 0)
209 
210 #ifdef DEBUG_THREADS
211 
212 #define __ast_mutex_logger(...) do { if (canlog) ast_log(LOG_ERROR, __VA_ARGS__); else fprintf(stderr, __VA_ARGS__); } while (0)
213 
214 #ifdef THREAD_CRASH
215 #define DO_THREAD_CRASH do { *((int *)(0)) = 1; } while(0)
216 #else
217 #define DO_THREAD_CRASH do { } while (0)
218 #endif
219 
220 #include <errno.h>
221 
222 enum ast_lock_type {
223  AST_MUTEX,
224  AST_RDLOCK,
225  AST_WRLOCK,
226 };
227 
228 /*!
229  * \brief Store lock info for the current thread
230  *
231  * This function gets called in ast_mutex_lock() and ast_mutex_trylock() so
232  * that information about this lock can be stored in this thread's
233  * lock info struct. The lock is marked as pending as the thread is waiting
234  * on the lock. ast_mark_lock_acquired() will mark it as held by this thread.
235  */
236 #if !defined(LOW_MEMORY)
237 #ifdef HAVE_BKTR
238 void ast_store_lock_info(enum ast_lock_type type, const char *filename,
239  int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt);
240 #else
241 void ast_store_lock_info(enum ast_lock_type type, const char *filename,
242  int line_num, const char *func, const char *lock_name, void *lock_addr);
243 #endif /* HAVE_BKTR */
244 
245 #else
246 
247 #ifdef HAVE_BKTR
248 #define ast_store_lock_info(I,DONT,CARE,ABOUT,THE,PARAMETERS,BUD)
249 #else
250 #define ast_store_lock_info(I,DONT,CARE,ABOUT,THE,PARAMETERS)
251 #endif /* HAVE_BKTR */
252 #endif /* !defined(LOW_MEMORY) */
253 
254 /*!
255  * \brief Mark the last lock as acquired
256  */
257 #if !defined(LOW_MEMORY)
258 void ast_mark_lock_acquired(void *lock_addr);
259 #else
260 #define ast_mark_lock_acquired(ignore)
261 #endif
262 
263 /*!
264  * \brief Mark the last lock as failed (trylock)
265  */
266 #if !defined(LOW_MEMORY)
267 void ast_mark_lock_failed(void *lock_addr);
268 #else
269 #define ast_mark_lock_failed(ignore)
270 #endif
271 
272 /*!
273  * \brief remove lock info for the current thread
274  *
275  * this gets called by ast_mutex_unlock so that information on the lock can
276  * be removed from the current thread's lock info struct.
277  */
278 #if !defined(LOW_MEMORY)
279 #ifdef HAVE_BKTR
280 void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt);
281 #else
282 void ast_remove_lock_info(void *lock_addr);
283 #endif /* HAVE_BKTR */
284 void ast_suspend_lock_info(void *lock_addr);
285 void ast_restore_lock_info(void *lock_addr);
286 #else
287 #ifdef HAVE_BKTR
288 #define ast_remove_lock_info(ignore,me)
289 #else
290 #define ast_remove_lock_info(ignore)
291 #endif /* HAVE_BKTR */
292 #define ast_suspend_lock_info(ignore);
293 #define ast_restore_lock_info(ignore);
294 #endif /* !defined(LOW_MEMORY) */
295 
296 /*!
297  * \brief log info for the current lock with ast_log().
298  *
299  * this function would be mostly for debug. If you come across a lock
300  * that is unexpectedly but momentarily locked, and you wonder who
301  * are fighting with for the lock, this routine could be called, IF
302  * you have the thread debugging stuff turned on.
303  * \param this_lock_addr lock address to return lock information
304  * \since 1.6.1
305  */
306 void log_show_lock(void *this_lock_addr);
307 
308 /*!
309  * \brief retrieve lock info for the specified mutex
310  *
311  * this gets called during deadlock avoidance, so that the information may
312  * be preserved as to what location originally acquired the lock.
313  */
314 #if !defined(LOW_MEMORY)
315 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);
316 #else
317 #define ast_find_lock_info(a,b,c,d,e,f,g,h) -1
318 #endif
319 
320 /*!
321  * \brief Unlock a lock briefly
322  *
323  * used during deadlock avoidance, to preserve the original location where
324  * a lock was originally acquired.
325  */
326 #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
327  do { \
328  char __filename[80], __func[80], __mutex_name[80]; \
329  int __lineno; \
330  int __res = ast_find_lock_info(ao2_object_get_lockaddr(chan), __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
331  int __res2 = ast_channel_unlock(chan); \
332  usleep(1); \
333  if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
334  if (__res2) { \
335  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)); \
336  } else { \
337  ast_channel_lock(chan); \
338  } \
339  } else { \
340  if (__res2) { \
341  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); \
342  } else { \
343  __ao2_lock(chan, __filename, __func, __lineno, __mutex_name); \
344  } \
345  } \
346  } while (0)
347 
348 #define DEADLOCK_AVOIDANCE(lock) \
349  do { \
350  char __filename[80], __func[80], __mutex_name[80]; \
351  int __lineno; \
352  int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
353  int __res2 = ast_mutex_unlock(lock); \
354  usleep(1); \
355  if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
356  if (__res2 == 0) { \
357  ast_mutex_lock(lock); \
358  } else { \
359  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)); \
360  } \
361  } else { \
362  if (__res2 == 0) { \
363  __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
364  } else { \
365  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); \
366  } \
367  } \
368  } while (0)
369 
370 /*!
371  * \brief Deadlock avoidance unlock
372  *
373  * In certain deadlock avoidance scenarios, there is more than one lock to be
374  * unlocked and relocked. Therefore, this pair of macros is provided for that
375  * purpose. Note that every DLA_UNLOCK _MUST_ be paired with a matching
376  * DLA_LOCK. The intent of this pair of macros is to be used around another
377  * set of deadlock avoidance code, mainly CHANNEL_DEADLOCK_AVOIDANCE, as the
378  * locking order specifies that we may safely lock a channel, followed by its
379  * pvt, with no worries about a deadlock. In any other scenario, this macro
380  * may not be safe to use.
381  */
382 #define DLA_UNLOCK(lock) \
383  do { \
384  char __filename[80], __func[80], __mutex_name[80]; \
385  int __lineno; \
386  int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
387  int __res2 = ast_mutex_unlock(lock);
388 
389 /*!
390  * \brief Deadlock avoidance lock
391  *
392  * In certain deadlock avoidance scenarios, there is more than one lock to be
393  * unlocked and relocked. Therefore, this pair of macros is provided for that
394  * purpose. Note that every DLA_UNLOCK _MUST_ be paired with a matching
395  * DLA_LOCK. The intent of this pair of macros is to be used around another
396  * set of deadlock avoidance code, mainly CHANNEL_DEADLOCK_AVOIDANCE, as the
397  * locking order specifies that we may safely lock a channel, followed by its
398  * pvt, with no worries about a deadlock. In any other scenario, this macro
399  * may not be safe to use.
400  */
401 #define DLA_LOCK(lock) \
402  if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
403  if (__res2) { \
404  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)); \
405  } else { \
406  ast_mutex_lock(lock); \
407  } \
408  } else { \
409  if (__res2) { \
410  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); \
411  } else { \
412  __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
413  } \
414  } \
415  } while (0)
416 
417 static inline void ast_reentrancy_lock(struct ast_lock_track *lt)
418 {
419  int res;
420  if ((res = pthread_mutex_lock(&lt->reentr_mutex))) {
421  fprintf(stderr, "ast_reentrancy_lock failed: '%s' (%d)\n", strerror(res), res);
422 #if defined(DO_CRASH) || defined(THREAD_CRASH)
423  abort();
424 #endif
425  }
426 }
427 
428 static inline void ast_reentrancy_unlock(struct ast_lock_track *lt)
429 {
430  int res;
431  if ((res = pthread_mutex_unlock(&lt->reentr_mutex))) {
432  fprintf(stderr, "ast_reentrancy_unlock failed: '%s' (%d)\n", strerror(res), res);
433 #if defined(DO_CRASH) || defined(THREAD_CRASH)
434  abort();
435 #endif
436  }
437 }
438 
439 static inline void ast_reentrancy_init(struct ast_lock_track **plt)
440 {
441  int i;
442  pthread_mutexattr_t reentr_attr;
443  struct ast_lock_track *lt = *plt;
444 
445  if (!lt) {
446  lt = *plt = (struct ast_lock_track *) calloc(1, sizeof(*lt));
447  }
448 
449  for (i = 0; i < AST_MAX_REENTRANCY; i++) {
450  lt->file[i] = NULL;
451  lt->lineno[i] = 0;
452  lt->func[i] = NULL;
453  lt->thread[i] = 0;
454 #ifdef HAVE_BKTR
455  memset(&lt->backtrace[i], 0, sizeof(lt->backtrace[i]));
456 #endif
457  }
458 
459  lt->reentrancy = 0;
460 
461  pthread_mutexattr_init(&reentr_attr);
462  pthread_mutexattr_settype(&reentr_attr, AST_MUTEX_KIND);
463  pthread_mutex_init(&lt->reentr_mutex, &reentr_attr);
464  pthread_mutexattr_destroy(&reentr_attr);
465 }
466 
467 static inline void delete_reentrancy_cs(struct ast_lock_track **plt)
468 {
469  struct ast_lock_track *lt;
470  if (*plt) {
471  lt = *plt;
473  free(lt);
474  *plt = NULL;
475  }
476 }
477 
478 #else /* !DEBUG_THREADS */
479 
480 #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
481  ast_channel_unlock(chan); \
482  usleep(1); \
483  ast_channel_lock(chan);
484 
485 #define DEADLOCK_AVOIDANCE(lock) \
486  do { \
487  int __res; \
488  if (!(__res = ast_mutex_unlock(lock))) { \
489  usleep(1); \
490  ast_mutex_lock(lock); \
491  } else { \
492  ast_log(LOG_WARNING, "Failed to unlock mutex '%s' (%s). I will NOT try to relock. {{{ THIS IS A BUG. }}}\n", #lock, strerror(__res)); \
493  } \
494  } while (0)
495 
496 #define DLA_UNLOCK(lock) ast_mutex_unlock(lock)
497 
498 #define DLA_LOCK(lock) ast_mutex_lock(lock)
499 
500 #endif /* !DEBUG_THREADS */
501 
502 #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
503 /*
504  * If AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope constructors
505  * and destructors to create/destroy global mutexes.
506  */
507 #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track) \
508  scope ast_mutex_t mutex = init_val; \
509 static void __attribute__((constructor)) init_##mutex(void) \
510 { \
511  if (track) \
512  ast_mutex_init(&mutex); \
513  else \
514  ast_mutex_init_notracking(&mutex); \
515 } \
516  \
517 static void __attribute__((destructor)) fini_##mutex(void) \
518 { \
519  ast_mutex_destroy(&mutex); \
520 }
521 #else /* !AST_MUTEX_INIT_W_CONSTRUCTORS */
522 /* By default, use static initialization of mutexes. */
523 #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track) scope ast_mutex_t mutex = init_val
524 #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
525 
526 #define AST_MUTEX_DEFINE_STATIC(mutex) __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE, 1)
527 #define AST_MUTEX_DEFINE_STATIC_NOTRACKING(mutex) __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE_NOTRACKING, 0)
528 
529 
530 /* Statically declared read/write locks */
531 #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
532 #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) \
533  scope ast_rwlock_t rwlock = init_val; \
534 static void __attribute__((constructor)) init_##rwlock(void) \
535 { \
536  if (track) \
537  ast_rwlock_init(&rwlock); \
538  else \
539  ast_rwlock_init_notracking(&rwlock); \
540 } \
541 static void __attribute__((destructor)) fini_##rwlock(void) \
542 { \
543  ast_rwlock_destroy(&rwlock); \
544 }
545 #else
546 #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) scope ast_rwlock_t rwlock = init_val
547 #endif
548 
549 #define AST_RWLOCK_DEFINE_STATIC(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE, 1)
550 #define AST_RWLOCK_DEFINE_STATIC_NOTRACKING(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE_NOTRACKING, 0)
551 
552 #ifndef __CYGWIN__ /* temporary disabled for cygwin */
553 #define pthread_mutex_t use_ast_mutex_t_instead_of_pthread_mutex_t
554 #define pthread_cond_t use_ast_cond_t_instead_of_pthread_cond_t
555 #endif
556 #define pthread_mutex_lock use_ast_mutex_lock_instead_of_pthread_mutex_lock
557 #define pthread_mutex_unlock use_ast_mutex_unlock_instead_of_pthread_mutex_unlock
558 #define pthread_mutex_trylock use_ast_mutex_trylock_instead_of_pthread_mutex_trylock
559 #define pthread_mutex_init use_ast_mutex_init_instead_of_pthread_mutex_init
560 #define pthread_mutex_destroy use_ast_mutex_destroy_instead_of_pthread_mutex_destroy
561 #define pthread_cond_init use_ast_cond_init_instead_of_pthread_cond_init
562 #define pthread_cond_destroy use_ast_cond_destroy_instead_of_pthread_cond_destroy
563 #define pthread_cond_signal use_ast_cond_signal_instead_of_pthread_cond_signal
564 #define pthread_cond_broadcast use_ast_cond_broadcast_instead_of_pthread_cond_broadcast
565 #define pthread_cond_wait use_ast_cond_wait_instead_of_pthread_cond_wait
566 #define pthread_cond_timedwait use_ast_cond_timedwait_instead_of_pthread_cond_timedwait
567 
568 #define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__
569 
570 #define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
571 
572 #ifndef __linux__
573 #define pthread_create __use_ast_pthread_create_instead__
574 #endif
575 
576 /*
577  * Support for atomic instructions.
578  * For platforms that have it, use the native cpu instruction to
579  * implement them. For other platforms, resort to a 'slow' version
580  * (defined in utils.c) that protects the atomic instruction with
581  * a single lock.
582  * The slow versions is always available, for testing purposes,
583  * as ast_atomic_fetchadd_int_slow()
584  */
585 
586 int ast_atomic_fetchadd_int_slow(volatile int *p, int v);
587 
588 #include "asterisk/inline_api.h"
589 
590 #if defined(HAVE_OSX_ATOMICS)
591 #include "libkern/OSAtomic.h"
592 #endif
593 
594 /*! \brief Atomically add v to *p and return * the previous value of *p.
595  * This can be used to handle reference counts, and the return value
596  * can be used to generate unique identifiers.
597  */
598 
599 #if defined(HAVE_GCC_ATOMICS)
600 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
601 {
602  return __sync_fetch_and_add(p, v);
603 })
604 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
605 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
606 {
607  return OSAtomicAdd32(v, (int32_t *) p) - v;
608 })
609 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
610 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
611 {
612  return OSAtomicAdd64(v, (int64_t *) p) - v;
613 #elif defined (__i386__) || defined(__x86_64__)
614 #ifdef sun
615 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
616 {
617  __asm __volatile (
618  " lock; xaddl %0, %1 ; "
619  : "+r" (v), /* 0 (result) */
620  "=m" (*p) /* 1 */
621  : "m" (*p)); /* 2 */
622  return (v);
623 })
624 #else /* ifndef sun */
625 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
626 {
627  __asm __volatile (
628  " lock xaddl %0, %1 ; "
629  : "+r" (v), /* 0 (result) */
630  "=m" (*p) /* 1 */
631  : "m" (*p)); /* 2 */
632  return (v);
633 })
634 #endif
635 #else /* low performance version in utils.c */
636 AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
637 {
638  return ast_atomic_fetchadd_int_slow(p, v);
639 })
640 #endif
641 
642 /*! \brief decrement *p by 1 and return true if the variable has reached 0.
643  * Useful e.g. to check if a refcount has reached 0.
644  */
645 #if defined(HAVE_GCC_ATOMICS)
646 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
647 {
648  return __sync_sub_and_fetch(p, 1) == 0;
649 })
650 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
651 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
652 {
653  return OSAtomicAdd32( -1, (int32_t *) p) == 0;
654 })
655 #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
656 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
657 {
658  return OSAtomicAdd64( -1, (int64_t *) p) == 0;
659 #else
660 AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
661 {
662  int a = ast_atomic_fetchadd_int(p, -1);
663  return a == 1; /* true if the value is 0 now (so it was 1 previously) */
664 })
665 #endif
666 
667 #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:484
#define pthread_mutex_init
Definition: lock.h:559
pthread_t thread[AST_MAX_REENTRANCY]
Definition: lock.h:109
Main Channel structure associated with a channel.
Definition: channel.h:742
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:1089
int __ast_rwlock_trywrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
Definition: lock.c:1393
#define SIZEOF_INT
Definition: autoconfig.h:1170
const char * file[AST_MAX_REENTRANCY]
Definition: lock.h:105
int __ast_pthread_mutex_destroy(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
Definition: lock.c:99
Time-related functions and macros.
unsigned int tracking
Definition: lock.h:125
int __ast_rwlock_rdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
Definition: lock.c:832
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:1200
int reentrancy
Definition: lock.h:107
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:65
int __ast_rwlock_unlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
Definition: lock.c:746
int lineno[AST_MAX_REENTRANCY]
Definition: lock.h:106
#define pthread_mutex_t
Definition: lock.h:553
struct ast_bt backtrace[AST_MAX_REENTRANCY]
Definition: lock.h:111
#define calloc(a, b)
Definition: astmm.h:79
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return * the previous value of *p. This can be used to handle reference co...
Definition: lock.h:603
struct ast_lock_track * track
Definition: lock.h:124
pthread_cond_t ast_cond_t
Definition: lock.h:144
Compiler-specific macros and other items.
#define pthread_mutex_destroy
Definition: lock.h:560
Definition: logger.h:267
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:589
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:508
#define pthread_mutex_lock
Definition: lock.h:556
Inlinable API function macro.
ast_cond_t cond
Definition: app_meetme.c:963
int ast_atomic_dec_and_test(volatile int *p)
decrement *p by 1 and return true if the variable has reached 0. Useful e.g. to check if a refcount h...
Definition: lock.h:649
int __ast_pthread_mutex_unlock(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
Definition: lock.c:399
int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
Definition: utils.c:2078
#define HAVE_GCC_ATOMICS
Definition: autoconfig.h:257
int __ast_cond_broadcast(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond)
Definition: lock.c:496
#define free(a)
Definition: astmm.h:94
const char * func[AST_MAX_REENTRANCY]
Definition: lock.h:108
int __ast_cond_destroy(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond)
Definition: lock.c:502
static const char name[]
unsigned int tracking
Definition: lock.h:137
int __ast_rwlock_destroy(const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
Definition: lock.c:703
static const char type[]
Definition: chan_nbs.c:57
int __ast_rwlock_wrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
Definition: lock.c:959
#define AST_MAX_REENTRANCY
Definition: lock.h:100
Support for logging to various files, console and syslog Configuration in file logger.conf.
Structure for rwlock and tracking information.
Definition: lock.h:133
int __ast_pthread_mutex_lock(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
Definition: lock.c:177
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:670
pthread_mutex_t mutex
Definition: lock.h:122
int __ast_pthread_mutex_trylock(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t)
Definition: lock.c:317
pthread_mutex_t reentr_mutex
Definition: lock.h:113
int __ast_cond_signal(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond)
Definition: lock.c:490
#define pthread_mutex_unlock
Definition: lock.h:557
pthread_rwlock_t lock
Definition: lock.h:134
int __ast_rwlock_tryrdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name)
Definition: lock.c:1315
static enum AST_LOCK_TYPE ast_lock_type
Definition: app.c:229
#define pthread_cond_t
Definition: lock.h:554
#define AST_MUTEX_KIND
Definition: lock.h:76
Structure for mutex and tracking information.
Definition: lock.h:121
struct ast_lock_track * track
Definition: lock.h:136
#define AST_INLINE_API(hdr, body)
Definition: inline_api.h:49