Commit 3c3620b3 authored by Max Kellermann's avatar Max Kellermann Committed by Eric Wong

fix race condition in main_notify.c

The function wait_main_task() is racy: if the function wakeup_via_cond() sees the mutex is locked just before wait_main_task() executes pthread_cond_wait(), the main thread blocks forever. Work around this issue by adding a "pending" flag just like in my notify.c code. A standards-compliant solution should be implemented later. git-svn-id: https://svn.musicpd.org/mpd/trunk@7365 09075e82-0dd4-0310-85a5-a0d7c8717e4f
parent 7c952c4f
...@@ -30,6 +30,7 @@ static int main_pipe[2]; ...@@ -30,6 +30,7 @@ static int main_pipe[2];
static pthread_t main_task; static pthread_t main_task;
static pthread_cond_t main_wakeup = PTHREAD_COND_INITIALIZER; static pthread_cond_t main_wakeup = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t main_wakeup_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t main_wakeup_mutex = PTHREAD_MUTEX_INITIALIZER;
static volatile int pending;
static pthread_mutex_t select_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t select_mutex = PTHREAD_MUTEX_INITIALIZER;
static int ioops_fdset(fd_set * rfds, static int ioops_fdset(fd_set * rfds,
...@@ -94,6 +95,8 @@ void wakeup_main_task(void) ...@@ -94,6 +95,8 @@ void wakeup_main_task(void)
{ {
assert(!pthread_equal(main_task, pthread_self())); assert(!pthread_equal(main_task, pthread_self()));
pending = 1;
if (!wakeup_via_pipe()) if (!wakeup_via_pipe())
pthread_cond_signal(&main_wakeup); pthread_cond_signal(&main_wakeup);
} }
...@@ -115,7 +118,9 @@ void wait_main_task(void) ...@@ -115,7 +118,9 @@ void wait_main_task(void)
assert(pthread_equal(main_task, pthread_self())); assert(pthread_equal(main_task, pthread_self()));
pthread_mutex_lock(&main_wakeup_mutex); pthread_mutex_lock(&main_wakeup_mutex);
if (!pending)
pthread_cond_wait(&main_wakeup, &main_wakeup_mutex); pthread_cond_wait(&main_wakeup, &main_wakeup_mutex);
pending = 0;
pthread_mutex_unlock(&main_wakeup_mutex); pthread_mutex_unlock(&main_wakeup_mutex);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment