lock.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. /*! \file
  19. * \brief Asterisk locking-related definitions:
  20. * - ast_mutex_t, ast_rwlock_t and related functions;
  21. * - atomic arithmetic instructions;
  22. * - wrappers for channel locking.
  23. *
  24. * - See \ref LockDef
  25. */
  26. /*! \page LockDef Asterisk thread locking models
  27. *
  28. * This file provides different implementation of the functions,
  29. * depending on the platform, the use of DEBUG_THREADS, and the way
  30. * module-level mutexes are initialized.
  31. *
  32. * - \b static: the mutex is assigned the value AST_MUTEX_INIT_VALUE
  33. * this is done at compile time, and is the way used on Linux.
  34. * This method is not applicable to all platforms e.g. when the
  35. * initialization needs that some code is run.
  36. *
  37. * - \b through constructors: for each mutex, a constructor function is
  38. * defined, which then runs when the program (or the module)
  39. * starts. The problem with this approach is that there is a
  40. * lot of code duplication (a new block of code is created for
  41. * each mutex). Also, it does not prevent a user from declaring
  42. * a global mutex without going through the wrapper macros,
  43. * so sane programming practices are still required.
  44. */
  45. #ifndef _ASTERISK_LOCK_H
  46. #define _ASTERISK_LOCK_H
  47. #include <pthread.h>
  48. #include <time.h>
  49. #include <sys/param.h>
  50. #ifdef HAVE_BKTR
  51. #include <execinfo.h>
  52. #endif
  53. #ifndef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
  54. #include "asterisk/time.h"
  55. #endif
  56. #include "asterisk/backtrace.h"
  57. #include "asterisk/logger.h"
  58. #include "asterisk/compiler.h"
  59. #define AST_PTHREADT_NULL (pthread_t) -1
  60. #define AST_PTHREADT_STOP (pthread_t) -2
  61. #if (defined(SOLARIS) || defined(BSD))
  62. #define AST_MUTEX_INIT_W_CONSTRUCTORS
  63. #endif /* SOLARIS || BSD */
  64. /* Asterisk REQUIRES recursive (not error checking) mutexes
  65. and will not run without them. */
  66. #if defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) && defined(HAVE_PTHREAD_MUTEX_RECURSIVE_NP)
  67. #define PTHREAD_MUTEX_INIT_VALUE PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
  68. #define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE_NP
  69. #else
  70. #define PTHREAD_MUTEX_INIT_VALUE PTHREAD_MUTEX_INITIALIZER
  71. #define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE
  72. #endif /* PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP */
  73. #ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
  74. #define __AST_RWLOCK_INIT_VALUE PTHREAD_RWLOCK_INITIALIZER
  75. #else /* HAVE_PTHREAD_RWLOCK_INITIALIZER */
  76. #define __AST_RWLOCK_INIT_VALUE {0}
  77. #endif /* HAVE_PTHREAD_RWLOCK_INITIALIZER */
  78. #ifdef HAVE_BKTR
  79. #define AST_LOCK_TRACK_INIT_VALUE { { NULL }, { 0 }, 0, { NULL }, { 0 }, {{{ 0 }}}, PTHREAD_MUTEX_INIT_VALUE }
  80. #else
  81. #define AST_LOCK_TRACK_INIT_VALUE { { NULL }, { 0 }, 0, { NULL }, { 0 }, PTHREAD_MUTEX_INIT_VALUE }
  82. #endif
  83. #define AST_MUTEX_INIT_VALUE { PTHREAD_MUTEX_INIT_VALUE, NULL, {1, 0} }
  84. #define AST_MUTEX_INIT_VALUE_NOTRACKING { PTHREAD_MUTEX_INIT_VALUE, NULL, {0, 0} }
  85. #define AST_RWLOCK_INIT_VALUE { __AST_RWLOCK_INIT_VALUE, NULL, {1, 0} }
  86. #define AST_RWLOCK_INIT_VALUE_NOTRACKING { __AST_RWLOCK_INIT_VALUE, NULL, {0, 0} }
  87. #define AST_MAX_REENTRANCY 10
  88. struct ast_channel;
  89. /*!
  90. * \brief Lock tracking information.
  91. *
  92. * \note Any changes to this struct MUST be reflected in the
  93. * lock.c:restore_lock_tracking() function.
  94. */
  95. struct ast_lock_track {
  96. const char *file[AST_MAX_REENTRANCY];
  97. int lineno[AST_MAX_REENTRANCY];
  98. int reentrancy;
  99. const char *func[AST_MAX_REENTRANCY];
  100. pthread_t thread_id[AST_MAX_REENTRANCY];
  101. #ifdef HAVE_BKTR
  102. struct ast_bt backtrace[AST_MAX_REENTRANCY];
  103. #endif
  104. pthread_mutex_t reentr_mutex;
  105. };
  106. struct ast_lock_track_flags {
  107. /*! non-zero if lock tracking is enabled */
  108. unsigned int tracking:1;
  109. /*! non-zero if track is setup */
  110. volatile unsigned int setup:1;
  111. };
  112. /*! \brief Structure for mutex and tracking information.
  113. *
  114. * We have tracking information in this structure regardless of DEBUG_THREADS being enabled.
  115. * The information will just be ignored in the core if a module does not request it..
  116. */
  117. struct ast_mutex_info {
  118. pthread_mutex_t mutex;
  119. #if !defined(DEBUG_THREADS) && !defined(DEBUG_THREADS_LOOSE_ABI)
  120. /*!
  121. * These fields are renamed to ensure they are never used when
  122. * DEBUG_THREADS is not defined.
  123. */
  124. struct ast_lock_track *_track;
  125. struct ast_lock_track_flags _flags;
  126. #elif defined(DEBUG_THREADS)
  127. /*! Track which thread holds this mutex. */
  128. struct ast_lock_track *track;
  129. struct ast_lock_track_flags flags;
  130. #endif
  131. };
  132. /*! \brief Structure for rwlock and tracking information.
  133. *
  134. * We have tracking information in this structure regardless of DEBUG_THREADS being enabled.
  135. * The information will just be ignored in the core if a module does not request it..
  136. */
  137. struct ast_rwlock_info {
  138. pthread_rwlock_t lock;
  139. #if !defined(DEBUG_THREADS) && !defined(DEBUG_THREADS_LOOSE_ABI)
  140. /*!
  141. * These fields are renamed to ensure they are never used when
  142. * DEBUG_THREADS is not defined.
  143. */
  144. struct ast_lock_track *_track;
  145. struct ast_lock_track_flags _flags;
  146. #elif defined(DEBUG_THREADS)
  147. /*! Track which thread holds this lock */
  148. struct ast_lock_track *track;
  149. struct ast_lock_track_flags flags;
  150. #endif
  151. };
  152. typedef struct ast_mutex_info ast_mutex_t;
  153. typedef struct ast_rwlock_info ast_rwlock_t;
  154. typedef pthread_cond_t ast_cond_t;
  155. int __ast_pthread_mutex_init(int tracking, const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t);
  156. int __ast_pthread_mutex_destroy(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t);
  157. int __ast_pthread_mutex_lock(const char *filename, int lineno, const char *func, const char* mutex_name, ast_mutex_t *t);
  158. int __ast_pthread_mutex_trylock(const char *filename, int lineno, const char *func, const char* mutex_name, ast_mutex_t *t);
  159. int __ast_pthread_mutex_unlock(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t);
  160. #define ast_mutex_init(pmutex) __ast_pthread_mutex_init(1, __FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex)
  161. #define ast_mutex_init_notracking(pmutex) __ast_pthread_mutex_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex)
  162. #define ast_mutex_destroy(a) __ast_pthread_mutex_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
  163. #define ast_mutex_lock(a) __ast_pthread_mutex_lock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
  164. #define ast_mutex_unlock(a) __ast_pthread_mutex_unlock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
  165. #define ast_mutex_trylock(a) __ast_pthread_mutex_trylock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
  166. 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);
  167. int __ast_cond_signal(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond);
  168. int __ast_cond_broadcast(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond);
  169. int __ast_cond_destroy(const char *filename, int lineno, const char *func, const char *cond_name, ast_cond_t *cond);
  170. 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);
  171. 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);
  172. #define ast_cond_init(cond, attr) __ast_cond_init(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond, attr)
  173. #define ast_cond_destroy(cond) __ast_cond_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
  174. #define ast_cond_signal(cond) __ast_cond_signal(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
  175. #define ast_cond_broadcast(cond) __ast_cond_broadcast(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, cond)
  176. #define ast_cond_wait(cond, mutex) __ast_cond_wait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex)
  177. #define ast_cond_timedwait(cond, mutex, time) __ast_cond_timedwait(__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #mutex, cond, mutex, time)
  178. int __ast_rwlock_init(int tracking, const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t);
  179. int __ast_rwlock_destroy(const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t);
  180. int __ast_rwlock_unlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
  181. int __ast_rwlock_rdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
  182. int __ast_rwlock_wrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
  183. int __ast_rwlock_timedrdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name, const struct timespec *abs_timeout);
  184. int __ast_rwlock_timedwrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name, const struct timespec *abs_timeout);
  185. int __ast_rwlock_tryrdlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
  186. int __ast_rwlock_trywrlock(const char *filename, int lineno, const char *func, ast_rwlock_t *t, const char *name);
  187. /*!
  188. * \brief wrapper for rwlock with tracking enabled
  189. * \return 0 on success, non zero for error
  190. * \since 1.6.1
  191. */
  192. #define ast_rwlock_init(rwlock) __ast_rwlock_init(1, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
  193. /*!
  194. * \brief wrapper for ast_rwlock_init with tracking disabled
  195. * \return 0 on success, non zero for error
  196. * \since 1.6.1
  197. */
  198. #define ast_rwlock_init_notracking(rwlock) __ast_rwlock_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
  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. #define ROFFSET ((lt->reentrancy > 0) ? (lt->reentrancy-1) : 0)
  208. #ifdef DEBUG_THREADS
  209. #ifdef THREAD_CRASH
  210. #define DO_THREAD_CRASH do { *((int *)(0)) = 1; } while(0)
  211. #else
  212. #define DO_THREAD_CRASH do { } while (0)
  213. #endif
  214. #include <errno.h>
  215. enum ast_lock_type {
  216. AST_MUTEX,
  217. AST_RDLOCK,
  218. AST_WRLOCK,
  219. };
  220. /*!
  221. * \brief Store lock info for the current thread
  222. *
  223. * This function gets called in ast_mutex_lock() and ast_mutex_trylock() so
  224. * that information about this lock can be stored in this thread's
  225. * lock info struct. The lock is marked as pending as the thread is waiting
  226. * on the lock. ast_mark_lock_acquired() will mark it as held by this thread.
  227. */
  228. #if !defined(LOW_MEMORY)
  229. #ifdef HAVE_BKTR
  230. void ast_store_lock_info(enum ast_lock_type type, const char *filename,
  231. int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt);
  232. #else
  233. void ast_store_lock_info(enum ast_lock_type type, const char *filename,
  234. int line_num, const char *func, const char *lock_name, void *lock_addr);
  235. #endif /* HAVE_BKTR */
  236. #else
  237. #ifdef HAVE_BKTR
  238. #define ast_store_lock_info(I,DONT,CARE,ABOUT,THE,PARAMETERS,BUD)
  239. #else
  240. #define ast_store_lock_info(I,DONT,CARE,ABOUT,THE,PARAMETERS)
  241. #endif /* HAVE_BKTR */
  242. #endif /* !defined(LOW_MEMORY) */
  243. /*!
  244. * \brief Mark the last lock as acquired
  245. */
  246. #if !defined(LOW_MEMORY)
  247. void ast_mark_lock_acquired(void *lock_addr);
  248. #else
  249. #define ast_mark_lock_acquired(ignore)
  250. #endif
  251. /*!
  252. * \brief Mark the last lock as failed (trylock)
  253. */
  254. #if !defined(LOW_MEMORY)
  255. void ast_mark_lock_failed(void *lock_addr);
  256. #else
  257. #define ast_mark_lock_failed(ignore)
  258. #endif
  259. /*!
  260. * \brief remove lock info for the current thread
  261. *
  262. * this gets called by ast_mutex_unlock so that information on the lock can
  263. * be removed from the current thread's lock info struct.
  264. */
  265. #if !defined(LOW_MEMORY)
  266. #ifdef HAVE_BKTR
  267. void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt);
  268. #else
  269. void ast_remove_lock_info(void *lock_addr);
  270. #endif /* HAVE_BKTR */
  271. void ast_suspend_lock_info(void *lock_addr);
  272. void ast_restore_lock_info(void *lock_addr);
  273. #else
  274. #ifdef HAVE_BKTR
  275. #define ast_remove_lock_info(ignore,me)
  276. #else
  277. #define ast_remove_lock_info(ignore)
  278. #endif /* HAVE_BKTR */
  279. #define ast_suspend_lock_info(ignore);
  280. #define ast_restore_lock_info(ignore);
  281. #endif /* !defined(LOW_MEMORY) */
  282. /*!
  283. * \brief log info for the current lock with ast_log().
  284. *
  285. * this function would be mostly for debug. If you come across a lock
  286. * that is unexpectedly but momentarily locked, and you wonder who
  287. * are fighting with for the lock, this routine could be called, IF
  288. * you have the thread debugging stuff turned on.
  289. * \param this_lock_addr lock address to return lock information
  290. * \since 1.6.1
  291. */
  292. void ast_log_show_lock(void *this_lock_addr);
  293. /*!
  294. * \brief Generate a lock dump equivalent to "core show locks".
  295. *
  296. * The lock dump generated is generally too large to be output by a
  297. * single ast_verbose/log/debug/etc. call. Only ast_cli() handles it
  298. * properly without changing BUFSIZ in logger.c.
  299. *
  300. * Note: This must be ast_free()d when you're done with it.
  301. *
  302. * \retval An ast_str containing the lock dump
  303. * \retval NULL on error
  304. * \since 12
  305. */
  306. struct ast_str *ast_dump_locks(void);
  307. /*!
  308. * \brief retrieve lock info for the specified mutex
  309. *
  310. * this gets called during deadlock avoidance, so that the information may
  311. * be preserved as to what location originally acquired the lock.
  312. */
  313. #if !defined(LOW_MEMORY)
  314. 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);
  315. #else
  316. #define ast_find_lock_info(a,b,c,d,e,f,g,h) -1
  317. #endif
  318. /*!
  319. * \brief Unlock a lock briefly
  320. *
  321. * used during deadlock avoidance, to preserve the original location where
  322. * a lock was originally acquired.
  323. */
  324. #define AO2_DEADLOCK_AVOIDANCE(obj) \
  325. do { \
  326. char __filename[80], __func[80], __mutex_name[80]; \
  327. int __lineno; \
  328. int __res = ast_find_lock_info(ao2_object_get_lockaddr(obj), __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
  329. int __res2 = ao2_unlock(obj); \
  330. usleep(1); \
  331. if (__res < 0) { /* Could happen if the ao2 object does not have a mutex. */ \
  332. if (__res2) { \
  333. 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)); \
  334. } else { \
  335. ao2_lock(obj); \
  336. } \
  337. } else { \
  338. if (__res2) { \
  339. 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); \
  340. } else { \
  341. __ao2_lock(obj, AO2_LOCK_REQ_MUTEX, __filename, __func, __lineno, __mutex_name); \
  342. } \
  343. } \
  344. } while (0)
  345. #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
  346. do { \
  347. char __filename[80], __func[80], __mutex_name[80]; \
  348. int __lineno; \
  349. int __res = ast_find_lock_info(ao2_object_get_lockaddr(chan), __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
  350. int __res2 = ast_channel_unlock(chan); \
  351. usleep(1); \
  352. if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
  353. if (__res2) { \
  354. 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)); \
  355. } else { \
  356. ast_channel_lock(chan); \
  357. } \
  358. } else { \
  359. if (__res2) { \
  360. 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); \
  361. } else { \
  362. __ao2_lock(chan, AO2_LOCK_REQ_MUTEX, __filename, __func, __lineno, __mutex_name); \
  363. } \
  364. } \
  365. } while (0)
  366. #define DEADLOCK_AVOIDANCE(lock) \
  367. do { \
  368. char __filename[80], __func[80], __mutex_name[80]; \
  369. int __lineno; \
  370. int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
  371. int __res2 = ast_mutex_unlock(lock); \
  372. usleep(1); \
  373. if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
  374. if (__res2 == 0) { \
  375. ast_mutex_lock(lock); \
  376. } else { \
  377. 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)); \
  378. } \
  379. } else { \
  380. if (__res2 == 0) { \
  381. __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
  382. } else { \
  383. 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); \
  384. } \
  385. } \
  386. } while (0)
  387. /*!
  388. * \brief Deadlock avoidance unlock
  389. *
  390. * In certain deadlock avoidance scenarios, there is more than one lock to be
  391. * unlocked and relocked. Therefore, this pair of macros is provided for that
  392. * purpose. Note that every DLA_UNLOCK _MUST_ be paired with a matching
  393. * DLA_LOCK. The intent of this pair of macros is to be used around another
  394. * set of deadlock avoidance code, mainly CHANNEL_DEADLOCK_AVOIDANCE, as the
  395. * locking order specifies that we may safely lock a channel, followed by its
  396. * pvt, with no worries about a deadlock. In any other scenario, this macro
  397. * may not be safe to use.
  398. */
  399. #define DLA_UNLOCK(lock) \
  400. do { \
  401. char __filename[80], __func[80], __mutex_name[80]; \
  402. int __lineno; \
  403. int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
  404. int __res2 = ast_mutex_unlock(lock);
  405. /*!
  406. * \brief Deadlock avoidance lock
  407. *
  408. * In certain deadlock avoidance scenarios, there is more than one lock to be
  409. * unlocked and relocked. Therefore, this pair of macros is provided for that
  410. * purpose. Note that every DLA_UNLOCK _MUST_ be paired with a matching
  411. * DLA_LOCK. The intent of this pair of macros is to be used around another
  412. * set of deadlock avoidance code, mainly CHANNEL_DEADLOCK_AVOIDANCE, as the
  413. * locking order specifies that we may safely lock a channel, followed by its
  414. * pvt, with no worries about a deadlock. In any other scenario, this macro
  415. * may not be safe to use.
  416. */
  417. #define DLA_LOCK(lock) \
  418. if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
  419. if (__res2) { \
  420. 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)); \
  421. } else { \
  422. ast_mutex_lock(lock); \
  423. } \
  424. } else { \
  425. if (__res2) { \
  426. 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); \
  427. } else { \
  428. __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
  429. } \
  430. } \
  431. } while (0)
  432. static inline void ast_reentrancy_lock(struct ast_lock_track *lt)
  433. {
  434. int res;
  435. if ((res = pthread_mutex_lock(&lt->reentr_mutex))) {
  436. fprintf(stderr, "ast_reentrancy_lock failed: '%s' (%d)\n", strerror(res), res);
  437. #if defined(DO_CRASH) || defined(THREAD_CRASH)
  438. abort();
  439. #endif
  440. }
  441. }
  442. static inline void ast_reentrancy_unlock(struct ast_lock_track *lt)
  443. {
  444. int res;
  445. if ((res = pthread_mutex_unlock(&lt->reentr_mutex))) {
  446. fprintf(stderr, "ast_reentrancy_unlock failed: '%s' (%d)\n", strerror(res), res);
  447. #if defined(DO_CRASH) || defined(THREAD_CRASH)
  448. abort();
  449. #endif
  450. }
  451. }
  452. #else /* !DEBUG_THREADS */
  453. #define AO2_DEADLOCK_AVOIDANCE(obj) \
  454. ao2_unlock(obj); \
  455. usleep(1); \
  456. ao2_lock(obj);
  457. #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \
  458. ast_channel_unlock(chan); \
  459. usleep(1); \
  460. ast_channel_lock(chan);
  461. #define DEADLOCK_AVOIDANCE(lock) \
  462. do { \
  463. int __res; \
  464. if (!(__res = ast_mutex_unlock(lock))) { \
  465. usleep(1); \
  466. ast_mutex_lock(lock); \
  467. } else { \
  468. ast_log(LOG_WARNING, "Failed to unlock mutex '%s' (%s). I will NOT try to relock. {{{ THIS IS A BUG. }}}\n", #lock, strerror(__res)); \
  469. } \
  470. } while (0)
  471. #define DLA_UNLOCK(lock) ast_mutex_unlock(lock)
  472. #define DLA_LOCK(lock) ast_mutex_lock(lock)
  473. #endif /* !DEBUG_THREADS */
  474. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
  475. /*
  476. * If AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope constructors
  477. * and destructors to create/destroy global mutexes.
  478. */
  479. #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track) \
  480. scope ast_mutex_t mutex = init_val; \
  481. static void __attribute__((constructor)) init_##mutex(void) \
  482. { \
  483. if (track) \
  484. ast_mutex_init(&mutex); \
  485. else \
  486. ast_mutex_init_notracking(&mutex); \
  487. } \
  488. \
  489. static void __attribute__((destructor)) fini_##mutex(void) \
  490. { \
  491. ast_mutex_destroy(&mutex); \
  492. }
  493. #else /* !AST_MUTEX_INIT_W_CONSTRUCTORS */
  494. /* By default, use static initialization of mutexes. */
  495. #define __AST_MUTEX_DEFINE(scope, mutex, init_val, track) scope ast_mutex_t mutex = init_val
  496. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  497. #define AST_MUTEX_DEFINE_STATIC(mutex) __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE, 1)
  498. #define AST_MUTEX_DEFINE_STATIC_NOTRACKING(mutex) __AST_MUTEX_DEFINE(static, mutex, AST_MUTEX_INIT_VALUE_NOTRACKING, 0)
  499. /* Statically declared read/write locks */
  500. #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
  501. #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) \
  502. scope ast_rwlock_t rwlock = init_val; \
  503. static void __attribute__((constructor)) init_##rwlock(void) \
  504. { \
  505. if (track) \
  506. ast_rwlock_init(&rwlock); \
  507. else \
  508. ast_rwlock_init_notracking(&rwlock); \
  509. } \
  510. static void __attribute__((destructor)) fini_##rwlock(void) \
  511. { \
  512. ast_rwlock_destroy(&rwlock); \
  513. }
  514. #else
  515. #define __AST_RWLOCK_DEFINE(scope, rwlock, init_val, track) scope ast_rwlock_t rwlock = init_val
  516. #endif
  517. #define AST_RWLOCK_DEFINE_STATIC(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE, 1)
  518. #define AST_RWLOCK_DEFINE_STATIC_NOTRACKING(rwlock) __AST_RWLOCK_DEFINE(static, rwlock, AST_RWLOCK_INIT_VALUE_NOTRACKING, 0)
  519. /*!
  520. * \brief Scoped Locks
  521. *
  522. * Scoped locks provide a way to use RAII locks. In other words,
  523. * declaration of a scoped lock will automatically define and lock
  524. * the lock. When the lock goes out of scope, it will automatically
  525. * be unlocked.
  526. *
  527. * \code
  528. * int some_function(struct ast_channel *chan)
  529. * {
  530. * SCOPED_LOCK(lock, chan, ast_channel_lock, ast_channel_unlock);
  531. *
  532. * if (!strcmp(ast_channel_name(chan, "foo")) {
  533. * return 0;
  534. * }
  535. *
  536. * return -1;
  537. * }
  538. * \endcode
  539. *
  540. * In the above example, neither return path requires explicit unlocking
  541. * of the channel.
  542. *
  543. * \note
  544. * Care should be taken when using SCOPED_LOCKS in conjunction with ao2 objects.
  545. * ao2 objects should be unlocked before they are unreffed. Since SCOPED_LOCK runs
  546. * once the variable goes out of scope, this can easily lead to situations where the
  547. * variable gets unlocked after it is unreffed.
  548. *
  549. * \param varname The unique name to give to the scoped lock. You are not likely to reference
  550. * this outside of the SCOPED_LOCK invocation.
  551. * \param lock The variable to lock. This can be anything that can be passed to a locking
  552. * or unlocking function.
  553. * \param lockfunc The function to call to lock the lock
  554. * \param unlockfunc The function to call to unlock the lock
  555. */
  556. #define SCOPED_LOCK(varname, lock, lockfunc, unlockfunc) \
  557. RAII_VAR(typeof((lock)), varname, ({lockfunc((lock)); (lock); }), unlockfunc)
  558. /*!
  559. * \brief scoped lock specialization for mutexes
  560. */
  561. #define SCOPED_MUTEX(varname, lock) SCOPED_LOCK(varname, (lock), ast_mutex_lock, ast_mutex_unlock)
  562. /*!
  563. * \brief scoped lock specialization for read locks
  564. */
  565. #define SCOPED_RDLOCK(varname, lock) SCOPED_LOCK(varname, (lock), ast_rwlock_rdlock, ast_rwlock_unlock)
  566. /*!
  567. * \brief scoped lock specialization for write locks
  568. */
  569. #define SCOPED_WRLOCK(varname, lock) SCOPED_LOCK(varname, (lock), ast_rwlock_wrlock, ast_rwlock_unlock)
  570. /*!
  571. * \brief scoped lock specialization for ao2 mutexes.
  572. */
  573. #define SCOPED_AO2LOCK(varname, obj) SCOPED_LOCK(varname, (obj), ao2_lock, ao2_unlock)
  574. /*!
  575. * \brief scoped lock specialization for ao2 read locks.
  576. */
  577. #define SCOPED_AO2RDLOCK(varname, obj) SCOPED_LOCK(varname, (obj), ao2_rdlock, ao2_unlock)
  578. /*!
  579. * \brief scoped lock specialization for ao2 write locks.
  580. */
  581. #define SCOPED_AO2WRLOCK(varname, obj) SCOPED_LOCK(varname, (obj), ao2_wrlock, ao2_unlock)
  582. /*!
  583. * \brief scoped lock specialization for channels.
  584. */
  585. #define SCOPED_CHANNELLOCK(varname, chan) SCOPED_LOCK(varname, (chan), ast_channel_lock, ast_channel_unlock)
  586. #ifndef __CYGWIN__ /* temporary disabled for cygwin */
  587. #define pthread_mutex_t use_ast_mutex_t_instead_of_pthread_mutex_t
  588. #define pthread_cond_t use_ast_cond_t_instead_of_pthread_cond_t
  589. #endif
  590. #define pthread_mutex_lock use_ast_mutex_lock_instead_of_pthread_mutex_lock
  591. #define pthread_mutex_unlock use_ast_mutex_unlock_instead_of_pthread_mutex_unlock
  592. #define pthread_mutex_trylock use_ast_mutex_trylock_instead_of_pthread_mutex_trylock
  593. #define pthread_mutex_init use_ast_mutex_init_instead_of_pthread_mutex_init
  594. #define pthread_mutex_destroy use_ast_mutex_destroy_instead_of_pthread_mutex_destroy
  595. #define pthread_cond_init use_ast_cond_init_instead_of_pthread_cond_init
  596. #define pthread_cond_destroy use_ast_cond_destroy_instead_of_pthread_cond_destroy
  597. #define pthread_cond_signal use_ast_cond_signal_instead_of_pthread_cond_signal
  598. #define pthread_cond_broadcast use_ast_cond_broadcast_instead_of_pthread_cond_broadcast
  599. #define pthread_cond_wait use_ast_cond_wait_instead_of_pthread_cond_wait
  600. #define pthread_cond_timedwait use_ast_cond_timedwait_instead_of_pthread_cond_timedwait
  601. #define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__
  602. #define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
  603. #ifndef __linux__
  604. #define pthread_create __use_ast_pthread_create_instead__
  605. #endif
  606. /*
  607. * Support for atomic instructions.
  608. * For platforms that have it, use the native cpu instruction to
  609. * implement them. For other platforms, resort to a 'slow' version
  610. * (defined in utils.c) that protects the atomic instruction with
  611. * a single lock.
  612. * The slow versions is always available, for testing purposes,
  613. * as ast_atomic_fetchadd_int_slow()
  614. */
  615. int ast_atomic_fetchadd_int_slow(volatile int *p, int v);
  616. #include "asterisk/inline_api.h"
  617. #if defined(HAVE_OSX_ATOMICS)
  618. #include "libkern/OSAtomic.h"
  619. #endif
  620. /*! \brief Atomically add v to *p and return * the previous value of *p.
  621. * This can be used to handle reference counts, and the return value
  622. * can be used to generate unique identifiers.
  623. */
  624. #if defined(HAVE_GCC_ATOMICS)
  625. AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
  626. {
  627. return __sync_fetch_and_add(p, v);
  628. })
  629. #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
  630. AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
  631. {
  632. return OSAtomicAdd32(v, (int32_t *) p) - v;
  633. })
  634. #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
  635. AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
  636. {
  637. return OSAtomicAdd64(v, (int64_t *) p) - v;
  638. })
  639. #elif defined (__i386__) || defined(__x86_64__)
  640. #ifdef sun
  641. AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
  642. {
  643. __asm __volatile (
  644. " lock; xaddl %0, %1 ; "
  645. : "+r" (v), /* 0 (result) */
  646. "=m" (*p) /* 1 */
  647. : "m" (*p)); /* 2 */
  648. return (v);
  649. })
  650. #else /* ifndef sun */
  651. AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
  652. {
  653. __asm __volatile (
  654. " lock xaddl %0, %1 ; "
  655. : "+r" (v), /* 0 (result) */
  656. "=m" (*p) /* 1 */
  657. : "m" (*p)); /* 2 */
  658. return (v);
  659. })
  660. #endif
  661. #else /* low performance version in utils.c */
  662. AST_INLINE_API(int ast_atomic_fetchadd_int(volatile int *p, int v),
  663. {
  664. return ast_atomic_fetchadd_int_slow(p, v);
  665. })
  666. #endif
  667. /*! \brief decrement *p by 1 and return true if the variable has reached 0.
  668. * Useful e.g. to check if a refcount has reached 0.
  669. */
  670. #if defined(HAVE_GCC_ATOMICS)
  671. AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
  672. {
  673. return __sync_sub_and_fetch(p, 1) == 0;
  674. })
  675. #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 4)
  676. AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
  677. {
  678. return OSAtomicAdd32( -1, (int32_t *) p) == 0;
  679. })
  680. #elif defined(HAVE_OSX_ATOMICS) && (SIZEOF_INT == 8)
  681. AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
  682. {
  683. return OSAtomicAdd64( -1, (int64_t *) p) == 0;
  684. })
  685. #else
  686. AST_INLINE_API(int ast_atomic_dec_and_test(volatile int *p),
  687. {
  688. int a = ast_atomic_fetchadd_int(p, -1);
  689. return a == 1; /* true if the value is 0 now (so it was 1 previously) */
  690. })
  691. #endif
  692. #endif /* _ASTERISK_LOCK_H */