[FFmpeg-devel] [PATCH 2/4] avformat/allformats: make av_register_all thread safe

Muhammad Faiz mfcc64 at gmail.com
Mon Mar 6 21:47:37 EET 2017


Signed-off-by: Muhammad Faiz <mfcc64 at gmail.com>
---
 libavformat/allformats.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 35869e3..e91a59b 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -19,6 +19,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <stdatomic.h>
+
 #include "avformat.h"
 #include "rtp.h"
 #include "rdt.h"
@@ -43,10 +45,14 @@
 
 void av_register_all(void)
 {
-    static int initialized;
+    static atomic_int initialized;
+    static atomic_int ready;
 
-    if (initialized)
+    if (atomic_exchange_explicit(&initialized, 1, memory_order_relaxed)) {
+        while (!atomic_load_explicit(&ready, memory_order_relaxed))
+            ;
         return;
+    }
 
     avcodec_register_all();
 
@@ -384,5 +390,5 @@ void av_register_all(void)
     REGISTER_MUXDEMUX(LIBNUT,           libnut);
     REGISTER_DEMUXER (LIBOPENMPT,       libopenmpt);
 
-    initialized = 1;
+    atomic_store_explicit(&ready, 1, memory_order_relaxed);
 }
-- 
2.9.3



More information about the ffmpeg-devel mailing list