vo: don't sleep 1ms always when requested time is in the past

Fixes a899e14b which changed clamp from 0 to 1 ms which effectivelly
introduced 1ms sleep always, even if requested until_time_ns is in the
past and should request 0 timeout.

While at it also fix mp_poll wrapper to respect negative timeout which
should mean infinite wait.

Also keep the 37d6604 behaviour for very short timeouts, but round only
the ones > 100us, anything else is 0.

Fixes: a899e14b
This commit is contained in:
Kacper Michajłow 2023-11-08 22:49:17 -06:00 committed by Dudemanguy
parent a89ba2c749
commit 7d86807a5f
5 changed files with 16 additions and 6 deletions

View File

@ -20,6 +20,7 @@
#include <sys/select.h>
#include <stdio.h>
#include "common/common.h"
#include "config.h"
#include "poll_wrapper.h"
#include "timer.h"
@ -31,9 +32,15 @@ int mp_poll(struct pollfd *fds, int nfds, int64_t timeout_ns)
struct timespec ts;
ts.tv_sec = timeout_ns / MP_TIME_S_TO_NS(1);
ts.tv_nsec = timeout_ns % MP_TIME_S_TO_NS(1);
return ppoll(fds, nfds, &ts, NULL);
struct timespec *tsp = timeout_ns >= 0 ? &ts : NULL;
return ppoll(fds, nfds, tsp, NULL);
#endif
return poll(fds, nfds, timeout_ns / 1e6);
// Round-up to 1ms for short timeouts (100us, 1000us]
if (timeout_ns > MP_TIME_US_TO_NS(100))
timeout_ns = MPMAX(timeout_ns, MP_TIME_MS_TO_NS(1));
if (timeout_ns > 0)
timeout_ns /= MP_TIME_MS_TO_NS(1);
return poll(fds, nfds, timeout_ns);
}
// poll shim that supports device files on macOS.

View File

@ -1257,7 +1257,7 @@ void vo_drm_wait_events(struct vo *vo, int64_t until_time_ns)
struct vo_drm_state *drm = vo->drm;
if (drm->vt_switcher_active) {
int64_t wait_ns = until_time_ns - mp_time_ns();
int64_t timeout_ns = MPCLAMP(wait_ns, 1e6, 1e10);
int64_t timeout_ns = MPCLAMP(wait_ns, 0, MP_TIME_S_TO_NS(10));
vt_switcher_poll(&drm->vt_switcher, timeout_ns);
} else {
vo_wait_default(vo, until_time_ns);

View File

@ -523,7 +523,10 @@ static void wakeup(struct vo *vo)
static void wait_events(struct vo *vo, int64_t until_time_ns)
{
int64_t wait_ns = until_time_ns - mp_time_ns();
int timeout_ms = MPCLAMP(wait_ns / 1e6, 1, 10000);
// Round-up to 1ms for short timeouts (100us, 1000us]
if (wait_ns > MP_TIME_US_TO_NS(100))
wait_ns = MPMAX(wait_ns, MP_TIME_MS_TO_NS(1));
int timeout_ms = MPCLAMP(wait_ns / MP_TIME_MS_TO_NS(1), 0, 10000);
SDL_Event ev;
while (SDL_WaitEventTimeout(&ev, timeout_ms)) {

View File

@ -2611,7 +2611,7 @@ void vo_wayland_wait_events(struct vo *vo, int64_t until_time_ns)
struct vo_wayland_state *wl = vo->wl;
int64_t wait_ns = until_time_ns - mp_time_ns();
int64_t timeout_ns = MPCLAMP(wait_ns, 1e6, 1e10);
int64_t timeout_ns = MPCLAMP(wait_ns, 0, MP_TIME_S_TO_NS(10));
wayland_dispatch_events(wl, 2, timeout_ns);
}

View File

@ -2178,7 +2178,7 @@ void vo_x11_wait_events(struct vo *vo, int64_t until_time_ns)
{ .fd = x11->wakeup_pipe[0], .events = POLLIN },
};
int64_t wait_ns = until_time_ns - mp_time_ns();
int64_t timeout_ns = MPCLAMP(wait_ns, 1e6, 1e10);
int64_t timeout_ns = MPCLAMP(wait_ns, 0, MP_TIME_S_TO_NS(10));
mp_poll(fds, 2, timeout_ns);