Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
To: ffmpeg-devel@ffmpeg.org
Subject: Re: [FFmpeg-devel] [PATCH 1/6] avformat/utils: Use static mutexes instead of ff_lock_avformat()
Date: Sun, 19 May 2024 11:57:59 +0200
Message-ID: <AS8P250MB074456BD0D0F7A7B22C6E7F98FE82@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <AS8P250MB0744CC67273864DA864725B98FEE2@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM>

Andreas Rheinhardt:
> Its existence is a remnant of (libavcodec's) lock-manager API
> which has been removed in a04c2c707de2ce850f79870e84ac9d7ec7aa9143.
> There is no need to use the same lock for avisynth, chromaprint
> or tls, so switch to ordinary static mutexes instead.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  libavformat/avisynth.c    | 15 +++++++++------
>  libavformat/chromaprint.c | 12 +++++++-----
>  libavformat/internal.h    |  3 ---
>  libavformat/tls_gnutls.c  | 16 +++++++---------
>  libavformat/tls_openssl.c | 17 +++++++----------
>  libavformat/utils.c       | 13 -------------
>  6 files changed, 30 insertions(+), 46 deletions(-)
> 
> diff --git a/libavformat/avisynth.c b/libavformat/avisynth.c
> index 1709bf4051..625bdf7e3a 100644
> --- a/libavformat/avisynth.c
> +++ b/libavformat/avisynth.c
> @@ -23,6 +23,7 @@
>  #include "libavutil/internal.h"
>  #include "libavutil/mem.h"
>  #include "libavutil/opt.h"
> +#include "libavutil/thread.h"
>  
>  #include "avformat.h"
>  #include "demux.h"
> @@ -130,6 +131,8 @@ static const int avs_planes_yuva[4]   = { AVS_PLANAR_Y, AVS_PLANAR_U,
>  static const int avs_planes_rgba[4]   = { AVS_PLANAR_G, AVS_PLANAR_B,
>                                            AVS_PLANAR_R, AVS_PLANAR_A };
>  
> +static AVMutex avisynth_mutex = AV_MUTEX_INITIALIZER;
> +
>  /* A conflict between C++ global objects, atexit, and dynamic loading requires
>   * us to register our own atexit handler to prevent double freeing. */
>  static AviSynthLibrary avs_library;
> @@ -1083,15 +1086,15 @@ static av_cold int avisynth_read_header(AVFormatContext *s)
>      int ret;
>  
>      // Calling library must implement a lock for thread-safe opens.
> -    if (ret = ff_lock_avformat())
> -        return ret;
> +    if (ff_mutex_lock(&avisynth_mutex))
> +        return AVERROR_UNKNOWN;
>  
>      if (ret = avisynth_open_file(s)) {
> -        ff_unlock_avformat();
> +        ff_mutex_unlock(&avisynth_mutex);
>          return ret;
>      }
>  
> -    ff_unlock_avformat();
> +    ff_mutex_unlock(&avisynth_mutex);
>      return 0;
>  }
>  
> @@ -1127,11 +1130,11 @@ static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
>  
>  static av_cold int avisynth_read_close(AVFormatContext *s)
>  {
> -    if (ff_lock_avformat())
> +    if (ff_mutex_lock(&avisynth_mutex))
>          return AVERROR_UNKNOWN;
>  
>      avisynth_context_destroy(s->priv_data);
> -    ff_unlock_avformat();
> +    ff_mutex_unlock(&avisynth_mutex);
>      return 0;
>  }
>  
> diff --git a/libavformat/chromaprint.c b/libavformat/chromaprint.c
> index 1cdca47ea5..eae233a651 100644
> --- a/libavformat/chromaprint.c
> +++ b/libavformat/chromaprint.c
> @@ -20,15 +20,17 @@
>   */
>  
>  #include "avformat.h"
> -#include "internal.h"
>  #include "mux.h"
>  #include "libavutil/opt.h"
> +#include "libavutil/thread.h"
>  #include <chromaprint.h>
>  
>  #define CPR_VERSION_INT AV_VERSION_INT(CHROMAPRINT_VERSION_MAJOR, \
>                                         CHROMAPRINT_VERSION_MINOR, \
>                                         CHROMAPRINT_VERSION_PATCH)
>  
> +static AVMutex chromaprint_mutex = AV_MUTEX_INITIALIZER;
> +
>  typedef enum FingerprintFormat {
>      FINGERPRINT_RAW,
>      FINGERPRINT_COMPRESSED,
> @@ -52,9 +54,9 @@ static void deinit(AVFormatContext *s)
>      ChromaprintMuxContext *const cpr = s->priv_data;
>  
>      if (cpr->ctx) {
> -        ff_lock_avformat();
> +        ff_mutex_lock(&chromaprint_mutex);
>          chromaprint_free(cpr->ctx);
> -        ff_unlock_avformat();
> +        ff_mutex_unlock(&chromaprint_mutex);
>      }
>  }
>  
> @@ -63,9 +65,9 @@ static av_cold int init(AVFormatContext *s)
>      ChromaprintMuxContext *cpr = s->priv_data;
>      AVStream *st;
>  
> -    ff_lock_avformat();
> +    ff_mutex_lock(&chromaprint_mutex);
>      cpr->ctx = chromaprint_new(cpr->algorithm);
> -    ff_unlock_avformat();
> +    ff_mutex_unlock(&chromaprint_mutex);
>  
>      if (!cpr->ctx) {
>          av_log(s, AV_LOG_ERROR, "Failed to create chromaprint context.\n");
> diff --git a/libavformat/internal.h b/libavformat/internal.h
> index 7f3d1c0086..6bad4fd119 100644
> --- a/libavformat/internal.h
> +++ b/libavformat/internal.h
> @@ -727,9 +727,6 @@ struct AVBPrint;
>   */
>  int ff_bprint_to_codecpar_extradata(AVCodecParameters *par, struct AVBPrint *buf);
>  
> -int ff_lock_avformat(void);
> -int ff_unlock_avformat(void);
> -
>  /**
>   * Set AVFormatContext url field to the provided pointer. The pointer must
>   * point to a valid string. The existing url field is freed if necessary. Also
> diff --git a/libavformat/tls_gnutls.c b/libavformat/tls_gnutls.c
> index 2ab38a199b..df251ad79c 100644
> --- a/libavformat/tls_gnutls.c
> +++ b/libavformat/tls_gnutls.c
> @@ -25,15 +25,12 @@
>  #include <gnutls/x509.h>
>  
>  #include "avformat.h"
> -#include "internal.h"
>  #include "network.h"
>  #include "os_support.h"
>  #include "url.h"
>  #include "tls.h"
> -#include "libavcodec/internal.h"
> -#include "libavutil/avstring.h"
>  #include "libavutil/opt.h"
> -#include "libavutil/parseutils.h"
> +#include "libavutil/thread.h"
>  
>  #ifndef GNUTLS_VERSION_NUMBER
>  #define GNUTLS_VERSION_NUMBER LIBGNUTLS_VERSION_NUMBER
> @@ -41,7 +38,6 @@
>  
>  #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
>  #include <gcrypt.h>
> -#include "libavutil/thread.h"
>  GCRY_THREAD_OPTION_PTHREAD_IMPL;
>  #endif
>  
> @@ -54,22 +50,24 @@ typedef struct TLSContext {
>      int io_err;
>  } TLSContext;
>  
> +static AVMutex gnutls_mutex = AV_MUTEX_INITIALIZER;
> +
>  void ff_gnutls_init(void)
>  {
> -    ff_lock_avformat();
> +    ff_mutex_lock(&gnutls_mutex);
>  #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
>      if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
>          gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
>  #endif
>      gnutls_global_init();
> -    ff_unlock_avformat();
> +    ff_mutex_unlock(&gnutls_mutex);
>  }
>  
>  void ff_gnutls_deinit(void)
>  {
> -    ff_lock_avformat();
> +    ff_mutex_lock(&gnutls_mutex);
>      gnutls_global_deinit();
> -    ff_unlock_avformat();
> +    ff_mutex_unlock(&gnutls_mutex);
>  }
>  
>  static int print_tls_error(URLContext *h, int ret)
> diff --git a/libavformat/tls_openssl.c b/libavformat/tls_openssl.c
> index b875be32f0..89d7c6e1ea 100644
> --- a/libavformat/tls_openssl.c
> +++ b/libavformat/tls_openssl.c
> @@ -19,17 +19,12 @@
>   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>   */
>  
> -#include "avformat.h"
> -#include "internal.h"
>  #include "network.h"
>  #include "os_support.h"
>  #include "url.h"
>  #include "tls.h"
> -#include "libavutil/avstring.h"
> -#include "libavutil/avutil.h"
>  #include "libavutil/mem.h"
>  #include "libavutil/opt.h"
> -#include "libavutil/parseutils.h"
>  #include "libavutil/thread.h"
>  
>  #include <openssl/bio.h>
> @@ -49,6 +44,8 @@ typedef struct TLSContext {
>      int io_err;
>  } TLSContext;
>  
> +static AVMutex openssl_mutex = AV_MUTEX_INITIALIZER;
> +
>  #if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L
>  #include <openssl/crypto.h>
>  pthread_mutex_t *openssl_mutexes;
> @@ -69,7 +66,7 @@ static unsigned long openssl_thread_id(void)
>  
>  int ff_openssl_init(void)
>  {
> -    ff_lock_avformat();
> +    ff_mutex_lock(&openssl_mutex);
>      if (!openssl_init) {
>          /* OpenSSL 1.0.2 or below, then you would use SSL_library_init. If you are
>           * using OpenSSL 1.1.0 or above, then the library will initialize
> @@ -85,7 +82,7 @@ int ff_openssl_init(void)
>              int i;
>              openssl_mutexes = av_malloc_array(sizeof(pthread_mutex_t), CRYPTO_num_locks());
>              if (!openssl_mutexes) {
> -                ff_unlock_avformat();
> +                ff_mutex_unlock(&openssl_mutex);
>                  return AVERROR(ENOMEM);
>              }
>  
> @@ -99,14 +96,14 @@ int ff_openssl_init(void)
>  #endif
>      }
>      openssl_init++;
> -    ff_unlock_avformat();
> +    ff_mutex_unlock(&openssl_mutex);
>  
>      return 0;
>  }
>  
>  void ff_openssl_deinit(void)
>  {
> -    ff_lock_avformat();
> +    ff_mutex_lock(&openssl_mutex);
>      openssl_init--;
>      if (!openssl_init) {
>  #if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L
> @@ -119,7 +116,7 @@ void ff_openssl_deinit(void)
>          }
>  #endif
>      }
> -    ff_unlock_avformat();
> +    ff_mutex_unlock(&openssl_mutex);
>  }
>  
>  static int print_tls_error(URLContext *h, int ret)
> diff --git a/libavformat/utils.c b/libavformat/utils.c
> index 4dded7aea4..e9ded627ad 100644
> --- a/libavformat/utils.c
> +++ b/libavformat/utils.c
> @@ -27,7 +27,6 @@
>  #include "libavutil/bprint.h"
>  #include "libavutil/internal.h"
>  #include "libavutil/mem.h"
> -#include "libavutil/thread.h"
>  #include "libavutil/time.h"
>  
>  #include "libavcodec/internal.h"
> @@ -40,23 +39,11 @@
>  #endif
>  #include "os_support.h"
>  
> -static AVMutex avformat_mutex = AV_MUTEX_INITIALIZER;
> -
>  /**
>   * @file
>   * various utility functions for use within FFmpeg
>   */
>  
> -int ff_lock_avformat(void)
> -{
> -    return ff_mutex_lock(&avformat_mutex) ? -1 : 0;
> -}
> -
> -int ff_unlock_avformat(void)
> -{
> -    return ff_mutex_unlock(&avformat_mutex) ? -1 : 0;
> -}
> -
>  /* an arbitrarily chosen "sane" max packet size -- 50M */
>  #define SANE_CHUNK_SIZE (50000000)
>  

Will apply this patchset tomorrow unless there are objections.

- Andreas

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

      parent reply	other threads:[~2024-05-19  9:58 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-17 15:23 Andreas Rheinhardt
2024-05-17 15:25 ` [FFmpeg-devel] [PATCH 2/6] avformat/tls_openssl: #if ff_openssl_init/deinit() away if possible Andreas Rheinhardt
2024-05-17 15:25 ` [FFmpeg-devel] [PATCH 3/6] avformat/tee: Constify AVDictionaryEntry* pointee where possible Andreas Rheinhardt
2024-05-17 15:42   ` epirat07
2024-05-17 16:17   ` Andreas Rheinhardt
2024-05-17 15:25 ` [FFmpeg-devel] [PATCH 4/6] avformat/tee: Use smaller scope for variables Andreas Rheinhardt
2024-05-17 15:25 ` [FFmpeg-devel] [PATCH 5/6] avcodec/lib*, avformat/tee: Simplify iterating over AVDictionary Andreas Rheinhardt
2024-05-17 15:41   ` epirat07
2024-05-17 15:44     ` Andreas Rheinhardt
2024-05-17 15:48       ` epirat07
2024-05-17 15:25 ` [FFmpeg-devel] [PATCH 6/6] fftools, avfilter, avformat: Simplify check for "is dictionary empty?" Andreas Rheinhardt
2024-05-17 15:39   ` epirat07
2024-05-19  9:57 ` Andreas Rheinhardt [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AS8P250MB074456BD0D0F7A7B22C6E7F98FE82@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM \
    --to=andreas.rheinhardt@outlook.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git