lock.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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. *
  20. * \brief General Asterisk locking.
  21. */
  22. /*** MODULEINFO
  23. <support_level>core</support_level>
  24. ***/
  25. #include "asterisk.h"
  26. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  27. #include "asterisk/utils.h"
  28. #include "asterisk/lock.h"
  29. /* Allow direct use of pthread_mutex_* / pthread_cond_* */
  30. #undef pthread_mutex_init
  31. #undef pthread_mutex_destroy
  32. #undef pthread_mutex_lock
  33. #undef pthread_mutex_trylock
  34. #undef pthread_mutex_t
  35. #undef pthread_mutex_unlock
  36. #undef pthread_cond_init
  37. #undef pthread_cond_signal
  38. #undef pthread_cond_broadcast
  39. #undef pthread_cond_destroy
  40. #undef pthread_cond_wait
  41. #undef pthread_cond_timedwait
  42. #if defined(DEBUG_THREADS) && defined(HAVE_BKTR)
  43. static void __dump_backtrace(struct ast_bt *bt, int canlog)
  44. {
  45. char **strings;
  46. ssize_t i;
  47. strings = backtrace_symbols(bt->addresses, bt->num_frames);
  48. for (i = 0; i < bt->num_frames; i++) {
  49. __ast_mutex_logger("%s\n", strings[i]);
  50. }
  51. ast_std_free(strings);
  52. }
  53. #endif /* defined(DEBUG_THREADS) && defined(HAVE_BKTR) */
  54. int __ast_pthread_mutex_init(int tracking, const char *filename, int lineno, const char *func,
  55. const char *mutex_name, ast_mutex_t *t)
  56. {
  57. int res;
  58. pthread_mutexattr_t attr;
  59. #ifdef DEBUG_THREADS
  60. t->track = NULL;
  61. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  62. if ((t->mutex) != ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
  63. /*
  64. int canlog = strcmp(filename, "logger.c") & track;
  65. __ast_mutex_logger("%s line %d (%s): NOTICE: mutex '%s' is already initialized.\n",
  66. filename, lineno, func, mutex_name);
  67. DO_THREAD_CRASH;
  68. */
  69. return 0;
  70. }
  71. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  72. if ((t->tracking = tracking)) {
  73. ast_reentrancy_init(&t->track);
  74. }
  75. #endif /* DEBUG_THREADS */
  76. pthread_mutexattr_init(&attr);
  77. pthread_mutexattr_settype(&attr, AST_MUTEX_KIND);
  78. res = pthread_mutex_init(&t->mutex, &attr);
  79. pthread_mutexattr_destroy(&attr);
  80. return res;
  81. }
  82. int __ast_pthread_mutex_destroy(const char *filename, int lineno, const char *func,
  83. const char *mutex_name, ast_mutex_t *t)
  84. {
  85. int res;
  86. #ifdef DEBUG_THREADS
  87. struct ast_lock_track *lt;
  88. int canlog = strcmp(filename, "logger.c") & t->tracking;
  89. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  90. if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
  91. /* Don't try to uninitialize non initialized mutex
  92. * This may no effect on linux
  93. * And always ganerate core on *BSD with
  94. * linked libpthread
  95. * This not error condition if the mutex created on the fly.
  96. */
  97. __ast_mutex_logger("%s line %d (%s): NOTICE: mutex '%s' is uninitialized.\n",
  98. filename, lineno, func, mutex_name);
  99. return 0;
  100. }
  101. #endif
  102. if (t->tracking && !t->track) {
  103. ast_reentrancy_init(&t->track);
  104. }
  105. lt = t->track;
  106. res = pthread_mutex_trylock(&t->mutex);
  107. switch (res) {
  108. case 0:
  109. pthread_mutex_unlock(&t->mutex);
  110. break;
  111. case EINVAL:
  112. __ast_mutex_logger("%s line %d (%s): Error: attempt to destroy invalid mutex '%s'.\n",
  113. filename, lineno, func, mutex_name);
  114. break;
  115. case EBUSY:
  116. __ast_mutex_logger("%s line %d (%s): Error: attempt to destroy locked mutex '%s'.\n",
  117. filename, lineno, func, mutex_name);
  118. if (t->tracking) {
  119. ast_reentrancy_lock(lt);
  120. __ast_mutex_logger("%s line %d (%s): Error: '%s' was locked here.\n",
  121. lt->file[ROFFSET], lt->lineno[ROFFSET], lt->func[ROFFSET], mutex_name);
  122. #ifdef HAVE_BKTR
  123. __dump_backtrace(&lt->backtrace[ROFFSET], canlog);
  124. #endif
  125. ast_reentrancy_unlock(lt);
  126. }
  127. break;
  128. }
  129. #endif /* DEBUG_THREADS */
  130. res = pthread_mutex_destroy(&t->mutex);
  131. #ifdef DEBUG_THREADS
  132. if (res) {
  133. __ast_mutex_logger("%s line %d (%s): Error destroying mutex %s: %s\n",
  134. filename, lineno, func, mutex_name, strerror(res));
  135. }
  136. if (t->tracking) {
  137. ast_reentrancy_lock(lt);
  138. lt->file[0] = filename;
  139. lt->lineno[0] = lineno;
  140. lt->func[0] = func;
  141. lt->reentrancy = 0;
  142. lt->thread[0] = 0;
  143. #ifdef HAVE_BKTR
  144. memset(&lt->backtrace[0], 0, sizeof(lt->backtrace[0]));
  145. #endif
  146. ast_reentrancy_unlock(lt);
  147. delete_reentrancy_cs(&t->track);
  148. }
  149. #endif /* DEBUG_THREADS */
  150. return res;
  151. }
  152. int __ast_pthread_mutex_lock(const char *filename, int lineno, const char *func,
  153. const char* mutex_name, ast_mutex_t *t)
  154. {
  155. int res;
  156. #ifdef DEBUG_THREADS
  157. struct ast_lock_track *lt;
  158. int canlog = strcmp(filename, "logger.c") & t->tracking;
  159. #ifdef HAVE_BKTR
  160. struct ast_bt *bt = NULL;
  161. #endif
  162. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  163. if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
  164. /* Don't warn abount uninitialized mutex.
  165. * Simple try to initialize it.
  166. * May be not needed in linux system.
  167. */
  168. res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
  169. if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
  170. __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
  171. filename, lineno, func, mutex_name);
  172. return res;
  173. }
  174. }
  175. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  176. if (t->tracking && !t->track) {
  177. ast_reentrancy_init(&t->track);
  178. }
  179. lt = t->track;
  180. if (t->tracking) {
  181. #ifdef HAVE_BKTR
  182. struct ast_bt tmp;
  183. /* The implementation of backtrace() may have its own locks.
  184. * Capture the backtrace outside of the reentrancy lock to
  185. * avoid deadlocks. See ASTERISK-22455. */
  186. ast_bt_get_addresses(&tmp);
  187. ast_reentrancy_lock(lt);
  188. if (lt->reentrancy != AST_MAX_REENTRANCY) {
  189. lt->backtrace[lt->reentrancy] = tmp;
  190. bt = &lt->backtrace[lt->reentrancy];
  191. }
  192. ast_reentrancy_unlock(lt);
  193. ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t, bt);
  194. #else
  195. ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t);
  196. #endif
  197. }
  198. #endif /* DEBUG_THREADS */
  199. #if defined(DETECT_DEADLOCKS) && defined(DEBUG_THREADS)
  200. {
  201. time_t seconds = time(NULL);
  202. time_t wait_time, reported_wait = 0;
  203. do {
  204. #ifdef HAVE_MTX_PROFILE
  205. ast_mark(mtx_prof, 1);
  206. #endif
  207. res = pthread_mutex_trylock(&t->mutex);
  208. #ifdef HAVE_MTX_PROFILE
  209. ast_mark(mtx_prof, 0);
  210. #endif
  211. if (res == EBUSY) {
  212. wait_time = time(NULL) - seconds;
  213. if (wait_time > reported_wait && (wait_time % 5) == 0) {
  214. __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for mutex '%s'?\n",
  215. filename, lineno, func, (int) wait_time, mutex_name);
  216. ast_reentrancy_lock(lt);
  217. #ifdef HAVE_BKTR
  218. __dump_backtrace(&lt->backtrace[lt->reentrancy], canlog);
  219. #endif
  220. __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
  221. lt->file[ROFFSET], lt->lineno[ROFFSET],
  222. lt->func[ROFFSET], mutex_name);
  223. #ifdef HAVE_BKTR
  224. __dump_backtrace(&lt->backtrace[ROFFSET], canlog);
  225. #endif
  226. ast_reentrancy_unlock(lt);
  227. reported_wait = wait_time;
  228. }
  229. usleep(200);
  230. }
  231. } while (res == EBUSY);
  232. }
  233. #else /* !DETECT_DEADLOCKS || !DEBUG_THREADS */
  234. #ifdef HAVE_MTX_PROFILE
  235. ast_mark(mtx_prof, 1);
  236. res = pthread_mutex_trylock(&t->mutex);
  237. ast_mark(mtx_prof, 0);
  238. if (res)
  239. #endif
  240. res = pthread_mutex_lock(&t->mutex);
  241. #endif /* !DETECT_DEADLOCKS || !DEBUG_THREADS */
  242. #ifdef DEBUG_THREADS
  243. if (t->tracking && !res) {
  244. ast_reentrancy_lock(lt);
  245. if (lt->reentrancy < AST_MAX_REENTRANCY) {
  246. lt->file[lt->reentrancy] = filename;
  247. lt->lineno[lt->reentrancy] = lineno;
  248. lt->func[lt->reentrancy] = func;
  249. lt->thread[lt->reentrancy] = pthread_self();
  250. lt->reentrancy++;
  251. } else {
  252. __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
  253. filename, lineno, func, mutex_name);
  254. }
  255. ast_reentrancy_unlock(lt);
  256. if (t->tracking) {
  257. ast_mark_lock_acquired(t);
  258. }
  259. } else if (t->tracking) {
  260. #ifdef HAVE_BKTR
  261. if (lt->reentrancy) {
  262. ast_reentrancy_lock(lt);
  263. bt = &lt->backtrace[lt->reentrancy-1];
  264. ast_reentrancy_unlock(lt);
  265. } else {
  266. bt = NULL;
  267. }
  268. ast_remove_lock_info(t, bt);
  269. #else
  270. ast_remove_lock_info(t);
  271. #endif
  272. }
  273. if (res) {
  274. __ast_mutex_logger("%s line %d (%s): Error obtaining mutex: %s\n",
  275. filename, lineno, func, strerror(res));
  276. DO_THREAD_CRASH;
  277. }
  278. #endif /* DEBUG_THREADS */
  279. return res;
  280. }
  281. int __ast_pthread_mutex_trylock(const char *filename, int lineno, const char *func,
  282. const char* mutex_name, ast_mutex_t *t)
  283. {
  284. int res;
  285. #ifdef DEBUG_THREADS
  286. struct ast_lock_track *lt;
  287. int canlog = strcmp(filename, "logger.c") & t->tracking;
  288. #ifdef HAVE_BKTR
  289. struct ast_bt *bt = NULL;
  290. #endif
  291. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  292. if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
  293. /* Don't warn abount uninitialized mutex.
  294. * Simple try to initialize it.
  295. * May be not needed in linux system.
  296. */
  297. res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
  298. if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
  299. __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
  300. filename, lineno, func, mutex_name);
  301. return res;
  302. }
  303. }
  304. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  305. if (t->tracking && !t->track) {
  306. ast_reentrancy_init(&t->track);
  307. }
  308. lt = t->track;
  309. if (t->tracking) {
  310. #ifdef HAVE_BKTR
  311. struct ast_bt tmp;
  312. /* The implementation of backtrace() may have its own locks.
  313. * Capture the backtrace outside of the reentrancy lock to
  314. * avoid deadlocks. See ASTERISK-22455. */
  315. ast_bt_get_addresses(&tmp);
  316. ast_reentrancy_lock(lt);
  317. if (lt->reentrancy != AST_MAX_REENTRANCY) {
  318. lt->backtrace[lt->reentrancy] = tmp;
  319. bt = &lt->backtrace[lt->reentrancy];
  320. }
  321. ast_reentrancy_unlock(lt);
  322. ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t, bt);
  323. #else
  324. ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t);
  325. #endif
  326. }
  327. #endif /* DEBUG_THREADS */
  328. res = pthread_mutex_trylock(&t->mutex);
  329. #ifdef DEBUG_THREADS
  330. if (t->tracking && !res) {
  331. ast_reentrancy_lock(lt);
  332. if (lt->reentrancy < AST_MAX_REENTRANCY) {
  333. lt->file[lt->reentrancy] = filename;
  334. lt->lineno[lt->reentrancy] = lineno;
  335. lt->func[lt->reentrancy] = func;
  336. lt->thread[lt->reentrancy] = pthread_self();
  337. lt->reentrancy++;
  338. } else {
  339. __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
  340. filename, lineno, func, mutex_name);
  341. }
  342. ast_reentrancy_unlock(lt);
  343. if (t->tracking) {
  344. ast_mark_lock_acquired(t);
  345. }
  346. } else if (t->tracking) {
  347. ast_mark_lock_failed(t);
  348. }
  349. #endif /* DEBUG_THREADS */
  350. return res;
  351. }
  352. int __ast_pthread_mutex_unlock(const char *filename, int lineno, const char *func,
  353. const char *mutex_name, ast_mutex_t *t)
  354. {
  355. int res;
  356. #ifdef DEBUG_THREADS
  357. struct ast_lock_track *lt;
  358. int canlog = strcmp(filename, "logger.c") & t->tracking;
  359. #ifdef HAVE_BKTR
  360. struct ast_bt *bt = NULL;
  361. #endif
  362. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  363. if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
  364. __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
  365. filename, lineno, func, mutex_name);
  366. res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
  367. if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
  368. __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
  369. filename, lineno, func, mutex_name);
  370. }
  371. return res;
  372. }
  373. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  374. if (t->tracking && !t->track) {
  375. ast_reentrancy_init(&t->track);
  376. }
  377. lt = t->track;
  378. if (t->tracking) {
  379. ast_reentrancy_lock(lt);
  380. if (lt->reentrancy && (lt->thread[ROFFSET] != pthread_self())) {
  381. __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
  382. filename, lineno, func, mutex_name);
  383. __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
  384. lt->file[ROFFSET], lt->lineno[ROFFSET], lt->func[ROFFSET], mutex_name);
  385. #ifdef HAVE_BKTR
  386. __dump_backtrace(&lt->backtrace[ROFFSET], canlog);
  387. #endif
  388. DO_THREAD_CRASH;
  389. }
  390. if (--lt->reentrancy < 0) {
  391. __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
  392. filename, lineno, func, mutex_name);
  393. lt->reentrancy = 0;
  394. }
  395. if (lt->reentrancy < AST_MAX_REENTRANCY) {
  396. lt->file[lt->reentrancy] = NULL;
  397. lt->lineno[lt->reentrancy] = 0;
  398. lt->func[lt->reentrancy] = NULL;
  399. lt->thread[lt->reentrancy] = 0;
  400. }
  401. #ifdef HAVE_BKTR
  402. if (lt->reentrancy) {
  403. bt = &lt->backtrace[lt->reentrancy - 1];
  404. }
  405. #endif
  406. ast_reentrancy_unlock(lt);
  407. #ifdef HAVE_BKTR
  408. ast_remove_lock_info(t, bt);
  409. #else
  410. ast_remove_lock_info(t);
  411. #endif
  412. }
  413. #endif /* DEBUG_THREADS */
  414. res = pthread_mutex_unlock(&t->mutex);
  415. #ifdef DEBUG_THREADS
  416. if (res) {
  417. __ast_mutex_logger("%s line %d (%s): Error releasing mutex: %s\n",
  418. filename, lineno, func, strerror(res));
  419. DO_THREAD_CRASH;
  420. }
  421. #endif /* DEBUG_THREADS */
  422. return res;
  423. }
  424. int __ast_cond_init(const char *filename, int lineno, const char *func,
  425. const char *cond_name, ast_cond_t *cond, pthread_condattr_t *cond_attr)
  426. {
  427. return pthread_cond_init(cond, cond_attr);
  428. }
  429. int __ast_cond_signal(const char *filename, int lineno, const char *func,
  430. const char *cond_name, ast_cond_t *cond)
  431. {
  432. return pthread_cond_signal(cond);
  433. }
  434. int __ast_cond_broadcast(const char *filename, int lineno, const char *func,
  435. const char *cond_name, ast_cond_t *cond)
  436. {
  437. return pthread_cond_broadcast(cond);
  438. }
  439. int __ast_cond_destroy(const char *filename, int lineno, const char *func,
  440. const char *cond_name, ast_cond_t *cond)
  441. {
  442. return pthread_cond_destroy(cond);
  443. }
  444. int __ast_cond_wait(const char *filename, int lineno, const char *func,
  445. const char *cond_name, const char *mutex_name,
  446. ast_cond_t *cond, ast_mutex_t *t)
  447. {
  448. int res;
  449. #ifdef DEBUG_THREADS
  450. struct ast_lock_track *lt;
  451. struct ast_lock_track lt_orig;
  452. int canlog = strcmp(filename, "logger.c") & t->tracking;
  453. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  454. if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
  455. __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
  456. filename, lineno, func, mutex_name);
  457. res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
  458. if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
  459. __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
  460. filename, lineno, func, mutex_name);
  461. }
  462. return res;
  463. }
  464. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  465. if (t->tracking && !t->track) {
  466. ast_reentrancy_init(&t->track);
  467. }
  468. lt = t->track;
  469. if (t->tracking) {
  470. ast_reentrancy_lock(lt);
  471. if (lt->reentrancy && (lt->thread[ROFFSET] != pthread_self())) {
  472. __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
  473. filename, lineno, func, mutex_name);
  474. __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
  475. lt->file[ROFFSET], lt->lineno[ROFFSET], lt->func[ROFFSET], mutex_name);
  476. #ifdef HAVE_BKTR
  477. __dump_backtrace(&lt->backtrace[ROFFSET], canlog);
  478. #endif
  479. DO_THREAD_CRASH;
  480. } else if (lt->reentrancy <= 0) {
  481. __ast_mutex_logger("%s line %d (%s): attempted to wait on an unlocked mutex '%s'\n",
  482. filename, lineno, func, mutex_name);
  483. DO_THREAD_CRASH;
  484. }
  485. /* Waiting on a condition completely suspends a recursive mutex,
  486. * even if it's been recursively locked multiple times. Make a
  487. * copy of the lock tracking, and reset reentrancy to zero */
  488. lt_orig = *lt;
  489. lt->reentrancy = 0;
  490. ast_reentrancy_unlock(lt);
  491. ast_suspend_lock_info(t);
  492. }
  493. #endif /* DEBUG_THREADS */
  494. res = pthread_cond_wait(cond, &t->mutex);
  495. #ifdef DEBUG_THREADS
  496. if (res) {
  497. __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n",
  498. filename, lineno, func, strerror(res));
  499. DO_THREAD_CRASH;
  500. } else if (t->tracking) {
  501. pthread_mutex_t reentr_mutex_orig;
  502. ast_reentrancy_lock(lt);
  503. /* Restore lock tracking to what it was prior to the wait */
  504. reentr_mutex_orig = lt->reentr_mutex;
  505. *lt = lt_orig;
  506. /* un-trash the mutex we just copied over */
  507. lt->reentr_mutex = reentr_mutex_orig;
  508. ast_reentrancy_unlock(lt);
  509. ast_restore_lock_info(t);
  510. }
  511. #endif /* DEBUG_THREADS */
  512. return res;
  513. }
  514. int __ast_cond_timedwait(const char *filename, int lineno, const char *func,
  515. const char *cond_name, const char *mutex_name, ast_cond_t *cond,
  516. ast_mutex_t *t, const struct timespec *abstime)
  517. {
  518. int res;
  519. #ifdef DEBUG_THREADS
  520. struct ast_lock_track *lt;
  521. struct ast_lock_track lt_orig;
  522. int canlog = strcmp(filename, "logger.c") & t->tracking;
  523. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  524. if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
  525. __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
  526. filename, lineno, func, mutex_name);
  527. res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
  528. if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
  529. __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
  530. filename, lineno, func, mutex_name);
  531. }
  532. return res;
  533. }
  534. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  535. if (t->tracking && !t->track) {
  536. ast_reentrancy_init(&t->track);
  537. }
  538. lt = t->track;
  539. if (t->tracking) {
  540. ast_reentrancy_lock(lt);
  541. if (lt->reentrancy && (lt->thread[ROFFSET] != pthread_self())) {
  542. __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
  543. filename, lineno, func, mutex_name);
  544. __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
  545. lt->file[ROFFSET], lt->lineno[ROFFSET], lt->func[ROFFSET], mutex_name);
  546. #ifdef HAVE_BKTR
  547. __dump_backtrace(&lt->backtrace[ROFFSET], canlog);
  548. #endif
  549. DO_THREAD_CRASH;
  550. } else if (lt->reentrancy <= 0) {
  551. __ast_mutex_logger("%s line %d (%s): attempted to wait on an unlocked mutex '%s'\n",
  552. filename, lineno, func, mutex_name);
  553. DO_THREAD_CRASH;
  554. }
  555. /* Waiting on a condition completely suspends a recursive mutex,
  556. * even if it's been recursively locked multiple times. Make a
  557. * copy of the lock tracking, and reset reentrancy to zero */
  558. lt_orig = *lt;
  559. lt->reentrancy = 0;
  560. ast_reentrancy_unlock(lt);
  561. ast_suspend_lock_info(t);
  562. }
  563. #endif /* DEBUG_THREADS */
  564. res = pthread_cond_timedwait(cond, &t->mutex, abstime);
  565. #ifdef DEBUG_THREADS
  566. if (res && (res != ETIMEDOUT)) {
  567. __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n",
  568. filename, lineno, func, strerror(res));
  569. DO_THREAD_CRASH;
  570. } else if (t->tracking) {
  571. pthread_mutex_t reentr_mutex_orig;
  572. ast_reentrancy_lock(lt);
  573. /* Restore lock tracking to what it was prior to the wait */
  574. reentr_mutex_orig = lt->reentr_mutex;
  575. *lt = lt_orig;
  576. /* un-trash the mutex we just copied over */
  577. lt->reentr_mutex = reentr_mutex_orig;
  578. ast_reentrancy_unlock(lt);
  579. ast_suspend_lock_info(t);
  580. }
  581. #endif /* DEBUG_THREADS */
  582. return res;
  583. }
  584. int __ast_rwlock_init(int tracking, const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
  585. {
  586. int res;
  587. pthread_rwlockattr_t attr;
  588. #ifdef DEBUG_THREADS
  589. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  590. int canlog = strcmp(filename, "logger.c") & t->tracking;
  591. if (t->lock != ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  592. __ast_mutex_logger("%s line %d (%s): Warning: rwlock '%s' is already initialized.\n",
  593. filename, lineno, func, rwlock_name);
  594. return 0;
  595. }
  596. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  597. if ((t->tracking = tracking)) {
  598. ast_reentrancy_init(&t->track);
  599. }
  600. #endif /* DEBUG_THREADS */
  601. pthread_rwlockattr_init(&attr);
  602. #ifdef HAVE_PTHREAD_RWLOCK_PREFER_WRITER_NP
  603. pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP);
  604. #endif
  605. res = pthread_rwlock_init(&t->lock, &attr);
  606. pthread_rwlockattr_destroy(&attr);
  607. return res;
  608. }
  609. int __ast_rwlock_destroy(const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
  610. {
  611. int res;
  612. #ifdef DEBUG_THREADS
  613. struct ast_lock_track *lt = t->track;
  614. int canlog = strcmp(filename, "logger.c") & t->tracking;
  615. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  616. if (t->lock == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  617. __ast_mutex_logger("%s line %d (%s): Warning: rwlock '%s' is uninitialized.\n",
  618. filename, lineno, func, rwlock_name);
  619. return 0;
  620. }
  621. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  622. #endif /* DEBUG_THREADS */
  623. res = pthread_rwlock_destroy(&t->lock);
  624. #ifdef DEBUG_THREADS
  625. if (res) {
  626. __ast_mutex_logger("%s line %d (%s): Error destroying rwlock %s: %s\n",
  627. filename, lineno, func, rwlock_name, strerror(res));
  628. }
  629. if (t->tracking && lt) {
  630. ast_reentrancy_lock(lt);
  631. lt->file[0] = filename;
  632. lt->lineno[0] = lineno;
  633. lt->func[0] = func;
  634. lt->reentrancy = 0;
  635. lt->thread[0] = 0;
  636. #ifdef HAVE_BKTR
  637. memset(&lt->backtrace[0], 0, sizeof(lt->backtrace[0]));
  638. #endif
  639. ast_reentrancy_unlock(lt);
  640. delete_reentrancy_cs(&t->track);
  641. }
  642. #endif /* DEBUG_THREADS */
  643. return res;
  644. }
  645. int __ast_rwlock_unlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name)
  646. {
  647. int res;
  648. #ifdef DEBUG_THREADS
  649. struct ast_lock_track *lt;
  650. int canlog = strcmp(filename, "logger.c") & t->tracking;
  651. #ifdef HAVE_BKTR
  652. struct ast_bt *bt = NULL;
  653. #endif
  654. int lock_found = 0;
  655. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  656. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  657. __ast_mutex_logger("%s line %d (%s): Warning: rwlock '%s' is uninitialized.\n",
  658. filename, line, func, name);
  659. res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
  660. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  661. __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
  662. filename, line, func, name);
  663. }
  664. return res;
  665. }
  666. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  667. if (t->tracking && !t->track) {
  668. ast_reentrancy_init(&t->track);
  669. }
  670. lt = t->track;
  671. if (t->tracking) {
  672. ast_reentrancy_lock(lt);
  673. if (lt->reentrancy) {
  674. int i;
  675. pthread_t self = pthread_self();
  676. for (i = lt->reentrancy - 1; i >= 0; --i) {
  677. if (lt->thread[i] == self) {
  678. lock_found = 1;
  679. if (i != lt->reentrancy - 1) {
  680. lt->file[i] = lt->file[lt->reentrancy - 1];
  681. lt->lineno[i] = lt->lineno[lt->reentrancy - 1];
  682. lt->func[i] = lt->func[lt->reentrancy - 1];
  683. lt->thread[i] = lt->thread[lt->reentrancy - 1];
  684. }
  685. #ifdef HAVE_BKTR
  686. bt = &lt->backtrace[i];
  687. #endif
  688. lt->file[lt->reentrancy - 1] = NULL;
  689. lt->lineno[lt->reentrancy - 1] = 0;
  690. lt->func[lt->reentrancy - 1] = NULL;
  691. lt->thread[lt->reentrancy - 1] = AST_PTHREADT_NULL;
  692. break;
  693. }
  694. }
  695. }
  696. if (lock_found && --lt->reentrancy < 0) {
  697. __ast_mutex_logger("%s line %d (%s): rwlock '%s' freed more times than we've locked!\n",
  698. filename, line, func, name);
  699. lt->reentrancy = 0;
  700. }
  701. ast_reentrancy_unlock(lt);
  702. #ifdef HAVE_BKTR
  703. ast_remove_lock_info(t, bt);
  704. #else
  705. ast_remove_lock_info(t);
  706. #endif
  707. }
  708. #endif /* DEBUG_THREADS */
  709. res = pthread_rwlock_unlock(&t->lock);
  710. #ifdef DEBUG_THREADS
  711. if (res) {
  712. __ast_mutex_logger("%s line %d (%s): Error releasing rwlock: %s\n",
  713. filename, line, func, strerror(res));
  714. DO_THREAD_CRASH;
  715. }
  716. #endif /* DEBUG_THREADS */
  717. return res;
  718. }
  719. int __ast_rwlock_rdlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name)
  720. {
  721. int res;
  722. #ifdef DEBUG_THREADS
  723. struct ast_lock_track *lt;
  724. int canlog = strcmp(filename, "logger.c") & t->tracking;
  725. #ifdef HAVE_BKTR
  726. struct ast_bt *bt = NULL;
  727. #endif
  728. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  729. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  730. /* Don't warn abount uninitialized lock.
  731. * Simple try to initialize it.
  732. * May be not needed in linux system.
  733. */
  734. res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
  735. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  736. __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
  737. filename, line, func, name);
  738. return res;
  739. }
  740. }
  741. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  742. if (t->tracking && !t->track) {
  743. ast_reentrancy_init(&t->track);
  744. }
  745. lt = t->track;
  746. if (t->tracking) {
  747. #ifdef HAVE_BKTR
  748. struct ast_bt tmp;
  749. /* The implementation of backtrace() may have its own locks.
  750. * Capture the backtrace outside of the reentrancy lock to
  751. * avoid deadlocks. See ASTERISK-22455. */
  752. ast_bt_get_addresses(&tmp);
  753. ast_reentrancy_lock(lt);
  754. if (lt->reentrancy != AST_MAX_REENTRANCY) {
  755. lt->backtrace[lt->reentrancy] = tmp;
  756. bt = &lt->backtrace[lt->reentrancy];
  757. }
  758. ast_reentrancy_unlock(lt);
  759. ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t, bt);
  760. #else
  761. ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t);
  762. #endif
  763. }
  764. #endif /* DEBUG_THREADS */
  765. #if defined(DETECT_DEADLOCKS) && defined(DEBUG_THREADS)
  766. {
  767. time_t seconds = time(NULL);
  768. time_t wait_time, reported_wait = 0;
  769. do {
  770. res = pthread_rwlock_tryrdlock(&t->lock);
  771. if (res == EBUSY) {
  772. wait_time = time(NULL) - seconds;
  773. if (wait_time > reported_wait && (wait_time % 5) == 0) {
  774. __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for readlock '%s'?\n",
  775. filename, line, func, (int)wait_time, name);
  776. if (t->tracking) {
  777. ast_reentrancy_lock(lt);
  778. #ifdef HAVE_BKTR
  779. __dump_backtrace(&lt->backtrace[lt->reentrancy], canlog);
  780. #endif
  781. __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
  782. lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1],
  783. lt->func[lt->reentrancy-1], name);
  784. #ifdef HAVE_BKTR
  785. __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
  786. #endif
  787. ast_reentrancy_unlock(lt);
  788. }
  789. reported_wait = wait_time;
  790. }
  791. usleep(200);
  792. }
  793. } while (res == EBUSY);
  794. }
  795. #else /* !DETECT_DEADLOCKS || !DEBUG_THREADS */
  796. res = pthread_rwlock_rdlock(&t->lock);
  797. #endif /* !DETECT_DEADLOCKS || !DEBUG_THREADS */
  798. #ifdef DEBUG_THREADS
  799. if (!res && t->tracking) {
  800. ast_reentrancy_lock(lt);
  801. if (lt->reentrancy < AST_MAX_REENTRANCY) {
  802. lt->file[lt->reentrancy] = filename;
  803. lt->lineno[lt->reentrancy] = line;
  804. lt->func[lt->reentrancy] = func;
  805. lt->thread[lt->reentrancy] = pthread_self();
  806. lt->reentrancy++;
  807. }
  808. ast_reentrancy_unlock(lt);
  809. if (t->tracking) {
  810. ast_mark_lock_acquired(t);
  811. }
  812. } else if (t->tracking) {
  813. #ifdef HAVE_BKTR
  814. if (lt->reentrancy) {
  815. ast_reentrancy_lock(lt);
  816. bt = &lt->backtrace[lt->reentrancy-1];
  817. ast_reentrancy_unlock(lt);
  818. } else {
  819. bt = NULL;
  820. }
  821. ast_remove_lock_info(t, bt);
  822. #else
  823. ast_remove_lock_info(t);
  824. #endif
  825. }
  826. if (res) {
  827. __ast_mutex_logger("%s line %d (%s): Error obtaining read lock: %s\n",
  828. filename, line, func, strerror(res));
  829. DO_THREAD_CRASH;
  830. }
  831. #endif /* DEBUG_THREADS */
  832. return res;
  833. }
  834. int __ast_rwlock_wrlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name)
  835. {
  836. int res;
  837. #ifdef DEBUG_THREADS
  838. struct ast_lock_track *lt;
  839. int canlog = strcmp(filename, "logger.c") & t->tracking;
  840. #ifdef HAVE_BKTR
  841. struct ast_bt *bt = NULL;
  842. #endif
  843. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  844. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  845. /* Don't warn abount uninitialized lock.
  846. * Simple try to initialize it.
  847. * May be not needed in linux system.
  848. */
  849. res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
  850. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  851. __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
  852. filename, line, func, name);
  853. return res;
  854. }
  855. }
  856. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  857. if (t->tracking && !t->track) {
  858. ast_reentrancy_init(&t->track);
  859. }
  860. lt = t->track;
  861. if (t->tracking) {
  862. #ifdef HAVE_BKTR
  863. struct ast_bt tmp;
  864. /* The implementation of backtrace() may have its own locks.
  865. * Capture the backtrace outside of the reentrancy lock to
  866. * avoid deadlocks. See ASTERISK-22455. */
  867. ast_bt_get_addresses(&tmp);
  868. ast_reentrancy_lock(lt);
  869. if (lt->reentrancy != AST_MAX_REENTRANCY) {
  870. lt->backtrace[lt->reentrancy] = tmp;
  871. bt = &lt->backtrace[lt->reentrancy];
  872. }
  873. ast_reentrancy_unlock(lt);
  874. ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
  875. #else
  876. ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
  877. #endif
  878. }
  879. #endif /* DEBUG_THREADS */
  880. #if defined(DETECT_DEADLOCKS) && defined(DEBUG_THREADS)
  881. {
  882. time_t seconds = time(NULL);
  883. time_t wait_time, reported_wait = 0;
  884. do {
  885. res = pthread_rwlock_trywrlock(&t->lock);
  886. if (res == EBUSY) {
  887. wait_time = time(NULL) - seconds;
  888. if (wait_time > reported_wait && (wait_time % 5) == 0) {
  889. __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for writelock '%s'?\n",
  890. filename, line, func, (int)wait_time, name);
  891. if (t->tracking) {
  892. ast_reentrancy_lock(lt);
  893. #ifdef HAVE_BKTR
  894. __dump_backtrace(&lt->backtrace[lt->reentrancy], canlog);
  895. #endif
  896. __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
  897. lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1],
  898. lt->func[lt->reentrancy-1], name);
  899. #ifdef HAVE_BKTR
  900. __dump_backtrace(&lt->backtrace[lt->reentrancy-1], canlog);
  901. #endif
  902. ast_reentrancy_unlock(lt);
  903. }
  904. reported_wait = wait_time;
  905. }
  906. usleep(200);
  907. }
  908. } while (res == EBUSY);
  909. }
  910. #else /* !DETECT_DEADLOCKS || !DEBUG_THREADS */
  911. res = pthread_rwlock_wrlock(&t->lock);
  912. #endif /* !DETECT_DEADLOCKS || !DEBUG_THREADS */
  913. #ifdef DEBUG_THREADS
  914. if (!res && t->tracking) {
  915. ast_reentrancy_lock(lt);
  916. if (lt->reentrancy < AST_MAX_REENTRANCY) {
  917. lt->file[lt->reentrancy] = filename;
  918. lt->lineno[lt->reentrancy] = line;
  919. lt->func[lt->reentrancy] = func;
  920. lt->thread[lt->reentrancy] = pthread_self();
  921. lt->reentrancy++;
  922. }
  923. ast_reentrancy_unlock(lt);
  924. if (t->tracking) {
  925. ast_mark_lock_acquired(t);
  926. }
  927. } else if (t->tracking) {
  928. #ifdef HAVE_BKTR
  929. if (lt->reentrancy) {
  930. ast_reentrancy_lock(lt);
  931. bt = &lt->backtrace[lt->reentrancy-1];
  932. ast_reentrancy_unlock(lt);
  933. } else {
  934. bt = NULL;
  935. }
  936. if (t->tracking) {
  937. ast_remove_lock_info(t, bt);
  938. }
  939. #else
  940. if (t->tracking) {
  941. ast_remove_lock_info(t);
  942. }
  943. #endif
  944. }
  945. if (res) {
  946. __ast_mutex_logger("%s line %d (%s): Error obtaining write lock: %s\n",
  947. filename, line, func, strerror(res));
  948. DO_THREAD_CRASH;
  949. }
  950. #endif /* DEBUG_THREADS */
  951. return res;
  952. }
  953. int __ast_rwlock_timedrdlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name,
  954. const struct timespec *abs_timeout)
  955. {
  956. int res;
  957. #ifdef DEBUG_THREADS
  958. struct ast_lock_track *lt;
  959. int canlog = strcmp(filename, "logger.c") & t->tracking;
  960. #ifdef HAVE_BKTR
  961. struct ast_bt *bt = NULL;
  962. #endif
  963. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  964. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  965. /* Don't warn abount uninitialized lock.
  966. * Simple try to initialize it.
  967. * May be not needed in linux system.
  968. */
  969. res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
  970. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  971. __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
  972. filename, line, func, name);
  973. return res;
  974. }
  975. }
  976. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  977. if (t->tracking && !t->track) {
  978. ast_reentrancy_init(&t->track);
  979. }
  980. lt = t->track;
  981. if (t->tracking) {
  982. #ifdef HAVE_BKTR
  983. struct ast_bt tmp;
  984. /* The implementation of backtrace() may have its own locks.
  985. * Capture the backtrace outside of the reentrancy lock to
  986. * avoid deadlocks. See ASTERISK-22455. */
  987. ast_bt_get_addresses(&tmp);
  988. ast_reentrancy_lock(lt);
  989. if (lt->reentrancy != AST_MAX_REENTRANCY) {
  990. lt->backtrace[lt->reentrancy] = tmp;
  991. bt = &lt->backtrace[lt->reentrancy];
  992. }
  993. ast_reentrancy_unlock(lt);
  994. ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
  995. #else
  996. ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
  997. #endif
  998. }
  999. #endif /* DEBUG_THREADS */
  1000. #ifdef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
  1001. res = pthread_rwlock_timedrdlock(&t->lock, abs_timeout);
  1002. #else
  1003. do {
  1004. struct timeval _now;
  1005. for (;;) {
  1006. if (!(res = pthread_rwlock_tryrdlock(&t->lock))) {
  1007. break;
  1008. }
  1009. _now = ast_tvnow();
  1010. if (_now.tv_sec > abs_timeout->tv_sec || (_now.tv_sec == abs_timeout->tv_sec && _now.tv_usec * 1000 > abs_timeout->tv_nsec)) {
  1011. break;
  1012. }
  1013. usleep(1);
  1014. }
  1015. } while (0);
  1016. #endif
  1017. #ifdef DEBUG_THREADS
  1018. if (!res && t->tracking) {
  1019. ast_reentrancy_lock(lt);
  1020. if (lt->reentrancy < AST_MAX_REENTRANCY) {
  1021. lt->file[lt->reentrancy] = filename;
  1022. lt->lineno[lt->reentrancy] = line;
  1023. lt->func[lt->reentrancy] = func;
  1024. lt->thread[lt->reentrancy] = pthread_self();
  1025. lt->reentrancy++;
  1026. }
  1027. ast_reentrancy_unlock(lt);
  1028. if (t->tracking) {
  1029. ast_mark_lock_acquired(t);
  1030. }
  1031. } else if (t->tracking) {
  1032. #ifdef HAVE_BKTR
  1033. if (lt->reentrancy) {
  1034. ast_reentrancy_lock(lt);
  1035. bt = &lt->backtrace[lt->reentrancy-1];
  1036. ast_reentrancy_unlock(lt);
  1037. } else {
  1038. bt = NULL;
  1039. }
  1040. ast_remove_lock_info(t, bt);
  1041. #else
  1042. ast_remove_lock_info(t);
  1043. #endif
  1044. }
  1045. if (res) {
  1046. __ast_mutex_logger("%s line %d (%s): Error obtaining read lock: %s\n",
  1047. filename, line, func, strerror(res));
  1048. DO_THREAD_CRASH;
  1049. }
  1050. #endif /* DEBUG_THREADS */
  1051. return res;
  1052. }
  1053. int __ast_rwlock_timedwrlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name,
  1054. const struct timespec *abs_timeout)
  1055. {
  1056. int res;
  1057. #ifdef DEBUG_THREADS
  1058. struct ast_lock_track *lt;
  1059. int canlog = strcmp(filename, "logger.c") & t->tracking;
  1060. #ifdef HAVE_BKTR
  1061. struct ast_bt *bt = NULL;
  1062. #endif
  1063. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  1064. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  1065. /* Don't warn abount uninitialized lock.
  1066. * Simple try to initialize it.
  1067. * May be not needed in linux system.
  1068. */
  1069. res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
  1070. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  1071. __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
  1072. filename, line, func, name);
  1073. return res;
  1074. }
  1075. }
  1076. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  1077. if (t->tracking && !t->track) {
  1078. ast_reentrancy_init(&t->track);
  1079. }
  1080. lt = t->track;
  1081. if (t->tracking) {
  1082. #ifdef HAVE_BKTR
  1083. struct ast_bt tmp;
  1084. /* The implementation of backtrace() may have its own locks.
  1085. * Capture the backtrace outside of the reentrancy lock to
  1086. * avoid deadlocks. See ASTERISK-22455. */
  1087. ast_bt_get_addresses(&tmp);
  1088. ast_reentrancy_lock(lt);
  1089. if (lt->reentrancy != AST_MAX_REENTRANCY) {
  1090. lt->backtrace[lt->reentrancy] = tmp;
  1091. bt = &lt->backtrace[lt->reentrancy];
  1092. }
  1093. ast_reentrancy_unlock(lt);
  1094. ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
  1095. #else
  1096. ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
  1097. #endif
  1098. }
  1099. #endif /* DEBUG_THREADS */
  1100. #ifdef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
  1101. res = pthread_rwlock_timedwrlock(&t->lock, abs_timeout);
  1102. #else
  1103. do {
  1104. struct timeval _now;
  1105. for (;;) {
  1106. if (!(res = pthread_rwlock_trywrlock(&t->lock))) {
  1107. break;
  1108. }
  1109. _now = ast_tvnow();
  1110. if (_now.tv_sec > abs_timeout->tv_sec || (_now.tv_sec == abs_timeout->tv_sec && _now.tv_usec * 1000 > abs_timeout->tv_nsec)) {
  1111. break;
  1112. }
  1113. usleep(1);
  1114. }
  1115. } while (0);
  1116. #endif
  1117. #ifdef DEBUG_THREADS
  1118. if (!res && t->tracking) {
  1119. ast_reentrancy_lock(lt);
  1120. if (lt->reentrancy < AST_MAX_REENTRANCY) {
  1121. lt->file[lt->reentrancy] = filename;
  1122. lt->lineno[lt->reentrancy] = line;
  1123. lt->func[lt->reentrancy] = func;
  1124. lt->thread[lt->reentrancy] = pthread_self();
  1125. lt->reentrancy++;
  1126. }
  1127. ast_reentrancy_unlock(lt);
  1128. if (t->tracking) {
  1129. ast_mark_lock_acquired(t);
  1130. }
  1131. } else if (t->tracking) {
  1132. #ifdef HAVE_BKTR
  1133. if (lt->reentrancy) {
  1134. ast_reentrancy_lock(lt);
  1135. bt = &lt->backtrace[lt->reentrancy-1];
  1136. ast_reentrancy_unlock(lt);
  1137. } else {
  1138. bt = NULL;
  1139. }
  1140. if (t->tracking) {
  1141. ast_remove_lock_info(t, bt);
  1142. }
  1143. #else
  1144. if (t->tracking) {
  1145. ast_remove_lock_info(t);
  1146. }
  1147. #endif
  1148. }
  1149. if (res) {
  1150. __ast_mutex_logger("%s line %d (%s): Error obtaining read lock: %s\n",
  1151. filename, line, func, strerror(res));
  1152. DO_THREAD_CRASH;
  1153. }
  1154. #endif /* DEBUG_THREADS */
  1155. return res;
  1156. }
  1157. int __ast_rwlock_tryrdlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name)
  1158. {
  1159. int res;
  1160. #ifdef DEBUG_THREADS
  1161. struct ast_lock_track *lt;
  1162. #ifdef HAVE_BKTR
  1163. struct ast_bt *bt = NULL;
  1164. #endif
  1165. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  1166. int canlog = strcmp(filename, "logger.c") & t->tracking;
  1167. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  1168. /* Don't warn abount uninitialized lock.
  1169. * Simple try to initialize it.
  1170. * May be not needed in linux system.
  1171. */
  1172. res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
  1173. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  1174. __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
  1175. filename, line, func, name);
  1176. return res;
  1177. }
  1178. }
  1179. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  1180. if (t->tracking && !t->track) {
  1181. ast_reentrancy_init(&t->track);
  1182. }
  1183. lt = t->track;
  1184. if (t->tracking) {
  1185. #ifdef HAVE_BKTR
  1186. struct ast_bt tmp;
  1187. /* The implementation of backtrace() may have its own locks.
  1188. * Capture the backtrace outside of the reentrancy lock to
  1189. * avoid deadlocks. See ASTERISK-22455. */
  1190. ast_bt_get_addresses(&tmp);
  1191. ast_reentrancy_lock(lt);
  1192. if (lt->reentrancy != AST_MAX_REENTRANCY) {
  1193. lt->backtrace[lt->reentrancy] = tmp;
  1194. bt = &lt->backtrace[lt->reentrancy];
  1195. }
  1196. ast_reentrancy_unlock(lt);
  1197. ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t, bt);
  1198. #else
  1199. ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t);
  1200. #endif
  1201. }
  1202. #endif /* DEBUG_THREADS */
  1203. res = pthread_rwlock_tryrdlock(&t->lock);
  1204. #ifdef DEBUG_THREADS
  1205. if (!res && t->tracking) {
  1206. ast_reentrancy_lock(lt);
  1207. if (lt->reentrancy < AST_MAX_REENTRANCY) {
  1208. lt->file[lt->reentrancy] = filename;
  1209. lt->lineno[lt->reentrancy] = line;
  1210. lt->func[lt->reentrancy] = func;
  1211. lt->thread[lt->reentrancy] = pthread_self();
  1212. lt->reentrancy++;
  1213. }
  1214. ast_reentrancy_unlock(lt);
  1215. if (t->tracking) {
  1216. ast_mark_lock_acquired(t);
  1217. }
  1218. } else if (t->tracking) {
  1219. ast_mark_lock_failed(t);
  1220. }
  1221. #endif /* DEBUG_THREADS */
  1222. return res;
  1223. }
  1224. int __ast_rwlock_trywrlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name)
  1225. {
  1226. int res;
  1227. #ifdef DEBUG_THREADS
  1228. struct ast_lock_track *lt;
  1229. #ifdef HAVE_BKTR
  1230. struct ast_bt *bt = NULL;
  1231. #endif
  1232. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
  1233. int canlog = strcmp(filename, "logger.c") & t->tracking;
  1234. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  1235. /* Don't warn abount uninitialized lock.
  1236. * Simple try to initialize it.
  1237. * May be not needed in linux system.
  1238. */
  1239. res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
  1240. if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
  1241. __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
  1242. filename, line, func, name);
  1243. return res;
  1244. }
  1245. }
  1246. #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
  1247. if (t->tracking && !t->track) {
  1248. ast_reentrancy_init(&t->track);
  1249. }
  1250. lt = t->track;
  1251. if (t->tracking) {
  1252. #ifdef HAVE_BKTR
  1253. struct ast_bt tmp;
  1254. /* The implementation of backtrace() may have its own locks.
  1255. * Capture the backtrace outside of the reentrancy lock to
  1256. * avoid deadlocks. See ASTERISK-22455. */
  1257. ast_bt_get_addresses(&tmp);
  1258. ast_reentrancy_lock(lt);
  1259. if (lt->reentrancy != AST_MAX_REENTRANCY) {
  1260. lt->backtrace[lt->reentrancy] = tmp;
  1261. bt = &lt->backtrace[lt->reentrancy];
  1262. }
  1263. ast_reentrancy_unlock(lt);
  1264. ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
  1265. #else
  1266. ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
  1267. #endif
  1268. }
  1269. #endif /* DEBUG_THREADS */
  1270. res = pthread_rwlock_trywrlock(&t->lock);
  1271. #ifdef DEBUG_THREADS
  1272. if (!res && t->tracking) {
  1273. ast_reentrancy_lock(lt);
  1274. if (lt->reentrancy < AST_MAX_REENTRANCY) {
  1275. lt->file[lt->reentrancy] = filename;
  1276. lt->lineno[lt->reentrancy] = line;
  1277. lt->func[lt->reentrancy] = func;
  1278. lt->thread[lt->reentrancy] = pthread_self();
  1279. lt->reentrancy++;
  1280. }
  1281. ast_reentrancy_unlock(lt);
  1282. ast_mark_lock_acquired(t);
  1283. } else if (t->tracking) {
  1284. ast_mark_lock_failed(t);
  1285. }
  1286. #endif /* DEBUG_THREADS */
  1287. return res;
  1288. }