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

ao: avoid unnecessary wakeups

If ao_add_events() is used, but all events flags are already set, then
we don't need to wakeup the core again.

Also, make the underrun message "exact" by avoiding the race condition
mentioned in the comment.

Avoiding redundant wakeups is not really worth the trouble, and it's
actually just a bonus in the change making the ao_underrun_event()
function return whether a new underrun was set, which is needed by the
following commit.
This commit is contained in:
wm4 2020-02-13 01:06:02 +01:00
parent 7e6ea02183
commit f3c498c7f1
3 changed files with 14 additions and 9 deletions

View File

@ -444,10 +444,14 @@ int ao_query_and_reset_events(struct ao *ao, int events)
return atomic_fetch_and(&ao->events_, ~(unsigned)events) & events;
}
void ao_add_events(struct ao *ao, int events)
// Returns events that were set by this calls.
int ao_add_events(struct ao *ao, int events)
{
atomic_fetch_or(&ao->events_, events);
ao->wakeup_cb(ao->wakeup_ctx);
unsigned prev_events = atomic_fetch_or(&ao->events_, events);
unsigned new = events & ~prev_events;
if (new)
ao->wakeup_cb(ao->wakeup_ctx);
return new;
}
// Request that the player core destroys and recreates the AO. Fully thread-safe.
@ -462,12 +466,13 @@ void ao_hotplug_event(struct ao *ao)
ao_add_events(ao, AO_EVENT_HOTPLUG);
}
void ao_underrun_event(struct ao *ao)
// Returns whether this call actually set a new underrun flag.
bool ao_underrun_event(struct ao *ao)
{
// Racy check, but it's just for the message.
if (!(atomic_load(&ao->events_) & AO_EVENT_UNDERRUN))
bool new_underrun = ao_add_events(ao, AO_EVENT_UNDERRUN);
if (new_underrun)
MP_WARN(ao, "Device underrun detected.\n");
ao_add_events(ao, AO_EVENT_UNDERRUN);
return new_underrun;
}
bool ao_chmap_sel_adjust(struct ao *ao, const struct mp_chmap_sel *s,

View File

@ -110,7 +110,7 @@ void ao_resume(struct ao *ao);
void ao_drain(struct ao *ao);
bool ao_eof_reached(struct ao *ao);
int ao_query_and_reset_events(struct ao *ao, int events);
void ao_add_events(struct ao *ao, int events);
int ao_add_events(struct ao *ao, int events);
void ao_unblock(struct ao *ao);
void ao_request_reload(struct ao *ao);
void ao_hotplug_event(struct ao *ao);

View File

@ -211,7 +211,7 @@ struct pollfd;
int ao_wait_poll(struct ao *ao, struct pollfd *fds, int num_fds,
pthread_mutex_t *lock);
void ao_wakeup_poll(struct ao *ao);
void ao_underrun_event(struct ao *ao);
bool ao_underrun_event(struct ao *ao);
bool ao_chmap_sel_adjust(struct ao *ao, const struct mp_chmap_sel *s,
struct mp_chmap *map);