diff mbox

[FFmpeg-devel] frame_thread_encoder: extend critical code covered by finished_task_mutex.

Message ID 1495714846-65456-1-git-send-email-rsbultje@gmail.com
State Accepted
Commit d98f34d7d440ab3671be4e55c00d2cc7bf007eab
Headers show

Commit Message

Ronald S. Bultje May 25, 2017, 12:20 p.m. UTC
Should fix tsan errors in utvideoenc_rgb_left and related tests.
---
 libavcodec/frame_thread_encoder.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

Comments

Michael Niedermayer May 25, 2017, 1:10 p.m. UTC | #1
On Thu, May 25, 2017 at 08:20:46AM -0400, Ronald S. Bultje wrote:
> Should fix tsan errors in utvideoenc_rgb_left and related tests.
> ---
>  libavcodec/frame_thread_encoder.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/libavcodec/frame_thread_encoder.c b/libavcodec/frame_thread_encoder.c
> index 27ae356..6cf1a68 100644
> --- a/libavcodec/frame_thread_encoder.c
> +++ b/libavcodec/frame_thread_encoder.c
> @@ -272,15 +272,16 @@ int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVF
>          pthread_mutex_unlock(&c->task_fifo_mutex);
>  
>          c->task_index = (c->task_index+1) % BUFFER_SIZE;
> -
> -        if(!c->finished_tasks[c->finished_task_index].outdata && (c->task_index - c->finished_task_index) % BUFFER_SIZE <= avctx->thread_count)
> -            return 0;
>      }
>  
> -    if(c->task_index == c->finished_task_index)
> -        return 0;
> -
>      pthread_mutex_lock(&c->finished_task_mutex);
> +    if (c->task_index == c->finished_task_index ||
> +        (frame && !c->finished_tasks[c->finished_task_index].outdata &&
> +         (c->task_index - c->finished_task_index) % BUFFER_SIZE <= avctx->thread_count)) {
> +            pthread_mutex_unlock(&c->finished_task_mutex);
> +            return 0;
> +        }
> +
>      while (!c->finished_tasks[c->finished_task_index].outdata) {
>          pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex);
>      }

LGTM

thx

[...]

--
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

The worst form of inequality is to try to make unequal things equal.
-- Aristotle
diff mbox

Patch

diff --git a/libavcodec/frame_thread_encoder.c b/libavcodec/frame_thread_encoder.c
index 27ae356..6cf1a68 100644
--- a/libavcodec/frame_thread_encoder.c
+++ b/libavcodec/frame_thread_encoder.c
@@ -272,15 +272,16 @@  int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVF
         pthread_mutex_unlock(&c->task_fifo_mutex);
 
         c->task_index = (c->task_index+1) % BUFFER_SIZE;
-
-        if(!c->finished_tasks[c->finished_task_index].outdata && (c->task_index - c->finished_task_index) % BUFFER_SIZE <= avctx->thread_count)
-            return 0;
     }
 
-    if(c->task_index == c->finished_task_index)
-        return 0;
-
     pthread_mutex_lock(&c->finished_task_mutex);
+    if (c->task_index == c->finished_task_index ||
+        (frame && !c->finished_tasks[c->finished_task_index].outdata &&
+         (c->task_index - c->finished_task_index) % BUFFER_SIZE <= avctx->thread_count)) {
+            pthread_mutex_unlock(&c->finished_task_mutex);
+            return 0;
+        }
+
     while (!c->finished_tasks[c->finished_task_index].outdata) {
         pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex);
     }