Skip to content

Commit c55ff40

Browse files
authored
Allow pthread programs to run without SharedArrayBuffer (#22710)
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 ff27062 commit c55ff40

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12071,6 +12071,18 @@ def test_pthread_hello(self, args):
1207112071
def test_pthread_relocatable(self):
1207212072
self.do_run_in_out_file_test('hello_world.c', emcc_args=['-sRELOCATABLE'])
1207312073

12074+
@node_pthreads
12075+
def test_pthread_unavailable(self):
12076+
# Run a simple hello world program that uses pthreads
12077+
self.emcc_args += ['-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME']
12078+
self.do_run_in_out_file_test('hello_world.c')
12079+
12080+
# Now run the same program but with SharedArrayBuffer undefined, it should run
12081+
# fine and then fail on the first call to pthread_create.
12082+
create_file('pre.js', 'SharedArrayBuffer = undefined\n')
12083+
expected = 'pthread_create: environment does not support SharedArrayBuffer, pthreads are not available'
12084+
self.do_runf('hello_world.c', expected, assert_returncode=NON_ZERO, emcc_args=['--pre-js=pre.js'])
12085+
1207412086
def test_stdin_preprocess(self):
1207512087
create_file('temp.h', '#include <string>')
1207612088
outputStdin = self.run_process([EMCC, '-x', 'c++', '-dM', '-E', '-'], input="#include <string>", stdout=PIPE).stdout

0 commit comments

Comments
 (0)