From ef88712f5dcc9e245b4e3819be1889e659731b59 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Sun, 1 Dec 2019 11:56:03 -0500 Subject: rts/OSThreads: Fix data race Previously we would race on the cached processor count. Avoiding this is straightforward; just use relaxed operations. --- rts/posix/OSThreads.c | 6 ++++-- 1 file 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; -- cgit v1.2.1