static_init_lazy.cc (1095B)
1 #include "../common/platform.h" 2 #include "../common/static_init.h" 3 #include "static_init.h" 4 5 #if (BROTLI_STATIC_INIT != BROTLI_STATIC_INIT_LAZY) 6 #error "BROTLI_STATIC_INIT should be BROTLI_STATIC_INIT_LAZY" 7 #else 8 void BrotliEncoderLazyStaticInit(void) { 9 /* From https://en.cppreference.com/w/cpp/language/storage_duration.html: 10 ### Static block variables ### 11 Block variables with static or thread (since C++11) storage duration are 12 initialized the first time control passes through their declaration... 13 On all further calls, the declaration is skipped... 14 If multiple threads attempt to initialize the same static local variable 15 concurrently, the initialization occurs exactly once... 16 Usual implementations of this feature use variants of the double-checked 17 locking pattern, which reduces runtime overhead for already-initialized 18 local statics to a single non-atomic boolean comparison. 19 */ 20 static bool ok = [](){ 21 BrotliEncoderLazyStaticInitInner(); 22 return true; 23 }(); 24 if (!ok) BROTLI_CRASH(); 25 } 26 #endif /* BROTLI_STATIC_INIT_LAZY */