[FFmpeg-devel] [PATCH 2/7] avcodec/utils: convert to stdatomic

James Almer jamrial at gmail.com
Thu Mar 23 01:34:07 EET 2017


Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/utils.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 365ee26056..5e983557fc 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -26,7 +26,6 @@
  */
 
 #include "config.h"
-#include "libavutil/atomic.h"
 #include "libavutil/attributes.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
@@ -54,6 +53,7 @@
 #include "bytestream.h"
 #include "version.h"
 #include <stdlib.h>
+#include <stdatomic.h>
 #include <stdarg.h>
 #include <limits.h>
 #include <float.h>
@@ -76,13 +76,14 @@ static int default_lockmgr_cb(void **arg, enum AVLockOp op)
     case AV_LOCK_OBTAIN:
         if (!*mutex) {
             pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
+            const pthread_mutex_t *cmp = NULL;
             if (!tmp)
                 return AVERROR(ENOMEM);
             if ((err = pthread_mutex_init(tmp, NULL))) {
                 av_free(tmp);
                 return AVERROR(err);
             }
-            if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
+            if (!atomic_compare_exchange_strong(mutex, &cmp, tmp)) {
                 pthread_mutex_destroy(tmp);
                 av_free(tmp);
             }
@@ -101,7 +102,7 @@ static int default_lockmgr_cb(void **arg, enum AVLockOp op)
         if (*mutex)
             pthread_mutex_destroy(*mutex);
         av_free(*mutex);
-        avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
+        atomic_compare_exchange_strong(mutex, mutex, NULL);
         return 0;
     }
     return 1;
@@ -113,7 +114,7 @@ static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
 
 
 volatile int ff_avcodec_locked;
-static int volatile entangled_thread_counter = 0;
+static atomic_int entangled_thread_counter = ATOMIC_VAR_INIT(0);
 static void *codec_mutex;
 static void *avformat_mutex;
 
@@ -178,11 +179,12 @@ int av_codec_is_decoder(const AVCodec *codec)
 av_cold void avcodec_register(AVCodec *codec)
 {
     AVCodec **p;
+    const AVCodec *cmp = NULL;
     avcodec_init();
     p = last_avcodec;
     codec->next = NULL;
 
-    while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
+    while(*p || !atomic_compare_exchange_strong(p, &cmp, codec))
         p = &(*p)->next;
     last_avcodec = &codec->next;
 
@@ -3859,8 +3861,9 @@ static AVHWAccel **last_hwaccel = &first_hwaccel;
 void av_register_hwaccel(AVHWAccel *hwaccel)
 {
     AVHWAccel **p = last_hwaccel;
+    const AVHWAccel *cmp = NULL;
     hwaccel->next = NULL;
-    while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
+    while(*p || !atomic_compare_exchange_strong(p, &cmp, hwaccel))
         p = &(*p)->next;
     last_hwaccel = &hwaccel->next;
 }
@@ -3912,7 +3915,7 @@ int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
             return -1;
     }
 
-    if (avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, 1) != 1) {
+    if (atomic_fetch_add(&entangled_thread_counter, 1) != 0) {
         av_log(log_ctx, AV_LOG_ERROR,
                "Insufficient thread locking. At least %d threads are "
                "calling avcodec_open2() at the same time right now.\n",
@@ -3935,7 +3938,7 @@ int ff_unlock_avcodec(const AVCodec *codec)
 
     av_assert0(ff_avcodec_locked);
     ff_avcodec_locked = 0;
-    avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, -1);
+    atomic_fetch_sub(&entangled_thread_counter, 1);
     if (lockmgr_cb) {
         if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
             return -1;
-- 
2.12.0



More information about the ffmpeg-devel mailing list