diff options
author | Ben Gamari <ben@smart-cactus.org> | 2019-12-01 11:56:03 -0500 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2020-03-11 18:45:36 -0400 |
commit | 2d5860f7afce1f18600a3c73e51a2b665b569381 (patch) | |
tree | 5e50ac4fcadc258102e0fb7c67191da3218115a1 | |
parent | 513afdc3f1017513d13c0153e90b4658644d7638 (diff) | |
download | haskell-2d5860f7afce1f18600a3c73e51a2b665b569381.tar.gz |
rts/OSThreads: Fix data race
Previously we would race on the cached processor count. Avoiding this is
straightforward; just use relaxed operations.
-rw-r--r-- | rts/posix/OSThreads.c | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c index 7dcf0eed15..bc556e4193 100644 --- a/rts/posix/OSThreads.c +++ b/rts/posix/OSThreads.c @@ -240,11 +240,12 @@ forkOS_createThread ( HsStablePtr entry ) void freeThreadingResources (void) { /* nothing */ } +static uint32_t nproc_cache = 0; + uint32_t getNumberOfProcessors (void) { - static uint32_t nproc = 0; - + uint32_t nproc = RELAXED_LOAD(&nproc_cache); if (nproc == 0) { #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) nproc = sysconf(_SC_NPROCESSORS_ONLN); @@ -263,6 +264,7 @@ getNumberOfProcessors (void) #else nproc = 1; #endif + RELAXED_STORE(&nproc_cache, nproc); } return nproc; |