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

Submitted by Muhammad Faiz on March 6, 2017, 7:47 p.m.

Details

Message ID 20170306194739.1870-2-mfcc64@gmail.com
State New
Headers show

Commit Message

Muhammad Faiz March 6, 2017, 7:47 p.m.
Signed-off-by: Muhammad Faiz <mfcc64@gmail.com>
---
 libavformat/allformats.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

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);
 }