From acb9658f8146dd2d3ca1da58653c797d45c301a2 Mon Sep 17 00:00:00 2001 From: janwas Date: Fri, 31 Jul 2009 17:48:45 +0000 Subject: [PATCH] philip correctly points out that local static init from constants isn't thread-safe, and we shouldn't rely on the compiler moving variables to BSS if their constant initializer is 0. This was SVN commit r7055. --- source/lib/debug.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/source/lib/debug.cpp b/source/lib/debug.cpp index 7e359a91ad..1fbcf7ea4e 100644 --- a/source/lib/debug.cpp +++ b/source/lib/debug.cpp @@ -215,7 +215,12 @@ LibError debug_WriteCrashlog(const wchar_t* text) BUSY, FAILED }; - static volatile uintptr_t state = IDLE; + // note: the initial state is IDLE. we rely on zero-init because + // initializing local static objects from constants may happen when + // this is first called, which isn't thread-safe. (see C++ 6.7.4) + cassert(IDLE == 0); + static volatile uintptr_t state; + if(!cpu_CAS(&state, IDLE, BUSY)) return ERR::REENTERED; // NOWARN