mirror of
https://github.com/mpv-player/mpv
synced 2024-11-14 22:48:35 +01:00
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.
This commit is contained in:
parent
3fb4eb2ba4
commit
2fa695c3f9
@ -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
|
||||
|
10
meson.build
10
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!',
|
||||
|
@ -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')
|
||||
|
@ -22,90 +22,10 @@
|
||||
#include <inttypes.h>
|
||||
#include "config.h"
|
||||
|
||||
#if HAVE_STDATOMIC
|
||||
#include <stdatomic.h>
|
||||
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 <pthread.h>
|
||||
|
||||
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
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user