12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331 |
- /*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 1999 - 2010, Digium, Inc.
- *
- * Mark Spencer <markster@digium.com>
- *
- * See http://www.asterisk.org for more information about
- * the Asterisk project. Please do not directly contact
- * any of the maintainers of this project for assistance;
- * the project provides a web site, mailing lists and IRC
- * channels for your use.
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License Version 2. See the LICENSE file
- * at the top of the source tree.
- */
- /*! \file
- *
- * \brief General Asterisk locking.
- */
- /*** MODULEINFO
- <support_level>core</support_level>
- ***/
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "asterisk/utils.h"
- #include "asterisk/lock.h"
- /* Allow direct use of pthread_mutex_* / pthread_cond_* */
- #undef pthread_mutex_init
- #undef pthread_mutex_destroy
- #undef pthread_mutex_lock
- #undef pthread_mutex_trylock
- #undef pthread_mutex_t
- #undef pthread_mutex_unlock
- #undef pthread_cond_init
- #undef pthread_cond_signal
- #undef pthread_cond_broadcast
- #undef pthread_cond_destroy
- #undef pthread_cond_wait
- #undef pthread_cond_timedwait
- #if defined(DEBUG_THREADS)
- #define log_mutex_error(canlog, ...) \
- do { \
- if (canlog) { \
- ast_log(LOG_ERROR, __VA_ARGS__); \
- } else { \
- fprintf(stderr, __VA_ARGS__); \
- } \
- } while (0)
- #endif
- #if defined(DEBUG_THREADS) && defined(HAVE_BKTR)
- static void __dump_backtrace(struct ast_bt *bt, int canlog)
- {
- char **strings;
- ssize_t i;
- strings = backtrace_symbols(bt->addresses, bt->num_frames);
- for (i = 0; i < bt->num_frames; i++) {
- log_mutex_error(canlog, "%s\n", strings[i]);
- }
- ast_std_free(strings);
- }
- #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,
- struct ast_lock_track_flags *flags, int no_setup)
- {
- pthread_mutexattr_t reentr_attr;
- struct ast_lock_track *lt;
- if (!flags->tracking || flags->setup) {
- return *plt;
- }
- pthread_mutex_lock(&reentrancy_lock.mutex);
- if (*plt) {
- pthread_mutex_unlock(&reentrancy_lock.mutex);
- return *plt;
- }
- if (no_setup) {
- pthread_mutex_unlock(&reentrancy_lock.mutex);
- return NULL;
- }
- 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);
- flags->setup = 1;
- 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)
- {
- int res;
- pthread_mutexattr_t attr;
- #ifdef DEBUG_THREADS
- #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 = tracking && strcmp(filename, "logger.c");
- log_mutex_error(canlog, "%s line %d (%s): NOTICE: mutex '%s' is already initialized.\n",
- filename, lineno, func, mutex_name);
- DO_THREAD_CRASH;
- return EBUSY;
- }
- #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
- t->track = NULL;
- t->flags.tracking = tracking;
- t->flags.setup = 0;
- #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 __ast_pthread_mutex_destroy(const char *filename, int lineno, const char *func,
- const char *mutex_name, ast_mutex_t *t)
- {
- int res;
- #ifdef DEBUG_THREADS
- struct ast_lock_track *lt = ast_get_reentrancy(&t->track, &t->flags, 1);
- int canlog = t->flags.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 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.
- */
- log_mutex_error(canlog, "%s line %d (%s): NOTICE: mutex '%s' is uninitialized.\n",
- filename, lineno, func, mutex_name);
- DO_THREAD_CRASH;
- res = EINVAL;
- goto lt_cleanup;
- }
- #endif
- res = pthread_mutex_trylock(&t->mutex);
- switch (res) {
- case 0:
- pthread_mutex_unlock(&t->mutex);
- break;
- case EINVAL:
- log_mutex_error(canlog, "%s line %d (%s): Error: attempt to destroy invalid mutex '%s'.\n",
- filename, lineno, func, mutex_name);
- break;
- case EBUSY:
- log_mutex_error(canlog, "%s line %d (%s): Error: attempt to destroy locked mutex '%s'.\n",
- filename, lineno, func, mutex_name);
- if (lt) {
- ast_reentrancy_lock(lt);
- log_mutex_error(canlog, "%s line %d (%s): Error: '%s' was locked here.\n",
- lt->file[ROFFSET], lt->lineno[ROFFSET], lt->func[ROFFSET], mutex_name);
- #ifdef HAVE_BKTR
- __dump_backtrace(<->backtrace[ROFFSET], canlog);
- #endif
- ast_reentrancy_unlock(lt);
- }
- break;
- }
- #endif /* DEBUG_THREADS */
- res = pthread_mutex_destroy(&t->mutex);
- #ifdef DEBUG_THREADS
- if (res) {
- log_mutex_error(canlog, "%s line %d (%s): Error destroying mutex %s: %s\n",
- filename, lineno, func, mutex_name, strerror(res));
- }
- #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;
- lt->func[0] = func;
- lt->reentrancy = 0;
- lt->thread_id[0] = 0;
- #ifdef HAVE_BKTR
- memset(<->backtrace[0], 0, sizeof(lt->backtrace[0]));
- #endif
- ast_reentrancy_unlock(lt);
- delete_reentrancy_cs(&t->track);
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
- int __ast_pthread_mutex_lock(const char *filename, int lineno, const char *func,
- const char* mutex_name, ast_mutex_t *t)
- {
- int res;
- #ifdef DEBUG_THREADS
- struct ast_lock_track *lt = ast_get_reentrancy(&t->track, &t->flags, 0);
- int canlog = t->flags.tracking && strcmp(filename, "logger.c");
- #ifdef HAVE_BKTR
- struct ast_bt *bt = NULL;
- #endif
- if (lt) {
- #ifdef HAVE_BKTR
- struct ast_bt tmp;
- /* The implementation of backtrace() may have its own locks.
- * Capture the backtrace outside of the reentrancy lock to
- * avoid deadlocks. See ASTERISK-22455. */
- ast_bt_get_addresses(&tmp);
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->backtrace[lt->reentrancy] = tmp;
- bt = <->backtrace[lt->reentrancy];
- }
- ast_reentrancy_unlock(lt);
- ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t, bt);
- #else
- ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t);
- #endif
- }
- #endif /* DEBUG_THREADS */
- #if defined(DETECT_DEADLOCKS) && defined(DEBUG_THREADS)
- {
- time_t seconds = time(NULL);
- time_t wait_time, reported_wait = 0;
- do {
- #ifdef HAVE_MTX_PROFILE
- ast_mark(mtx_prof, 1);
- #endif
- res = pthread_mutex_trylock(&t->mutex);
- #ifdef HAVE_MTX_PROFILE
- ast_mark(mtx_prof, 0);
- #endif
- if (res == EBUSY) {
- wait_time = time(NULL) - seconds;
- if (wait_time > reported_wait && (wait_time % 5) == 0) {
- log_mutex_error(canlog, "%s line %d (%s): Deadlock? waited %d sec for mutex '%s'?\n",
- filename, lineno, func, (int) wait_time, mutex_name);
- if (lt) {
- ast_reentrancy_lock(lt);
- #ifdef HAVE_BKTR
- __dump_backtrace(<->backtrace[lt->reentrancy], canlog);
- #endif
- log_mutex_error(canlog, "%s line %d (%s): '%s' was locked here.\n",
- lt->file[ROFFSET], lt->lineno[ROFFSET],
- lt->func[ROFFSET], mutex_name);
- #ifdef HAVE_BKTR
- __dump_backtrace(<->backtrace[ROFFSET], canlog);
- #endif
- ast_reentrancy_unlock(lt);
- }
- reported_wait = wait_time;
- }
- usleep(200);
- }
- } while (res == EBUSY);
- }
- #else /* !DETECT_DEADLOCKS || !DEBUG_THREADS */
- #ifdef HAVE_MTX_PROFILE
- ast_mark(mtx_prof, 1);
- res = pthread_mutex_trylock(&t->mutex);
- ast_mark(mtx_prof, 0);
- if (res)
- #endif
- res = pthread_mutex_lock(&t->mutex);
- #endif /* !DETECT_DEADLOCKS || !DEBUG_THREADS */
- #ifdef DEBUG_THREADS
- if (lt && !res) {
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->file[lt->reentrancy] = filename;
- lt->lineno[lt->reentrancy] = lineno;
- lt->func[lt->reentrancy] = func;
- lt->thread_id[lt->reentrancy] = pthread_self();
- lt->reentrancy++;
- } else {
- log_mutex_error(canlog, "%s line %d (%s): '%s' really deep reentrancy!\n",
- filename, lineno, func, mutex_name);
- }
- ast_reentrancy_unlock(lt);
- ast_mark_lock_acquired(t);
- } else if (lt) {
- #ifdef HAVE_BKTR
- if (lt->reentrancy) {
- ast_reentrancy_lock(lt);
- bt = <->backtrace[lt->reentrancy-1];
- ast_reentrancy_unlock(lt);
- } else {
- bt = NULL;
- }
- ast_remove_lock_info(t, bt);
- #else
- ast_remove_lock_info(t);
- #endif
- }
- if (res) {
- log_mutex_error(canlog, "%s line %d (%s): Error obtaining mutex: %s\n",
- filename, lineno, func, strerror(res));
- DO_THREAD_CRASH;
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
- int __ast_pthread_mutex_trylock(const char *filename, int lineno, const char *func,
- const char* mutex_name, ast_mutex_t *t)
- {
- int res;
- #ifdef DEBUG_THREADS
- struct ast_lock_track *lt = ast_get_reentrancy(&t->track, &t->flags, 0);
- int canlog = t->flags.tracking && strcmp(filename, "logger.c");
- #ifdef HAVE_BKTR
- struct ast_bt *bt = NULL;
- #endif
- if (lt) {
- #ifdef HAVE_BKTR
- struct ast_bt tmp;
- /* The implementation of backtrace() may have its own locks.
- * Capture the backtrace outside of the reentrancy lock to
- * avoid deadlocks. See ASTERISK-22455. */
- ast_bt_get_addresses(&tmp);
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->backtrace[lt->reentrancy] = tmp;
- bt = <->backtrace[lt->reentrancy];
- }
- ast_reentrancy_unlock(lt);
- ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t, bt);
- #else
- ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, t);
- #endif
- }
- #endif /* DEBUG_THREADS */
- res = pthread_mutex_trylock(&t->mutex);
- #ifdef DEBUG_THREADS
- if (lt && !res) {
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->file[lt->reentrancy] = filename;
- lt->lineno[lt->reentrancy] = lineno;
- lt->func[lt->reentrancy] = func;
- lt->thread_id[lt->reentrancy] = pthread_self();
- lt->reentrancy++;
- } else {
- log_mutex_error(canlog, "%s line %d (%s): '%s' really deep reentrancy!\n",
- filename, lineno, func, mutex_name);
- }
- ast_reentrancy_unlock(lt);
- ast_mark_lock_acquired(t);
- } else if (lt) {
- ast_mark_lock_failed(t);
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
- int __ast_pthread_mutex_unlock(const char *filename, int lineno, const char *func,
- const char *mutex_name, ast_mutex_t *t)
- {
- int res;
- #ifdef DEBUG_THREADS
- struct ast_lock_track *lt = NULL;
- int canlog = t->flags.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)) {
- log_mutex_error(canlog, "%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
- filename, lineno, func, mutex_name);
- DO_THREAD_CRASH;
- return EINVAL;
- }
- #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
- lt = ast_get_reentrancy(&t->track, &t->flags, 0);
- if (lt) {
- ast_reentrancy_lock(lt);
- if (lt->reentrancy && (lt->thread_id[ROFFSET] != pthread_self())) {
- log_mutex_error(canlog, "%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
- filename, lineno, func, mutex_name);
- log_mutex_error(canlog, "%s line %d (%s): '%s' was locked here.\n",
- lt->file[ROFFSET], lt->lineno[ROFFSET], lt->func[ROFFSET], mutex_name);
- #ifdef HAVE_BKTR
- __dump_backtrace(<->backtrace[ROFFSET], canlog);
- #endif
- DO_THREAD_CRASH;
- }
- if (--lt->reentrancy < 0) {
- log_mutex_error(canlog, "%s line %d (%s): mutex '%s' freed more times than we've locked!\n",
- filename, lineno, func, mutex_name);
- lt->reentrancy = 0;
- }
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->file[lt->reentrancy] = NULL;
- lt->lineno[lt->reentrancy] = 0;
- lt->func[lt->reentrancy] = NULL;
- lt->thread_id[lt->reentrancy] = 0;
- }
- #ifdef HAVE_BKTR
- if (lt->reentrancy) {
- bt = <->backtrace[lt->reentrancy - 1];
- }
- #endif
- ast_reentrancy_unlock(lt);
- #ifdef HAVE_BKTR
- ast_remove_lock_info(t, bt);
- #else
- ast_remove_lock_info(t);
- #endif
- }
- #endif /* DEBUG_THREADS */
- res = pthread_mutex_unlock(&t->mutex);
- #ifdef DEBUG_THREADS
- if (res) {
- log_mutex_error(canlog, "%s line %d (%s): Error releasing mutex: %s\n",
- filename, lineno, func, strerror(res));
- DO_THREAD_CRASH;
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
- int __ast_cond_init(const char *filename, int lineno, const char *func,
- const char *cond_name, ast_cond_t *cond, pthread_condattr_t *cond_attr)
- {
- return pthread_cond_init(cond, cond_attr);
- }
- int __ast_cond_signal(const char *filename, int lineno, const char *func,
- const char *cond_name, ast_cond_t *cond)
- {
- return pthread_cond_signal(cond);
- }
- int __ast_cond_broadcast(const char *filename, int lineno, const char *func,
- const char *cond_name, ast_cond_t *cond)
- {
- return pthread_cond_broadcast(cond);
- }
- int __ast_cond_destroy(const char *filename, int lineno, const char *func,
- const char *cond_name, ast_cond_t *cond)
- {
- 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_id, lt_saved->thread_id, sizeof(lt->thread_id));
- #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 = NULL;
- struct ast_lock_track lt_orig;
- int canlog = t->flags.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)) {
- log_mutex_error(canlog, "%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
- filename, lineno, func, mutex_name);
- DO_THREAD_CRASH;
- return EINVAL;
- }
- #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
- lt = ast_get_reentrancy(&t->track, &t->flags, 0);
- if (lt) {
- ast_reentrancy_lock(lt);
- if (lt->reentrancy && (lt->thread_id[ROFFSET] != pthread_self())) {
- log_mutex_error(canlog, "%s line %d (%s): attempted wait using mutex '%s' without owning it!\n",
- filename, lineno, func, mutex_name);
- log_mutex_error(canlog, "%s line %d (%s): '%s' was locked here.\n",
- lt->file[ROFFSET], lt->lineno[ROFFSET], lt->func[ROFFSET], mutex_name);
- #ifdef HAVE_BKTR
- __dump_backtrace(<->backtrace[ROFFSET], canlog);
- #endif
- DO_THREAD_CRASH;
- } else if (lt->reentrancy <= 0) {
- log_mutex_error(canlog, "%s line %d (%s): attempted wait using an unlocked mutex '%s'\n",
- filename, lineno, func, mutex_name);
- DO_THREAD_CRASH;
- }
- /* Waiting on a condition completely suspends a recursive mutex,
- * even if it's been recursively locked multiple times. Make a
- * copy of the lock tracking, and reset reentrancy to zero */
- lt_orig = *lt;
- lt->reentrancy = 0;
- ast_reentrancy_unlock(lt);
- ast_suspend_lock_info(t);
- }
- #endif /* DEBUG_THREADS */
- res = pthread_cond_wait(cond, &t->mutex);
- #ifdef DEBUG_THREADS
- if (res) {
- log_mutex_error(canlog, "%s line %d (%s): Error waiting on condition mutex '%s'\n",
- filename, lineno, func, strerror(res));
- DO_THREAD_CRASH;
- } else if (lt) {
- restore_lock_tracking(lt, <_orig);
- ast_restore_lock_info(t);
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
- int __ast_cond_timedwait(const char *filename, int lineno, const char *func,
- const char *cond_name, const char *mutex_name, ast_cond_t *cond,
- ast_mutex_t *t, const struct timespec *abstime)
- {
- int res;
- #ifdef DEBUG_THREADS
- struct ast_lock_track *lt = NULL;
- struct ast_lock_track lt_orig;
- int canlog = t->flags.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)) {
- log_mutex_error(canlog, "%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
- filename, lineno, func, mutex_name);
- DO_THREAD_CRASH;
- return EINVAL;
- }
- #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
- lt = ast_get_reentrancy(&t->track, &t->flags, 0);
- if (lt) {
- ast_reentrancy_lock(lt);
- if (lt->reentrancy && (lt->thread_id[ROFFSET] != pthread_self())) {
- log_mutex_error(canlog, "%s line %d (%s): attempted wait using mutex '%s' without owning it!\n",
- filename, lineno, func, mutex_name);
- log_mutex_error(canlog, "%s line %d (%s): '%s' was locked here.\n",
- lt->file[ROFFSET], lt->lineno[ROFFSET], lt->func[ROFFSET], mutex_name);
- #ifdef HAVE_BKTR
- __dump_backtrace(<->backtrace[ROFFSET], canlog);
- #endif
- DO_THREAD_CRASH;
- } else if (lt->reentrancy <= 0) {
- log_mutex_error(canlog, "%s line %d (%s): attempted wait using an unlocked mutex '%s'\n",
- filename, lineno, func, mutex_name);
- DO_THREAD_CRASH;
- }
- /* Waiting on a condition completely suspends a recursive mutex,
- * even if it's been recursively locked multiple times. Make a
- * copy of the lock tracking, and reset reentrancy to zero */
- lt_orig = *lt;
- lt->reentrancy = 0;
- ast_reentrancy_unlock(lt);
- ast_suspend_lock_info(t);
- }
- #endif /* DEBUG_THREADS */
- res = pthread_cond_timedwait(cond, &t->mutex, abstime);
- #ifdef DEBUG_THREADS
- if (res && (res != ETIMEDOUT)) {
- log_mutex_error(canlog, "%s line %d (%s): Error waiting on condition mutex '%s'\n",
- filename, lineno, func, strerror(res));
- DO_THREAD_CRASH;
- } else if (lt) {
- restore_lock_tracking(lt, <_orig);
- ast_restore_lock_info(t);
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
- int __ast_rwlock_init(int tracking, const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
- {
- int res;
- pthread_rwlockattr_t attr;
- #ifdef DEBUG_THREADS
- #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
- if (t->lock != ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
- int canlog = tracking && strcmp(filename, "logger.c");
- log_mutex_error(canlog, "%s line %d (%s): Warning: rwlock '%s' is already initialized.\n",
- filename, lineno, func, rwlock_name);
- DO_THREAD_CRASH;
- return EBUSY;
- }
- #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
- t->track = NULL;
- t->flags.tracking = tracking;
- t->flags.setup = 0;
- #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;
- }
- int __ast_rwlock_destroy(const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
- {
- int res;
- #ifdef DEBUG_THREADS
- struct ast_lock_track *lt = ast_get_reentrancy(&t->track, &t->flags, 1);
- int canlog = t->flags.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)) {
- log_mutex_error(canlog, "%s line %d (%s): Warning: rwlock '%s' is uninitialized.\n",
- filename, lineno, func, rwlock_name);
- DO_THREAD_CRASH;
- res = EINVAL;
- goto lt_cleanup;
- }
- #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
- #endif /* DEBUG_THREADS */
- res = pthread_rwlock_destroy(&t->lock);
- #ifdef DEBUG_THREADS
- if (res) {
- log_mutex_error(canlog, "%s line %d (%s): Error destroying rwlock %s: %s\n",
- filename, lineno, func, rwlock_name, strerror(res));
- }
- #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;
- lt->func[0] = func;
- lt->reentrancy = 0;
- lt->thread_id[0] = 0;
- #ifdef HAVE_BKTR
- memset(<->backtrace[0], 0, sizeof(lt->backtrace[0]));
- #endif
- ast_reentrancy_unlock(lt);
- delete_reentrancy_cs(&t->track);
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
- int __ast_rwlock_unlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name)
- {
- int res;
- #ifdef DEBUG_THREADS
- struct ast_lock_track *lt = NULL;
- int canlog = t->flags.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)) {
- log_mutex_error(canlog, "%s line %d (%s): Warning: rwlock '%s' is uninitialized.\n",
- filename, line, func, name);
- DO_THREAD_CRASH;
- return EINVAL;
- }
- #endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
- lt = ast_get_reentrancy(&t->track, &t->flags, 0);
- if (lt) {
- ast_reentrancy_lock(lt);
- if (lt->reentrancy) {
- int i;
- pthread_t self = pthread_self();
- for (i = lt->reentrancy - 1; i >= 0; --i) {
- if (lt->thread_id[i] == self) {
- lock_found = 1;
- if (i != lt->reentrancy - 1) {
- lt->file[i] = lt->file[lt->reentrancy - 1];
- lt->lineno[i] = lt->lineno[lt->reentrancy - 1];
- lt->func[i] = lt->func[lt->reentrancy - 1];
- lt->thread_id[i] = lt->thread_id[lt->reentrancy - 1];
- }
- #ifdef HAVE_BKTR
- bt = <->backtrace[i];
- #endif
- lt->file[lt->reentrancy - 1] = NULL;
- lt->lineno[lt->reentrancy - 1] = 0;
- lt->func[lt->reentrancy - 1] = NULL;
- lt->thread_id[lt->reentrancy - 1] = AST_PTHREADT_NULL;
- break;
- }
- }
- }
- if (lock_found && --lt->reentrancy < 0) {
- log_mutex_error(canlog, "%s line %d (%s): rwlock '%s' freed more times than we've locked!\n",
- filename, line, func, name);
- lt->reentrancy = 0;
- }
- ast_reentrancy_unlock(lt);
- #ifdef HAVE_BKTR
- ast_remove_lock_info(t, bt);
- #else
- ast_remove_lock_info(t);
- #endif
- }
- #endif /* DEBUG_THREADS */
- res = pthread_rwlock_unlock(&t->lock);
- #ifdef DEBUG_THREADS
- if (res) {
- log_mutex_error(canlog, "%s line %d (%s): Error releasing rwlock: %s\n",
- filename, line, func, strerror(res));
- DO_THREAD_CRASH;
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
- int __ast_rwlock_rdlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name)
- {
- int res;
- #ifdef DEBUG_THREADS
- struct ast_lock_track *lt = ast_get_reentrancy(&t->track, &t->flags, 0);
- int canlog = t->flags.tracking && strcmp(filename, "logger.c");
- #ifdef HAVE_BKTR
- struct ast_bt *bt = NULL;
- #endif
- if (lt) {
- #ifdef HAVE_BKTR
- struct ast_bt tmp;
- /* The implementation of backtrace() may have its own locks.
- * Capture the backtrace outside of the reentrancy lock to
- * avoid deadlocks. See ASTERISK-22455. */
- ast_bt_get_addresses(&tmp);
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->backtrace[lt->reentrancy] = tmp;
- bt = <->backtrace[lt->reentrancy];
- }
- ast_reentrancy_unlock(lt);
- ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t, bt);
- #else
- ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t);
- #endif
- }
- #endif /* DEBUG_THREADS */
- #if defined(DETECT_DEADLOCKS) && defined(DEBUG_THREADS)
- {
- time_t seconds = time(NULL);
- time_t wait_time, reported_wait = 0;
- do {
- res = pthread_rwlock_tryrdlock(&t->lock);
- if (res == EBUSY) {
- wait_time = time(NULL) - seconds;
- if (wait_time > reported_wait && (wait_time % 5) == 0) {
- log_mutex_error(canlog, "%s line %d (%s): Deadlock? waited %d sec for readlock '%s'?\n",
- filename, line, func, (int)wait_time, name);
- if (lt) {
- ast_reentrancy_lock(lt);
- #ifdef HAVE_BKTR
- __dump_backtrace(<->backtrace[lt->reentrancy], canlog);
- #endif
- log_mutex_error(canlog, "%s line %d (%s): '%s' was locked here.\n",
- lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1],
- lt->func[lt->reentrancy-1], name);
- #ifdef HAVE_BKTR
- __dump_backtrace(<->backtrace[lt->reentrancy-1], canlog);
- #endif
- ast_reentrancy_unlock(lt);
- }
- reported_wait = wait_time;
- }
- usleep(200);
- }
- } while (res == EBUSY);
- }
- #else /* !DETECT_DEADLOCKS || !DEBUG_THREADS */
- res = pthread_rwlock_rdlock(&t->lock);
- #endif /* !DETECT_DEADLOCKS || !DEBUG_THREADS */
- #ifdef DEBUG_THREADS
- if (!res && lt) {
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->file[lt->reentrancy] = filename;
- lt->lineno[lt->reentrancy] = line;
- lt->func[lt->reentrancy] = func;
- lt->thread_id[lt->reentrancy] = pthread_self();
- lt->reentrancy++;
- }
- ast_reentrancy_unlock(lt);
- ast_mark_lock_acquired(t);
- } else if (lt) {
- #ifdef HAVE_BKTR
- if (lt->reentrancy) {
- ast_reentrancy_lock(lt);
- bt = <->backtrace[lt->reentrancy-1];
- ast_reentrancy_unlock(lt);
- } else {
- bt = NULL;
- }
- ast_remove_lock_info(t, bt);
- #else
- ast_remove_lock_info(t);
- #endif
- }
- if (res) {
- log_mutex_error(canlog, "%s line %d (%s): Error obtaining read lock: %s\n",
- filename, line, func, strerror(res));
- DO_THREAD_CRASH;
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
- int __ast_rwlock_wrlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name)
- {
- int res;
- #ifdef DEBUG_THREADS
- struct ast_lock_track *lt = ast_get_reentrancy(&t->track, &t->flags, 0);
- int canlog = t->flags.tracking && strcmp(filename, "logger.c");
- #ifdef HAVE_BKTR
- struct ast_bt *bt = NULL;
- #endif
- if (lt) {
- #ifdef HAVE_BKTR
- struct ast_bt tmp;
- /* The implementation of backtrace() may have its own locks.
- * Capture the backtrace outside of the reentrancy lock to
- * avoid deadlocks. See ASTERISK-22455. */
- ast_bt_get_addresses(&tmp);
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->backtrace[lt->reentrancy] = tmp;
- bt = <->backtrace[lt->reentrancy];
- }
- ast_reentrancy_unlock(lt);
- ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
- #else
- ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
- #endif
- }
- #endif /* DEBUG_THREADS */
- #if defined(DETECT_DEADLOCKS) && defined(DEBUG_THREADS)
- {
- time_t seconds = time(NULL);
- time_t wait_time, reported_wait = 0;
- do {
- res = pthread_rwlock_trywrlock(&t->lock);
- if (res == EBUSY) {
- wait_time = time(NULL) - seconds;
- if (wait_time > reported_wait && (wait_time % 5) == 0) {
- log_mutex_error(canlog, "%s line %d (%s): Deadlock? waited %d sec for writelock '%s'?\n",
- filename, line, func, (int)wait_time, name);
- if (lt) {
- ast_reentrancy_lock(lt);
- #ifdef HAVE_BKTR
- __dump_backtrace(<->backtrace[lt->reentrancy], canlog);
- #endif
- log_mutex_error(canlog, "%s line %d (%s): '%s' was locked here.\n",
- lt->file[lt->reentrancy-1], lt->lineno[lt->reentrancy-1],
- lt->func[lt->reentrancy-1], name);
- #ifdef HAVE_BKTR
- __dump_backtrace(<->backtrace[lt->reentrancy-1], canlog);
- #endif
- ast_reentrancy_unlock(lt);
- }
- reported_wait = wait_time;
- }
- usleep(200);
- }
- } while (res == EBUSY);
- }
- #else /* !DETECT_DEADLOCKS || !DEBUG_THREADS */
- res = pthread_rwlock_wrlock(&t->lock);
- #endif /* !DETECT_DEADLOCKS || !DEBUG_THREADS */
- #ifdef DEBUG_THREADS
- if (!res && lt) {
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->file[lt->reentrancy] = filename;
- lt->lineno[lt->reentrancy] = line;
- lt->func[lt->reentrancy] = func;
- lt->thread_id[lt->reentrancy] = pthread_self();
- lt->reentrancy++;
- }
- ast_reentrancy_unlock(lt);
- ast_mark_lock_acquired(t);
- } else if (lt) {
- #ifdef HAVE_BKTR
- if (lt->reentrancy) {
- ast_reentrancy_lock(lt);
- bt = <->backtrace[lt->reentrancy-1];
- ast_reentrancy_unlock(lt);
- } else {
- bt = NULL;
- }
- ast_remove_lock_info(t, bt);
- #else
- ast_remove_lock_info(t);
- #endif
- }
- if (res) {
- log_mutex_error(canlog, "%s line %d (%s): Error obtaining write lock: %s\n",
- filename, line, func, strerror(res));
- DO_THREAD_CRASH;
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
- int __ast_rwlock_timedrdlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name,
- const struct timespec *abs_timeout)
- {
- int res;
- #ifdef DEBUG_THREADS
- struct ast_lock_track *lt = ast_get_reentrancy(&t->track, &t->flags, 0);
- int canlog = t->flags.tracking && strcmp(filename, "logger.c");
- #ifdef HAVE_BKTR
- struct ast_bt *bt = NULL;
- #endif
- if (lt) {
- #ifdef HAVE_BKTR
- struct ast_bt tmp;
- /* The implementation of backtrace() may have its own locks.
- * Capture the backtrace outside of the reentrancy lock to
- * avoid deadlocks. See ASTERISK-22455. */
- ast_bt_get_addresses(&tmp);
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->backtrace[lt->reentrancy] = tmp;
- bt = <->backtrace[lt->reentrancy];
- }
- ast_reentrancy_unlock(lt);
- ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
- #else
- ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
- #endif
- }
- #endif /* DEBUG_THREADS */
- #ifdef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
- res = pthread_rwlock_timedrdlock(&t->lock, abs_timeout);
- #else
- do {
- struct timeval _now;
- for (;;) {
- if (!(res = pthread_rwlock_tryrdlock(&t->lock))) {
- break;
- }
- _now = ast_tvnow();
- if (_now.tv_sec > abs_timeout->tv_sec || (_now.tv_sec == abs_timeout->tv_sec && _now.tv_usec * 1000 > abs_timeout->tv_nsec)) {
- break;
- }
- usleep(1);
- }
- } while (0);
- #endif
- #ifdef DEBUG_THREADS
- if (!res && lt) {
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->file[lt->reentrancy] = filename;
- lt->lineno[lt->reentrancy] = line;
- lt->func[lt->reentrancy] = func;
- lt->thread_id[lt->reentrancy] = pthread_self();
- lt->reentrancy++;
- }
- ast_reentrancy_unlock(lt);
- ast_mark_lock_acquired(t);
- } else if (lt) {
- #ifdef HAVE_BKTR
- if (lt->reentrancy) {
- ast_reentrancy_lock(lt);
- bt = <->backtrace[lt->reentrancy-1];
- ast_reentrancy_unlock(lt);
- } else {
- bt = NULL;
- }
- ast_remove_lock_info(t, bt);
- #else
- ast_remove_lock_info(t);
- #endif
- }
- if (res) {
- log_mutex_error(canlog, "%s line %d (%s): Error obtaining read lock: %s\n",
- filename, line, func, strerror(res));
- DO_THREAD_CRASH;
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
- int __ast_rwlock_timedwrlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name,
- const struct timespec *abs_timeout)
- {
- int res;
- #ifdef DEBUG_THREADS
- struct ast_lock_track *lt = ast_get_reentrancy(&t->track, &t->flags, 0);
- int canlog = t->flags.tracking && strcmp(filename, "logger.c");
- #ifdef HAVE_BKTR
- struct ast_bt *bt = NULL;
- #endif
- if (lt) {
- #ifdef HAVE_BKTR
- struct ast_bt tmp;
- /* The implementation of backtrace() may have its own locks.
- * Capture the backtrace outside of the reentrancy lock to
- * avoid deadlocks. See ASTERISK-22455. */
- ast_bt_get_addresses(&tmp);
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->backtrace[lt->reentrancy] = tmp;
- bt = <->backtrace[lt->reentrancy];
- }
- ast_reentrancy_unlock(lt);
- ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
- #else
- ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
- #endif
- }
- #endif /* DEBUG_THREADS */
- #ifdef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
- res = pthread_rwlock_timedwrlock(&t->lock, abs_timeout);
- #else
- do {
- struct timeval _now;
- for (;;) {
- if (!(res = pthread_rwlock_trywrlock(&t->lock))) {
- break;
- }
- _now = ast_tvnow();
- if (_now.tv_sec > abs_timeout->tv_sec || (_now.tv_sec == abs_timeout->tv_sec && _now.tv_usec * 1000 > abs_timeout->tv_nsec)) {
- break;
- }
- usleep(1);
- }
- } while (0);
- #endif
- #ifdef DEBUG_THREADS
- if (!res && lt) {
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->file[lt->reentrancy] = filename;
- lt->lineno[lt->reentrancy] = line;
- lt->func[lt->reentrancy] = func;
- lt->thread_id[lt->reentrancy] = pthread_self();
- lt->reentrancy++;
- }
- ast_reentrancy_unlock(lt);
- ast_mark_lock_acquired(t);
- } else if (lt) {
- #ifdef HAVE_BKTR
- if (lt->reentrancy) {
- ast_reentrancy_lock(lt);
- bt = <->backtrace[lt->reentrancy-1];
- ast_reentrancy_unlock(lt);
- } else {
- bt = NULL;
- }
- ast_remove_lock_info(t, bt);
- #else
- ast_remove_lock_info(t);
- #endif
- }
- if (res) {
- log_mutex_error(canlog, "%s line %d (%s): Error obtaining read lock: %s\n",
- filename, line, func, strerror(res));
- DO_THREAD_CRASH;
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
- int __ast_rwlock_tryrdlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name)
- {
- int res;
- #ifdef DEBUG_THREADS
- struct ast_lock_track *lt = ast_get_reentrancy(&t->track, &t->flags, 0);
- #ifdef HAVE_BKTR
- struct ast_bt *bt = NULL;
- #endif
- if (lt) {
- #ifdef HAVE_BKTR
- struct ast_bt tmp;
- /* The implementation of backtrace() may have its own locks.
- * Capture the backtrace outside of the reentrancy lock to
- * avoid deadlocks. See ASTERISK-22455. */
- ast_bt_get_addresses(&tmp);
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->backtrace[lt->reentrancy] = tmp;
- bt = <->backtrace[lt->reentrancy];
- }
- ast_reentrancy_unlock(lt);
- ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t, bt);
- #else
- ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t);
- #endif
- }
- #endif /* DEBUG_THREADS */
- res = pthread_rwlock_tryrdlock(&t->lock);
- #ifdef DEBUG_THREADS
- if (!res && lt) {
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->file[lt->reentrancy] = filename;
- lt->lineno[lt->reentrancy] = line;
- lt->func[lt->reentrancy] = func;
- lt->thread_id[lt->reentrancy] = pthread_self();
- lt->reentrancy++;
- }
- ast_reentrancy_unlock(lt);
- ast_mark_lock_acquired(t);
- } else if (lt) {
- ast_mark_lock_failed(t);
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
- int __ast_rwlock_trywrlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name)
- {
- int res;
- #ifdef DEBUG_THREADS
- struct ast_lock_track *lt = ast_get_reentrancy(&t->track, &t->flags, 0);
- #ifdef HAVE_BKTR
- struct ast_bt *bt = NULL;
- #endif
- if (lt) {
- #ifdef HAVE_BKTR
- struct ast_bt tmp;
- /* The implementation of backtrace() may have its own locks.
- * Capture the backtrace outside of the reentrancy lock to
- * avoid deadlocks. See ASTERISK-22455. */
- ast_bt_get_addresses(&tmp);
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->backtrace[lt->reentrancy] = tmp;
- bt = <->backtrace[lt->reentrancy];
- }
- ast_reentrancy_unlock(lt);
- ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t, bt);
- #else
- ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
- #endif
- }
- #endif /* DEBUG_THREADS */
- res = pthread_rwlock_trywrlock(&t->lock);
- #ifdef DEBUG_THREADS
- if (!res && lt) {
- ast_reentrancy_lock(lt);
- if (lt->reentrancy < AST_MAX_REENTRANCY) {
- lt->file[lt->reentrancy] = filename;
- lt->lineno[lt->reentrancy] = line;
- lt->func[lt->reentrancy] = func;
- lt->thread_id[lt->reentrancy] = pthread_self();
- lt->reentrancy++;
- }
- ast_reentrancy_unlock(lt);
- ast_mark_lock_acquired(t);
- } else if (lt) {
- ast_mark_lock_failed(t);
- }
- #endif /* DEBUG_THREADS */
- return res;
- }
|