diff mbox

[FFmpeg-devel] frame_thread_encoder: make 'exit' member atomic.

Message ID 1505138335-29960-1-git-send-email-rsbultje@gmail.com
State Accepted
Commit 183216b21870f21c86c904a7530d53682d7db46d
Headers show

Commit Message

Ronald S. Bultje Sept. 11, 2017, 1:58 p.m. UTC
Should fix the following tsan warning:

WARNING: ThreadSanitizer: data race (pid=19806)
  Read of size 4 at 0x7b84000012f0 by thread T9:
    #0 worker src/libavcodec/frame_thread_encoder.c:66 (ffmpeg+0x0000007f349e)
[..]
  Previous write of size 4 at 0x7b84000012f0 by main thread (mutexes: write M1395):
    #0 ff_frame_thread_encoder_free src/libavcodec/frame_thread_encoder.c:239 (ffmpeg+0x0000007f379e)
[..]
---
 libavcodec/frame_thread_encoder.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

Comments

Michael Niedermayer Sept. 11, 2017, 9:30 p.m. UTC | #1
On Mon, Sep 11, 2017 at 09:58:55AM -0400, Ronald S. Bultje wrote:
> Should fix the following tsan warning:
> 
> WARNING: ThreadSanitizer: data race (pid=19806)
>   Read of size 4 at 0x7b84000012f0 by thread T9:
>     #0 worker src/libavcodec/frame_thread_encoder.c:66 (ffmpeg+0x0000007f349e)
> [..]
>   Previous write of size 4 at 0x7b84000012f0 by main thread (mutexes: write M1395):
>     #0 ff_frame_thread_encoder_free src/libavcodec/frame_thread_encoder.c:239 (ffmpeg+0x0000007f379e)
> [..]
> ---
>  libavcodec/frame_thread_encoder.c | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)

LGTM

thanks

[...]
Ronald S. Bultje Sept. 12, 2017, 12:22 p.m. UTC | #2
Hi,

On Mon, Sep 11, 2017 at 5:30 PM, Michael Niedermayer <michael@niedermayer.cc
> wrote:

> On Mon, Sep 11, 2017 at 09:58:55AM -0400, Ronald S. Bultje wrote:
> > Should fix the following tsan warning:
> >
> > WARNING: ThreadSanitizer: data race (pid=19806)
> >   Read of size 4 at 0x7b84000012f0 by thread T9:
> >     #0 worker src/libavcodec/frame_thread_encoder.c:66
> (ffmpeg+0x0000007f349e)
> > [..]
> >   Previous write of size 4 at 0x7b84000012f0 by main thread (mutexes:
> write M1395):
> >     #0 ff_frame_thread_encoder_free src/libavcodec/frame_thread_encoder.c:239
> (ffmpeg+0x0000007f379e)
> > [..]
> > ---
> >  libavcodec/frame_thread_encoder.c | 13 ++++++++-----
> >  1 file changed, 8 insertions(+), 5 deletions(-)
>
> LGTM


Pushed.

Ronald
diff mbox

Patch

diff --git a/libavcodec/frame_thread_encoder.c b/libavcodec/frame_thread_encoder.c
index 33928fe..35a37c4 100644
--- a/libavcodec/frame_thread_encoder.c
+++ b/libavcodec/frame_thread_encoder.c
@@ -18,6 +18,8 @@ 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <stdatomic.h>
+
 #include "frame_thread_encoder.h"
 
 #include "libavutil/fifo.h"
@@ -55,7 +57,7 @@  typedef struct{
     unsigned finished_task_index;
 
     pthread_t worker[MAX_THREADS];
-    int exit;
+    atomic_int exit;
 } ThreadContext;
 
 static void * attribute_align_arg worker(void *v){
@@ -63,7 +65,7 @@  static void * attribute_align_arg worker(void *v){
     ThreadContext *c = avctx->internal->frame_thread_encoder;
     AVPacket *pkt = NULL;
 
-    while(!c->exit){
+    while (!atomic_load(&c->exit)) {
         int got_packet, ret;
         AVFrame *frame;
         Task task;
@@ -73,8 +75,8 @@  static void * attribute_align_arg worker(void *v){
         av_init_packet(pkt);
 
         pthread_mutex_lock(&c->task_fifo_mutex);
-        while (av_fifo_size(c->task_fifo) <= 0 || c->exit) {
-            if(c->exit){
+        while (av_fifo_size(c->task_fifo) <= 0 || atomic_load(&c->exit)) {
+            if (atomic_load(&c->exit)) {
                 pthread_mutex_unlock(&c->task_fifo_mutex);
                 goto end;
             }
@@ -187,6 +189,7 @@  int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options){
     pthread_mutex_init(&c->buffer_mutex, NULL);
     pthread_cond_init(&c->task_fifo_cond, NULL);
     pthread_cond_init(&c->finished_task_cond, NULL);
+    atomic_init(&c->exit, 0);
 
     for(i=0; i<avctx->thread_count ; i++){
         AVDictionary *tmp = NULL;
@@ -236,7 +239,7 @@  void ff_frame_thread_encoder_free(AVCodecContext *avctx){
     ThreadContext *c= avctx->internal->frame_thread_encoder;
 
     pthread_mutex_lock(&c->task_fifo_mutex);
-    c->exit = 1;
+    atomic_store(&c->exit, 1);
     pthread_cond_broadcast(&c->task_fifo_cond);
     pthread_mutex_unlock(&c->task_fifo_mutex);