Fix deadlock when executing CLI "pri show channels" and "ss7 show channels" commands.
authorRichard Mudgett <rmudgett@digium.com>
Wed, 30 May 2012 17:50:38 +0000 (17:50 +0000)
committerRichard Mudgett <rmudgett@digium.com>
Wed, 30 May 2012 17:50:38 +0000 (17:50 +0000)
* Fix sig_pri_lock_owner() to avoid deadlock properly.

* Code pri_grab() better.

* Fix sig_ss7_lock_owner() to avoid deadlock properly.

* Code ss7_grab() better.

(closes issue ASTERISK-19854)
Reported by: Jaxon
Patches:
      jira_asterisk_19854_v1.8.6.patch (license #5621) patch uploaded by rmudgett (Modified to do the same thing to sig_ss7)
Tested by: Jaxon
........

Merged revisions 367976 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........

Merged revisions 367978 from http://svn.asterisk.org/svn/asterisk/branches/10

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

channels/sig_pri.c
channels/sig_ss7.c

index e94f08e..4be5269 100644 (file)
@@ -115,13 +115,6 @@ static int pri_gendigittimeout = 8000;
 
 #define DCHAN_AVAILABLE        (DCHAN_NOTINALARM | DCHAN_UP)
 
-#define PRI_DEADLOCK_AVOIDANCE(p) \
-       do { \
-               sig_pri_unlock_private(p); \
-               usleep(1); \
-               sig_pri_lock_private(p); \
-       } while (0)
-
 static int pri_active_dchan_index(struct sig_pri_span *pri);
 
 static const char *sig_pri_call_level2str(enum sig_pri_call_level level)
@@ -349,23 +342,23 @@ static void sig_pri_deadlock_avoidance_private(struct sig_pri_chan *p)
                p->calls->deadlock_avoidance_private(p->chan_pvt);
        } else {
                /* Fallback to the old way if callback not present. */
-               PRI_DEADLOCK_AVOIDANCE(p);
+               sig_pri_unlock_private(p);
+               sched_yield();
+               sig_pri_lock_private(p);
        }
 }
 
 static void pri_grab(struct sig_pri_chan *p, struct sig_pri_span *pri)
 {
-       int res;
-
        /* Grab the lock first */
-       do {
-               res = ast_mutex_trylock(&pri->lock);
-               if (res) {
-                       sig_pri_deadlock_avoidance_private(p);
-               }
-       } while (res);
+       while (ast_mutex_trylock(&pri->lock)) {
+               /* Avoid deadlock */
+               sig_pri_deadlock_avoidance_private(p);
+       }
        /* Then break the poll */
-       pthread_kill(pri->master, SIGURG);
+       if (pri->master != AST_PTHREADT_NULL) {
+               pthread_kill(pri->master, SIGURG);
+       }
 }
 
 /*!
@@ -1205,10 +1198,11 @@ static void sig_pri_lock_owner(struct sig_pri_span *pri, int chanpos)
                        /* We got the lock */
                        break;
                }
-               /* We must unlock the PRI to avoid the possibility of a deadlock */
-               ast_mutex_unlock(&pri->lock);
-               sig_pri_deadlock_avoidance_private(pri->pvts[chanpos]);
-               ast_mutex_lock(&pri->lock);
+
+               /* Avoid deadlock */
+               sig_pri_unlock_private(pri->pvts[chanpos]);
+               DEADLOCK_AVOIDANCE(&pri->lock);
+               sig_pri_lock_private(pri->pvts[chanpos]);
        }
 }
 
index 490d767..8584d8a 100644 (file)
@@ -67,13 +67,6 @@ static const char *sig_ss7_call_level2str(enum sig_ss7_call_level level)
        return "Unknown";
 }
 
-#define SIG_SS7_DEADLOCK_AVOIDANCE(p) \
-       do { \
-               sig_ss7_unlock_private(p); \
-               usleep(1); \
-               sig_ss7_lock_private(p); \
-       } while (0)
-
 static void sig_ss7_unlock_private(struct sig_ss7_chan *p)
 {
        if (p->calls->unlock_private) {
@@ -94,7 +87,9 @@ static void sig_ss7_deadlock_avoidance_private(struct sig_ss7_chan *p)
                p->calls->deadlock_avoidance_private(p->chan_pvt);
        } else {
                /* Fallback to the old way if callback not present. */
-               SIG_SS7_DEADLOCK_AVOIDANCE(p);
+               sig_ss7_unlock_private(p);
+               sched_yield();
+               sig_ss7_lock_private(p);
        }
 }
 
@@ -314,10 +309,11 @@ static void sig_ss7_lock_owner(struct sig_ss7_linkset *ss7, int chanpos)
                        /* We got the lock */
                        break;
                }
-               /* We must unlock the SS7 to avoid the possibility of a deadlock */
-               ast_mutex_unlock(&ss7->lock);
-               sig_ss7_deadlock_avoidance_private(ss7->pvts[chanpos]);
-               ast_mutex_lock(&ss7->lock);
+
+               /* Avoid deadlock */
+               sig_ss7_unlock_private(ss7->pvts[chanpos]);
+               DEADLOCK_AVOIDANCE(&ss7->lock);
+               sig_ss7_lock_private(ss7->pvts[chanpos]);
        }
 }
 
@@ -1266,17 +1262,15 @@ static inline void ss7_rel(struct sig_ss7_linkset *ss7)
 
 static void ss7_grab(struct sig_ss7_chan *pvt, struct sig_ss7_linkset *ss7)
 {
-       int res;
        /* Grab the lock first */
-       do {
-               res = ast_mutex_trylock(&ss7->lock);
-               if (res) {
-                       sig_ss7_deadlock_avoidance_private(pvt);
-               }
-       } while (res);
+       while (ast_mutex_trylock(&ss7->lock)) {
+               /* Avoid deadlock */
+               sig_ss7_deadlock_avoidance_private(pvt);
+       }
        /* Then break the poll */
-       if (ss7->master != AST_PTHREADT_NULL)
+       if (ss7->master != AST_PTHREADT_NULL) {
                pthread_kill(ss7->master, SIGURG);
+       }
 }
 
 /*!