Skip to content

Commit a52cc74

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 72d1062 commit a52cc74

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

src/jsifier.mjs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -284,24 +284,21 @@ ${argConversions}
284284

285285
// apply LIBRARY_DEBUG if relevant
286286
if (LIBRARY_DEBUG && !isJsOnlySymbol(symbol)) {
287-
snippet = modifyJSFunction(
288-
snippet,
289-
(args, body, async, oneliner) => {
290-
var dobody;
291-
if (oneliner) {
292-
dobody = `var ret = ${body}`;
293-
} else {
294-
295-
dobody = `var ret = (() => { ${body} })();`;
296-
}
297-
return `\
287+
snippet = modifyJSFunction(snippet, (args, body, async, oneliner) => {
288+
var dobody;
289+
if (oneliner) {
290+
dobody = `var ret = ${body}`;
291+
} else {
292+
dobody = `var ret = (() => { ${body} })();`;
293+
}
294+
return `\
298295
function(${args}) {
299296
if (runtimeDebug) err("[library call:${mangled}: " + Array.prototype.slice.call(arguments).map(prettyPrint) + "]");
300297
${dobody}
301298
if (runtimeDebug) err(" [ return:" + prettyPrint(ret));
302299
return ret;
303300
}`;
304-
});
301+
});
305302
}
306303

307304
const sig = LibraryManager.library[symbol + '__sig'];

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
@@ -12065,6 +12065,18 @@ 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+
# Run a simple hello world program that uses pthreads
12071+
self.emcc_args += ['-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME']
12072+
self.do_run_in_out_file_test('hello_world.c')
12073+
12074+
# Now run the same program but with SharedArrayBuffer undefined, it should run
12075+
# fine and then fail on the first call to pthread_create.
12076+
create_file('pre.js', 'SharedArrayBuffer = undefined\n')
12077+
expected = 'pthread_create: environment does not support SharedArrayBuffer, pthreads are not available'
12078+
self.do_runf('hello_world.c', expected, assert_returncode=NON_ZERO, emcc_args=['--pre-js=pre.js'])
12079+
1206812080
def test_stdin_preprocess(self):
1206912081
create_file('temp.h', '#include <string>')
1207012082
outputStdin = self.run_process([EMCC, '-x', 'c++', '-dM', '-E', '-'], input="#include <string>", stdout=PIPE).stdout

0 commit comments

Comments
 (0)