Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avutil/log: use atomics to load and store logging level, flags and callback pointer (PR #21186)
@ 2025-12-13 15:48 James Almer via ffmpeg-devel
  0 siblings, 0 replies; only message in thread
From: James Almer via ffmpeg-devel @ 2025-12-13 15:48 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: James Almer

PR #21186 opened by James Almer (jamrial)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21186
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21186.patch

Based on code setting CPU flags in libavutil/cpu.c


>From 717e0d27f68a38f0d239abe359d687233856de94 Mon Sep 17 00:00:00 2001
From: James Almer <jamrial@gmail.com>
Date: Sat, 13 Dec 2025 12:45:10 -0300
Subject: [PATCH] avutil/log: use atomics to load and store logging level,
 flags and callback pointer

Based on code setting cpu flags in libavutil/cpu.c

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavutil/log.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/libavutil/log.c b/libavutil/log.c
index af893dde17..f9d565613a 100644
--- a/libavutil/log.c
+++ b/libavutil/log.c
@@ -34,6 +34,7 @@
 #endif
 #include <inttypes.h>
 #include <stdarg.h>
+#include <stdatomic.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -55,8 +56,8 @@ static AVMutex mutex = AV_MUTEX_INITIALIZER;
 #define BACKTRACE_LOGLEVEL AV_LOG_ERROR
 #endif
 
-static int av_log_level = AV_LOG_INFO;
-static int flags;
+static atomic_int av_log_level = AV_LOG_INFO;
+static atomic_int av_log_flags = 0;
 
 #define NB_LEVELS 8
 #if defined(_WIN32) && HAVE_SETCONSOLETEXTATTRIBUTE && HAVE_GETSTDHANDLE
@@ -326,6 +327,7 @@ static void format_line(void *avcl, int level, const char *fmt, va_list vl,
     av_bprint_init(part+2, 0, AV_BPRINT_SIZE_AUTOMATIC);
     av_bprint_init(part+3, 0, 65536);
     av_bprint_init(part+4, 0, AV_BPRINT_SIZE_AUTOMATIC);
+    int flags = atomic_load_explicit(&av_log_flags, memory_order_relaxed);
 
     if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
     if (*print_prefix && avc) {
@@ -391,7 +393,7 @@ void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
         level &= 0xff;
     }
 
-    if (level > av_log_level)
+    if (level > atomic_load_explicit(&av_log_level, memory_order_relaxed))
         return;
     ff_mutex_lock(&mutex);
 
@@ -403,7 +405,7 @@ void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
         is_atty = isatty(2) ? 1 : -1;
 #endif
 
-    if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev) &&
+    if (print_prefix && (atomic_load_explicit(&av_log_flags, memory_order_relaxed) & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev) &&
         *line && line[strlen(line) - 1] != '\r'){
         count++;
         if (is_atty == 1)
@@ -436,8 +438,7 @@ end:
     ff_mutex_unlock(&mutex);
 }
 
-static void (*av_log_callback)(void*, int, const char*, va_list) =
-    av_log_default_callback;
+static uintptr_t av_log_callback = (uintptr_t)av_log_default_callback;
 
 void av_log(void* avcl, int level, const char *fmt, ...)
 {
@@ -459,7 +460,8 @@ void av_log_once(void* avcl, int initial_level, int subsequent_level, int *state
 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
 {
     AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
-    void (*log_callback)(void*, int, const char*, va_list) = av_log_callback;
+    void (*log_callback)(void*, int, const char*, va_list) =
+        (void *)atomic_load_explicit(&av_log_callback, memory_order_relaxed);
     if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
         avc->log_level_offset_offset && level >= AV_LOG_FATAL)
         level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
@@ -469,27 +471,27 @@ void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
 
 int av_log_get_level(void)
 {
-    return av_log_level;
+    return atomic_load_explicit(&av_log_level, memory_order_relaxed);
 }
 
 void av_log_set_level(int level)
 {
-    av_log_level = level;
+    atomic_store_explicit(&av_log_level, level, memory_order_relaxed);
 }
 
 void av_log_set_flags(int arg)
 {
-    flags = arg;
+    atomic_store_explicit(&av_log_flags, arg, memory_order_relaxed);
 }
 
 int av_log_get_flags(void)
 {
-    return flags;
+    return atomic_load_explicit(&av_log_flags, memory_order_relaxed);
 }
 
 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
 {
-    av_log_callback = callback;
+    atomic_store_explicit(&av_log_callback, (uintptr_t)callback, memory_order_relaxed);
 }
 
 static void missing_feature_sample(int sample, void *avc, const char *msg,
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-12-13 15:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-13 15:48 [FFmpeg-devel] [PATCH] avutil/log: use atomics to load and store logging level, flags and callback pointer (PR #21186) James Almer via ffmpeg-devel

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