diff options
author | Evgenii Kliuchnikov <eustas@google.com> | 2025-08-07 01:30:51 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2025-08-07 01:31:31 -0700 |
commit | 6a4c96b1104c361c8d1d8b93db08943a6c459770 (patch) | |
tree | 614f9163e23594d442cb61db43e13771b18abff6 | |
parent | 29e040b8cb40fb0527eb21aad3e117cb37762598 (diff) | |
download | brotli-master.zip brotli-master.tar.gz brotli-master.tar.bz2 |
PiperOrigin-RevId: 792046166
-rw-r--r-- | c/common/platform.h | 8 | ||||
-rw-r--r-- | c/enc/static_init.h | 4 | ||||
-rw-r--r-- | c/enc/static_init_lazy.cc | 22 |
3 files changed, 29 insertions, 5 deletions
diff --git a/c/common/platform.h b/c/common/platform.h index b238218..cf48338 100644 --- a/c/common/platform.h +++ b/c/common/platform.h @@ -586,4 +586,12 @@ BROTLI_UNUSED_FUNCTION void BrotliSuppressUnusedFunctions(void) { #endif } +#if defined(_MSC_VER) +#define BROTLI_CRASH() __debugbreak(), (void)abort() +#elif BROTLI_GNUC_HAS_BUILTIN(__builtin_trap, 3, 0, 0) +#define BROTLI_CRASH() (void)__builtin_trap() +#else +#define BROTLI_CRASH() (void)abort() +#endif + #endif /* BROTLI_COMMON_PLATFORM_H_ */ 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(); } |