Skip to content

Commit

Permalink
Acquire the global lock before initializing malloc (#410)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MoritzS committed Jul 11, 2023
1 parent d8abbaa commit ba5318e
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions dlmalloc/src/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4593,14 +4593,14 @@ void* dlmalloc(size_t bytes) {
ensure_initialization(); /* initialize in sys_alloc if not using locks */
#endif

if (!PREACTION(gm)) {
#if __wasilibc_unmodified_upstream // Try to initialize the allocator.
#else
if (!is_initialized(gm)) {
try_init_allocator();
}
if (!is_initialized(gm)) {
try_init_allocator();
}
#endif

if (!PREACTION(gm)) {
void* mem;
size_t nb;
if (bytes <= MAX_SMALL_REQUEST) {
Expand Down

0 comments on commit ba5318e

Please sign in to comment.