From patchwork Sat Dec 23 23:51:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wm4 X-Patchwork-Id: 6942 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.79.195 with SMTP id r64csp4096515jad; Sat, 23 Dec 2017 15:51:39 -0800 (PST) X-Google-Smtp-Source: ACJfBovKmN4xTKFFbI4mZWyxFeC6jp5JW430pqBZOeogmEHwLmTXRH1H+r4y1QPV14aG4z6pTiGE X-Received: by 10.28.14.141 with SMTP id 135mr16475147wmo.104.1514073099719; Sat, 23 Dec 2017 15:51:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514073099; cv=none; d=google.com; s=arc-20160816; b=fSnE+D+BGFfmYh6wB9UOJYLe5sSHSeXX4NOZW0vpzKE6ZzPAwZ6906Otcl+DdJcfQZ aJ1AyyURfPGq/t49B9gDpcYw6oCIKNJ44bXRlRFcEMb6B4cvWopNTi4hFiSFXXCHscfN tMgDfYSo7DgK4mTh70IUnkn3JtzPvX3RZ5dvNqXAHGqEyJpj+MtZPTqdhyz2+4QwDZJa GAF09TXjwxqs8ubHuwp3Ai0I+BqPJW+yAT3gDp8WrxRvgDE6I/kuErKyib0eY1V2Z/Sp tR58aVP1GM8qWk1ZbM1m5Au5vkirmBNwRiUdbNw56axv+0ox112OlriYwt8nUYe4NY6D LP+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=6D2bKhxv1Qb9ZHBXGBNvrHv2PohyW2ZMUi0l4Oqz/yw=; b=LKwGDr9u/g/w1gZb5qBK7LFdDBjaPA53pxXerRRWEqyMUCsBr+IcCfuYFzeQWPcxIr 7ItG4NqXKoMhbqAa1XnOc0U6wUpo1rfXNFAeu0EYZGSuq9X4ZSK2qRW4+YBjK0DkQ3DN nDmSfS+NddinLWJBtteE7oDhJGZ/iav9E3OcXQCih9ME5l/IYnKJ0cuh9MUID5w4P0VZ W7NYT/Jk0Tefa3ULvenyT1pv0uXYhdx+2rpakTaF4di+dhJlORLXn4Y62/T84rpspC8E YdVO/+wfucr3Fha4IHX0Mt5JLZXxzRqE8ftTxadd2MXqOBq3I7LApRzImjQiyKJ52N0Y 565w== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@googlemail.com header.s=20161025 header.b=YbtosJy1; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id k16si4850283wrg.112.2017.12.23.15.51.39; Sat, 23 Dec 2017 15:51:39 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@googlemail.com header.s=20161025 header.b=YbtosJy1; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4CE0668839D; Sun, 24 Dec 2017 01:51:19 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f195.google.com (mail-wr0-f195.google.com [209.85.128.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BE23C688249 for ; Sun, 24 Dec 2017 01:51:10 +0200 (EET) Received: by mail-wr0-f195.google.com with SMTP id p17so7022194wre.7 for ; Sat, 23 Dec 2017 15:51:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=UgEsIrntysJWJxf8m48wZyNPcdQpvg+yqYMgBo1veqY=; b=YbtosJy1oJIChZKZ9RmhN2jh0dTfkdqdhQ16Af4AKewvhW+/bHffqUt2BrBFuVIADJ RYN6cZlBVS+eUmMJVg2BTKKTWsRqr8i3sLALZDNuFW0FYPcP6JH6wXd8ZPbn9tzbSmA4 0zXf8rQeM4ufdtJev5uJD1jySb3Np4aTc7yv9be23WWEHowlFM2iwcp2N/ju9yNgPuoJ C1s7OVPNzvaER+tVIuCpzPq+wR2L5a/+Ud9FwS/CJayOEkLuwwrYPSbM1ab0z1RksFwT zHn+GjQ/DDb0IEyj7c7/AZsf/AFhwJV1rFinAkuwwbYVZMRd2vHEX8msnYWvvTMksGNs N2/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=UgEsIrntysJWJxf8m48wZyNPcdQpvg+yqYMgBo1veqY=; b=Ma4o6hl5KCCIQEDfREx0Q8YzJgGfgsPI5gEp4NvkLOO62qaMOlwCWK2ctphote+PqC c2SRleCTG2cdyjAdK2vj1iLrlBRO74GA7pbk8hGDJRSw1vuYTyz4eyI/MLMBPmtVr1DQ HrLyq2jU0ZHAkQ+nL/mprdQvq6Mj3i14AblNG0ZUW70reraUW8SlntNVsNC7esVA7lbe SL5JEtkzbv+iFW9UrWS41qwG9UXCJ0nxtikNs1rAHjy/tiIB9TntohftNyibAzf7NuHB A48IoP3agp+37Y7m6fSCSbdLNW7HuccZVFc5Q2+UDQVlhBd8w0EJXR1pY4XSlbaQd7Ll ILRQ== X-Gm-Message-State: AKGB3mIXN5xpScg6Qk8ShQL0ed0cY7EiBODObnQ2dEBZhqLcZw6XRnys iMt3rXVHxgeNKc6hne9Q7J87vw== X-Received: by 10.223.169.67 with SMTP id u61mr20211203wrc.30.1514073082520; Sat, 23 Dec 2017 15:51:22 -0800 (PST) Received: from debian.speedport.ip (p2003006CCD4EDC353C06725800124D49.dip0.t-ipconnect.de. [2003:6c:cd4e:dc35:3c06:7258:12:4d49]) by smtp.googlemail.com with ESMTPSA id i66sm7743406wmf.3.2017.12.23.15.51.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 23 Dec 2017 15:51:21 -0800 (PST) From: wm4 To: ffmpeg-devel@ffmpeg.org Date: Sun, 24 Dec 2017 00:51:17 +0100 Message-Id: <20171223235120.31578-2-nfxjfg@googlemail.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171223235120.31578-1-nfxjfg@googlemail.com> References: <20171221222224.18577-1-nfxjfg@googlemail.com> <20171223235120.31578-1-nfxjfg@googlemail.com> Subject: [FFmpeg-devel] [PATCH v3 2/5] lavc: replace and deprecate the lock manager X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: wm4 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Use static mutexes instead of requiring a lock manager. The behavior should be roughly the same before and after this change for API users which did not set the lock manager at all (except that a minor memory leak disappears). --- doc/APIchanges | 5 +++ libavcodec/avcodec.h | 8 +++- libavcodec/utils.c | 107 +++++---------------------------------------------- libavcodec/version.h | 5 ++- libavutil/thread.h | 2 + 5 files changed, 28 insertions(+), 99 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index da444ffb7c..df1391d83a 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -15,6 +15,11 @@ libavutil: 2017-10-21 API changes, most recent first: +2017-xx-xx - xxxxxxx - lavc 58.9.100 - avcodec.h + Deprecate av_lockmgr_register(). You need to build FFmpeg with threading + support enabled to get basic thread-safety (which is the default build + configuration). + 2017-xx-xx - xxxxxxx - lavc 58.8.100 - avcodec.h The MediaCodec decoders now support AVCodecContext.hw_device_ctx. diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index ce089b7c4a..a9182a9e3d 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -5930,10 +5930,11 @@ attribute_deprecated AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel); #endif - +#if FF_API_LOCKMGR /** * Lock operation used by lockmgr */ +attribute_deprecated enum AVLockOp { AV_LOCK_CREATE, ///< Create a mutex AV_LOCK_OBTAIN, ///< Lock the mutex @@ -5963,8 +5964,13 @@ enum AVLockOp { * mechanism (i.e. do not use a single static object to * implement your lock manager). If cb is set to NULL the * lockmgr will be unregistered. + * + * @deprecated This function does nothing, and always returns 0. Be sure to + * build with thread support to get basic thread safety. */ +attribute_deprecated int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op)); +#endif /** * Get the type of the given codec. diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 873f39f9bd..eec4437693 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -67,58 +67,10 @@ #include "libavutil/ffversion.h" const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION; -#if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS -static int default_lockmgr_cb(void **arg, enum AVLockOp op) -{ - void * volatile * mutex = arg; - int err; - - switch (op) { - case AV_LOCK_CREATE: - return 0; - case AV_LOCK_OBTAIN: - if (!*mutex) { - pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t)); - 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)) { - pthread_mutex_destroy(tmp); - av_free(tmp); - } - } - - if ((err = pthread_mutex_lock(*mutex))) - return AVERROR(err); - - return 0; - case AV_LOCK_RELEASE: - if ((err = pthread_mutex_unlock(*mutex))) - return AVERROR(err); - - return 0; - case AV_LOCK_DESTROY: - if (*mutex) - pthread_mutex_destroy(*mutex); - av_free(*mutex); - avpriv_atomic_ptr_cas(mutex, *mutex, NULL); - return 0; - } - return 1; -} -static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb; -#else -static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL; -#endif - - volatile int ff_avcodec_locked; static atomic_int entangled_thread_counter = ATOMIC_VAR_INIT(0); -static void *codec_mutex; -static void *avformat_mutex; +static AVMutex codec_mutex = AV_MUTEX_INITIALIZER; +static AVMutex avformat_mutex = AV_MUTEX_INITIALIZER; void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size) { @@ -1909,55 +1861,26 @@ void av_register_hwaccel(AVHWAccel *hwaccel) } #endif +#if FF_API_LOCKMGR int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op)) { - if (lockmgr_cb) { - // There is no good way to rollback a failure to destroy the - // mutex, so we ignore failures. - lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY); - lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY); - lockmgr_cb = NULL; - codec_mutex = NULL; - avformat_mutex = NULL; - } - - if (cb) { - void *new_codec_mutex = NULL; - void *new_avformat_mutex = NULL; - int err; - if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) { - return err > 0 ? AVERROR_UNKNOWN : err; - } - if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) { - // Ignore failures to destroy the newly created mutex. - cb(&new_codec_mutex, AV_LOCK_DESTROY); - return err > 0 ? AVERROR_UNKNOWN : err; - } - lockmgr_cb = cb; - codec_mutex = new_codec_mutex; - avformat_mutex = new_avformat_mutex; - } - return 0; } +#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 (lockmgr_cb) { - if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) - return -1; - } + 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)); - if (!lockmgr_cb) - av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n"); ff_avcodec_locked = 1; ff_unlock_avcodec(codec); return AVERROR(EINVAL); @@ -1975,30 +1898,20 @@ int ff_unlock_avcodec(const AVCodec *codec) av_assert0(ff_avcodec_locked); ff_avcodec_locked = 0; atomic_fetch_add(&entangled_thread_counter, -1); - if (lockmgr_cb) { - if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE)) - return -1; - } + if (ff_mutex_unlock(&codec_mutex)) + return -1; return 0; } int avpriv_lock_avformat(void) { - if (lockmgr_cb) { - if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN)) - return -1; - } - return 0; + return ff_mutex_lock(&avformat_mutex) ? -1 : 0; } int avpriv_unlock_avformat(void) { - if (lockmgr_cb) { - if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE)) - return -1; - } - return 0; + return ff_mutex_unlock(&avformat_mutex) ? -1 : 0; } unsigned int avpriv_toupper4(unsigned int x) diff --git a/libavcodec/version.h b/libavcodec/version.h index d55de89797..47a15d52b8 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -28,7 +28,7 @@ #include "libavutil/version.h" #define LIBAVCODEC_VERSION_MAJOR 58 -#define LIBAVCODEC_VERSION_MINOR 8 +#define LIBAVCODEC_VERSION_MINOR 9 #define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ @@ -126,6 +126,9 @@ #ifndef FF_API_USER_VISIBLE_AVHWACCEL #define FF_API_USER_VISIBLE_AVHWACCEL (LIBAVCODEC_VERSION_MAJOR < 59) #endif +#ifndef FF_API_LOCKMGR +#define FF_API_LOCKMGR (LIBAVCODEC_VERSION_MAJOR < 59) +#endif #endif /* AVCODEC_VERSION_H */ diff --git a/libavutil/thread.h b/libavutil/thread.h index 309414aa7d..cc5272d379 100644 --- a/libavutil/thread.h +++ b/libavutil/thread.h @@ -134,6 +134,7 @@ static inline int strict_pthread_once(pthread_once_t *once_control, void (*init_ #endif #define AVMutex pthread_mutex_t +#define AV_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER #define ff_mutex_init pthread_mutex_init #define ff_mutex_lock pthread_mutex_lock @@ -148,6 +149,7 @@ static inline int strict_pthread_once(pthread_once_t *once_control, void (*init_ #else #define AVMutex char +#define AV_MUTEX_INITIALIZER 0 static inline int ff_mutex_init(AVMutex *mutex, const void *attr){ return 0; } static inline int ff_mutex_lock(AVMutex *mutex){ return 0; }