0
0
mirror of https://github.com/mpv-player/mpv.git synced 2024-09-20 12:02:23 +02:00

ao: make better use of atomics

The main reason for this was compatibility; but some associated problems
have been solved in the previous commit.
This commit is contained in:
wm4 2015-05-11 23:27:41 +02:00
parent cc24ec5b3c
commit ca9964a4fb
3 changed files with 12 additions and 14 deletions

View File

@ -385,28 +385,26 @@ bool ao_eof_reached(struct ao *ao)
// Query the AO_EVENT_*s as requested by the events parameter, and return them. // Query the AO_EVENT_*s as requested by the events parameter, and return them.
int ao_query_and_reset_events(struct ao *ao, int events) int ao_query_and_reset_events(struct ao *ao, int events)
{ {
int actual_events = 0; return atomic_fetch_and(&ao->events_, ~(unsigned)events) & events;
if (atomic_load(&ao->request_reload)) // don't need to reset it }
actual_events |= AO_EVENT_RELOAD;
if (atomic_load(&ao->request_hotplug)) static void ao_add_events(struct ao *ao, int events)
actual_events |= AO_EVENT_HOTPLUG; {
return actual_events & events; atomic_fetch_or(&ao->events_, events);
if (ao->input_ctx)
mp_input_wakeup(ao->input_ctx);
} }
// Request that the player core destroys and recreates the AO. Fully thread-safe. // Request that the player core destroys and recreates the AO. Fully thread-safe.
void ao_request_reload(struct ao *ao) void ao_request_reload(struct ao *ao)
{ {
atomic_store(&ao->request_reload, true); ao_add_events(ao, AO_EVENT_RELOAD);
if (ao->input_ctx)
mp_input_wakeup(ao->input_ctx);
} }
// Notify the player that the device list changed. Fully thread-safe. // Notify the player that the device list changed. Fully thread-safe.
void ao_hotplug_event(struct ao *ao) void ao_hotplug_event(struct ao *ao)
{ {
atomic_store(&ao->request_hotplug, true); ao_add_events(ao, AO_EVENT_HOTPLUG);
if (ao->input_ctx)
mp_input_wakeup(ao->input_ctx);
} }
bool ao_chmap_sel_adjust(struct ao *ao, const struct mp_chmap_sel *s, bool ao_chmap_sel_adjust(struct ao *ao, const struct mp_chmap_sel *s,
@ -503,7 +501,6 @@ bool ao_hotplug_check_update(struct ao_hotplug *hp)
{ {
if (hp->ao && ao_query_and_reset_events(hp->ao, AO_EVENT_HOTPLUG)) { if (hp->ao && ao_query_and_reset_events(hp->ao, AO_EVENT_HOTPLUG)) {
hp->needs_update = true; hp->needs_update = true;
atomic_store(&hp->ao->request_hotplug, false);
return true; return true;
} }
return false; return false;

View File

@ -59,7 +59,7 @@ struct ao {
char *redirect; char *redirect;
// Internal events (use ao_request_reload(), ao_hotplug_event()) // Internal events (use ao_request_reload(), ao_hotplug_event())
atomic_bool request_reload, request_hotplug; atomic_int events_;
int buffer; int buffer;
double def_buffer; double def_buffer;

View File

@ -34,6 +34,7 @@
typedef struct { volatile unsigned long v, t; } atomic_ulong; typedef struct { volatile unsigned long v, t; } atomic_ulong;
typedef struct { volatile int v, t; } atomic_int; typedef struct { volatile int v, t; } atomic_int;
typedef struct { volatile unsigned int v, t; } atomic_uint;
typedef struct { volatile _Bool v, t; } atomic_bool; typedef struct { volatile _Bool v, t; } atomic_bool;
typedef struct { volatile long long v, t; } atomic_llong; typedef struct { volatile long long v, t; } atomic_llong;
typedef struct { volatile uint_least32_t v, t; } atomic_uint_least32_t; typedef struct { volatile uint_least32_t v, t; } atomic_uint_least32_t;