summaryrefslogtreecommitdiff
path: root/rts
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2019-12-01 11:56:03 -0500
committerBen Gamari <ben@smart-cactus.org>2020-10-24 20:59:39 -0400
commitef88712f5dcc9e245b4e3819be1889e659731b59 (patch)
tree40e14f3b5488d3592f3ef28c668f399cf4b8cc96 /rts
parentd3890ac737e282a582f0cc9819dedd2a8c363501 (diff)
downloadhaskell-ef88712f5dcc9e245b4e3819be1889e659731b59.tar.gz
rts/OSThreads: Fix data race
Previously we would race on the cached processor count. Avoiding this is straightforward; just use relaxed operations.
Diffstat (limited to 'rts')
-rw-r--r--rts/posix/OSThreads.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c
index 42d72c2e0d..c51ccfcafb 100644
--- a/rts/posix/OSThreads.c
+++ b/rts/posix/OSThreads.c
@@ -240,13 +240,14 @@ forkOS_createThread ( HsStablePtr entry )
void freeThreadingResources (void) { /* nothing */ }
+static uint32_t nproc_cache = 0;
+
// Get the number of logical CPU cores available to us. Note that this is
// different from the number of physical cores (see #14781).
uint32_t
getNumberOfProcessors (void)
{
- static uint32_t nproc = 0;
-
+ uint32_t nproc = RELAXED_LOAD(&nproc_cache);
if (nproc == 0) {
#if defined(HAVE_SCHED_GETAFFINITY)
cpu_set_t mask;
@@ -287,6 +288,7 @@ getNumberOfProcessors (void)
#else
nproc = 1;
#endif
+ RELAXED_STORE(&nproc_cache, nproc);
}
return nproc;