| 1 | #if defined(_REENTRANT) |
| 2 | #include <stdatomic.h> |
| 3 | extern void __wasi_init_tp(void); |
| 4 | #endif |
| 5 | extern void __wasm_call_ctors(void); |
| 6 | |
| 7 | __attribute__((export_name("_initialize"))) |
| 8 | void _initialize(void) { |
| 9 | #if defined(_REENTRANT) |
| 10 | static volatile atomic_int initialized = 0; |
| 11 | int expected = 0; |
| 12 | if (!atomic_compare_exchange_strong(&initialized, &expected, 1)) { |
| 13 | __builtin_trap(); |
| 14 | } |
| 15 | |
| 16 | __wasi_init_tp(); |
| 17 | #else |
| 18 | static volatile int initialized = 0; |
| 19 | if (initialized != 0) { |
| 20 | __builtin_trap(); |
| 21 | } |
| 22 | initialized = 1; |
| 23 | #endif |
| 24 | |
| 25 | // The linker synthesizes this to call constructors. |
| 26 | __wasm_call_ctors(); |
| 27 | } |