diff options
author | tilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b> | 2008-05-29 17:42:28 +0000 |
---|---|---|
committer | tilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b> | 2008-05-29 17:42:28 +0000 |
commit | 5f08a3ee3a2125e69352c5404d7f57bed9b480f6 (patch) | |
tree | 1423c2f41c452512d73905e0542e62bb6c598735 | |
parent | 119443fc3f48cf7d749e58ea9c7d47af6511a3b4 (diff) |
Merged revisions 118955,118957 via svnmerge from
https://origsvn.digium.com/svn/asterisk/trunk
................
r118955 | tilghman | 2008-05-29 12:35:19 -0500 (Thu, 29 May 2008) | 11 lines
Merged revisions 118953 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r118953 | tilghman | 2008-05-29 12:20:16 -0500 (Thu, 29 May 2008) | 3 lines
Add some debugging code that ensures that when we do deadlock avoidance, we
don't lose the information about how a lock was originally acquired.
........
................
r118957 | tilghman | 2008-05-29 12:39:50 -0500 (Thu, 29 May 2008) | 10 lines
Merged revisions 118954 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r118954 | tilghman | 2008-05-29 12:33:01 -0500 (Thu, 29 May 2008) | 2 lines
Define also when not DEBUG_THREADS
........
................
git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.6.0@118958 f38db490-d61c-443f-a65b-d21fe96a405b
-rw-r--r-- | channels/chan_agent.c | 8 | ||||
-rw-r--r-- | channels/chan_alsa.c | 4 | ||||
-rw-r--r-- | channels/chan_iax2.c | 28 | ||||
-rw-r--r-- | channels/chan_mgcp.c | 8 | ||||
-rw-r--r-- | channels/chan_zap.c | 21 | ||||
-rw-r--r-- | include/asterisk/lock.h | 37 | ||||
-rw-r--r-- | main/utils.c | 28 |
7 files changed, 82 insertions, 52 deletions
diff --git a/channels/chan_agent.c b/channels/chan_agent.c index decf022c4..8d22eadf9 100644 --- a/channels/chan_agent.c +++ b/channels/chan_agent.c @@ -1520,9 +1520,7 @@ static int agent_logoff(const char *agent, int soft) ast_mutex_lock(&p->lock); while (p->owner && ast_channel_trylock(p->owner)) { - ast_mutex_unlock(&p->lock); - usleep(1); - ast_mutex_lock(&p->lock); + DEADLOCK_AVOIDANCE(&p->lock); } if (p->owner) { ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT); @@ -1530,9 +1528,7 @@ static int agent_logoff(const char *agent, int soft) } while (p->chan && ast_channel_trylock(p->chan)) { - ast_mutex_unlock(&p->lock); - usleep(1); - ast_mutex_lock(&p->lock); + DEADLOCK_AVOIDANCE(&p->lock); } if (p->chan) { ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT); diff --git a/channels/chan_alsa.c b/channels/chan_alsa.c index 27c372608..6b831ecad 100644 --- a/channels/chan_alsa.c +++ b/channels/chan_alsa.c @@ -297,9 +297,7 @@ static int alsa_text(struct ast_channel *c, const char *text) static void grab_owner(void) { while (alsa.owner && ast_channel_trylock(alsa.owner)) { - ast_mutex_unlock(&alsalock); - usleep(1); - ast_mutex_lock(&alsalock); + DEADLOCK_AVOIDANCE(&alsalock); } } diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c index 772491111..7e1822557 100644 --- a/channels/chan_iax2.c +++ b/channels/chan_iax2.c @@ -1734,9 +1734,7 @@ static int iax2_queue_frame(int callno, struct ast_frame *f) if (iaxs[callno] && iaxs[callno]->owner) { if (ast_channel_trylock(iaxs[callno]->owner)) { /* Avoid deadlock by pausing and trying again */ - ast_mutex_unlock(&iaxsl[callno]); - usleep(1); - ast_mutex_lock(&iaxsl[callno]); + DEADLOCK_AVOIDANCE(&iaxsl[callno]); } else { ast_queue_frame(iaxs[callno]->owner, f); ast_channel_unlock(iaxs[callno]->owner); @@ -1767,9 +1765,7 @@ static int iax2_queue_hangup(int callno) if (iaxs[callno] && iaxs[callno]->owner) { if (ast_channel_trylock(iaxs[callno]->owner)) { /* Avoid deadlock by pausing and trying again */ - ast_mutex_unlock(&iaxsl[callno]); - usleep(1); - ast_mutex_lock(&iaxsl[callno]); + DEADLOCK_AVOIDANCE(&iaxsl[callno]); } else { ast_queue_hangup(iaxs[callno]->owner); ast_channel_unlock(iaxs[callno]->owner); @@ -1801,9 +1797,7 @@ static int iax2_queue_control_data(int callno, if (iaxs[callno] && iaxs[callno]->owner) { if (ast_channel_trylock(iaxs[callno]->owner)) { /* Avoid deadlock by pausing and trying again */ - ast_mutex_unlock(&iaxsl[callno]); - usleep(1); - ast_mutex_lock(&iaxsl[callno]); + DEADLOCK_AVOIDANCE(&iaxsl[callno]); } else { ast_queue_control_data(iaxs[callno]->owner, control, data, datalen); ast_channel_unlock(iaxs[callno]->owner); @@ -3734,9 +3728,7 @@ static void lock_both(unsigned short callno0, unsigned short callno1) { ast_mutex_lock(&iaxsl[callno0]); while (ast_mutex_trylock(&iaxsl[callno1])) { - ast_mutex_unlock(&iaxsl[callno0]); - usleep(10); - ast_mutex_lock(&iaxsl[callno0]); + DEADLOCK_AVOIDANCE(&iaxsl[callno0]); } } @@ -3927,9 +3919,7 @@ static int iax2_indicate(struct ast_channel *c, int condition, const void *data, /* We don't know the remote side's call number, yet. :( */ int count = 10; while (count-- && pvt && !pvt->peercallno) { - ast_mutex_unlock(&iaxsl[callno]); - usleep(1); - ast_mutex_lock(&iaxsl[callno]); + DEADLOCK_AVOIDANCE(&iaxsl[callno]); pvt = iaxs[callno]; } if (!pvt->peercallno) { @@ -8220,9 +8210,7 @@ static int socket_process(struct iax2_thread *thread) int orignative; retryowner: if (ast_channel_trylock(iaxs[fr->callno]->owner)) { - ast_mutex_unlock(&iaxsl[fr->callno]); - usleep(1); - ast_mutex_lock(&iaxsl[fr->callno]); + DEADLOCK_AVOIDANCE(&iaxsl[fr->callno]); if (iaxs[fr->callno] && iaxs[fr->callno]->owner) goto retryowner; } if (iaxs[fr->callno]) { @@ -8659,9 +8647,7 @@ retryowner: ast_verb(3, "Format for call is %s\n", ast_getformatname(iaxs[fr->callno]->owner->nativeformats)); retryowner2: if (ast_channel_trylock(iaxs[fr->callno]->owner)) { - ast_mutex_unlock(&iaxsl[fr->callno]); - usleep(1); - ast_mutex_lock(&iaxsl[fr->callno]); + DEADLOCK_AVOIDANCE(&iaxsl[fr->callno]); if (iaxs[fr->callno] && iaxs[fr->callno]->owner) goto retryowner2; } diff --git a/channels/chan_mgcp.c b/channels/chan_mgcp.c index 9f334298e..11c8b1032 100644 --- a/channels/chan_mgcp.c +++ b/channels/chan_mgcp.c @@ -594,9 +594,7 @@ static void mgcp_queue_frame(struct mgcp_subchannel *sub, struct ast_frame *f) ast_channel_unlock(sub->owner); break; } else { - ast_mutex_unlock(&sub->lock); - usleep(1); - ast_mutex_lock(&sub->lock); + DEADLOCK_AVOIDANCE(&sub->lock); } } else break; @@ -612,9 +610,7 @@ static void mgcp_queue_hangup(struct mgcp_subchannel *sub) ast_channel_unlock(sub->owner); break; } else { - ast_mutex_unlock(&sub->lock); - usleep(1); - ast_mutex_lock(&sub->lock); + DEADLOCK_AVOIDANCE(&sub->lock); } } else break; diff --git a/channels/chan_zap.c b/channels/chan_zap.c index f96ecc0b1..491c94a2f 100644 --- a/channels/chan_zap.c +++ b/channels/chan_zap.c @@ -877,10 +877,7 @@ static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri) do { res = ast_mutex_trylock(&pri->lock); if (res) { - ast_mutex_unlock(&pvt->lock); - /* Release the lock and try again */ - usleep(1); - ast_mutex_lock(&pvt->lock); + DEADLOCK_AVOIDANCE(&pvt->lock); } } while (res); /* Then break the poll */ @@ -971,9 +968,7 @@ static void wakeup_sub(struct zt_pvt *p, int a, void *pri) for (;;) { if (p->subs[a].owner) { if (ast_channel_trylock(p->subs[a].owner)) { - ast_mutex_unlock(&p->lock); - usleep(1); - ast_mutex_lock(&p->lock); + DEADLOCK_AVOIDANCE(&p->lock); } else { ast_queue_frame(p->subs[a].owner, &ast_null_frame); ast_channel_unlock(p->subs[a].owner); @@ -1020,9 +1015,7 @@ static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *data) for (;;) { if (p->owner) { if (ast_channel_trylock(p->owner)) { - ast_mutex_unlock(&p->lock); - usleep(1); - ast_mutex_lock(&p->lock); + DEADLOCK_AVOIDANCE(&p->lock); } else { ast_queue_frame(p->owner, f); ast_channel_unlock(p->owner); @@ -3637,9 +3630,7 @@ static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock) ast_mutex_lock(&master->lock); if (slave) { while (ast_mutex_trylock(&slave->lock)) { - ast_mutex_unlock(&master->lock); - usleep(1); - ast_mutex_lock(&master->lock); + DEADLOCK_AVOIDANCE(&master->lock); } } } @@ -10121,9 +10112,7 @@ static int pri_hangup_all(struct zt_pvt *p, struct zt_pri *pri) for (x = 0; x < 3; x++) { while (p->subs[x].owner && ast_channel_trylock(p->subs[x].owner)) { redo++; - ast_mutex_unlock(&p->lock); - usleep(1); - ast_mutex_lock(&p->lock); + DEADLOCK_AVOIDANCE(&p->lock); } if (p->subs[x].owner) { ast_queue_hangup(p->subs[x].owner); diff --git a/include/asterisk/lock.h b/include/asterisk/lock.h index edc285a5f..e7dfb3075 100644 --- a/include/asterisk/lock.h +++ b/include/asterisk/lock.h @@ -186,6 +186,38 @@ void ast_remove_lock_info(void *lock_addr); #define ast_remove_lock_info(ignore) #endif +/*! + * \brief retrieve lock info for the specified mutex + * + * this gets called during deadlock avoidance, so that the information may + * be preserved as to what location originally acquired the lock. + */ +#if !defined(LOW_MEMORY) +int ast_find_lock_info(void *lock_addr, const char **filename, int *lineno, const char **func, const char **mutex_name); +#else +#define ast_find_lock_info(a,b,c,d,e) -1 +#endif + +/*! + * \brief Unlock a lock briefly + * + * used during deadlock avoidance, to preserve the original location where + * a lock was originally acquired. + */ +#define DEADLOCK_AVOIDANCE(lock) \ + do { \ + const char *__filename, *__func, *__mutex_name; \ + int __lineno; \ + int __res = ast_find_lock_info(lock, &__filename, &__lineno, &__func, &__mutex_name); \ + ast_mutex_unlock(lock); \ + usleep(1); \ + if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \ + ast_mutex_lock(lock); \ + } else { \ + __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \ + } \ + } while (0) + static void __attribute__((constructor)) init_empty_mutex(void) { memset(&empty_mutex, 0, sizeof(empty_mutex)); @@ -1035,6 +1067,11 @@ static inline int _ast_rwlock_trywrlock(ast_rwlock_t *lock, const char *name, #else /* !DEBUG_THREADS */ +#define DEADLOCK_AVOIDANCE(lock) \ + ast_mutex_lock(lock); \ + usleep(1); \ + ast_mutex_unlock(lock); + static inline int ast_rwlock_init(ast_rwlock_t *prwlock) { int res; diff --git a/main/utils.c b/main/utils.c index b08296263..c14d4c4b0 100644 --- a/main/utils.c +++ b/main/utils.c @@ -662,6 +662,34 @@ void ast_mark_lock_failed(void *lock_addr) pthread_mutex_unlock(&lock_info->lock); } +int ast_find_lock_info(void *lock_addr, const char **filename, int *lineno, const char **func, const char **mutex_name) +{ + struct thr_lock_info *lock_info; + int i = 0; + + if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info)))) + return -1; + + pthread_mutex_lock(&lock_info->lock); + + for (i = lock_info->num_locks - 1; i >= 0; i--) { + if (lock_info->locks[i].lock_addr == lock_addr) + break; + } + + if (i == -1) { + /* Lock not found :( */ + pthread_mutex_unlock(&lock_info->lock); + return -1; + } + + *filename = lock_info->locks[i].file; + *lineno = lock_info->locks[i].line_num; + *func = lock_info->locks[i].func; + *mutex_name = lock_info->locks[i].lock_name; + return 0; +} + void ast_remove_lock_info(void *lock_addr) { struct thr_lock_info *lock_info; |