summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2021-07-26 11:01:11 -0700
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-08-02 04:14:25 -0400
commitf454c0ea2e7de32786635a987885706fcd7cb01a (patch)
treeefa5034ef77674b767b8d33401f5373e908421f5 /includes
parent2e0f4ca128d17a7161fa41ee9e82315a1cddffb7 (diff)
downloadhaskell-f454c0ea2e7de32786635a987885706fcd7cb01a.tar.gz
rts/OSThreads: Fix reference clock of timedWaitCondition
Previously `timedWaitCondition` assumed that timeouts were referenced against `CLOCK_MONOTONIC`. This is wrong; by default `pthread_cond_timedwait` references against `CLOCK_REALTIME`, although this can be overridden using `pthread_condattr_setclock`. Fix this and add support for using `CLOCK_MONOTONIC` whenever possible as it is more robust against system time changes and is likely cheaper to query. Unfortunately, this is complicated by the fact that older versions of Darwin did not provide `clock_gettime`, which means we also need to introduce a fallback path using `gettimeofday`. Fixes #20144.
Diffstat (limited to 'includes')
-rw-r--r--includes/rts/OSThreads.h12
1 files changed, 11 insertions, 1 deletions
diff --git a/includes/rts/OSThreads.h b/includes/rts/OSThreads.h
index 08d90de06e..d24a1313a6 100644
--- a/includes/rts/OSThreads.h
+++ b/includes/rts/OSThreads.h
@@ -27,7 +27,17 @@
#include <pthread.h>
#include <errno.h>
-typedef pthread_cond_t Condition;
+typedef struct {
+ pthread_cond_t cond;
+
+ // Which clock are pthread_cond_timedwait calls referenced against?
+ // N.B. Some older Darwin releases don't support clock_gettime. However, we
+ // do want to reference to CLOCK_MONOTONIC whenever possible as it is more
+ // robust against system time changes and is likely cheaper to query.
+#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK)
+ clockid_t timeout_clk;
+#endif
+} Condition;
typedef pthread_mutex_t Mutex;
typedef pthread_t OSThreadId;
typedef pthread_key_t ThreadLocalKey;