diff options
author | Douglas Wilson <douglas.wilson@gmail.com> | 2021-01-04 09:46:58 +0000 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2021-01-17 05:49:54 -0500 |
commit | 33fc453fd46e54410472a7bad7585e5d1821c0ec (patch) | |
tree | 948b2b5af172fcac62364c656e71b70af4ce0bde | |
parent | 345ae06b3334a64e9d6db9ea69573ef3227e535a (diff) | |
download | haskell-33fc453fd46e54410472a7bad7585e5d1821c0ec.tar.gz |
rts: add timedWaitCondition
-rw-r--r-- | includes/rts/OSThreads.h | 1 | ||||
-rw-r--r-- | rts/posix/OSThreads.c | 16 | ||||
-rw-r--r-- | rts/win32/OSThreads.c | 10 |
3 files changed, 27 insertions, 0 deletions
diff --git a/includes/rts/OSThreads.h b/includes/rts/OSThreads.h index 21b92950b2..ed9c0a3b5e 100644 --- a/includes/rts/OSThreads.h +++ b/includes/rts/OSThreads.h @@ -175,6 +175,7 @@ extern void closeCondition ( Condition* pCond ); extern bool broadcastCondition ( Condition* pCond ); extern bool signalCondition ( Condition* pCond ); extern bool waitCondition ( Condition* pCond, Mutex* pMut ); +extern bool timedWaitCondition ( Condition* pCond, Mutex* pMut, Time timeout); // // Mutexes diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c index be2b596c35..200a4ccdad 100644 --- a/rts/posix/OSThreads.c +++ b/rts/posix/OSThreads.c @@ -78,6 +78,9 @@ #include <numa.h> #endif +// TODO does this need configure magic? +#include <time.h> + /* * This (allegedly) OS threads independent layer was initially * abstracted away from code that used Pthreads, so the functions @@ -117,6 +120,19 @@ waitCondition ( Condition* pCond, Mutex* pMut ) return (pthread_cond_wait(pCond,pMut) == 0); } +bool +timedWaitCondition ( Condition* pCond, Mutex* pMut, Time timeout) { + timeout += getMonotonicNSec(); + uint64_t secs = TimeToSeconds(timeout); + + const struct timespec t = (struct timespec) { + .tv_sec = secs, + .tv_nsec = TimeToNS(timeout - SecondsToTime(secs)) + }; + + return pthread_cond_timedwait(pCond,pMut, &t) == 0; +} + void yieldThread(void) { diff --git a/rts/win32/OSThreads.c b/rts/win32/OSThreads.c index ed8a598e51..07b0e3f034 100644 --- a/rts/win32/OSThreads.c +++ b/rts/win32/OSThreads.c @@ -559,6 +559,16 @@ waitCondition ( Condition* pCond, Mutex* pMut ) return true; } +bool +timedWaitCondition ( Condition* pCond, Mutex* pMut, Time timeout ) +{ + // If we pass a timeout of 0 SleepConditionVariableSRW will return immediately + // https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleepconditionvariablesrw + DWORD ms = (DWORD)stg_min(1, TimeToMS(timeout)); + SleepConditionVariableSRW(pCond, pMut, ms, 0); + return true; +} + void initMutex (Mutex* pMut) { |