[FFmpeg-devel,4/4] avdevice/alldevice: make avdevice_register_all thread safe

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

Details

Message ID 20170306194739.1870-4-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>
---
 libavdevice/alldevices.c | 13 ++++++++++---
 libavdevice/avdevice.h   |  1 -
 2 files changed, 10 insertions(+), 4 deletions(-)

Patch hide | download patch | download mbox

diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c
index a761be4..cc8b8ce 100644
--- a/libavdevice/alldevices.c
+++ b/libavdevice/alldevices.c
@@ -18,6 +18,8 @@ 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <stdatomic.h>
+
 #include "config.h"
 #include "avdevice.h"
 
@@ -39,11 +41,14 @@ 
 
 void avdevice_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;
-    initialized = 1;
+    }
 
     /* devices */
     REGISTER_INOUTDEV(ALSA,             alsa);
@@ -75,4 +80,6 @@  void avdevice_register_all(void)
     /* external libraries */
     REGISTER_INDEV   (LIBCDIO,          libcdio);
     REGISTER_INDEV   (LIBDC1394,        libdc1394);
+
+    atomic_store_explicit(&ready, 1, memory_order_relaxed);
 }
diff --git a/libavdevice/avdevice.h b/libavdevice/avdevice.h
index 6153f2c..84f374a 100644
--- a/libavdevice/avdevice.h
+++ b/libavdevice/avdevice.h
@@ -67,7 +67,6 @@  const char *avdevice_license(void);
 
 /**
  * Initialize libavdevice and register all the input and output devices.
- * @warning This function is not thread safe.
  */
 void avdevice_register_all(void);