more portable emergency exit

PiperOrigin-RevId: 792017029
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 @@
 #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 @@
    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();
 }