Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

osdep: drop atomic fallback #12667

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions DOCS/tech-overview.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions audio/out/ao_coreaudio_chmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include <AudioToolbox/AudioToolbox.h>

#include "config.h"

struct mp_chmap;

int ca_label_to_mp_speaker_id(AudioChannelLabel label);
Expand Down
3 changes: 2 additions & 1 deletion audio/out/ao_coreaudio_exclusive.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* when you are wanting to do good buffering of audio).
*/

#include <stdatomic.h>

#include <CoreAudio/HostTime.h>

#include <libavutil/intreadwrite.h>
Expand All @@ -43,7 +45,6 @@
#include "internal.h"
#include "audio/format.h"
#include "osdep/timer.h"
#include "osdep/atomic.h"
#include "options/m_option.h"
#include "common/msg.h"
#include "audio/out/ao_coreaudio_chmap.h"
Expand Down
2 changes: 2 additions & 0 deletions audio/out/ao_coreaudio_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <AudioToolbox/AudioToolbox.h>
#include <inttypes.h>
#include <stdbool.h>

#include "config.h"
#include "common/msg.h"
#include "audio/out/ao.h"
#include "internal.h"
Expand Down
2 changes: 1 addition & 1 deletion audio/out/ao_jack.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* with mpv. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -33,7 +34,6 @@
#include "ao.h"
#include "internal.h"
#include "audio/format.h"
#include "osdep/atomic.h"
#include "osdep/timer.h"
#include "options/m_config.h"
#include "options/m_option.h"
Expand Down
3 changes: 2 additions & 1 deletion audio/out/ao_wasapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
#ifndef MP_AO_WASAPI_H_
#define MP_AO_WASAPI_H_

#include <stdatomic.h>
#include <stdlib.h>
#include <stdbool.h>

#include <windows.h>
#include <mmdeviceapi.h>
#include <audioclient.h>
#include <audiopolicy.h>
#include <endpointvolume.h>

#include "common/msg.h"
#include "osdep/atomic.h"
#include "osdep/windows_utils.h"
#include "internal.h"
#include "ao.h"
Expand Down
6 changes: 3 additions & 3 deletions audio/out/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
#ifndef MP_AO_INTERNAL_H_
#define MP_AO_INTERNAL_H_

#include <stdbool.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>

#include "osdep/atomic.h"
#include "audio/out/ao.h"

