Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Anton Khirnov <anton@khirnov.net>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 9/9] fftools/ffmpeg: use a mutex for enc_stats_write()
Date: Thu, 14 Dec 2023 20:31:38 +0100
Message-ID: <20231214193138.2503-9-anton@khirnov.net> (raw)
In-Reply-To: <20231214193138.2503-1-anton@khirnov.net>

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".

      parent reply	other threads:[~2023-12-14 19:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-14 19:31 [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg_mux: stop logging to AVFormatContext Anton Khirnov
2023-12-14 19:31 ` [FFmpeg-devel] [PATCH 2/9] fftools/ffmpeg: deprecate -fps_mode/vsync drop Anton Khirnov
2023-12-14 19:31 ` [FFmpeg-devel] [PATCH 3/9] fftools/ffmpeg_mux: factor timestamps processing out of write_packet() Anton Khirnov
2023-12-14 19:31 ` [FFmpeg-devel] [PATCH 4/9] fftools/cmdutils: change option flags to (1 << N) style Anton Khirnov
2023-12-14 19:31 ` [FFmpeg-devel] [PATCH 5/9] fftools/ffmpeg_mux_init: change 1-bit bitfields from int to unsigned Anton Khirnov
2023-12-14 19:31 ` [FFmpeg-devel] [PATCH 6/9] fftools/ffmpeg: print keyframe information with -stats_* Anton Khirnov
2023-12-14 19:31 ` [FFmpeg-devel] [PATCH 7/9] doc/ffmpeg: drop misleading claims from -stats_*_fmt Anton Khirnov
2023-12-14 19:31 ` [FFmpeg-devel] [PATCH 8/9] fftools/ffmpeg_mux: deduplicate uniniting EncStats Anton Khirnov
2023-12-14 19:31 ` Anton Khirnov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231214193138.2503-9-anton@khirnov.net \
    --to=anton@khirnov.net \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git