mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-05-14 15:42:49 +02:00
Convert ticks to 64-bit, added nanosecond precision to the API
Fixes https://github.com/libsdl-org/SDL/issues/5512 Fixes https://github.com/libsdl-org/SDL/issues/6731
This commit is contained in:
@@ -27,8 +27,7 @@ typedef SDL_cond *(*pfnSDL_CreateCond)(void);
|
||||
typedef void (*pfnSDL_DestroyCond)(SDL_cond *);
|
||||
typedef int (*pfnSDL_CondSignal)(SDL_cond *);
|
||||
typedef int (*pfnSDL_CondBroadcast)(SDL_cond *);
|
||||
typedef int (*pfnSDL_CondWait)(SDL_cond *, SDL_mutex *);
|
||||
typedef int (*pfnSDL_CondWaitTimeout)(SDL_cond *, SDL_mutex *, Uint32);
|
||||
typedef int (*pfnSDL_CondWaitTimeoutNS)(SDL_cond *, SDL_mutex *, Sint64);
|
||||
|
||||
typedef struct SDL_cond_impl_t
|
||||
{
|
||||
@@ -36,8 +35,7 @@ typedef struct SDL_cond_impl_t
|
||||
pfnSDL_DestroyCond Destroy;
|
||||
pfnSDL_CondSignal Signal;
|
||||
pfnSDL_CondBroadcast Broadcast;
|
||||
pfnSDL_CondWait Wait;
|
||||
pfnSDL_CondWaitTimeout WaitTimeout;
|
||||
pfnSDL_CondWaitTimeoutNS WaitTimeoutNS;
|
||||
} SDL_cond_impl_t;
|
||||
|
||||
/* Implementation will be chosen at runtime based on available Kernel features */
|
||||
@@ -125,7 +123,7 @@ static int SDL_CondBroadcast_cv(SDL_cond *_cond)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int SDL_CondWaitTimeout_cv(SDL_cond *_cond, SDL_mutex *_mutex, Uint32 ms)
|
||||
static int SDL_CondWaitTimeoutNS_cv(SDL_cond *_cond, SDL_mutex *_mutex, Sint64 timeoutNS)
|
||||
{
|
||||
SDL_cond_cv *cond = (SDL_cond_cv *)_cond;
|
||||
DWORD timeout;
|
||||
@@ -138,10 +136,10 @@ static int SDL_CondWaitTimeout_cv(SDL_cond *_cond, SDL_mutex *_mutex, Uint32 ms)
|
||||
return SDL_InvalidParamError("mutex");
|
||||
}
|
||||
|
||||
if (ms == SDL_MUTEX_MAXWAIT) {
|
||||
if (timeoutNS < 0) {
|
||||
timeout = INFINITE;
|
||||
} else {
|
||||
timeout = (DWORD)ms;
|
||||
timeout = (DWORD)SDL_NS_TO_MS(timeoutNS);
|
||||
}
|
||||
|
||||
if (SDL_mutex_impl_active.Type == SDL_MUTEX_SRW) {
|
||||
@@ -188,18 +186,12 @@ static int SDL_CondWaitTimeout_cv(SDL_cond *_cond, SDL_mutex *_mutex, Uint32 ms)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int SDL_CondWait_cv(SDL_cond *cond, SDL_mutex *mutex)
|
||||
{
|
||||
return SDL_CondWaitTimeout_cv(cond, mutex, SDL_MUTEX_MAXWAIT);
|
||||
}
|
||||
|
||||
static const SDL_cond_impl_t SDL_cond_impl_cv = {
|
||||
&SDL_CreateCond_cv,
|
||||
&SDL_DestroyCond_cv,
|
||||
&SDL_CondSignal_cv,
|
||||
&SDL_CondBroadcast_cv,
|
||||
&SDL_CondWait_cv,
|
||||
&SDL_CondWaitTimeout_cv,
|
||||
&SDL_CondWaitTimeoutNS_cv,
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -211,8 +203,7 @@ static const SDL_cond_impl_t SDL_cond_impl_generic = {
|
||||
&SDL_DestroyCond_generic,
|
||||
&SDL_CondSignal_generic,
|
||||
&SDL_CondBroadcast_generic,
|
||||
&SDL_CondWait_generic,
|
||||
&SDL_CondWaitTimeout_generic,
|
||||
&SDL_CondWaitTimeoutNS_generic,
|
||||
};
|
||||
|
||||
SDL_cond *
|
||||
@@ -272,14 +263,9 @@ int SDL_CondBroadcast(SDL_cond *cond)
|
||||
return SDL_cond_impl_active.Broadcast(cond);
|
||||
}
|
||||
|
||||
int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
|
||||
int SDL_CondWaitTimeoutNS(SDL_cond *cond, SDL_mutex *mutex, Sint64 timeoutNS)
|
||||
{
|
||||
return SDL_cond_impl_active.WaitTimeout(cond, mutex, ms);
|
||||
}
|
||||
|
||||
int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
|
||||
{
|
||||
return SDL_cond_impl_active.Wait(cond, mutex);
|
||||
return SDL_cond_impl_active.WaitTimeoutNS(cond, mutex, timeoutNS);
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
||||
@@ -37,9 +37,7 @@
|
||||
|
||||
typedef SDL_sem *(*pfnSDL_CreateSemaphore)(Uint32);
|
||||
typedef void (*pfnSDL_DestroySemaphore)(SDL_sem *);
|
||||
typedef int (*pfnSDL_SemWaitTimeout)(SDL_sem *, Uint32);
|
||||
typedef int (*pfnSDL_SemTryWait)(SDL_sem *);
|
||||
typedef int (*pfnSDL_SemWait)(SDL_sem *);
|
||||
typedef int (*pfnSDL_SemWaitTimeoutNS)(SDL_sem *, Sint64);
|
||||
typedef Uint32 (*pfnSDL_SemValue)(SDL_sem *);
|
||||
typedef int (*pfnSDL_SemPost)(SDL_sem *);
|
||||
|
||||
@@ -47,9 +45,7 @@ typedef struct SDL_semaphore_impl_t
|
||||
{
|
||||
pfnSDL_CreateSemaphore Create;
|
||||
pfnSDL_DestroySemaphore Destroy;
|
||||
pfnSDL_SemWaitTimeout WaitTimeout;
|
||||
pfnSDL_SemTryWait TryWait;
|
||||
pfnSDL_SemWait Wait;
|
||||
pfnSDL_SemWaitTimeoutNS WaitTimeoutNS;
|
||||
pfnSDL_SemValue Value;
|
||||
pfnSDL_SemPost Post;
|
||||
} SDL_sem_impl_t;
|
||||
@@ -108,81 +104,60 @@ static void SDL_DestroySemaphore_atom(SDL_sem *sem)
|
||||
}
|
||||
}
|
||||
|
||||
static int SDL_SemTryWait_atom(SDL_sem *_sem)
|
||||
static int SDL_SemWaitTimeoutNS_atom(SDL_sem *_sem, Sint64 timeoutNS)
|
||||
{
|
||||
SDL_sem_atom *sem = (SDL_sem_atom *)_sem;
|
||||
LONG count;
|
||||
Uint64 now;
|
||||
Uint64 deadline;
|
||||
DWORD timeout_eff;
|
||||
|
||||
if (sem == NULL) {
|
||||
return SDL_InvalidParamError("sem");
|
||||
}
|
||||
|
||||
count = sem->count;
|
||||
if (count == 0) {
|
||||
return SDL_MUTEX_TIMEDOUT;
|
||||
}
|
||||
|
||||
if (InterlockedCompareExchange(&sem->count, count - 1, count) == count) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SDL_MUTEX_TIMEDOUT;
|
||||
}
|
||||
|
||||
static int SDL_SemWait_atom(SDL_sem *_sem)
|
||||
{
|
||||
SDL_sem_atom *sem = (SDL_sem_atom *)_sem;
|
||||
LONG count;
|
||||
|
||||
if (sem == NULL) {
|
||||
return SDL_InvalidParamError("sem");
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
if (timeoutNS == 0) {
|
||||
count = sem->count;
|
||||
while (count == 0) {
|
||||
if (pWaitOnAddress(&sem->count, &count, sizeof(sem->count), INFINITE) == FALSE) {
|
||||
return SDL_SetError("WaitOnAddress() failed");
|
||||
}
|
||||
count = sem->count;
|
||||
if (count == 0) {
|
||||
return SDL_MUTEX_TIMEDOUT;
|
||||
}
|
||||
|
||||
if (InterlockedCompareExchange(&sem->count, count - 1, count) == count) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SDL_MUTEX_TIMEDOUT;
|
||||
}
|
||||
}
|
||||
if (timeoutNS < 0) {
|
||||
for (;;) {
|
||||
count = sem->count;
|
||||
while (count == 0) {
|
||||
if (pWaitOnAddress(&sem->count, &count, sizeof(sem->count), INFINITE) == FALSE) {
|
||||
return SDL_SetError("WaitOnAddress() failed");
|
||||
}
|
||||
count = sem->count;
|
||||
}
|
||||
|
||||
static int SDL_SemWaitTimeout_atom(SDL_sem *_sem, Uint32 timeout)
|
||||
{
|
||||
SDL_sem_atom *sem = (SDL_sem_atom *)_sem;
|
||||
LONG count;
|
||||
Uint32 now;
|
||||
Uint32 deadline;
|
||||
DWORD timeout_eff;
|
||||
|
||||
if (timeout == SDL_MUTEX_MAXWAIT) {
|
||||
return SDL_SemWait_atom(_sem);
|
||||
}
|
||||
|
||||
if (sem == NULL) {
|
||||
return SDL_InvalidParamError("sem");
|
||||
if (InterlockedCompareExchange(&sem->count, count - 1, count) == count) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* WaitOnAddress is subject to spurious and stolen wakeups so we
|
||||
* need to recalculate the effective timeout before every wait
|
||||
*/
|
||||
now = SDL_GetTicks();
|
||||
deadline = now + (DWORD)timeout;
|
||||
now = SDL_GetTicksNS();
|
||||
deadline = now + timeoutNS;
|
||||
|
||||
for (;;) {
|
||||
count = sem->count;
|
||||
/* If no semaphore is available we need to wait */
|
||||
while (count == 0) {
|
||||
now = SDL_GetTicks();
|
||||
now = SDL_GetTicksNS();
|
||||
if (deadline > now) {
|
||||
timeout_eff = deadline - now;
|
||||
timeout_eff = (DWORD)SDL_NS_TO_MS(deadline - now);
|
||||
} else {
|
||||
return SDL_MUTEX_TIMEDOUT;
|
||||
}
|
||||
@@ -232,9 +207,7 @@ static int SDL_SemPost_atom(SDL_sem *_sem)
|
||||
static const SDL_sem_impl_t SDL_sem_impl_atom = {
|
||||
&SDL_CreateSemaphore_atom,
|
||||
&SDL_DestroySemaphore_atom,
|
||||
&SDL_SemWaitTimeout_atom,
|
||||
&SDL_SemTryWait_atom,
|
||||
&SDL_SemWait_atom,
|
||||
&SDL_SemWaitTimeoutNS_atom,
|
||||
&SDL_SemValue_atom,
|
||||
&SDL_SemPost_atom,
|
||||
};
|
||||
@@ -289,7 +262,7 @@ static void SDL_DestroySemaphore_kern(SDL_sem *_sem)
|
||||
}
|
||||
}
|
||||
|
||||
static int SDL_SemWaitTimeout_kern(SDL_sem *_sem, Uint32 timeout)
|
||||
static int SDL_SemWaitTimeoutNS_kern(SDL_sem *_sem, Sint64 timeoutNS)
|
||||
{
|
||||
SDL_sem_kern *sem = (SDL_sem_kern *)_sem;
|
||||
int retval;
|
||||
@@ -299,10 +272,10 @@ static int SDL_SemWaitTimeout_kern(SDL_sem *_sem, Uint32 timeout)
|
||||
return SDL_InvalidParamError("sem");
|
||||
}
|
||||
|
||||
if (timeout == SDL_MUTEX_MAXWAIT) {
|
||||
if (timeoutNS < 0) {
|
||||
dwMilliseconds = INFINITE;
|
||||
} else {
|
||||
dwMilliseconds = (DWORD)timeout;
|
||||
dwMilliseconds = (DWORD)SDL_NS_TO_MS(timeoutNS);
|
||||
}
|
||||
switch (WaitForSingleObjectEx(sem->id, dwMilliseconds, FALSE)) {
|
||||
case WAIT_OBJECT_0:
|
||||
@@ -319,16 +292,6 @@ static int SDL_SemWaitTimeout_kern(SDL_sem *_sem, Uint32 timeout)
|
||||
return retval;
|
||||
}
|
||||
|
||||
static int SDL_SemTryWait_kern(SDL_sem *sem)
|
||||
{
|
||||
return SDL_SemWaitTimeout_kern(sem, 0);
|
||||
}
|
||||
|
||||
static int SDL_SemWait_kern(SDL_sem *sem)
|
||||
{
|
||||
return SDL_SemWaitTimeout_kern(sem, SDL_MUTEX_MAXWAIT);
|
||||
}
|
||||
|
||||
/* Returns the current count of the semaphore */
|
||||
static Uint32 SDL_SemValue_kern(SDL_sem *_sem)
|
||||
{
|
||||
@@ -362,9 +325,7 @@ static int SDL_SemPost_kern(SDL_sem *_sem)
|
||||
static const SDL_sem_impl_t SDL_sem_impl_kern = {
|
||||
&SDL_CreateSemaphore_kern,
|
||||
&SDL_DestroySemaphore_kern,
|
||||
&SDL_SemWaitTimeout_kern,
|
||||
&SDL_SemTryWait_kern,
|
||||
&SDL_SemWait_kern,
|
||||
&SDL_SemWaitTimeoutNS_kern,
|
||||
&SDL_SemValue_kern,
|
||||
&SDL_SemPost_kern,
|
||||
};
|
||||
@@ -417,19 +378,9 @@ void SDL_DestroySemaphore(SDL_sem *sem)
|
||||
SDL_sem_impl_active.Destroy(sem);
|
||||
}
|
||||
|
||||
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
|
||||
int SDL_SemWaitTimeoutNS(SDL_sem *sem, Sint64 timeoutNS)
|
||||
{
|
||||
return SDL_sem_impl_active.WaitTimeout(sem, timeout);
|
||||
}
|
||||
|
||||
int SDL_SemTryWait(SDL_sem *sem)
|
||||
{
|
||||
return SDL_sem_impl_active.TryWait(sem);
|
||||
}
|
||||
|
||||
int SDL_SemWait(SDL_sem *sem)
|
||||
{
|
||||
return SDL_sem_impl_active.Wait(sem);
|
||||
return SDL_sem_impl_active.WaitTimeoutNS(sem, timeoutNS);
|
||||
}
|
||||
|
||||
Uint32
|
||||
|
||||
Reference in New Issue
Block a user