[FFmpeg-cvslog] lavfi/pthread: Avoid crashes/odd behavior caused by spurious wakeups

Derek Buitenhuis git at videolan.org
Tue Oct 22 18:07:16 CEST 2013


ffmpeg | branch: master | Derek Buitenhuis <derek.buitenhuis at gmail.com> | Fri Oct 18 15:28:51 2013 +0100| [692b93090443519c407834fc61e78b23a9019ac2] | committer: Derek Buitenhuis

lavfi/pthread: Avoid crashes/odd behavior caused by spurious wakeups

This is similar to 5152196b2b47d446d9a509b9bfb318daa26a1f2b.

Signed-off-by: Derek Buitenhuis <derek.buitenhuis at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=692b93090443519c407834fc61e78b23a9019ac2
---

 libavfilter/pthread.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/libavfilter/pthread.c b/libavfilter/pthread.c
index 8ae2c3f..92c4d08 100644
--- a/libavfilter/pthread.c
+++ b/libavfilter/pthread.c
@@ -58,6 +58,7 @@ typedef struct ThreadContext {
     pthread_cond_t current_job_cond;
     pthread_mutex_t current_job_lock;
     int current_job;
+    unsigned int current_execute;
     int done;
 } ThreadContext;
 
@@ -66,6 +67,7 @@ static void* attribute_align_arg worker(void *v)
     ThreadContext *c = v;
     int our_job      = c->nb_jobs;
     int nb_threads   = c->nb_threads;
+    unsigned int last_execute = 0;
     int self_id;
 
     pthread_mutex_lock(&c->current_job_lock);
@@ -75,8 +77,9 @@ static void* attribute_align_arg worker(void *v)
             if (c->current_job == nb_threads + c->nb_jobs)
                 pthread_cond_signal(&c->last_job_cond);
 
-            if (!c->done)
+            while (last_execute == c->current_execute && !c->done)
                 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
+            last_execute = c->current_execute;
             our_job = self_id;
 
             if (c->done) {
@@ -113,7 +116,8 @@ static void slice_thread_uninit(ThreadContext *c)
 
 static void slice_thread_park_workers(ThreadContext *c)
 {
-    pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
+    while (c->current_job != c->nb_threads + c->nb_jobs)
+        pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
     pthread_mutex_unlock(&c->current_job_lock);
 }
 
@@ -140,6 +144,8 @@ static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func,
         c->rets    = &dummy_ret;
         c->nb_rets = 1;
     }
+    c->current_execute++;
+
     pthread_cond_broadcast(&c->current_job_cond);
 
     slice_thread_park_workers(c);



More information about the ffmpeg-cvslog mailing list