/* global data used by ao.c and ao drivers */
Expand Down Expand Up @@ -63,7 +63,7 @@ struct ao {
atomic_uint events_;

// Float gain multiplicator
mp_atomic_float gain;
_Atomic float gain;

int buffer;
double def_buffer;
Expand Down
12 changes: 6 additions & 6 deletions common/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#include <stdint.h>

#include "mpv_talloc.h"

#include "misc/bstr.h"
#include "osdep/atomic.h"
#include "common/common.h"
#include "common/global.h"
#include "misc/bstr.h"
Expand Down Expand Up @@ -524,7 +524,7 @@ void mp_msg_init(struct mpv_global *global)
struct mp_log_root *root = talloc_zero(NULL, struct mp_log_root);
*root = (struct mp_log_root){
.global = global,
.reload_counter = ATOMIC_VAR_INIT(1),
.reload_counter = 1,
};

pthread_mutex_init(&root->lock, NULL);
Expand Down
2 changes: 1 addition & 1 deletion common/stats.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <pthread.h>
#include <stdatomic.h>
#include <time.h>
#include <unistd.h>

Expand All @@ -8,7 +9,6 @@
#include "misc/node.h"
#include "msg.h"
#include "options/m_option.h"
#include "osdep/atomic.h"
#include "osdep/timer.h"
#include "stats.h"

Expand Down
10 changes: 5 additions & 5 deletions demux/demux.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include <float.h>
#include <limits.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <limits.h>
#include <pthread.h>
#include <stdint.h>

#include <math.h>

Expand All @@ -42,7 +43,6 @@
#include "common/stats.h"
#include "misc/charset_conv.h"
#include "misc/thread_tools.h"
#include "osdep/atomic.h"
#include "osdep/timer.h"
#include "osdep/threads.h"

Expand Down
6 changes: 3 additions & 3 deletions filters/f_async_queue.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <limits.h>
#include <pthread.h>
#include <stdatomic.h>

#include "audio/aframe.h"
#include "common/common.h"
#include "common/msg.h"
#include "osdep/atomic.h"

#include "f_async_queue.h"
#include "filter_internal.h"
Expand All @@ -16,7 +16,7 @@ struct mp_async_queue {
};

struct async_queue {
mp_atomic_uint64 refcount;
_Atomic uint64_t refcount;

pthread_mutex_t lock;

Expand Down Expand Up @@ -73,7 +73,7 @@ struct mp_async_queue *mp_async_queue_create(void)
struct mp_async_queue *r = talloc_zero(NULL, struct mp_async_queue);
r->q = talloc_zero(NULL, struct async_queue);
*r->q = (struct async_queue){
.refcount = ATOMIC_VAR_INIT(1),
.refcount = 1,
};
pthread_mutex_init(&r->q->lock, NULL);
talloc_set_destructor(r, on_free_queue);
Expand Down
2 changes: 1 addition & 1 deletion filters/filter.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <math.h>
#include <pthread.h>
#include <stdatomic.h>

#include <libavutil/hwcontext.h>

#include "common/common.h"
#include "common/global.h"
#include "common/msg.h"
#include "osdep/atomic.h"
#include "osdep/timer.h"
#include "video/hwdec.h"
#include "video/img_format.h"
Expand Down
10 changes: 3 additions & 7 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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)
N-R-K marked this conversation as resolved.
Show resolved Hide resolved

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!',
Expand Down Expand Up @@ -732,13 +735,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!',
Expand Down
1 change: 0 additions & 1 deletion meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions misc/thread_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
*/

#include <assert.h>
#include <errno.h>
#include <stdatomic.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

#ifdef __MINGW32__
#include <windows.h>
Expand All @@ -27,7 +28,6 @@

#include "common/common.h"
#include "misc/linked_list.h"
#include "osdep/atomic.h"
#include "osdep/io.h"
#include "osdep/timer.h"

Expand Down Expand Up @@ -126,7 +126,7 @@ struct mp_cancel *mp_cancel_new(void *talloc_ctx)
struct mp_cancel *c = talloc_ptrtype(talloc_ctx, c);
talloc_set_destructor(c, cancel_destroy);
*c = (struct mp_cancel){
.triggered = ATOMIC_VAR_INIT(false),
.triggered = false,
.wakeup_pipe = {-1, -1},
};
pthread_mutex_init(&c->lock, NULL);
Expand Down
14 changes: 7 additions & 7 deletions options/m_config_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include <stdbool.h>
#include <pthread.h>

#include "m_config_core.h"
#include "options/m_option.h"
Expand All @@ -31,13 +32,12 @@
#include "common/msg.h"
#include "common/msg_control.h"
#include "misc/dispatch.h"
#include "osdep/atomic.h"

// For use with m_config_cache.
struct m_config_shadow {
pthread_mutex_t lock;
// Incremented on every option change.
mp_atomic_uint64 ts;
_Atomic uint64_t ts;
// -- immutable after init
// List of m_sub_options instances.
// Index 0 is the top-level and is always present.
Expand Down
12 changes: 6 additions & 6 deletions options/m_config_frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include <errno.h>
#include <float.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include <stdbool.h>
#include <pthread.h>

#include "libmpv/client.h"

Expand All @@ -36,7 +37,6 @@
#include "common/msg_control.h"
#include "misc/dispatch.h"
#include "misc/node.h"
#include "osdep/atomic.h"

extern const char mp_help_text[];

Expand Down
4 changes: 2 additions & 2 deletions options/m_config_frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

#pragma once

#include <stdatomic.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

#include "common/common.h"
#include "common/global.h"
Expand All @@ -29,7 +30,6 @@
#include "misc/bstr.h"
#include "misc/dispatch.h"
#include "options/m_option.h"
#include "osdep/atomic.h"

// m_config provides an API to manipulate the config variables in MPlayer.
// It makes use of the Options API to provide a context stack that
Expand Down
Loading