Skip to content

Commit ba5318e

Browse files
authored
Acquire the global lock before initializing malloc (#410)
In a multi-threaded execution we need to make sure that only exactly one thread initializes malloc. The function try_init_allocator() can't easily be made thread-safe, so just move the call to try_init_allocator() inside the block that holds the lock.
1 parent d8abbaa commit ba5318e

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

dlmalloc/src/malloc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4593,14 +4593,14 @@ void* dlmalloc(size_t bytes) {
45934593
ensure_initialization(); /* initialize in sys_alloc if not using locks */
45944594
#endif
45954595

4596+
if (!PREACTION(gm)) {
45964597
#if __wasilibc_unmodified_upstream // Try to initialize the allocator.
45974598
#else
4598-
if (!is_initialized(gm)) {
4599-
try_init_allocator();
4600-
}
4599+
if (!is_initialized(gm)) {
4600+
try_init_allocator();
4601+
}
46014602
#endif
46024603

4603-
if (!PREACTION(gm)) {
46044604
void* mem;
46054605
size_t nb;
46064606
if (bytes <= MAX_SMALL_REQUEST) {

0 commit comments

Comments
 (0)