mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-09 13:29:59 -05:00
mainloop: calculate in pa_usec_t everywhere
This commit is contained in:
parent
a43118b730
commit
a049909a70
1 changed files with 46 additions and 21 deletions
|
|
@ -112,7 +112,7 @@ struct pa_mainloop {
|
||||||
struct pollfd *pollfds;
|
struct pollfd *pollfds;
|
||||||
unsigned max_pollfds, n_pollfds;
|
unsigned max_pollfds, n_pollfds;
|
||||||
|
|
||||||
int prepared_timeout;
|
pa_usec_t prepared_timeout;
|
||||||
pa_time_event *cached_next_time_event;
|
pa_time_event *cached_next_time_event;
|
||||||
|
|
||||||
pa_mainloop_api api;
|
pa_mainloop_api api;
|
||||||
|
|
@ -320,21 +320,19 @@ static void mainloop_defer_set_destroy(pa_defer_event *e, pa_defer_event_destroy
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Time events */
|
/* Time events */
|
||||||
static pa_usec_t timeval_load(const struct timeval *tv) {
|
static pa_usec_t make_rt(const struct timeval *tv) {
|
||||||
pa_bool_t is_rtclock;
|
|
||||||
struct timeval ttv;
|
struct timeval ttv;
|
||||||
|
|
||||||
if (!tv)
|
if (!tv)
|
||||||
return PA_USEC_INVALID;
|
return PA_USEC_INVALID;
|
||||||
|
|
||||||
|
if (tv->tv_usec & PA_TIMEVAL_RTCLOCK) {
|
||||||
ttv = *tv;
|
ttv = *tv;
|
||||||
is_rtclock = !!(ttv.tv_usec & PA_TIMEVAL_RTCLOCK);
|
|
||||||
ttv.tv_usec &= ~PA_TIMEVAL_RTCLOCK;
|
ttv.tv_usec &= ~PA_TIMEVAL_RTCLOCK;
|
||||||
|
tv = pa_rtclock_from_wallclock(&ttv);
|
||||||
|
}
|
||||||
|
|
||||||
if (!is_rtclock)
|
return pa_timeval_load(tv);
|
||||||
pa_rtclock_from_wallclock(&ttv);
|
|
||||||
|
|
||||||
return pa_timeval_load(&ttv);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static pa_time_event* mainloop_time_new(
|
static pa_time_event* mainloop_time_new(
|
||||||
|
|
@ -351,7 +349,7 @@ static pa_time_event* mainloop_time_new(
|
||||||
pa_assert(a->userdata);
|
pa_assert(a->userdata);
|
||||||
pa_assert(callback);
|
pa_assert(callback);
|
||||||
|
|
||||||
t = timeval_load(tv);
|
t = make_rt(tv);
|
||||||
|
|
||||||
m = a->userdata;
|
m = a->userdata;
|
||||||
pa_assert(a == &m->api);
|
pa_assert(a == &m->api);
|
||||||
|
|
@ -392,7 +390,7 @@ static void mainloop_time_restart(pa_time_event *e, const struct timeval *tv) {
|
||||||
pa_assert(e);
|
pa_assert(e);
|
||||||
pa_assert(!e->dead);
|
pa_assert(!e->dead);
|
||||||
|
|
||||||
t = timeval_load(tv);
|
t = make_rt(tv);
|
||||||
|
|
||||||
valid = (t != PA_USEC_INVALID);
|
valid = (t != PA_USEC_INVALID);
|
||||||
if (e->enabled && !valid) {
|
if (e->enabled && !valid) {
|
||||||
|
|
@ -763,12 +761,12 @@ static pa_time_event* find_next_time_event(pa_mainloop *m) {
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int calc_next_timeout(pa_mainloop *m) {
|
static pa_usec_t calc_next_timeout(pa_mainloop *m) {
|
||||||
pa_time_event *t;
|
pa_time_event *t;
|
||||||
pa_usec_t clock_now;
|
pa_usec_t clock_now;
|
||||||
|
|
||||||
if (!m->n_enabled_time_events)
|
if (m->n_enabled_time_events <= 0)
|
||||||
return -1;
|
return PA_USEC_INVALID;
|
||||||
|
|
||||||
pa_assert_se(t = find_next_time_event(m));
|
pa_assert_se(t = find_next_time_event(m));
|
||||||
|
|
||||||
|
|
@ -780,7 +778,7 @@ static int calc_next_timeout(pa_mainloop *m) {
|
||||||
if (t->time <= clock_now)
|
if (t->time <= clock_now)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return (int) ((t->time - clock_now) / 1000); /* in milliseconds */
|
return t->time - clock_now;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int dispatch_timeout(pa_mainloop *m) {
|
static int dispatch_timeout(pa_mainloop *m) {
|
||||||
|
|
@ -850,13 +848,18 @@ int pa_mainloop_prepare(pa_mainloop *m, int timeout) {
|
||||||
goto quit;
|
goto quit;
|
||||||
|
|
||||||
if (m->n_enabled_defer_events <= 0) {
|
if (m->n_enabled_defer_events <= 0) {
|
||||||
|
|
||||||
if (m->rebuild_pollfds)
|
if (m->rebuild_pollfds)
|
||||||
rebuild_pollfds(m);
|
rebuild_pollfds(m);
|
||||||
|
|
||||||
m->prepared_timeout = calc_next_timeout(m);
|
m->prepared_timeout = calc_next_timeout(m);
|
||||||
if (timeout >= 0 && (timeout < m->prepared_timeout || m->prepared_timeout < 0))
|
if (timeout >= 0) {
|
||||||
|
uint64_t u = (uint64_t) timeout * PA_USEC_PER_MSEC;
|
||||||
|
|
||||||
|
if (u < m->prepared_timeout || m->prepared_timeout == PA_USEC_INVALID)
|
||||||
m->prepared_timeout = timeout;
|
m->prepared_timeout = timeout;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
m->state = STATE_PREPARED;
|
m->state = STATE_PREPARED;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -866,6 +869,13 @@ quit:
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int usec_to_timeout(pa_usec_t u) {
|
||||||
|
if (u == PA_USEC_INVALID)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return (u + PA_USEC_PER_MSEC - 1) / PA_USEC_PER_MSEC;
|
||||||
|
}
|
||||||
|
|
||||||
int pa_mainloop_poll(pa_mainloop *m) {
|
int pa_mainloop_poll(pa_mainloop *m) {
|
||||||
pa_assert(m);
|
pa_assert(m);
|
||||||
pa_assert(m->state == STATE_PREPARED);
|
pa_assert(m->state == STATE_PREPARED);
|
||||||
|
|
@ -881,9 +891,24 @@ int pa_mainloop_poll(pa_mainloop *m) {
|
||||||
pa_assert(!m->rebuild_pollfds);
|
pa_assert(!m->rebuild_pollfds);
|
||||||
|
|
||||||
if (m->poll_func)
|
if (m->poll_func)
|
||||||
m->poll_func_ret = m->poll_func(m->pollfds, m->n_pollfds, m->prepared_timeout, m->poll_func_userdata);
|
m->poll_func_ret = m->poll_func(
|
||||||
else
|
m->pollfds, m->n_pollfds,
|
||||||
m->poll_func_ret = poll(m->pollfds, m->n_pollfds, m->prepared_timeout);
|
usec_to_timeout(m->prepared_timeout),
|
||||||
|
m->poll_func_userdata);
|
||||||
|
else {
|
||||||
|
#ifdef HAVE_PPOLL
|
||||||
|
struct timespec ts;
|
||||||
|
|
||||||
|
m->poll_func_ret = ppoll(
|
||||||
|
m->pollfds, m->n_pollfds,
|
||||||
|
m->prepared_timeout == PA_USEC_INVALID ? NULL : pa_timespec_store(&ts, m->prepared_timeout),
|
||||||
|
NULL);
|
||||||
|
#else
|
||||||
|
m->poll_func_ret = poll(
|
||||||
|
m->pollfds, m->n_pollfds,
|
||||||
|
usec_to_timeout(m->prepared_timeout));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
if (m->poll_func_ret < 0) {
|
if (m->poll_func_ret < 0) {
|
||||||
if (errno == EINTR)
|
if (errno == EINTR)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue