]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
Fix a race condition with the IAX scheduler thread. A lock and condition are
authorRussell Bryant <russell@russellbryant.com>
Tue, 26 Aug 2008 15:27:23 +0000 (15:27 +0000)
committerRussell Bryant <russell@russellbryant.com>
Tue, 26 Aug 2008 15:27:23 +0000 (15:27 +0000)
used here to allow newly scheduled tasks to wake up the scheduler just in case
the new task needs to run sooner than the current wakeup time when the thread
is sleeping.  However, there was a race condition such that a newly scheduled
task would not properly wake up the scheduler or affect the wake up period.
The order of execution would have been:

  1) Scheduler thread determines wake up time of N ms.
  2) Another thread schedules a task and signals the condition, with an
     execution time of < N ms.
  3) Scheduler thread locks and goes to sleep for N ms.

By moving the sleep time determination to inside the critical section, this
possibility is avoided.

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

channels/chan_iax2.c

index 64f616bb938da7cc17b1fdf635be2d7e74c30289..af380e9e5b687e7b936f59da1858db965c697f82 100644 (file)
@@ -9082,15 +9082,14 @@ static void *sched_thread(void *ignore)
        struct timespec ts;
 
        for (;;) {
+               pthread_testcancel();
+               ast_mutex_lock(&sched_lock);
                res = ast_sched_wait(sched);
                if ((res > 1000) || (res < 0))
                        res = 1000;
                tv = ast_tvadd(ast_tvnow(), ast_samp2tv(res, 1000));
                ts.tv_sec = tv.tv_sec;
                ts.tv_nsec = tv.tv_usec * 1000;
-
-               pthread_testcancel();
-               ast_mutex_lock(&sched_lock);
                ast_cond_timedwait(&sched_cond, &sched_lock, &ts);
                ast_mutex_unlock(&sched_lock);
                pthread_testcancel();