pthread: Fix deadlock during thread initialization
authorDerek Buitenhuis <derek.buitenhuis@gmail.com>
Thu, 10 Oct 2013 15:05:40 +0000 (11:05 -0400)
committerDerek Buitenhuis <derek.buitenhuis@gmail.com>
Sun, 13 Oct 2013 09:50:00 +0000 (05:50 -0400)
Sometimes, if pthread_create() failed, then pthread_cond_wait() could
accidentally be called in the worker threads after the uninit function
had already called pthread_cond_broadcast(), leading to a deadlock.

Don't call pthread_cond_wait() if c->done is set.

Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
libavcodec/pthread.c
libavfilter/pthread.c

index f4795f3444fa51b98b5c9b0ece5fba86cf007f0f..b0d9e27aa97ce5bea00219c6d854227baf859937 100644 (file)
@@ -152,7 +152,8 @@ static void* attribute_align_arg worker(void *v)
             if (c->current_job == thread_count + c->job_count)
                 pthread_cond_signal(&c->last_job_cond);
 
-            pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
+            if (!c->done)
+                pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
             our_job = self_id;
 
             if (c->done) {
index a4f45c82cd50b6921e425d76b456676b45e53f15..17e8e7b20f24b60e67788f1fa8d676ada028f191 100644 (file)
@@ -73,7 +73,8 @@ static void* attribute_align_arg worker(void *v)
             if (c->current_job == nb_threads + c->nb_jobs)
                 pthread_cond_signal(&c->last_job_cond);
 
-            pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
+            if (!c->done)
+                pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
             our_job = self_id;
 
             if (c->done) {