}
#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;
+ *plt = NULL;
+
+ pthread_mutex_destroy(<->reentr_mutex);
+ ast_std_free(lt);
+ }
+}
+
+#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)
{
pthread_mutexattr_t attr;
#ifdef DEBUG_THREADS
- t->track = NULL;
#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
if ((t->mutex) != ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
-/*
- int canlog = strcmp(filename, "logger.c") & track;
+ int canlog = tracking && strcmp(filename, "logger.c");
+
__ast_mutex_logger("%s line %d (%s): NOTICE: mutex '%s' is already initialized.\n",
filename, lineno, func, mutex_name);
DO_THREAD_CRASH;
-*/
- return 0;
+ return EBUSY;
}
-
#endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
- if ((t->tracking = tracking)) {
- ast_reentrancy_init(&t->track);
- }
+ t->track = NULL;
+ t->tracking = tracking;
#endif /* DEBUG_THREADS */
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, AST_MUTEX_KIND);
-
res = pthread_mutex_init(&t->mutex, &attr);
pthread_mutexattr_destroy(&attr);
+
return res;
}
int res;
#ifdef DEBUG_THREADS
- struct ast_lock_track *lt;
- int canlog = strcmp(filename, "logger.c") & t->tracking;
+ struct ast_lock_track *lt = t->track;
+ int canlog = t->tracking && strcmp(filename, "logger.c");
#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
- /* Don't try to uninitialize non initialized mutex
- * This may no effect on linux
- * And always ganerate core on *BSD with
- * linked libpthread
- * This not error condition if the mutex created on the fly.
+ /* Don't try to uninitialize an uninitialized mutex
+ * This may have no effect on linux
+ * but it always generates a core on *BSD when
+ * linked with libpthread.
+ * This is not an error condition if the mutex is created on the fly.
*/
__ast_mutex_logger("%s line %d (%s): NOTICE: mutex '%s' is uninitialized.\n",
filename, lineno, func, mutex_name);
- return 0;
+ DO_THREAD_CRASH;
+ res = EINVAL;
+ goto lt_cleanup;
}
#endif
- if (t->tracking && !t->track) {
- ast_reentrancy_init(&t->track);
- }
- lt = t->track;
-
res = pthread_mutex_trylock(&t->mutex);
switch (res) {
case 0:
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);
__ast_mutex_logger("%s line %d (%s): Error destroying mutex %s: %s\n",
filename, lineno, func, mutex_name, strerror(res));
}
- if (t->tracking) {
+#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
+lt_cleanup:
+#endif
+ if (lt) {
ast_reentrancy_lock(lt);
lt->file[0] = filename;
lt->lineno[0] = lineno;
int res;
#ifdef DEBUG_THREADS
- struct ast_lock_track *lt;
- int canlog = strcmp(filename, "logger.c") & t->tracking;
+ struct ast_lock_track *lt = NULL;
+ int canlog = t->tracking && strcmp(filename, "logger.c");
#ifdef HAVE_BKTR
struct ast_bt *bt = NULL;
#endif
-#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
- if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
- /* Don't warn abount uninitialized mutex.
- * Simple try to initialize it.
- * May be not needed in linux system.
- */
- res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
- if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
- __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
- filename, lineno, func, mutex_name);
- return res;
- }
- }
-#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;
ast_bt_get_addresses(&tmp);
ast_reentrancy_lock(lt);
- if (lt->reentrancy != AST_MAX_REENTRANCY) {
+ if (lt->reentrancy < AST_MAX_REENTRANCY) {
lt->backtrace[lt->reentrancy] = tmp;
bt = <->backtrace[lt->reentrancy];
}
#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;
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);
int res;
#ifdef DEBUG_THREADS
- struct ast_lock_track *lt;
- int canlog = strcmp(filename, "logger.c") & t->tracking;
+ struct ast_lock_track *lt = NULL;
+ int canlog = t->tracking && strcmp(filename, "logger.c");
#ifdef HAVE_BKTR
struct ast_bt *bt = NULL;
#endif
-#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
- if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
- /* Don't warn abount uninitialized mutex.
- * Simple try to initialize it.
- * May be not needed in linux system.
- */
- res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
- if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
- __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
- filename, lineno, func, mutex_name);
- return res;
- }
- }
-#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;
ast_bt_get_addresses(&tmp);
ast_reentrancy_lock(lt);
- if (lt->reentrancy != AST_MAX_REENTRANCY) {
+ if (lt->reentrancy < AST_MAX_REENTRANCY) {
lt->backtrace[lt->reentrancy] = tmp;
bt = <->backtrace[lt->reentrancy];
}
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;
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 */
int res;
#ifdef DEBUG_THREADS
- struct ast_lock_track *lt;
- int canlog = strcmp(filename, "logger.c") & t->tracking;
+ struct ast_lock_track *lt = NULL;
+ int canlog = t->tracking && strcmp(filename, "logger.c");
#ifdef HAVE_BKTR
struct ast_bt *bt = NULL;
#endif
if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
__ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
filename, lineno, func, mutex_name);
- res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
- if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
- __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
- filename, lineno, func, mutex_name);
- }
- return res;
+ DO_THREAD_CRASH;
+ return EINVAL;
}
#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",
return pthread_cond_destroy(cond);
}
+#ifdef DEBUG_THREADS
+static void restore_lock_tracking(struct ast_lock_track *lt, struct ast_lock_track *lt_saved)
+{
+ ast_reentrancy_lock(lt);
+
+ /*
+ * The following code must match the struct ast_lock_track
+ * definition with the explicit exception of the reentr_mutex
+ * member.
+ */
+ memcpy(lt->file, lt_saved->file, sizeof(lt->file));
+ memcpy(lt->lineno, lt_saved->lineno, sizeof(lt->lineno));
+ lt->reentrancy = lt_saved->reentrancy;
+ memcpy(lt->func, lt_saved->func, sizeof(lt->func));
+ memcpy(lt->thread, lt_saved->thread, sizeof(lt->thread));
+#ifdef HAVE_BKTR
+ memcpy(lt->backtrace, lt_saved->backtrace, sizeof(lt->backtrace));
+#endif
+
+ ast_reentrancy_unlock(lt);
+}
+#endif /* DEBUG_THREADS */
+
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)
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;
+ int canlog = t->tracking && strcmp(filename, "logger.c");
#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
__ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
filename, lineno, func, mutex_name);
- res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
- if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
- __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
- filename, lineno, func, mutex_name);
- }
- return res;
+ DO_THREAD_CRASH;
+ return EINVAL;
}
#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",
+ __ast_mutex_logger("%s line %d (%s): attempted wait using mutex '%s' without owning it!\n",
filename, lineno, func, mutex_name);
__ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
lt->file[ROFFSET], lt->lineno[ROFFSET], lt->func[ROFFSET], mutex_name);
#endif
DO_THREAD_CRASH;
} else if (lt->reentrancy <= 0) {
- __ast_mutex_logger("%s line %d (%s): attempted to wait on an unlocked mutex '%s'\n",
+ __ast_mutex_logger("%s line %d (%s): attempted wait using an unlocked mutex '%s'\n",
filename, lineno, func, mutex_name);
DO_THREAD_CRASH;
}
__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) {
- pthread_mutex_t reentr_mutex_orig;
- ast_reentrancy_lock(lt);
- /* Restore lock tracking to what it was prior to the wait */
- reentr_mutex_orig = lt->reentr_mutex;
- *lt = lt_orig;
- /* un-trash the mutex we just copied over */
- lt->reentr_mutex = reentr_mutex_orig;
- ast_reentrancy_unlock(lt);
-
+ } else if (lt) {
+ restore_lock_tracking(lt, <_orig);
ast_restore_lock_info(t);
}
#endif /* DEBUG_THREADS */
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;
+ int canlog = t->tracking && strcmp(filename, "logger.c");
#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
__ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
filename, lineno, func, mutex_name);
- res = __ast_pthread_mutex_init(t->tracking, filename, lineno, func, mutex_name, t);
- if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
- __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized and unable to initialize.\n",
- filename, lineno, func, mutex_name);
- }
- return res;
+ DO_THREAD_CRASH;
+ return EINVAL;
}
#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",
+ __ast_mutex_logger("%s line %d (%s): attempted wait using mutex '%s' without owning it!\n",
filename, lineno, func, mutex_name);
__ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
lt->file[ROFFSET], lt->lineno[ROFFSET], lt->func[ROFFSET], mutex_name);
#endif
DO_THREAD_CRASH;
} else if (lt->reentrancy <= 0) {
- __ast_mutex_logger("%s line %d (%s): attempted to wait on an unlocked mutex '%s'\n",
+ __ast_mutex_logger("%s line %d (%s): attempted wait using an unlocked mutex '%s'\n",
filename, lineno, func, mutex_name);
DO_THREAD_CRASH;
}
__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) {
- pthread_mutex_t reentr_mutex_orig;
- ast_reentrancy_lock(lt);
- /* Restore lock tracking to what it was prior to the wait */
- reentr_mutex_orig = lt->reentr_mutex;
- *lt = lt_orig;
- /* un-trash the mutex we just copied over */
- lt->reentr_mutex = reentr_mutex_orig;
- ast_reentrancy_unlock(lt);
-
- ast_suspend_lock_info(t);
+ } else if (lt) {
+ restore_lock_tracking(lt, <_orig);
+ ast_restore_lock_info(t);
}
#endif /* DEBUG_THREADS */
pthread_rwlockattr_t attr;
#ifdef DEBUG_THREADS
-
#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
- int canlog = strcmp(filename, "logger.c") & t->tracking;
-
if (t->lock != ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
+ int canlog = tracking && strcmp(filename, "logger.c");
+
__ast_mutex_logger("%s line %d (%s): Warning: rwlock '%s' is already initialized.\n",
filename, lineno, func, rwlock_name);
- return 0;
+ DO_THREAD_CRASH;
+ return EBUSY;
}
#endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
- if ((t->tracking = tracking)) {
- ast_reentrancy_init(&t->track);
- }
+ t->track = NULL;
+ t->tracking = tracking;
#endif /* DEBUG_THREADS */
pthread_rwlockattr_init(&attr);
-
#ifdef HAVE_PTHREAD_RWLOCK_PREFER_WRITER_NP
pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP);
#endif
-
res = pthread_rwlock_init(&t->lock, &attr);
pthread_rwlockattr_destroy(&attr);
+
return res;
}
#ifdef DEBUG_THREADS
struct ast_lock_track *lt = t->track;
- int canlog = strcmp(filename, "logger.c") & t->tracking;
+ int canlog = t->tracking && strcmp(filename, "logger.c");
#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
if (t->lock == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
__ast_mutex_logger("%s line %d (%s): Warning: rwlock '%s' is uninitialized.\n",
filename, lineno, func, rwlock_name);
- return 0;
+ DO_THREAD_CRASH;
+ res = EINVAL;
+ goto lt_cleanup;
}
#endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
-
#endif /* DEBUG_THREADS */
res = pthread_rwlock_destroy(&t->lock);
__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 defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
+lt_cleanup:
+#endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
+ if (lt) {
ast_reentrancy_lock(lt);
lt->file[0] = filename;
lt->lineno[0] = lineno;
int res;
#ifdef DEBUG_THREADS
- struct ast_lock_track *lt;
- int canlog = strcmp(filename, "logger.c") & t->tracking;
+ struct ast_lock_track *lt = NULL;
+ int canlog = t->tracking && strcmp(filename, "logger.c");
#ifdef HAVE_BKTR
struct ast_bt *bt = NULL;
#endif
int lock_found = 0;
-
#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
__ast_mutex_logger("%s line %d (%s): Warning: rwlock '%s' is uninitialized.\n",
filename, line, func, name);
- res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
- if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
- filename, line, func, name);
- }
- return res;
+ DO_THREAD_CRASH;
+ return EINVAL;
}
#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;
int res;
#ifdef DEBUG_THREADS
- struct ast_lock_track *lt;
- int canlog = strcmp(filename, "logger.c") & t->tracking;
+ struct ast_lock_track *lt = NULL;
+ int canlog = t->tracking && strcmp(filename, "logger.c");
#ifdef HAVE_BKTR
struct ast_bt *bt = NULL;
#endif
-#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
- if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- /* Don't warn abount uninitialized lock.
- * Simple try to initialize it.
- * May be not needed in linux system.
- */
- res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
- if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
- filename, line, func, name);
- return res;
- }
- }
-#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;
ast_bt_get_addresses(&tmp);
ast_reentrancy_lock(lt);
- if (lt->reentrancy != AST_MAX_REENTRANCY) {
+ if (lt->reentrancy < AST_MAX_REENTRANCY) {
lt->backtrace[lt->reentrancy] = tmp;
bt = <->backtrace[lt->reentrancy];
}
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);
#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;
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);
int res;
#ifdef DEBUG_THREADS
- struct ast_lock_track *lt;
- int canlog = strcmp(filename, "logger.c") & t->tracking;
+ struct ast_lock_track *lt = NULL;
+ int canlog = t->tracking && strcmp(filename, "logger.c");
#ifdef HAVE_BKTR
struct ast_bt *bt = NULL;
#endif
-#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
- if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- /* Don't warn abount uninitialized lock.
- * Simple try to initialize it.
- * May be not needed in linux system.
- */
- res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
- if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
- filename, line, func, name);
- return res;
- }
- }
-#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;
ast_bt_get_addresses(&tmp);
ast_reentrancy_lock(lt);
- if (lt->reentrancy != AST_MAX_REENTRANCY) {
+ if (lt->reentrancy < AST_MAX_REENTRANCY) {
lt->backtrace[lt->reentrancy] = tmp;
bt = <->backtrace[lt->reentrancy];
}
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);
#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;
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);
} 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) {
int res;
#ifdef DEBUG_THREADS
- struct ast_lock_track *lt;
- int canlog = strcmp(filename, "logger.c") & t->tracking;
+ struct ast_lock_track *lt = NULL;
+ int canlog = t->tracking && strcmp(filename, "logger.c");
#ifdef HAVE_BKTR
struct ast_bt *bt = NULL;
#endif
-#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
- if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- /* Don't warn abount uninitialized lock.
- * Simple try to initialize it.
- * May be not needed in linux system.
- */
- res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
- if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
- filename, line, func, name);
- return res;
- }
- }
-#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;
ast_bt_get_addresses(&tmp);
ast_reentrancy_lock(lt);
- if (lt->reentrancy != AST_MAX_REENTRANCY) {
+ if (lt->reentrancy < AST_MAX_REENTRANCY) {
lt->backtrace[lt->reentrancy] = tmp;
bt = <->backtrace[lt->reentrancy];
}
#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;
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);
int res;
#ifdef DEBUG_THREADS
- struct ast_lock_track *lt;
- int canlog = strcmp(filename, "logger.c") & t->tracking;
+ struct ast_lock_track *lt = NULL;
+ int canlog = t->tracking && strcmp(filename, "logger.c");
#ifdef HAVE_BKTR
struct ast_bt *bt = NULL;
#endif
-#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
- if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- /* Don't warn abount uninitialized lock.
- * Simple try to initialize it.
- * May be not needed in linux system.
- */
- res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
- if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
- filename, line, func, name);
- return res;
- }
- }
-#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;
ast_bt_get_addresses(&tmp);
ast_reentrancy_lock(lt);
- if (lt->reentrancy != AST_MAX_REENTRANCY) {
+ if (lt->reentrancy < AST_MAX_REENTRANCY) {
lt->backtrace[lt->reentrancy] = tmp;
bt = <->backtrace[lt->reentrancy];
}
#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;
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);
} 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) {
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
-#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
- int canlog = strcmp(filename, "logger.c") & t->tracking;
- if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- /* Don't warn abount uninitialized lock.
- * Simple try to initialize it.
- * May be not needed in linux system.
- */
- res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
- if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
- filename, line, func, name);
- return res;
- }
- }
-#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;
ast_bt_get_addresses(&tmp);
ast_reentrancy_lock(lt);
- if (lt->reentrancy != AST_MAX_REENTRANCY) {
+ if (lt->reentrancy < AST_MAX_REENTRANCY) {
lt->backtrace[lt->reentrancy] = tmp;
bt = <->backtrace[lt->reentrancy];
}
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;
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 */
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
-#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
- int canlog = strcmp(filename, "logger.c") & t->tracking;
-
- if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- /* Don't warn abount uninitialized lock.
- * Simple try to initialize it.
- * May be not needed in linux system.
- */
- res = __ast_rwlock_init(t->tracking, filename, line, func, name, t);
- if ((t->lock) == ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- __ast_mutex_logger("%s line %d (%s): Error: rwlock '%s' is uninitialized and unable to initialize.\n",
- filename, line, func, name);
- return res;
- }
- }
-#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;
ast_bt_get_addresses(&tmp);
ast_reentrancy_lock(lt);
- if (lt->reentrancy != AST_MAX_REENTRANCY) {
+ if (lt->reentrancy < AST_MAX_REENTRANCY) {
lt->backtrace[lt->reentrancy] = tmp;
bt = <->backtrace[lt->reentrancy];
}
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;
}
ast_reentrancy_unlock(lt);
ast_mark_lock_acquired(t);
- } else if (t->tracking) {
+ } else if (lt) {
ast_mark_lock_failed(t);
}
#endif /* DEBUG_THREADS */