Skip to content

Commit c52006b

Browse files
committed
Add support for Wasm Workers + dlmalloc
1 parent c11edf1 commit c52006b

File tree

4 files changed

+10
-16
lines changed

4 files changed

+10
-16
lines changed

emcc.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,10 +2136,6 @@ def phase_linker_setup(options, state, newargs, user_settings):
21362136
settings.WASM_WORKER_FILE = unsuffixed(os.path.basename(target)) + '.ww.js'
21372137
settings.JS_LIBRARIES.append((0, shared.path_from_root('src', 'library_wasm_worker.js')))
21382138

2139-
if 'MALLOC' in user_settings and 'dlmalloc' in user_settings['MALLOC']:
2140-
exit_with_error('dlmalloc does not work with Wasm Workers! Please remove -sMALLOC=dlmalloc')
2141-
default_setting(user_settings, 'MALLOC', 'emmalloc')
2142-
21432139
if settings.FORCE_FILESYSTEM and not settings.MINIMAL_RUNTIME:
21442140
# when the filesystem is forced, we export by default methods that filesystem usage
21452141
# may need, including filesystem usage from standalone file packager output (i.e.

system/lib/dlmalloc.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
#include <emscripten/trace.h>
2020

2121
/* Make malloc() and free() threadsafe by securing the memory allocations with pthread mutexes. */
22-
#if __EMSCRIPTEN_PTHREADS__
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__)
2326
#define USE_LOCKS 1
2427
#define USE_SPIN_LOCKS 0 // Ensure we use pthread_mutex_t.
2528
#endif

tests/test_browser.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5290,8 +5290,12 @@ def test_wasm_worker_semaphore_try_acquire(self):
52905290

52915291
# Tests that operating malloc from Wasm Workers is thread-safe.
52925292
@also_with_minimal_runtime
5293-
def test_wasm_worker_thread_safe_malloc(self):
5294-
self.btest(test_file('wasm_worker/thread_safe_malloc.cpp'), expected='0', args=['-sWASM_WORKERS', '-DEMMALLOC'])
5293+
def test_wasm_worker_thread_safe_emmalloc(self):
5294+
self.btest(test_file('wasm_worker/thread_safe_malloc.cpp'), expected='0', args=['-sWASM_WORKERS', '-DEMMALLOC', '-sMALLOC=emmalloc-debug'])
5295+
5296+
@also_with_minimal_runtime
5297+
def test_wasm_worker_thread_safe_dlmalloc(self):
5298+
self.btest(test_file('wasm_worker/thread_safe_malloc.cpp'), expected='0', args=['-sWASM_WORKERS', '-sMALLOC=dlmalloc'])
52955299

52965300
@no_firefox('no 4GB support yet')
52975301
@require_v8

tests/test_other.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11847,15 +11847,6 @@ def test_shared_memory_preprocessor_flags(self):
1184711847
def test_wasm_worker_preprocessor_flags(self):
1184811848
self.run_process([EMCC, '-c', test_file('wasm_worker/wasm_worker_preprocessor_flags.c'), '-sWASM_WORKERS'])
1184911849

11850-
# Tests that Wasm Workers are properly detected not to (at least for now) work with dlmalloc
11851-
@also_with_minimal_runtime
11852-
def test_wasm_worker_dlmalloc_not_supported(self):
11853-
# Using dlmalloc should produce a build error
11854-
stderr = self.run_process([EMCC, test_file('hello_world.c'), '-sWASM_WORKERS', '-sMALLOC=dlmalloc'], stderr=PIPE, check=False).stderr
11855-
self.assertContained('dlmalloc does not work with Wasm Workers! Please remove -sMALLOC=dlmalloc', stderr)
11856-
# Using emmalloc should compile ok
11857-
self.run_process([EMCC, test_file('hello_world.c'), '-sWASM_WORKERS', '-sMALLOC=emmalloc'])
11858-
1185911850
def test_debug_opt_warning(self):
1186011851
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-O2', '-g', '-Werror'])
1186111852
self.assertContained('error: running limited binaryen optimizations because DWARF info requested (or indirectly required) [-Wlimited-postlink-optimizations]', err)

0 commit comments

Comments
 (0)