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
This commit is contained in:
parent
7c952c4f4b
commit
3c3620b301
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user