From 2fa695c3f9d4164344228ef4083e87369de3bb33 Mon Sep 17 00:00:00 2001 From: NRK Date: Wed, 18 Oct 2023 20:32:16 +0600 Subject: [PATCH] osdep: drop atomic fallback even msvc (which mpv apparently doesn't support) supports C11 atomics now. no need to carry around fallback with subtle semantic differences. --- DOCS/tech-overview.txt | 3 +- meson.build | 10 ++---- meson_options.txt | 1 - osdep/atomic.h | 80 ------------------------------------------ player/main.c | 4 --- 5 files changed, 4 insertions(+), 94 deletions(-) diff --git a/DOCS/tech-overview.txt b/DOCS/tech-overview.txt index dd21322bbb..fefca1431d 100644 --- a/DOCS/tech-overview.txt +++ b/DOCS/tech-overview.txt @@ -434,8 +434,7 @@ See generally available literature. In mpv, we use pthread for this. Always keep locking clean. Don't skip locking just because it will work "in practice". (See undefined behavior section.) If your use case is simple, you may -use C11 atomics (osdep/atomic.h for partial C99 support), but most likely you -will only hurt yourself and others. +use C11 atomics, but most likely you will only hurt yourself and others. Always make clear which fields in a struct are protected by which lock. If a field is immutable, or simply not thread-safe (e.g. state for a single worker diff --git a/meson.build b/meson.build index 6561fa89e5..ba9e0caa0f 100644 --- a/meson.build +++ b/meson.build @@ -333,6 +333,9 @@ if features['libdl'] dependencies += libdl endif +# C11 atomics are mandatory but linking to the library is not always required. +dependencies += cc.find_library('atomic', required: false) + cplugins = get_option('cplugins').require( win32 or (features['libdl'] and cc.has_link_argument('-rdynamic')), error_message: 'cplugins not supported by the os or compiler!', @@ -731,13 +734,6 @@ if features['sdl2-gamepad'] sources += files('input/sdl_gamepad.c') endif -stdatomic_dep = cc.find_library('atomic', required: false) -features += {'stdatomic': cc.has_header_symbol('stdatomic.h', 'atomic_int', dependencies: stdatomic_dep, - required: get_option('stdatomic'))} -if features['stdatomic'] - dependencies += stdatomic_dep -endif - uchardet_opt = get_option('uchardet').require( features['iconv'], error_message: 'iconv was not found!', diff --git a/meson_options.txt b/meson_options.txt index 3e6c8ef929..b22884354f 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -30,7 +30,6 @@ option('pthread-debug', type: 'feature', value: 'disabled', description: 'pthrea option('rubberband', type: 'feature', value: 'auto', description: 'librubberband support') option('sdl2', type: 'feature', value: 'disabled', description: 'SDL2') option('sdl2-gamepad', type: 'feature', value: 'auto', description: 'SDL2 gamepad input') -option('stdatomic', type: 'feature', value: 'auto', description: 'C11 stdatomic.h') option('uchardet', type: 'feature', value: 'auto', description: 'uchardet support') option('uwp', type: 'feature', value: 'disabled', description: 'Universal Windows Platform') option('vapoursynth', type: 'feature', value: 'auto', description: 'VapourSynth filter bridge') diff --git a/osdep/atomic.h b/osdep/atomic.h index 0bd301f637..ea00cea9b7 100644 --- a/osdep/atomic.h +++ b/osdep/atomic.h @@ -22,90 +22,10 @@ #include #include "config.h" -#if HAVE_STDATOMIC #include typedef _Atomic float mp_atomic_float; typedef _Atomic double mp_atomic_double; typedef _Atomic int64_t mp_atomic_int64; typedef _Atomic uint64_t mp_atomic_uint64; -#else - -// Emulate the parts of C11 stdatomic.h needed by mpv. - -typedef struct { unsigned long v; } atomic_ulong; -typedef struct { int v; } atomic_int; -typedef struct { unsigned int v; } atomic_uint; -typedef struct { _Bool v; } atomic_bool; -typedef struct { long long v; } atomic_llong; -typedef struct { uint_least32_t v; } atomic_uint_least32_t; -typedef struct { unsigned long long v; } atomic_ullong; - -typedef struct { float v; } mp_atomic_float; -typedef struct { double v; } mp_atomic_double; -typedef struct { int64_t v; } mp_atomic_int64; -typedef struct { uint64_t v; } mp_atomic_uint64; - -#define ATOMIC_VAR_INIT(x) \ - {.v = (x)} - -#define memory_order_relaxed 1 -#define memory_order_seq_cst 2 -#define memory_order_acq_rel 3 - -#include - -extern pthread_mutex_t mp_atomic_mutex; - -#define atomic_load(p) \ - ({ __typeof__(p) p_ = (p); \ - pthread_mutex_lock(&mp_atomic_mutex); \ - __typeof__(p_->v) v_ = p_->v; \ - pthread_mutex_unlock(&mp_atomic_mutex); \ - v_; }) -#define atomic_store(p, val) \ - ({ __typeof__(val) val_ = (val); \ - __typeof__(p) p_ = (p); \ - pthread_mutex_lock(&mp_atomic_mutex); \ - p_->v = val_; \ - pthread_mutex_unlock(&mp_atomic_mutex); }) -#define atomic_fetch_op(a, b, op) \ - ({ __typeof__(a) a_ = (a); \ - __typeof__(b) b_ = (b); \ - pthread_mutex_lock(&mp_atomic_mutex); \ - __typeof__(a_->v) v_ = a_->v; \ - a_->v = v_ op b_; \ - pthread_mutex_unlock(&mp_atomic_mutex); \ - v_; }) -#define atomic_fetch_add(a, b) atomic_fetch_op(a, b, +) -#define atomic_fetch_and(a, b) atomic_fetch_op(a, b, &) -#define atomic_fetch_or(a, b) atomic_fetch_op(a, b, |) -#define atomic_exchange(p, new) \ - ({ __typeof__(p) p_ = (p); \ - pthread_mutex_lock(&mp_atomic_mutex); \ - __typeof__(p_->v) res_ = p_->v; \ - p_->v = (new); \ - pthread_mutex_unlock(&mp_atomic_mutex); \ - res_; }) -#define atomic_compare_exchange_strong(p, old, new) \ - ({ __typeof__(p) p_ = (p); \ - __typeof__(old) old_ = (old); \ - __typeof__(new) new_ = (new); \ - pthread_mutex_lock(&mp_atomic_mutex); \ - int res_ = p_->v == *old_; \ - if (res_) { \ - p_->v = new_; \ - } else { \ - *old_ = p_->v; \ - } \ - pthread_mutex_unlock(&mp_atomic_mutex); \ - res_; }) - -#define atomic_load_explicit(a, b) \ - atomic_load(a) - -#define atomic_exchange_explicit(a, b, c) \ - atomic_exchange(a, b) - -#endif /* else HAVE_STDATOMIC */ #endif diff --git a/player/main.c b/player/main.c index f38ed58aec..8f0e63b418 100644 --- a/player/main.c +++ b/player/main.c @@ -80,10 +80,6 @@ static const char def_config[] = #define FULLCONFIG "(missing)\n" #endif -#if !HAVE_STDATOMIC -pthread_mutex_t mp_atomic_mutex = PTHREAD_MUTEX_INITIALIZER; -#endif - enum exit_reason { EXIT_NONE, EXIT_NORMAL,