timer: add convenience time unit conversion macros

There's a lot of wild 1e6, 1000, etc. lying around in the code. A macro
is much easier to read and understand at a glance. Add some helpers for
this. We don't need to convert everything now but there's some simple
things that can be done so they are included in this commit.
This commit is contained in:
Dudemanguy 2023-10-11 11:34:14 -05:00
parent 9d3e607cf7
commit de9b800879
5 changed files with 17 additions and 7 deletions

View File

@ -945,7 +945,7 @@ bool wasapi_thread_init(struct ao *ao)
{
struct wasapi_state *state = ao->priv;
MP_DBG(ao, "Init wasapi thread\n");
int64_t retry_wait = 1000;
int64_t retry_wait = MP_TIME_US_TO_NS(1);
bool align_hack = false;
HRESULT hr;
@ -1028,12 +1028,12 @@ retry:
goto retry;
case AUDCLNT_E_DEVICE_IN_USE:
case AUDCLNT_E_DEVICE_INVALIDATED:
if (retry_wait > 8000) {
if (retry_wait > MP_TIME_US_TO_NS(8)) {
MP_FATAL(ao, "Bad device retry failed\n");
return false;
}
wasapi_thread_uninit(ao);
MP_WARN(ao, "Retrying in %"PRId64" us\n", retry_wait);
MP_WARN(ao, "Retrying in %"PRId64" ns\n", retry_wait);
mp_sleep_ns(retry_wait);
retry_wait *= 2;
goto retry;

View File

@ -50,6 +50,16 @@ void mp_end_hires_timers(int resolution_ms);
#define MP_START_TIME 10 * INT64_C(1000000000)
// Converts time units to nanoseconds (int64_t)
#define MP_TIME_S_TO_NS(s) ((s) * INT64_C(1000000000))
#define MP_TIME_MS_TO_NS(ms) ((ms) * INT64_C(1000000))
#define MP_TIME_US_TO_NS(us) ((us) * INT64_C(1000))
// Converts nanoseconds to specified time unit (double)
#define MP_TIME_NS_TO_S(ns) ((ns) / (double)1000000000)
#define MP_TIME_NS_TO_MS(ns) ((ns) / (double)1000000)
#define MP_TIME_NS_TO_US(ns) ((ns) / (double)1000)
// Duration of a second in mpv time.
#define MP_SECOND_US (1000 * 1000)

View File

@ -343,7 +343,7 @@ static int mapper_map(struct ra_hwdec_mapper *mapper)
// of the above StretchRect. Timeout of 8ms is required to reliably
// render 4k on Intel Haswell, Ivybridge and Cherry Trail Atom.
const int max_retries = 8;
const int64_t wait_ns = 1e6;
const int64_t wait_ns = MP_TIME_MS_TO_NS(1);
int retries = 0;
while (true) {
hr = IDirect3DQuery9_GetData(p->query9, NULL, 0, D3DGETDATA_FLUSH);
@ -353,7 +353,7 @@ static int mapper_map(struct ra_hwdec_mapper *mapper)
} else if (hr == S_FALSE) {
if (++retries > max_retries) {
MP_VERBOSE(mapper, "Failed to flush frame after %lld ms\n",
(long long)(wait_ns * max_retries) / 1000000);
(long long)MP_TIME_MS_TO_NS(wait_ns * max_retries));
break;
}
mp_sleep_ns(wait_ns);

View File

@ -299,7 +299,7 @@ static void wait_for_completion(struct vo *vo, int max_outstanding)
" for XShm completion events...\n");
ctx->Shm_Warned_Slow = 1;
}
mp_sleep_ns(1e6);
mp_sleep_ns(MP_TIME_MS_TO_NS(1));
vo_x11_check_events(vo);
}
}

View File

@ -670,7 +670,7 @@ static void wait_for_completion(struct vo *vo, int max_outstanding)
" for XShm completion events...\n");
ctx->Shm_Warned_Slow = 1;
}
mp_sleep_ns(1e6);
mp_sleep_ns(MP_TIME_MS_TO_NS(1));
vo_x11_check_events(vo);
}
}