Skip to content

Commit 778842c

Browse files
committedApr 13, 2022
Configure dlmalloc USE_LOCKS and USE_SPIN_LOCKS from system_libs.py since they need to configure different in Wasm Workers vs pthreads build modes.
1 parent 2149dd6 commit 778842c

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed
 

‎system/lib/dlmalloc.c

-9
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818
/* XXX Emscripten Tracing API. This defines away the code if tracing is disabled. */
1919
#include <emscripten/trace.h>
2020

21-
/* Make malloc() and free() threadsafe by securing the memory allocations with pthread mutexes. */
22-
#ifdef __EMSCRIPTEN_WASM_WORKERS__
23-
#define USE_LOCKS 1
24-
#define USE_SPIN_LOCKS 1 // Wasm Workers does not have the pthread API, so use spinwaiting
25-
#elif defined(__EMSCRIPTEN_PTHREADS__)
26-
#define USE_LOCKS 1
27-
#define USE_SPIN_LOCKS 0 // Ensure we use pthread_mutex_t.
28-
#endif
29-
3021
#ifndef MALLOC_ALIGNMENT
3122
#include <stddef.h>
3223
/* `malloc`ed pointers must be aligned at least as strictly as max_align_t. */

‎tools/system_libs.py

+9
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,8 @@ def __init__(self, **kwargs):
12711271
self.memvalidate = kwargs.pop('memvalidate')
12721272
self.verbose = kwargs.pop('verbose')
12731273
self.is_debug = kwargs.pop('is_debug') or self.memvalidate or self.verbose
1274+
self.thread_safe = kwargs.pop('thread_safe')
1275+
self.use_spinlocks = kwargs.pop('use_spinlocks')
12741276

12751277
super().__init__(**kwargs)
12761278

@@ -1296,6 +1298,11 @@ def get_cflags(self):
12961298
cflags += ['-DMALLOC_FAILURE_ACTION=', '-DEMSCRIPTEN_NO_ERRNO']
12971299
if self.is_tracing:
12981300
cflags += ['--tracing']
1301+
# Target dlmalloc multithreading aware implementation if using pthreads or Wasm Workers
1302+
cflags += [f'-DUSE_LOCKS={self.thread_safe}']
1303+
# When using Wasm Workers, target spinlocks. Otherwise target pthread mutexes when using pthreads.
1304+
cflags += [f'-DUSE_SPIN_LOCKS={self.use_spinlocks}']
1305+
12991306
return cflags
13001307

13011308
def get_base_name_prefix(self):
@@ -1338,6 +1345,8 @@ def get_default_variation(cls, **kwargs):
13381345
is_tracing=settings.EMSCRIPTEN_TRACING,
13391346
memvalidate='memvalidate' in settings.MALLOC,
13401347
verbose='verbose' in settings.MALLOC,
1348+
use_spinlocks=settings.WASM_WORKERS,
1349+
thread_safe=settings.SHARED_MEMORY,
13411350
**kwargs
13421351
)
13431352

0 commit comments

Comments
 (0)
Failed to load comments.