From 3c3620b3011f5b52128873a273728587f6a702ec Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 17 Apr 2008 03:05:23 +0000 Subject: 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 --- src/main_notify.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main_notify.c b/src/main_notify.c index 1ef514304..978a401ad 100644 --- a/src/main_notify.c +++ b/src/main_notify.c @@ -30,6 +30,7 @@ static int main_pipe[2]; static pthread_t main_task; static pthread_cond_t main_wakeup = PTHREAD_COND_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 int ioops_fdset(fd_set * rfds, @@ -94,6 +95,8 @@ void wakeup_main_task(void) { assert(!pthread_equal(main_task, pthread_self())); + pending = 1; + if (!wakeup_via_pipe()) pthread_cond_signal(&main_wakeup); } @@ -115,7 +118,9 @@ void wait_main_task(void) assert(pthread_equal(main_task, pthread_self())); pthread_mutex_lock(&main_wakeup_mutex); - pthread_cond_wait(&main_wakeup, &main_wakeup_mutex); + if (!pending) + pthread_cond_wait(&main_wakeup, &main_wakeup_mutex); + pending = 0; pthread_mutex_unlock(&main_wakeup_mutex); } -- cgit v1.2.3