[FFmpeg-devel] [PATCH 1/2] avutil/log: make default av_log/av_vlog() thread safe

Michael Niedermayer michaelni at gmx.at
Thu Oct 17 02:01:04 CEST 2013


This uses a spinlock

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
---
 libavutil/log.c |   12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/libavutil/log.c b/libavutil/log.c
index 5ee0c5d..7023c2d 100644
--- a/libavutil/log.c
+++ b/libavutil/log.c
@@ -34,6 +34,7 @@
 #endif
 #include <stdarg.h>
 #include <stdlib.h>
+#include "atomic.h"
 #include "avutil.h"
 #include "bprint.h"
 #include "common.h"
@@ -218,9 +219,14 @@ void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
     char line[LINE_SZ];
     static int is_atty;
     int type[2];
+    static void * volatile state;
 
     if (level > av_log_level)
         return;
+
+    while (avpriv_atomic_ptr_cas(&state, NULL, (void*)fmt))
+        ;
+
     format_line(ptr, level, fmt, vl, part, &print_prefix, type);
     snprintf(line, sizeof(line), "%s%s%s", part[0].str, part[1].str, part[2].str);
 
@@ -235,7 +241,7 @@ void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
         if (is_atty == 1)
             fprintf(stderr, "    Last message repeated %d times\r", count);
         av_bprint_finalize(part+2, NULL);
-        return;
+        goto end;
     }
     if (count > 0) {
         fprintf(stderr, "    Last message repeated %d times\n", count);
@@ -249,6 +255,10 @@ void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
     sanitize(part[2].str);
     colored_fputs(av_clip(level >> 3, 0, 6), part[2].str);
     av_bprint_finalize(part+2, NULL);
+
+end:
+    if (!fmt || avpriv_atomic_ptr_cas(&state, (void*)fmt, NULL) != (void*)fmt)
+        abort(); //cant use av_assert*() here due to that using av_log
 }
 
 static void (*av_log_callback)(void*, int, const char*, va_list) =
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list