[FFmpeg-devel] [PATCH 3/4] avformat: use mutexes instead of atomics in av_register_{input, output}_format()

James Almer jamrial at gmail.com
Thu Jan 4 20:19:03 EET 2018


Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavformat/format.c | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/libavformat/format.c b/libavformat/format.c
index 38ca2a3465..bc443f41ce 100644
--- a/libavformat/format.c
+++ b/libavformat/format.c
@@ -19,10 +19,10 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/atomic.h"
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
 #include "libavutil/opt.h"
+#include "libavutil/thread.h"
 
 #include "avio_internal.h"
 #include "avformat.h"
@@ -39,9 +39,6 @@ static AVInputFormat *first_iformat = NULL;
 /** head of registered output format linked list */
 static AVOutputFormat *first_oformat = NULL;
 
-static AVInputFormat **last_iformat = &first_iformat;
-static AVOutputFormat **last_oformat = &first_oformat;
-
 AVInputFormat *av_iformat_next(const AVInputFormat *f)
 {
     if (f)
@@ -58,28 +55,38 @@ AVOutputFormat *av_oformat_next(const AVOutputFormat *f)
         return first_oformat;
 }
 
+static AVMutex iformat_register_mutex = AV_MUTEX_INITIALIZER;
+
 void av_register_input_format(AVInputFormat *format)
 {
-    AVInputFormat **p = last_iformat;
+    AVInputFormat **p;
+
+    ff_mutex_lock(&iformat_register_mutex);
+    p = &first_iformat;
 
-    // Note, format could be added after the first 2 checks but that implies that *p is no longer NULL
-    while(p != &format->next && !format->next && avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
+    while (*p)
         p = &(*p)->next;
 
-    if (!format->next)
-        last_iformat = &format->next;
+    *p = format;
+    format->next = NULL;
+    ff_mutex_unlock(&iformat_register_mutex);
 }
 
+static AVMutex oformat_register_mutex = AV_MUTEX_INITIALIZER;
+
 void av_register_output_format(AVOutputFormat *format)
 {
-    AVOutputFormat **p = last_oformat;
+    AVOutputFormat **p;
+
+    ff_mutex_lock(&oformat_register_mutex);
+    p = &first_oformat;
 
-    // Note, format could be added after the first 2 checks but that implies that *p is no longer NULL
-    while(p != &format->next && !format->next && avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
+    while (*p)
         p = &(*p)->next;
+    *p = format;
+    format->next = NULL;
 
-    if (!format->next)
-        last_oformat = &format->next;
+    ff_mutex_unlock(&oformat_register_mutex);
 }
 
 int av_match_ext(const char *filename, const char *extensions)
-- 
2.15.0



More information about the ffmpeg-devel mailing list