summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-04-17 03:05:23 +0000
committerEric Wong <normalperson@yhbt.net>2008-04-17 03:05:23 +0000
commit3c3620b3011f5b52128873a273728587f6a702ec (patch)
tree265201615d49b9a38e1fa0be427a6656b7d03145 /src
parent7c952c4f4bb4272cd4082f85960eb65976286aa4 (diff)
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
Diffstat (limited to 'src')
-rw-r--r--src/main_notify.c7
1 files changed, 6 insertions, 1 deletions
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);
}