Skip to content

Commit 2565907

Browse files
committed
Allow pthread programs to run without SharedArrayBuffer
As long as they don't actually try to start any threads there is no reason to prevent them from running/starting. Such programs will now instead fail when they first try to create a thread. This is useful for programs that are built with threading support but might also want to run in environments without SharedArrayBuffer, e.g. when deployed without COOP/COEP.
1 parent 102aeeb commit 2565907

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

src/preamble_minimal.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ else {
9393
if (!ENVIRONMENT_IS_PTHREAD) {
9494
#endif
9595

96-
#if ASSERTIONS && SHARED_MEMORY
97-
assert(wasmMemory.buffer instanceof SharedArrayBuffer, 'requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag');
98-
#endif
99-
10096
updateMemoryViews();
10197

10298
#if PTHREADS

src/runtime_init_memory.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,6 @@ if (!ENVIRONMENT_IS_PTHREAD) {
4545
'index': 'i64',
4646
#endif
4747
});
48-
#if SHARED_MEMORY
49-
if (!(wasmMemory.buffer instanceof SharedArrayBuffer)) {
50-
err('requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag');
51-
if (ENVIRONMENT_IS_NODE) {
52-
err('(on node you may need: --experimental-wasm-threads --experimental-wasm-bulk-memory and/or recent version)');
53-
}
54-
throw Error('bad memory');
55-
}
56-
#endif
5748
}
5849

5950
updateMemoryViews();

test/test_other.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12065,6 +12065,14 @@ def test_pthread_hello(self, args):
1206512065
def test_pthread_relocatable(self):
1206612066
self.do_run_in_out_file_test('hello_world.c', emcc_args=['-sRELOCATABLE'])
1206712067

12068+
@node_pthreads
12069+
def test_pthread_unavailable(self):
12070+
self.emcc_args += ['-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME']
12071+
self.do_run_in_out_file_test('hello_world.c')
12072+
create_file('pre.js', 'SharedArrayBuffer = undefined\n')
12073+
expected = 'pthread_create: environment does not support SharedArrayBuffer, pthreads are not available'
12074+
self.do_runf('hello_world.c', expected, assert_returncode=NON_ZERO, emcc_args=['--pre-js=pre.js'])
12075+
1206812076
def test_stdin_preprocess(self):
1206912077
create_file('temp.h', '#include <string>')
1207012078
outputStdin = self.run_process([EMCC, '-x', 'c++', '-dM', '-E', '-'], input="#include <string>", stdout=PIPE).stdout

0 commit comments

Comments
 (0)