[FFmpeg-cvslog] lavc: remove complex debug code around avcodec init locking

wm4 git at videolan.org
Tue Dec 26 03:50:17 EET 2017


ffmpeg | branch: master | wm4 <nfxjfg at googlemail.com> | Thu Dec 21 23:03:24 2017 +0100| [4ed66517c62c599701b3793fa2843d5a8530a4f4] | committer: wm4

lavc: remove complex debug code around avcodec init locking

This is just a lot of complicated and confusing code that had no purpose
anymore.

Also, the functions return values were checked only sometimes. Locking
shouldn't fail anyway, so remove the return values. Barely any other
pthread lock calls check the return value (including more important code
that is more likely to fail horribly if locking fails).

It could be argued that it might be helpful in some debugging
situations, or when the user built FFmpeg without thread support against
all good advice.

But there are dummy atomics too, so the atomic check won't help with
ensuring correctness absolutely. You gain very little.

Also, for debugging, you can just raise the ASSERT_LEVEL, and then
libavutil/thread.h will redefine the locking functions to explicitly
check the return values.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=4ed66517c62c599701b3793fa2843d5a8530a4f4
---

 libavcodec/internal.h |  4 ----
 libavcodec/utils.c    | 55 +++++++++++++--------------------------------------
 2 files changed, 14 insertions(+), 45 deletions(-)

diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 30614bb2b1..16bd101284 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -243,10 +243,6 @@ int ff_init_buffer_info(AVCodecContext *s, AVFrame *frame);
 
 void ff_color_frame(AVFrame *frame, const int color[4]);
 
-extern volatile int ff_avcodec_locked;
-int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec);
-int ff_unlock_avcodec(const AVCodec *codec);
-
 /**
  * Maximum size in bytes of extradata.
  * This value was chosen such that every bit of the buffer is
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 9c631c4fb0..dfbfe98d63 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -67,8 +67,6 @@
 #include "libavutil/ffversion.h"
 const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
 
-volatile int ff_avcodec_locked;
-static atomic_int entangled_thread_counter = ATOMIC_VAR_INIT(0);
 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
 
 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
@@ -550,6 +548,19 @@ static int64_t get_bit_rate(AVCodecContext *ctx)
     return bit_rate;
 }
 
+
+static void ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
+{
+    if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
+        ff_mutex_lock(&codec_mutex);
+}
+
+static void ff_unlock_avcodec(const AVCodec *codec)
+{
+    if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
+        ff_mutex_unlock(&codec_mutex);
+}
+
 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
 {
     int ret = 0;
@@ -589,9 +600,7 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
     if (options)
         av_dict_copy(&tmp, *options, 0);
 
-    ret = ff_lock_avcodec(avctx, codec);
-    if (ret < 0)
-        return ret;
+    ff_lock_avcodec(avctx, codec);
 
     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
     if (!avctx->internal) {
@@ -1867,42 +1876,6 @@ int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
 }
 #endif
 
-int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
-{
-    if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
-        return 0;
-
-    if (ff_mutex_lock(&codec_mutex))
-        return -1;
-
-    if (atomic_fetch_add(&entangled_thread_counter, 1)) {
-        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",
-               atomic_load(&entangled_thread_counter));
-        ff_avcodec_locked = 1;
-        ff_unlock_avcodec(codec);
-        return AVERROR(EINVAL);
-    }
-    av_assert0(!ff_avcodec_locked);
-    ff_avcodec_locked = 1;
-    return 0;
-}
-
-int ff_unlock_avcodec(const AVCodec *codec)
-{
-    if (codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE || !codec->init)
-        return 0;
-
-    av_assert0(ff_avcodec_locked);
-    ff_avcodec_locked = 0;
-    atomic_fetch_add(&entangled_thread_counter, -1);
-    if (ff_mutex_unlock(&codec_mutex))
-        return -1;
-
-    return 0;
-}
-
 unsigned int avpriv_toupper4(unsigned int x)
 {
     return av_toupper(x & 0xFF) +



More information about the ffmpeg-cvslog mailing list