summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support
diff options
context:
space:
mode:
authorReid Kleckner <reid@kleckner.net>2015-06-11 22:22:45 +0000
committerReid Kleckner <reid@kleckner.net>2015-06-11 22:22:45 +0000
commit6bb26dafa4cda63c14da97e65eff1e982375fd16 (patch)
treefdf1733dc07557a01ec5907ebb716e2ad1baa1eb /llvm/lib/Support
parentc4e4f33e290ba21f97a871828ee2d1d39a620058 (diff)
downloadbcm5719-llvm-6bb26dafa4cda63c14da97e65eff1e982375fd16.tar.gz
bcm5719-llvm-6bb26dafa4cda63c14da97e65eff1e982375fd16.zip
[Support] Fix a race initializing a static local in MSVC
static local initialization isn't thread safe with MSVC and a race was reported in PR23817. We can't use std::atomic because it's not trivially constructible, so instead do some lame volatile global integer manipulation. llvm-svn: 239566
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r--llvm/lib/Support/Windows/Memory.inc10
1 files changed, 9 insertions, 1 deletions
diff --git a/llvm/lib/Support/Windows/Memory.inc b/llvm/lib/Support/Windows/Memory.inc
index ae8371abf5b..4b2ff2e2d32 100644
--- a/llvm/lib/Support/Windows/Memory.inc
+++ b/llvm/lib/Support/Windows/Memory.inc
@@ -78,7 +78,15 @@ MemoryBlock Memory::allocateMappedMemory(size_t NumBytes,
// While we'd be happy to allocate single pages, the Windows allocation
// granularity may be larger than a single page (in practice, it is 64K)
// so mapping less than that will create an unreachable fragment of memory.
- static const size_t Granularity = getAllocationGranularity();
+ // Avoid using one-time initialization of static locals here, since they
+ // aren't thread safe with MSVC.
+ static volatile size_t GranularityCached;
+ size_t Granularity = GranularityCached;
+ if (Granularity == 0) {
+ Granularity = getAllocationGranularity();
+ GranularityCached = Granularity;
+ }
+
const size_t NumBlocks = (NumBytes+Granularity-1)/Granularity;
uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +
OpenPOWER on IntegriCloud