From 01636817f9befbb09ec2fb39c03c853bcd6eb918 Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Thu, 29 Oct 2026 10:10:48 -0400
Subject: [PATCH] thread: Use the monotonic clock for pthread condition and
semaphore waits when possible
Use pthread_condattr_setclock() and sem_clockwait() when available, so that wait operations will be timed against the monotonic clock. This avoids potential hangs if the realtime clock experiences a discontinuity.
---
cmake/sdlchecks.cmake | 29 +++++++++++++--
include/build_config/SDL_build_config.h.cmake | 3 ++
src/thread/pthread/SDL_syscond.c | 24 ++++++++++---
src/thread/pthread/SDL_syssem.c | 35 +++++++++++--------
4 files changed, 68 insertions(+), 23 deletions(-)
diff --git a/cmake/sdlchecks.cmake b/cmake/sdlchecks.cmake
index fc940c79f9381..dddcefcba0e12 100644
--- a/cmake/sdlchecks.cmake
+++ b/cmake/sdlchecks.cmake
@@ -1074,17 +1074,40 @@ macro(CheckPTHREAD)
check_c_source_compiles("
#include <pthread.h>
#include <semaphore.h>
+ #include <time.h>
int main(int argc, char **argv) {
- sem_timedwait(NULL, NULL);
+ sem_clockwait(NULL, CLOCK_MONOTONIC, NULL);
return 0;
- }" COMPILER_HAS_SEM_TIMEDWAIT)
- set(HAVE_SEM_TIMEDWAIT ${COMPILER_HAS_SEM_TIMEDWAIT})
+ }" COMPILER_HAS_SEM_CLOCKWAIT)
+ set(HAVE_SEM_CLOCKWAIT ${COMPILER_HAS_SEM_CLOCKWAIT})
+ if (NOT HAVE_SEM_CLOCKWAIT)
+ check_c_source_compiles("
+ #include <pthread.h>
+ #include <semaphore.h>
+ #include <time.h>
+ int main(int argc, char **argv) {
+ sem_clockwait_np(NULL, CLOCK_MONOTONIC, 0, NULL, NULL);
+ return 0;
+ }" COMPILER_HAS_SEM_CLOCKWAIT_NP)
+ set(HAVE_SEM_CLOCKWAIT_NP ${COMPILER_HAS_SEM_CLOCKWAIT_NP})
+ if (NOT HAVE_SEM_CLOCKWAIT_NP)
+ check_c_source_compiles("
+ #include <pthread.h>
+ #include <semaphore.h>
+ int main(int argc, char **argv) {
+ sem_timedwait(NULL, NULL);
+ return 0;
+ }" COMPILER_HAS_SEM_TIMEDWAIT)
+ set(HAVE_SEM_TIMEDWAIT ${COMPILER_HAS_SEM_TIMEDWAIT})
+ endif ()
+ endif ()
endif()
endif()
check_include_file(pthread.h HAVE_PTHREAD_H)
check_include_file(pthread_np.h HAVE_PTHREAD_NP_H)
if (HAVE_PTHREAD_H)
+ check_symbol_exists(pthread_condattr_setclock "pthread.h" HAVE_PTHREAD_CONDATTR_SETCLOCK)
check_c_source_compiles("
#include <pthread.h>
int main(int argc, char **argv) {
diff --git a/include/build_config/SDL_build_config.h.cmake b/include/build_config/SDL_build_config.h.cmake
index bd342ceb67775..b461a8660b8f2 100644
--- a/include/build_config/SDL_build_config.h.cmake
+++ b/include/build_config/SDL_build_config.h.cmake
@@ -197,8 +197,11 @@
#cmakedefine HAVE_GETPAGESIZE 1
#cmakedefine HAVE_ICONV 1
#cmakedefine SDL_USE_LIBICONV 1
+#cmakedefine HAVE_PTHREAD_CONDATTR_SETCLOCK 1
#cmakedefine HAVE_PTHREAD_SETNAME_NP 1
#cmakedefine HAVE_PTHREAD_SET_NAME_NP 1
+#cmakedefine HAVE_SEM_CLOCKWAIT 1
+#cmakedefine HAVE_SEM_CLOCKWAIT_NP 1
#cmakedefine HAVE_SEM_TIMEDWAIT 1
#cmakedefine HAVE_GETAUXVAL 1
#cmakedefine HAVE_ELF_AUX_INFO 1
diff --git a/src/thread/pthread/SDL_syscond.c b/src/thread/pthread/SDL_syscond.c
index 9849f5e4f9a03..12ddf574915d4 100644
--- a/src/thread/pthread/SDL_syscond.c
+++ b/src/thread/pthread/SDL_syscond.c
@@ -28,6 +28,12 @@
#include "SDL_sysmutex_c.h"
+#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK)
+#define COND_CLOCK CLOCK_MONOTONIC
+#else
+#define COND_CLOCK CLOCK_REALTIME
+#endif
+
struct SDL_Condition
{
pthread_cond_t cond;
@@ -40,11 +46,21 @@ SDL_Condition *SDL_CreateCondition(void)
cond = (SDL_Condition *)SDL_malloc(sizeof(SDL_Condition));
if (cond) {
- if (pthread_cond_init(&cond->cond, NULL) != 0) {
+ pthread_condattr_t attr;
+ if (pthread_condattr_init(&attr) != 0) {
+ SDL_SetError("pthread_condattr_init() failed");
+ SDL_free(cond);
+ return NULL;
+ }
+#ifdef HAVE_PTHREAD_CONDATTR_SETCLOCK
+ pthread_condattr_setclock(&attr, COND_CLOCK);
+#endif
+ if (pthread_cond_init(&cond->cond, &attr) != 0) {
SDL_SetError("pthread_cond_init() failed");
SDL_free(cond);
cond = NULL;
}
+ pthread_condattr_destroy(&attr);
}
return cond;
}
@@ -80,9 +96,6 @@ void SDL_BroadcastCondition(SDL_Condition *cond)
bool SDL_WaitConditionTimeoutNS(SDL_Condition *cond, SDL_Mutex *mutex, Sint64 timeoutNS)
{
-#ifndef HAVE_CLOCK_GETTIME
- struct timeval delta;
-#endif
struct timespec abstime;
if (!cond || !mutex) {
@@ -94,11 +107,12 @@ bool SDL_WaitConditionTimeoutNS(SDL_Condition *cond, SDL_Mutex *mutex, Sint64 ti
}
#ifdef HAVE_CLOCK_GETTIME
- clock_gettime(CLOCK_REALTIME, &abstime);
+ clock_gettime(COND_CLOCK, &abstime);
abstime.tv_sec += (timeoutNS / SDL_NS_PER_SECOND);
abstime.tv_nsec += (timeoutNS % SDL_NS_PER_SECOND);
#else
+ struct timeval delta;
gettimeofday(&delta, NULL);
abstime.tv_sec = delta.tv_sec + (timeoutNS / SDL_NS_PER_SECOND);
diff --git a/src/thread/pthread/SDL_syssem.c b/src/thread/pthread/SDL_syssem.c
index a3a53185dd545..eb697c2d702b6 100644
--- a/src/thread/pthread/SDL_syssem.c
+++ b/src/thread/pthread/SDL_syssem.c
@@ -33,6 +33,12 @@
#include "../generic/SDL_syssem.c"
#else
+#if defined(HAVE_CLOCK_GETTIME) && (defined(HAVE_SEM_CLOCKWAIT) || defined(HAVE_SEM_CLOCKWAIT_NP))
+#define SEM_CLOCK CLOCK_MONOTONIC
+#else
+#define SEM_CLOCK CLOCK_REALTIME
+#endif
+
struct SDL_Semaphore
{
sem_t sem;
@@ -62,15 +68,6 @@ void SDL_DestroySemaphore(SDL_Semaphore *sem)
bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
{
-#ifdef HAVE_SEM_TIMEDWAIT
-#ifndef HAVE_CLOCK_GETTIME
- struct timeval now;
-#endif
- struct timespec ts_timeout;
-#else
- Uint64 stop_time;
-#endif
-
if (!sem) {
return true;
}
@@ -89,18 +86,20 @@ bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
return (rc == 0);
}
-#ifdef HAVE_SEM_TIMEDWAIT
- /* Setup the timeout. sem_timedwait doesn't wait for
- * a lapse of time, but until we reach a certain time.
+#if defined(HAVE_SEM_CLOCKWAIT) || defined(HAVE_SEM_CLOCKWAIT_NP) || defined(HAVE_SEM_TIMEDWAIT)
+ /* Setup the timeout. sem_clockwait and sem_timedwait don't wait
+ * for a lapse of time, but until we reach a certain time.
* This time is now plus the timeout.
*/
+ struct timespec ts_timeout;
#ifdef HAVE_CLOCK_GETTIME
- clock_gettime(CLOCK_REALTIME, &ts_timeout);
+ clock_gettime(SEM_CLOCK, &ts_timeout);
// Add our timeout to current time
ts_timeout.tv_sec += (timeoutNS / SDL_NS_PER_SECOND);
ts_timeout.tv_nsec += (timeoutNS % SDL_NS_PER_SECOND);
#else
+ struct timeval now;
gettimeofday(&now, NULL);
// Add our timeout to current time
@@ -117,12 +116,18 @@ bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
// Wait.
int rc;
do {
+#if defined(HAVE_SEM_CLOCKWAIT)
+ rc = sem_clockwait(&sem->sem, SEM_CLOCK, &ts_timeout);
+#elif defined(HAVE_SEM_CLOCKWAIT_NP)
+ rc = sem_clockwait_np(&sem->sem, SEM_CLOCK, TIMER_ABSTIME, &ts_timeout, NULL);
+#else
rc = sem_timedwait(&sem->sem, &ts_timeout);
+#endif
} while (rc < 0 && errno == EINTR);
return (rc == 0);
#else
- stop_time = SDL_GetTicksNS() + timeoutNS;
+ Uint64 stop_time = SDL_GetTicksNS() + timeoutNS;
while (sem_trywait(&sem->sem) != 0) {
if (SDL_GetTicksNS() >= stop_time) {
return false;
@@ -130,7 +135,7 @@ bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
SDL_DelayNS(100);
}
return true;
-#endif // HAVE_SEM_TIMEDWAIT
+#endif // defined(HAVE_SEM_CLOCKWAIT) || defined(HAVE_SEM_CLOCKWAIT_NP) || defined(HAVE_SEM_TIMEDWAIT)
}
Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem)