Index: main/lock.c =================================================================== --- main/lock.c (revision 406641) +++ main/lock.c (working copy) @@ -62,6 +62,65 @@ } #endif /* defined(DEBUG_THREADS) && defined(HAVE_BKTR) */ +#ifdef DEBUG_THREADS +AST_MUTEX_DEFINE_STATIC(reentrancy_lock); + +static inline struct ast_lock_track *ast_get_reentrancy(struct ast_lock_track **plt) +{ + pthread_mutexattr_t reentr_attr; + struct ast_lock_track *lt; + + /* It's a bit painful to lock a global mutex for every access to the + * reentrancy structure, but it's necessary to ensure that we don't + * double-allocate the structure or double-initialize the reentr_mutex. + * + * If you'd like to replace this with a double-checked lock, be sure to + * properly volatile-ize everything to avoid optimizer bugs. + * + * We also have to use the underlying pthread calls for manipulating + * the mutex, because this is called from the Asterisk mutex code. + */ + pthread_mutex_lock(&reentrancy_lock.mutex); + + if (*plt) { + pthread_mutex_unlock(&reentrancy_lock.mutex); + return *plt; + } + + lt = *plt = ast_std_calloc(1, sizeof(*lt)); + + if (!lt) { + fprintf(stderr, "%s: Failed to allocate lock tracking\n", __func__); +#if defined(DO_CRASH) || defined(THREAD_CRASH) + abort(); +#else + pthread_mutex_unlock(&reentrancy_lock.mutex); + return NULL; +#endif + } + + pthread_mutexattr_init(&reentr_attr); + pthread_mutexattr_settype(&reentr_attr, AST_MUTEX_KIND); + pthread_mutex_init(<->reentr_mutex, &reentr_attr); + pthread_mutexattr_destroy(&reentr_attr); + + pthread_mutex_unlock(&reentrancy_lock.mutex); + return lt; +} + +static inline void delete_reentrancy_cs(struct ast_lock_track **plt) +{ + struct ast_lock_track *lt; + if (*plt) { + lt = *plt; + pthread_mutex_destroy(<->reentr_mutex); + ast_std_free(lt); + *plt = NULL; + } +} + +#endif /* DEBUG_THREADS */ + int __ast_pthread_mutex_init(int tracking, const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t) { @@ -84,7 +143,7 @@ #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ if ((t->tracking = tracking)) { - ast_reentrancy_init(&t->track); + ast_get_reentrancy(&t->track); } #endif /* DEBUG_THREADS */ @@ -102,7 +161,7 @@ int res; #ifdef DEBUG_THREADS - struct ast_lock_track *lt; + struct ast_lock_track *lt = NULL; int canlog = strcmp(filename, "logger.c") & t->tracking; #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE) @@ -119,10 +178,9 @@ } #endif - if (t->tracking && !t->track) { - ast_reentrancy_init(&t->track); + if (t->tracking) { + lt = ast_get_reentrancy(&t->track); } - lt = t->track; res = pthread_mutex_trylock(&t->mutex); switch (res) { @@ -136,7 +194,7 @@ case EBUSY: __ast_mutex_logger("%s line %d (%s): Error: attempt to destroy locked mutex '%s'.\n", filename, lineno, func, mutex_name); - if (t->tracking) { + if (lt) { ast_reentrancy_lock(lt); __ast_mutex_logger("%s line %d (%s): Error: '%s' was locked here.\n", lt->file[ROFFSET], lt->lineno[ROFFSET], lt->func[ROFFSET], mutex_name); @@ -156,7 +214,7 @@ __ast_mutex_logger("%s line %d (%s): Error destroying mutex %s: %s\n", filename, lineno, func, mutex_name, strerror(res)); } - if (t->tracking) { + if (lt) { ast_reentrancy_lock(lt); lt->file[0] = filename; lt->lineno[0] = lineno; @@ -180,7 +238,7 @@ int res; #ifdef DEBUG_THREADS - struct ast_lock_track *lt; + struct ast_lock_track *lt = NULL; int canlog = strcmp(filename, "logger.c") & t->tracking; #ifdef HAVE_BKTR struct ast_bt *bt = NULL; @@ -201,12 +259,11 @@ } #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ - if (t->tracking && !t->track) { - ast_reentrancy_init(&t->track); + if (t->tracking) { + lt = ast_get_reentrancy(&t->track); } - lt = t->track; - if (t->tracking) { + if (lt) { #ifdef HAVE_BKTR struct ast_bt tmp; @@ -274,7 +331,7 @@ #endif /* !DETECT_DEADLOCKS || !DEBUG_THREADS */ #ifdef DEBUG_THREADS - if (t->tracking && !res) { + if (lt && !res) { ast_reentrancy_lock(lt); if (lt->reentrancy < AST_MAX_REENTRANCY) { lt->file[lt->reentrancy] = filename; @@ -287,10 +344,8 @@ filename, lineno, func, mutex_name); } ast_reentrancy_unlock(lt); - if (t->tracking) { - ast_mark_lock_acquired(t); - } - } else if (t->tracking) { + ast_mark_lock_acquired(t); + } else if (lt) { #ifdef HAVE_BKTR if (lt->reentrancy) { ast_reentrancy_lock(lt); @@ -320,7 +375,7 @@ int res; #ifdef DEBUG_THREADS - struct ast_lock_track *lt; + struct ast_lock_track *lt = NULL; int canlog = strcmp(filename, "logger.c") & t->tracking; #ifdef HAVE_BKTR struct ast_bt *bt = NULL; @@ -341,12 +396,11 @@ } #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ - if (t->tracking && !t->track) { - ast_reentrancy_init(&t->track); + if (t->tracking) { + lt = ast_get_reentrancy(&t->track); } - lt = t->track; - if (t->tracking) { + if (lt) { #ifdef HAVE_BKTR struct ast_bt tmp; @@ -372,7 +426,7 @@ res = pthread_mutex_trylock(&t->mutex); #ifdef DEBUG_THREADS - if (t->tracking && !res) { + if (lt && !res) { ast_reentrancy_lock(lt); if (lt->reentrancy < AST_MAX_REENTRANCY) { lt->file[lt->reentrancy] = filename; @@ -385,10 +439,8 @@ filename, lineno, func, mutex_name); } ast_reentrancy_unlock(lt); - if (t->tracking) { - ast_mark_lock_acquired(t); - } - } else if (t->tracking) { + ast_mark_lock_acquired(t); + } else if (lt) { ast_mark_lock_failed(t); } #endif /* DEBUG_THREADS */ @@ -402,7 +454,7 @@ int res; #ifdef DEBUG_THREADS - struct ast_lock_track *lt; + struct ast_lock_track *lt = NULL; int canlog = strcmp(filename, "logger.c") & t->tracking; #ifdef HAVE_BKTR struct ast_bt *bt = NULL; @@ -421,12 +473,11 @@ } #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ - if (t->tracking && !t->track) { - ast_reentrancy_init(&t->track); + if (t->tracking) { + lt = ast_get_reentrancy(&t->track); } - lt = t->track; - if (t->tracking) { + if (lt) { ast_reentrancy_lock(lt); if (lt->reentrancy && (lt->thread[ROFFSET] != pthread_self())) { __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n", @@ -512,7 +563,7 @@ int res; #ifdef DEBUG_THREADS - struct ast_lock_track *lt; + struct ast_lock_track *lt = NULL; struct ast_lock_track lt_orig; int canlog = strcmp(filename, "logger.c") & t->tracking; @@ -529,12 +580,11 @@ } #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ - if (t->tracking && !t->track) { - ast_reentrancy_init(&t->track); + if (t->tracking) { + lt = ast_get_reentrancy(&t->track); } - lt = t->track; - if (t->tracking) { + if (lt) { ast_reentrancy_lock(lt); if (lt->reentrancy && (lt->thread[ROFFSET] != pthread_self())) { __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n", @@ -569,7 +619,7 @@ __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n", filename, lineno, func, strerror(res)); DO_THREAD_CRASH; - } else if (t->tracking) { + } else if (lt) { pthread_mutex_t reentr_mutex_orig; ast_reentrancy_lock(lt); /* Restore lock tracking to what it was prior to the wait */ @@ -593,7 +643,7 @@ int res; #ifdef DEBUG_THREADS - struct ast_lock_track *lt; + struct ast_lock_track *lt = NULL; struct ast_lock_track lt_orig; int canlog = strcmp(filename, "logger.c") & t->tracking; @@ -610,12 +660,11 @@ } #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ - if (t->tracking && !t->track) { - ast_reentrancy_init(&t->track); + if (t->tracking) { + lt = ast_get_reentrancy(&t->track); } - lt = t->track; - if (t->tracking) { + if (lt) { ast_reentrancy_lock(lt); if (lt->reentrancy && (lt->thread[ROFFSET] != pthread_self())) { __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n", @@ -650,7 +699,7 @@ __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n", filename, lineno, func, strerror(res)); DO_THREAD_CRASH; - } else if (t->tracking) { + } else if (lt) { pthread_mutex_t reentr_mutex_orig; ast_reentrancy_lock(lt); /* Restore lock tracking to what it was prior to the wait */ @@ -685,7 +734,7 @@ #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ if ((t->tracking = tracking)) { - ast_reentrancy_init(&t->track); + ast_get_reentrancy(&t->track); } #endif /* DEBUG_THREADS */ @@ -725,7 +774,7 @@ __ast_mutex_logger("%s line %d (%s): Error destroying rwlock %s: %s\n", filename, lineno, func, rwlock_name, strerror(res)); } - if (t->tracking && lt) { + if (lt) { ast_reentrancy_lock(lt); lt->file[0] = filename; lt->lineno[0] = lineno; @@ -748,7 +797,7 @@ int res; #ifdef DEBUG_THREADS - struct ast_lock_track *lt; + struct ast_lock_track *lt = NULL; int canlog = strcmp(filename, "logger.c") & t->tracking; #ifdef HAVE_BKTR struct ast_bt *bt = NULL; @@ -769,12 +818,11 @@ } #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ - if (t->tracking && !t->track) { - ast_reentrancy_init(&t->track); + if (t->tracking) { + lt = ast_get_reentrancy(&t->track); } - lt = t->track; - if (t->tracking) { + if (lt) { ast_reentrancy_lock(lt); if (lt->reentrancy) { int i; @@ -834,7 +882,7 @@ int res; #ifdef DEBUG_THREADS - struct ast_lock_track *lt; + struct ast_lock_track *lt = NULL; int canlog = strcmp(filename, "logger.c") & t->tracking; #ifdef HAVE_BKTR struct ast_bt *bt = NULL; @@ -855,12 +903,11 @@ } #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ - if (t->tracking && !t->track) { - ast_reentrancy_init(&t->track); + if (t->tracking) { + lt = ast_get_reentrancy(&t->track); } - lt = t->track; - if (t->tracking) { + if (lt) { #ifdef HAVE_BKTR struct ast_bt tmp; @@ -894,7 +941,7 @@ if (wait_time > reported_wait && (wait_time % 5) == 0) { __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for readlock '%s'?\n", filename, line, func, (int)wait_time, name); - if (t->tracking) { + if (lt) { ast_reentrancy_lock(lt); #ifdef HAVE_BKTR __dump_backtrace(<->backtrace[lt->reentrancy], canlog); @@ -918,7 +965,7 @@ #endif /* !DETECT_DEADLOCKS || !DEBUG_THREADS */ #ifdef DEBUG_THREADS - if (!res && t->tracking) { + if (!res && lt) { ast_reentrancy_lock(lt); if (lt->reentrancy < AST_MAX_REENTRANCY) { lt->file[lt->reentrancy] = filename; @@ -928,10 +975,8 @@ lt->reentrancy++; } ast_reentrancy_unlock(lt); - if (t->tracking) { - ast_mark_lock_acquired(t); - } - } else if (t->tracking) { + ast_mark_lock_acquired(t); + } else if (lt) { #ifdef HAVE_BKTR if (lt->reentrancy) { ast_reentrancy_lock(lt); @@ -961,7 +1006,7 @@ int res; #ifdef DEBUG_THREADS - struct ast_lock_track *lt; + struct ast_lock_track *lt = NULL; int canlog = strcmp(filename, "logger.c") & t->tracking; #ifdef HAVE_BKTR struct ast_bt *bt = NULL; @@ -982,12 +1027,11 @@ } #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ - if (t->tracking && !t->track) { - ast_reentrancy_init(&t->track); + if (t->tracking) { + lt = ast_get_reentrancy(&t->track); } - lt = t->track; - if (t->tracking) { + if (lt) { #ifdef HAVE_BKTR struct ast_bt tmp; @@ -1021,7 +1065,7 @@ if (wait_time > reported_wait && (wait_time % 5) == 0) { __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for writelock '%s'?\n", filename, line, func, (int)wait_time, name); - if (t->tracking) { + if (lt) { ast_reentrancy_lock(lt); #ifdef HAVE_BKTR __dump_backtrace(<->backtrace[lt->reentrancy], canlog); @@ -1045,7 +1089,7 @@ #endif /* !DETECT_DEADLOCKS || !DEBUG_THREADS */ #ifdef DEBUG_THREADS - if (!res && t->tracking) { + if (!res && lt) { ast_reentrancy_lock(lt); if (lt->reentrancy < AST_MAX_REENTRANCY) { lt->file[lt->reentrancy] = filename; @@ -1055,10 +1099,8 @@ lt->reentrancy++; } ast_reentrancy_unlock(lt); - if (t->tracking) { - ast_mark_lock_acquired(t); - } - } else if (t->tracking) { + ast_mark_lock_acquired(t); + } else if (lt) { #ifdef HAVE_BKTR if (lt->reentrancy) { ast_reentrancy_lock(lt); @@ -1067,13 +1109,9 @@ } else { bt = NULL; } - if (t->tracking) { - ast_remove_lock_info(t, bt); - } + ast_remove_lock_info(t, bt); #else - if (t->tracking) { - ast_remove_lock_info(t); - } + ast_remove_lock_info(t); #endif } if (res) { @@ -1092,7 +1130,7 @@ int res; #ifdef DEBUG_THREADS - struct ast_lock_track *lt; + struct ast_lock_track *lt = NULL; int canlog = strcmp(filename, "logger.c") & t->tracking; #ifdef HAVE_BKTR struct ast_bt *bt = NULL; @@ -1113,12 +1151,11 @@ } #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ - if (t->tracking && !t->track) { - ast_reentrancy_init(&t->track); + if (t->tracking) { + lt = ast_get_reentrancy(&t->track); } - lt = t->track; - if (t->tracking) { + if (lt) { #ifdef HAVE_BKTR struct ast_bt tmp; @@ -1160,7 +1197,7 @@ #endif #ifdef DEBUG_THREADS - if (!res && t->tracking) { + if (!res && lt) { ast_reentrancy_lock(lt); if (lt->reentrancy < AST_MAX_REENTRANCY) { lt->file[lt->reentrancy] = filename; @@ -1170,10 +1207,8 @@ lt->reentrancy++; } ast_reentrancy_unlock(lt); - if (t->tracking) { - ast_mark_lock_acquired(t); - } - } else if (t->tracking) { + ast_mark_lock_acquired(t); + } else if (lt) { #ifdef HAVE_BKTR if (lt->reentrancy) { ast_reentrancy_lock(lt); @@ -1203,7 +1238,7 @@ int res; #ifdef DEBUG_THREADS - struct ast_lock_track *lt; + struct ast_lock_track *lt = NULL; int canlog = strcmp(filename, "logger.c") & t->tracking; #ifdef HAVE_BKTR struct ast_bt *bt = NULL; @@ -1224,12 +1259,11 @@ } #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ - if (t->tracking && !t->track) { - ast_reentrancy_init(&t->track); + if (t->tracking) { + lt = ast_get_reentrancy(&t->track); } - lt = t->track; - if (t->tracking) { + if (lt) { #ifdef HAVE_BKTR struct ast_bt tmp; @@ -1271,7 +1305,7 @@ #endif #ifdef DEBUG_THREADS - if (!res && t->tracking) { + if (!res && lt) { ast_reentrancy_lock(lt); if (lt->reentrancy < AST_MAX_REENTRANCY) { lt->file[lt->reentrancy] = filename; @@ -1281,10 +1315,8 @@ lt->reentrancy++; } ast_reentrancy_unlock(lt); - if (t->tracking) { - ast_mark_lock_acquired(t); - } - } else if (t->tracking) { + ast_mark_lock_acquired(t); + } else if (lt) { #ifdef HAVE_BKTR if (lt->reentrancy) { ast_reentrancy_lock(lt); @@ -1293,13 +1325,9 @@ } else { bt = NULL; } - if (t->tracking) { - ast_remove_lock_info(t, bt); - } + ast_remove_lock_info(t, bt); #else - if (t->tracking) { - ast_remove_lock_info(t); - } + ast_remove_lock_info(t); #endif } if (res) { @@ -1317,7 +1345,7 @@ int res; #ifdef DEBUG_THREADS - struct ast_lock_track *lt; + struct ast_lock_track *lt = NULL; #ifdef HAVE_BKTR struct ast_bt *bt = NULL; #endif @@ -1338,12 +1366,11 @@ } #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ - if (t->tracking && !t->track) { - ast_reentrancy_init(&t->track); + if (t->tracking) { + lt = ast_get_reentrancy(&t->track); } - lt = t->track; - if (t->tracking) { + if (lt) { #ifdef HAVE_BKTR struct ast_bt tmp; @@ -1369,7 +1396,7 @@ res = pthread_rwlock_tryrdlock(&t->lock); #ifdef DEBUG_THREADS - if (!res && t->tracking) { + if (!res && lt) { ast_reentrancy_lock(lt); if (lt->reentrancy < AST_MAX_REENTRANCY) { lt->file[lt->reentrancy] = filename; @@ -1379,10 +1406,8 @@ lt->reentrancy++; } ast_reentrancy_unlock(lt); - if (t->tracking) { - ast_mark_lock_acquired(t); - } - } else if (t->tracking) { + ast_mark_lock_acquired(t); + } else if (lt) { ast_mark_lock_failed(t); } #endif /* DEBUG_THREADS */ @@ -1395,7 +1420,7 @@ int res; #ifdef DEBUG_THREADS - struct ast_lock_track *lt; + struct ast_lock_track *lt = NULL; #ifdef HAVE_BKTR struct ast_bt *bt = NULL; #endif @@ -1416,12 +1441,11 @@ } #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ - if (t->tracking && !t->track) { - ast_reentrancy_init(&t->track); + if (t->tracking) { + lt = ast_get_reentrancy(&t->track); } - lt = t->track; - if (t->tracking) { + if (lt) { #ifdef HAVE_BKTR struct ast_bt tmp; @@ -1447,7 +1471,7 @@ res = pthread_rwlock_trywrlock(&t->lock); #ifdef DEBUG_THREADS - if (!res && t->tracking) { + if (!res && lt) { ast_reentrancy_lock(lt); if (lt->reentrancy < AST_MAX_REENTRANCY) { lt->file[lt->reentrancy] = filename; @@ -1458,7 +1482,7 @@ } ast_reentrancy_unlock(lt); ast_mark_lock_acquired(t); - } else if (t->tracking) { + } else if (lt) { ast_mark_lock_failed(t); } #endif /* DEBUG_THREADS */ Index: include/asterisk/lock.h =================================================================== --- include/asterisk/lock.h (revision 406641) +++ include/asterisk/lock.h (working copy) @@ -18,7 +18,7 @@ /*! \file * \brief Asterisk locking-related definitions: - * - ast_mutext_t, ast_rwlock_t and related functions; + * - ast_mutex_t, ast_rwlock_t and related functions; * - atomic arithmetic instructions; * - wrappers for channel locking. * @@ -436,45 +436,6 @@ } } -static inline void ast_reentrancy_init(struct ast_lock_track **plt) -{ - int i; - pthread_mutexattr_t reentr_attr; - struct ast_lock_track *lt = *plt; - - if (!lt) { - lt = *plt = (struct ast_lock_track *) calloc(1, sizeof(*lt)); - } - - for (i = 0; i < AST_MAX_REENTRANCY; i++) { - lt->file[i] = NULL; - lt->lineno[i] = 0; - lt->func[i] = NULL; - lt->thread[i] = 0; -#ifdef HAVE_BKTR - memset(<->backtrace[i], 0, sizeof(lt->backtrace[i])); -#endif - } - - lt->reentrancy = 0; - - pthread_mutexattr_init(&reentr_attr); - pthread_mutexattr_settype(&reentr_attr, AST_MUTEX_KIND); - pthread_mutex_init(<->reentr_mutex, &reentr_attr); - pthread_mutexattr_destroy(&reentr_attr); -} - -static inline void delete_reentrancy_cs(struct ast_lock_track **plt) -{ - struct ast_lock_track *lt; - if (*plt) { - lt = *plt; - pthread_mutex_destroy(<->reentr_mutex); - free(lt); - *plt = NULL; - } -} - #else /* !DEBUG_THREADS */ #define CHANNEL_DEADLOCK_AVOIDANCE(chan) \