From patchwork Thu Apr 6 17:48:41 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: 3322 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.44.195 with SMTP id s186csp762157vss; Thu, 6 Apr 2017 10:48:55 -0700 (PDT) X-Received: by 10.223.136.125 with SMTP id e58mr6706134wre.73.1491500935433; Thu, 06 Apr 2017 10:48:55 -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 p78si3538445wrc.102.2017.04.06.10.48.54; Thu, 06 Apr 2017 10:48:55 -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 78BBD680A80; Thu, 6 Apr 2017 20:48:48 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt0-f177.google.com (mail-qt0-f177.google.com [209.85.216.177]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CC8146806C5 for ; Thu, 6 Apr 2017 20:48:41 +0300 (EEST) Received: by mail-qt0-f177.google.com with SMTP id r45so42531659qte.3 for ; Thu, 06 Apr 2017 10:48:45 -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=OM8cmwHMkP0k3ab8WmKRAQB3o/piDjmraAOssUojsyQ=; b=PLuw+VRV6BeFdmuDSPe8j17NbpfYD1VBRckHaoUvwFRIGou6TSB+6hwqaIRlhGJ9Lq 1qWzNA1Pr+KnyDvjjYh1dNVBR52YTp+NfCdx0cBufLyKk8adSS1IOI1oGB+UBbsIJ3PN 8tTMxB8W1t1FXbQJKatfr8rg/jCTf2KSYDIya4ranWT42v96DHWzdhDeGTGFujqDRTqg 2cnwAaGCZwRAyIVAJ3RX5SGANKuYzGK59nzTWP2Gs3GxCud/WyxYrgqYXZ6+RQHx2QVc E0gYtHS6sbOBZXpq07B0o2LlqrZp+2wqscNYJzIx78W1TCmucobqZCwg2+KrDbABwOQD Bi3w== 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=OM8cmwHMkP0k3ab8WmKRAQB3o/piDjmraAOssUojsyQ=; b=BfDKHGs/m9F3mp5VCZLBqOsUSVXE6EQ9bqPh0BuY7eQRd4XRRx4Y0cwTvwYwt7Zc25 qecmM4CEU3W5OVg3YladdEwsuLsQGVWITr944t8NBzPC/4lw0a3peQ8U0NolCqUL0m5D 7lZJJUEviw+HKHBt2IlvnezE0Fl8998Aad56AD75cp2fH8EK77Gfw8Wu3zY3avbjWEBK r+14ktwMF9CyNStsj5h6iu7vz1YkzTYI5VfeCogBTuuP+UbTgiFm49wpamU/9qHoxCa7 OyBhdWX7qhcRlcVOx5hjJ6ykuWEdrO5sm96Ty9AUwg86tG2A8QHkVNHQDQ8Yb1zVBf17 hzYQ== X-Gm-Message-State: AFeK/H2sRewuf3IAb3rPvhyvPj5VfPlpyQWbsVziCBILfUndy6VpupI28Nz7K0iFG4+OBw== X-Received: by 10.200.3.230 with SMTP id z38mr37841260qtg.181.1491500923950; Thu, 06 Apr 2017 10:48:43 -0700 (PDT) Received: from localhost.localdomain ([65.206.95.146]) by smtp.gmail.com with ESMTPSA id z32sm1393978qtg.17.2017.04.06.10.48.43 (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 06 Apr 2017 10:48:43 -0700 (PDT) From: "Ronald S. Bultje" To: ffmpeg-devel@ffmpeg.org Date: Thu, 6 Apr 2017 13:48:41 -0400 Message-Id: <1491500921-18604-1-git-send-email-rsbultje@gmail.com> X-Mailer: git-send-email 2.8.1 Subject: [FFmpeg-devel] [PATCH] pthread_frame: make accesses to debug field be protected by owner lock. 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" The av_log() is done outside the lock, but this way the accesses to the field (reads and writes) are always protected by a mutex. The av_log() is not run inside the lock context because it may involve user callbacks and doing that in performance-sensitive code is probably not a good idea. This should fix occasional tsan warnings when running fate-h264, like: WARNING: ThreadSanitizer: data race (pid=10916) Write of size 4 at 0x7d64000174fc by main thread (mutexes: write M2313): #0 update_context_from_user src/libavcodec/pthread_frame.c:335 (ffmpeg+0x000000df7b06) [..] Previous read of size 4 at 0x7d64000174fc by thread T1 (mutexes: write M2311): #0 ff_thread_await_progress src/libavcodec/pthread_frame.c:592 (ffmpeg+0x000000df8b3e) --- libavcodec/pthread_frame.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c index c246c2f..8857bfc 100644 --- a/libavcodec/pthread_frame.c +++ b/libavcodec/pthread_frame.c @@ -559,6 +559,7 @@ void ff_thread_report_progress(ThreadFrame *f, int n, int field) { PerThreadContext *p; atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL; + int debug_mv; if (!progress || atomic_load_explicit(&progress[field], memory_order_relaxed) >= n) @@ -566,22 +567,24 @@ void ff_thread_report_progress(ThreadFrame *f, int n, int field) p = f->owner[field]->internal->thread_ctx; - if (f->owner[field]->debug&FF_DEBUG_THREADS) - av_log(f->owner[field], AV_LOG_DEBUG, - "%p finished %d field %d\n", progress, n, field); - pthread_mutex_lock(&p->progress_mutex); + debug_mv = f->owner[field]->debug&FF_DEBUG_THREADS; atomic_store_explicit(&progress[field], n, memory_order_release); pthread_cond_broadcast(&p->progress_cond); pthread_mutex_unlock(&p->progress_mutex); + + if (debug_mv) + av_log(f->owner[field], AV_LOG_DEBUG, + "%p finished %d field %d\n", progress, n, field); } void ff_thread_await_progress(ThreadFrame *f, int n, int field) { PerThreadContext *p; atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL; + int debug_mv; if (!progress || atomic_load_explicit(&progress[field], memory_order_acquire) >= n) @@ -589,14 +592,15 @@ void ff_thread_await_progress(ThreadFrame *f, int n, int field) p = f->owner[field]->internal->thread_ctx; - if (f->owner[field]->debug&FF_DEBUG_THREADS) - av_log(f->owner[field], AV_LOG_DEBUG, - "thread awaiting %d field %d from %p\n", n, field, progress); - pthread_mutex_lock(&p->progress_mutex); + debug_mv = f->owner[field]->debug&FF_DEBUG_THREADS; while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n) pthread_cond_wait(&p->progress_cond, &p->progress_mutex); pthread_mutex_unlock(&p->progress_mutex); + + if (debug_mv) + av_log(f->owner[field], AV_LOG_DEBUG, + "thread awaited %d field %d from %p\n", n, field, progress); } void ff_thread_finish_setup(AVCodecContext *avctx) {