1
0
mirror of https://github.com/mpv-player/mpv synced 2025-02-16 12:17:12 +00:00

semaphore_osx: change mp_sem_timedwait to mp_time

This commit is contained in:
Kacper Michajłow 2023-10-22 07:33:19 +02:00 committed by Dudemanguy
parent f659a60dfa
commit 729f2fed2c
3 changed files with 18 additions and 24 deletions

View File

@ -500,7 +500,7 @@ bool ca_change_physical_format_sync(struct ao *ao, AudioStreamID stream,
/* The AudioStreamSetProperty is not only asynchronous,
* it is also not Atomic, in its behaviour. */
struct timespec timeout = mp_rel_time_to_timespec(2.0);
int64_t wait_until = mp_time_ns() + MP_TIME_S_TO_NS(2);
AudioStreamBasicDescription actual_format = {0};
while (1) {
err = CA_GET(stream, kAudioStreamPropertyPhysicalFormat, &actual_format);
@ -511,7 +511,7 @@ bool ca_change_physical_format_sync(struct ao *ao, AudioStreamID stream,
if (format_set)
break;
if (mp_sem_timedwait(&wakeup, &timeout)) {
if (mp_sem_timedwait(&wakeup, wait_until)) {
MP_VERBOSE(ao, "reached timeout\n");
break;
}

View File

@ -28,7 +28,7 @@ typedef struct {
int mp_sem_init(mp_sem_t *sem, int pshared, unsigned int value);
int mp_sem_wait(mp_sem_t *sem);
int mp_sem_trywait(mp_sem_t *sem);
int mp_sem_timedwait(mp_sem_t *sem, const struct timespec *abs_timeout);
int mp_sem_timedwait(mp_sem_t *sem, int64_t until);
int mp_sem_post(mp_sem_t *sem);
int mp_sem_destroy(mp_sem_t *sem);

View File

@ -23,7 +23,9 @@
#include <sys/time.h>
#include <errno.h>
#include "osdep/io.h"
#include <common/common.h>
#include "io.h"
#include "timer.h"
int mp_sem_init(mp_sem_t *sem, int pshared, unsigned int value)
{
@ -44,7 +46,7 @@ int mp_sem_init(mp_sem_t *sem, int pshared, unsigned int value)
int mp_sem_wait(mp_sem_t *sem)
{
return mp_sem_timedwait(sem, NULL);
return mp_sem_timedwait(sem, MP_START_TIME - 1);
}
int mp_sem_trywait(mp_sem_t *sem)
@ -67,32 +69,24 @@ int mp_sem_trywait(mp_sem_t *sem)
return r;
}
int mp_sem_timedwait(mp_sem_t *sem, const struct timespec *abs_timeout)
int mp_sem_timedwait(mp_sem_t *sem, int64_t until)
{
while (1) {
if (!mp_sem_trywait(sem))
return 0;
int timeout_ms = -1;
if (abs_timeout) {
timeout_ms = 0;
// OSX does not provide clock_gettime() either.
struct timeval tv;
gettimeofday(&tv, NULL);
if (abs_timeout->tv_sec >= tv.tv_sec) {
long long msec = (abs_timeout->tv_sec - tv.tv_sec) * 1000LL +
abs_timeout->tv_nsec / 1000LL / 1000LL - tv.tv_usec / 1000LL;
if (msec > INT_MAX)
msec = INT_MAX;
if (msec < 0)
msec = 0;
timeout_ms = msec;
}
int timeout = 0;
if (until == MP_START_TIME - 1) {
timeout = -1;
} else if (until >= MP_START_TIME) {
timeout = (until - mp_time_ns()) / MP_TIME_MS_TO_NS(1);
timeout = MPCLAMP(timeout, 0, INT_MAX);
} else {
assert(false && "Invalid mp_time value!");
}
struct pollfd fd = {.fd = sem->wakeup_pipe[0], .events = POLLIN};
int r = poll(&fd, 1, timeout_ms);
int r = poll(&fd, 1, timeout);
if (r < 0)
return -1;
if (r == 0) {