Merged revisions 118953 via svnmerge from
authorTilghman Lesher <tilghman@meg.abyt.es>
Thu, 29 May 2008 17:35:19 +0000 (17:35 +0000)
committerTilghman Lesher <tilghman@meg.abyt.es>
Thu, 29 May 2008 17:35:19 +0000 (17:35 +0000)
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.

........

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@118955 65c4cc65-6c06-0410-ace0-fbb531ad65f3

channels/chan_agent.c
channels/chan_alsa.c
channels/chan_iax2.c
channels/chan_mgcp.c
channels/chan_zap.c
include/asterisk/lock.h
main/utils.c

index decf022..8d22ead 100644 (file)
@@ -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);
index b4e194b..5c2a395 100644 (file)
@@ -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);
        }
 }
 
index 7ce45d3..174a05a 100644 (file)
@@ -1746,9 +1746,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);
@@ -1779,9 +1777,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);
@@ -1813,9 +1809,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);
@@ -3773,9 +3767,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]);
        }
 }
 
@@ -3969,9 +3961,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) {
@@ -8284,9 +8274,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]) {
@@ -8728,9 +8716,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;
                                                }
                                                
index 021999a..4dc9f67 100644 (file)
@@ -596,9 +596,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;
@@ -614,9 +612,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;
index b9860f3..6646ee2 100644 (file)
@@ -882,10 +882,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 */
@@ -976,9 +973,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);
@@ -1025,9 +1020,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);
@@ -3643,9 +3636,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);
                        }
                }
        }
@@ -10128,9 +10119,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_with_cause(p->subs[x].owner, AST_CAUSE_PRE_EMPTED);
index 7f8d00e..5a9bc25 100644 (file)
@@ -220,7 +220,7 @@ static inline void __dump_backtrace(struct ast_bt *bt, int canlog)
 {
        char **strings;
 
-       size_t i;
+       ssize_t i;
 
        strings = backtrace_symbols(bt->addresses, bt->num_frames);
 
@@ -241,6 +241,38 @@ static inline void __dump_backtrace(struct ast_bt *bt, int canlog)
  */
 void log_show_lock(void *this_lock_addr);
 
+/*!
+ * \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));
index dfa32eb..19fbf21 100644 (file)
@@ -677,6 +677,34 @@ void ast_mark_lock_failed(void *lock_addr)
 #ifdef HAVE_BKTR
 void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt)
 #else
+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)
 #endif
 {