From patchwork Wed Apr 5 20:20:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ronald S. Bultje" X-Patchwork-Id: 3309 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.44.195 with SMTP id s186csp359975vss; Wed, 5 Apr 2017 13:20:18 -0700 (PDT) X-Received: by 10.28.126.77 with SMTP id z74mr4496556wmc.15.1491423618156; Wed, 05 Apr 2017 13:20:18 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id k193si14591211wmg.134.2017.04.05.13.20.17; Wed, 05 Apr 2017 13:20:17 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DE3CE6809A1; Wed, 5 Apr 2017 23:20:11 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk0-f194.google.com (mail-qk0-f194.google.com [209.85.220.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4F706680794 for ; Wed, 5 Apr 2017 23:20:05 +0300 (EEST) Received: by mail-qk0-f194.google.com with SMTP id v75so3230575qkb.3 for ; Wed, 05 Apr 2017 13:20:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=KBqj+gY8i1kr1Sg//AzBGBl5yMbMcumYNDIPinzTR8c=; b=hlTNN0iym7eaEztfJN9MtEe3EprmZ6yEvTGesoqKN9T9sp+13ivUbNAamT2/XEDILQ nQdh35x8YEYKlbnwCCZKXGDQeRAbH3zro8FosMC4ZbG9WERY8Bg8uL09tzcKg8obp+10 RnPtTxq6FVaNco0/6fh+gbDghVWkT5h4yfiRMYqV7aHaryAv3TpkwX+lGIjCeT6o4+uE m2XcucvqmbfynMESlGq/jYULKK5HiZzWtoPt0IXUcOmgCSaa/UMrJwS+rwSPX6N3L9J9 E14oDFHjB60zfayVfb5nIqSB47T+Ykn6mPXHMH13u3cvG/35ZhU5Pr4IejhtK8ttGi02 jEZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=KBqj+gY8i1kr1Sg//AzBGBl5yMbMcumYNDIPinzTR8c=; b=O4DV0DXUhYEKwejBFYqDFa007wM0P8lWcQ2MKg3GuUd8fmQSGF9H0KMxO2A7cP9KC3 A3Y2lXXwR1GfZwGMt586EFgbWOOl9V39F4cnFTdb+A/aFzgJSF2TV1x45FbSyAcnokXM rUPcHpsjA3+oXmCILN9s/4ZTlQZcLD6gJO9E3zKb0zymcFoWCluSoaitaMs9khH6ac8/ 1Ot1zhhQKh9w691ftLh9za22U6aD06memmslUL/0wgrPfSFMTDs/aNA/P33Q4jOMbzKY MoTA/dnjI7KsE9gvYoYDEkILgEG+3BD7jngvtV1QXNxbz0l0aKLlUsplEJ7V9NunmaHj IAkw== X-Gm-Message-State: AN3rC/6xcOUUBrJ8hzOTfyWtj/r/aaf82jRTCeVML5osWUoX5Ah9RNr6WROhyTT898xNUg== X-Received: by 10.55.110.67 with SMTP id j64mr1056302qkc.6.1491423606504; Wed, 05 Apr 2017 13:20:06 -0700 (PDT) Received: from localhost.localdomain ([65.206.95.146]) by smtp.gmail.com with ESMTPSA id m12sm14739653qtf.25.2017.04.05.13.20.05 (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 05 Apr 2017 13:20:05 -0700 (PDT) From: "Ronald S. Bultje" To: ffmpeg-devel@ffmpeg.org Date: Wed, 5 Apr 2017 16:20:03 -0400 Message-Id: <1491423604-66388-1-git-send-email-rsbultje@gmail.com> X-Mailer: git-send-email 2.8.1 Subject: [FFmpeg-devel] [PATCH 1/2] vp8: make wait/thread_mb_pos atomic. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: "Ronald S. Bultje" MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Fixes tsan warnings like this in fate-vp8-test-vector-007: WARNING: ThreadSanitizer: data race (pid=3590) Write of size 4 at 0x7d8c0000e07c by thread T2: #0 decode_mb_row_no_filter src/libavcodec/vp8.c:2330 (ffmpeg+0x000000ffb59e) [..] Previous write of size 4 at 0x7d8c0000e07c by thread T1: #0 decode_mb_row_no_filter src/libavcodec/vp8.c:2330 (ffmpeg+0x000000ffb59e) --- libavcodec/vp8.c | 29 ++++++++++++++--------------- libavcodec/vp8.h | 6 ++++-- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/libavcodec/vp8.c b/libavcodec/vp8.c index 1e8808c..9bc1d95 100644 --- a/libavcodec/vp8.c +++ b/libavcodec/vp8.c @@ -2247,15 +2247,15 @@ static void vp8_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame, #define check_thread_pos(td, otd, mb_x_check, mb_y_check) \ do { \ int tmp = (mb_y_check << 16) | (mb_x_check & 0xFFFF); \ - if (otd->thread_mb_pos < tmp) { \ + if (atomic_load(&otd->thread_mb_pos) < tmp) { \ pthread_mutex_lock(&otd->lock); \ - td->wait_mb_pos = tmp; \ + atomic_store(&td->wait_mb_pos, tmp); \ do { \ - if (otd->thread_mb_pos >= tmp) \ + if (atomic_load(&otd->thread_mb_pos) >= tmp) \ break; \ pthread_cond_wait(&otd->cond, &otd->lock); \ } while (1); \ - td->wait_mb_pos = INT_MAX; \ + atomic_store(&td->wait_mb_pos, INT_MAX); \ pthread_mutex_unlock(&otd->lock); \ } \ } while (0) @@ -2266,12 +2266,10 @@ static void vp8_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame, int sliced_threading = (avctx->active_thread_type == FF_THREAD_SLICE) && \ (num_jobs > 1); \ int is_null = !next_td || !prev_td; \ - int pos_check = (is_null) ? 1 \ - : (next_td != td && \ - pos >= next_td->wait_mb_pos) || \ - (prev_td != td && \ - pos >= prev_td->wait_mb_pos); \ - td->thread_mb_pos = pos; \ + int pos_check = (is_null) ? 1 : \ + (next_td != td && pos >= atomic_load(&next_td->wait_mb_pos)) || \ + (prev_td != td && pos >= atomic_load(&prev_td->wait_mb_pos)); \ + atomic_store(&td->thread_mb_pos, pos); \ if (sliced_threading && pos_check) { \ pthread_mutex_lock(&td->lock); \ pthread_cond_broadcast(&td->cond); \ @@ -2288,7 +2286,7 @@ static av_always_inline int decode_mb_row_no_filter(AVCodecContext *avctx, void { VP8Context *s = avctx->priv_data; VP8ThreadData *prev_td, *next_td, *td = &s->thread_data[threadnr]; - int mb_y = td->thread_mb_pos >> 16; + int mb_y = atomic_load(&td->thread_mb_pos) >> 16; int mb_x, mb_xy = mb_y * s->mb_width; int num_jobs = s->num_jobs; VP8Frame *curframe = s->curframe, *prev_frame = s->prev_frame; @@ -2428,7 +2426,7 @@ static av_always_inline void filter_mb_row(AVCodecContext *avctx, void *tdata, { VP8Context *s = avctx->priv_data; VP8ThreadData *td = &s->thread_data[threadnr]; - int mb_x, mb_y = td->thread_mb_pos >> 16, num_jobs = s->num_jobs; + int mb_x, mb_y = atomic_load(&td->thread_mb_pos) >> 16, num_jobs = s->num_jobs; AVFrame *curframe = s->curframe->tf.f; VP8Macroblock *mb; VP8ThreadData *prev_td, *next_td; @@ -2507,7 +2505,7 @@ int vp78_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata, int jobnr, td->thread_nr = threadnr; for (mb_y = jobnr; mb_y < s->mb_height; mb_y += num_jobs) { - td->thread_mb_pos = mb_y << 16; + atomic_store(&td->thread_mb_pos, mb_y << 16); ret = s->decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr); if (ret < 0) { update_pos(td, s->mb_height, INT_MAX & 0xFFFF); @@ -2667,8 +2665,9 @@ int vp78_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, s->mv_min.y = -MARGIN; s->mv_max.y = ((s->mb_height - 1) << 6) + MARGIN; for (i = 0; i < MAX_THREADS; i++) { - s->thread_data[i].thread_mb_pos = 0; - s->thread_data[i].wait_mb_pos = INT_MAX; + VP8ThreadData *td = &s->thread_data[i]; + atomic_init(&td->thread_mb_pos, 0); + atomic_init(&td->wait_mb_pos, INT_MAX); } if (is_vp7) avctx->execute2(avctx, vp7_decode_mb_row_sliced, s->thread_data, NULL, diff --git a/libavcodec/vp8.h b/libavcodec/vp8.h index 3910b5c..d7e7680 100644 --- a/libavcodec/vp8.h +++ b/libavcodec/vp8.h @@ -26,6 +26,8 @@ #ifndef AVCODEC_VP8_H #define AVCODEC_VP8_H +#include + #include "libavutil/buffer.h" #include "libavutil/thread.h" @@ -114,8 +116,8 @@ typedef struct VP8ThreadData { pthread_mutex_t lock; pthread_cond_t cond; #endif - int thread_mb_pos; // (mb_y << 16) | (mb_x & 0xFFFF) - int wait_mb_pos; // What the current thread is waiting on. + atomic_int thread_mb_pos; // (mb_y << 16) | (mb_x & 0xFFFF) + atomic_int wait_mb_pos; // What the current thread is waiting on. #define EDGE_EMU_LINESIZE 32 DECLARE_ALIGNED(16, uint8_t, edge_emu_buffer)[21 * EDGE_EMU_LINESIZE];