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 <[email protected]>
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 <[email protected]>
---
 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 -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to