From patchwork Tue Mar 28 15:59:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgQsWTc2No?= X-Patchwork-Id: 3147 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.44.195 with SMTP id s186csp692789vss; Tue, 28 Mar 2017 08:59:42 -0700 (PDT) X-Received: by 10.28.146.12 with SMTP id u12mr15489218wmd.142.1490716782308; Tue, 28 Mar 2017 08:59:42 -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 f15si5143927wrf.124.2017.03.28.08.59.41; Tue, 28 Mar 2017 08:59:42 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8E9C46898E2; Tue, 28 Mar 2017 18:59:16 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from golem.pkh.me (LStLambert-657-1-117-164.w92-154.abo.wanadoo.fr [92.154.28.164]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CA4166883CB for ; Tue, 28 Mar 2017 18:59:09 +0300 (EEST) Received: from localhost (golem.pkh.me [local]) by golem.pkh.me (OpenSMTPD) with ESMTPA id 2b041c9d; Tue, 28 Mar 2017 15:59:31 +0000 (UTC) From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Mar 2017 17:59:29 +0200 Message-Id: <20170328155929.13541-1-u@pkh.me> X-Mailer: git-send-email 2.12.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] lavfi: fix race when func rets holder is NULL 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: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Clément Bœsch If ret is NULL, a dummy common holder is created to hold *all* the parallel function returns, which gets written concurrently. This commit simplify the whole logic by simply not writing to that holder when not set. --- libavfilter/pthread.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/libavfilter/pthread.c b/libavfilter/pthread.c index ccb915eae5..c7a00210d6 100644 --- a/libavfilter/pthread.c +++ b/libavfilter/pthread.c @@ -43,7 +43,6 @@ typedef struct ThreadContext { AVFilterContext *ctx; void *arg; int *rets; - int nb_rets; int nb_jobs; pthread_cond_t last_job_cond; @@ -60,10 +59,11 @@ static void* attribute_align_arg worker(void *v) int our_job = c->nb_jobs; int nb_threads = c->nb_threads; unsigned int last_execute = 0; - int self_id; + int ret, self_id; pthread_mutex_lock(&c->current_job_lock); self_id = c->current_job++; + for (;;) { while (our_job >= c->nb_jobs) { if (c->current_job == nb_threads + c->nb_jobs) @@ -81,7 +81,9 @@ static void* attribute_align_arg worker(void *v) } pthread_mutex_unlock(&c->current_job_lock); - c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs); + ret = c->func(c->ctx, c->arg, our_job, c->nb_jobs); + if (c->rets) + c->rets[our_job % c->nb_jobs] = ret; pthread_mutex_lock(&c->current_job_lock); our_job = c->current_job++; @@ -117,7 +119,6 @@ static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs) { ThreadContext *c = ctx->graph->internal->thread; - int dummy_ret; if (nb_jobs <= 0) return 0; @@ -129,13 +130,7 @@ static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func, c->ctx = ctx; c->arg = arg; c->func = func; - if (ret) { - c->rets = ret; - c->nb_rets = nb_jobs; - } else { - c->rets = &dummy_ret; - c->nb_rets = 1; - } + c->rets = ret; c->current_execute++; pthread_cond_broadcast(&c->current_job_cond);