[FFmpeg-devel] [PATCH 1/4] lavc/parser: use C11 atomics

Rostislav Pehlivanov atomnuker at gmail.com
Mon Nov 27 06:30:18 EET 2017


Signed-off-by: Rostislav Pehlivanov <atomnuker at gmail.com>
---
 libavcodec/parser.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/libavcodec/parser.c b/libavcodec/parser.c
index 670680ea7c..baf1de4d88 100644
--- a/libavcodec/parser.c
+++ b/libavcodec/parser.c
@@ -23,30 +23,32 @@
 #include <inttypes.h>
 #include <stdint.h>
 #include <string.h>
+#include <stdatomic.h>
 
 #include "libavutil/avassert.h"
-#include "libavutil/atomic.h"
 #include "libavutil/internal.h"
 #include "libavutil/mem.h"
 
 #include "internal.h"
 #include "parser.h"
 
-static AVCodecParser *av_first_parser = NULL;
+static _Atomic(AVCodecParser *)av_first_parser = NULL;
 
 AVCodecParser *av_parser_next(const AVCodecParser *p)
 {
     if (p)
         return p->next;
     else
-        return av_first_parser;
+        return atomic_load(&av_first_parser);
 }
 
 void av_register_codec_parser(AVCodecParser *parser)
 {
+    AVCodecParser *old_parser;
     do {
-        parser->next = av_first_parser;
-    } while (parser->next != avpriv_atomic_ptr_cas((void * volatile *)&av_first_parser, parser->next, parser));
+        parser->next = old_parser = atomic_load(&av_first_parser);
+        atomic_compare_exchange_strong(&av_first_parser, &old_parser, parser);
+    } while (parser->next != old_parser);
 }
 
 AVCodecParserContext *av_parser_init(int codec_id)
@@ -58,7 +60,7 @@ AVCodecParserContext *av_parser_init(int codec_id)
     if (codec_id == AV_CODEC_ID_NONE)
         return NULL;
 
-    for (parser = av_first_parser; parser; parser = parser->next) {
+    for (parser = atomic_load(&av_first_parser); parser; parser = parser->next) {
         if (parser->codec_ids[0] == codec_id ||
             parser->codec_ids[1] == codec_id ||
             parser->codec_ids[2] == codec_id ||
-- 
2.15.0.417.g466bffb3ac



More information about the ffmpeg-devel mailing list