From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 9C3F047C67 for ; Thu, 14 Dec 2023 19:33:41 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5C9A968D2AD; Thu, 14 Dec 2023 21:32:54 +0200 (EET) Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4FACD68D28B for ; Thu, 14 Dec 2023 21:32:45 +0200 (EET) Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id AF8C71BA4 for ; Thu, 14 Dec 2023 20:32:44 +0100 (CET) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id YtTayiXFWUNG for ; Thu, 14 Dec 2023 20:32:44 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail1.khirnov.net (Postfix) with ESMTPS id 947221CAF for ; Thu, 14 Dec 2023 20:32:38 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id A58BD3A073A for ; Thu, 14 Dec 2023 20:32:31 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 14 Dec 2023 20:31:38 +0100 Message-ID: <20231214193138.2503-9-anton@khirnov.net> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231214193138.2503-1-anton@khirnov.net> References: <20231214193138.2503-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 9/9] fftools/ffmpeg: use a mutex for enc_stats_write() X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: It may be called concurrently from different threads to write into the same file. --- fftools/ffmpeg.h | 3 +++ fftools/ffmpeg_enc.c | 4 ++++ fftools/ffmpeg_mux.c | 4 ++++ fftools/ffmpeg_mux_init.c | 5 +++++ 4 files changed, 16 insertions(+) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 03dbb528c0..33a29b316f 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -480,6 +480,9 @@ typedef struct EncStats { int nb_components; AVIOContext *io; + + pthread_mutex_t lock; + int lock_initialized; } EncStats; extern const char *const forced_keyframes_const_names[]; diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index d774a7e008..57590a43a3 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -491,6 +491,8 @@ void enc_stats_write(OutputStream *ost, EncStats *es, ptsi = fd->dec.pts; } + pthread_mutex_lock(&es->lock); + for (size_t i = 0; i < es->nb_components; i++) { const EncStatsComponent *c = &es->components[i]; @@ -538,6 +540,8 @@ void enc_stats_write(OutputStream *ost, EncStats *es, } avio_w8(io, '\n'); avio_flush(io); + + pthread_mutex_unlock(&es->lock); } static inline double psnr(double d) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 357f34172f..ab86abee14 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -778,6 +778,10 @@ static void enc_stats_uninit(EncStats *es) for (int i = 0; i < es->nb_components; i++) av_freep(&es->components[i].str); av_freep(&es->components); + + if (es->lock_initialized) + pthread_mutex_destroy(&es->lock); + es->lock_initialized = 0; } static void ost_free(OutputStream **post) diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 0203701d78..52eca9f9d5 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -402,6 +402,11 @@ fail: return ret; } + ret = pthread_mutex_init(&es->lock, NULL); + if (ret) + return AVERROR(ret); + es->lock_initialized = 1; + ret = enc_stats_get_file(&es->io, path); if (ret < 0) return ret; -- 2.42.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".