diff options
Diffstat (limited to 'c/enc')
-rw-r--r-- | c/enc/static_init.h | 4 | ||||
-rw-r--r-- | c/enc/static_init_lazy.cc | 22 |
2 files changed, 21 insertions, 5 deletions
diff --git a/c/enc/static_init.h b/c/enc/static_init.h index 1969f36..d0b1580 100644 --- a/c/enc/static_init.h +++ b/c/enc/static_init.h @@ -45,8 +45,8 @@ BROTLI_INTERNAL void BrotliEncoderLazyStaticInitInner(void); the first invocation. This function should not return until execution of `BrotliEncoderLazyStaticInitInner` is finished. In C or before C++11 it is possible to call `BrotliEncoderLazyStaticInitInner` on start-up path and then - `BrotliEncoderLazyStaticInit` is no-op; another option is to use available - thread execution controls to meet the requirements. + `BrotliEncoderLazyStaticInit` is could be no-op; another option is to use + available thread execution controls to meet the requirements. For possible C++11 implementation see static_init_lazy.cc. */ BROTLI_INTERNAL void BrotliEncoderLazyStaticInit(void); diff --git a/c/enc/static_init_lazy.cc b/c/enc/static_init_lazy.cc index 726b0de..300357e 100644 --- a/c/enc/static_init_lazy.cc +++ b/c/enc/static_init_lazy.cc @@ -1,9 +1,25 @@ -#include "third_party/brotli/enc/static_init.h" +#include "third_party/brotli/common/platform.h" +#include "static_init.h" + +#if (BROTLI_STATIC_INIT != BROTLI_STATIC_INIT_LAZY) +#error "BROTLI_STATIC_INIT should be BROTLI_STATIC_INIT_LAZY" +#endif void BrotliEncoderLazyStaticInit(void) { + /* From https://en.cppreference.com/w/cpp/language/storage_duration.html: + ### Static block variables ### + Block variables with static or thread (since C++11) storage duration are + initialized the first time control passes through their declaration... + On all further calls, the declaration is skipped... + If multiple threads attempt to initialize the same static local variable + concurrently, the initialization occurs exactly once... + Usual implementations of this feature use variants of the double-checked + locking pattern, which reduces runtime overhead for already-initialized + local statics to a single non-atomic boolean comparison. + */ static bool ok = [](){ - BrotliEncoderLazyStaticInit(); + BrotliEncoderLazyStaticInitInner(); return true; }(); - if (!ok) __builtin_trap(); + if (!ok) BROTLI_CRASH(); } |