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

Ben Jackson git at videolan.org
Wed Sep 19 02:32:42 CEST 2012


ffmpeg | branch: release/0.10 | Ben Jackson <ben at ben.com> | Thu Sep 13 21:26:43 2012 -0700| [e2c7b37fd225d9e41c913bd21a5b481c78a11019] | committer: Michael Niedermayer

pthread: Avoid crashes/odd behavior caused by spurious wakeups

pthread_wait_cond can wake up for no reason (Wikipedia: Spurious_wakeup).
The FF_THREAD_SLICE thread mechanism could spontaneously execute jobs or
allow the caller of avctx->execute to return before all jobs were complete.
This adds tests to both cases to ensure the wakeup is real.

Signed-off-by: Ben Jackson <ben at ben.com>
Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
(cherry picked from commit e3329474a366de066b25e86f35f5abf9c5a4b7b2)

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
(cherry picked from commit f1ec792ae3011531d47070144b8c91d58bb3e76f)

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>

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

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

diff --git a/libavcodec/pthread.c b/libavcodec/pthread.c
index c58222b..302acab 100644
--- a/libavcodec/pthread.c
+++ b/libavcodec/pthread.c
@@ -79,6 +79,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;
 
@@ -203,6 +204,7 @@ static void* attribute_align_arg worker(void *v)
     AVCodecContext *avctx = v;
     ThreadContext *c = avctx->thread_opaque;
     int our_job = c->job_count;
+    int last_execute = 0;
     int thread_count = avctx->thread_count;
     int self_id;
 
@@ -213,7 +215,9 @@ 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);
+            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) {
@@ -233,7 +237,8 @@ static void* attribute_align_arg worker(void *v)
 
 static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
 {
-    pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
+    while (c->current_job != thread_count + c->job_count)
+        pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
     pthread_mutex_unlock(&c->current_job_lock);
 }
 
@@ -282,6 +287,7 @@ static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void
         c->rets = &dummy_ret;
         c->rets_count = 1;
     }
+    c->current_execute++;
     pthread_cond_broadcast(&c->current_job_cond);
 
     avcodec_thread_park_workers(c, avctx->thread_count);



More information about the ffmpeg-cvslog mailing list