Skip to content

Commit 094013d

Browse files
committed
Improve exit logic. NFC.
- Don't call `exitRuntime()` when `ENVIRONMENT_IS_PTHREAD` is true. - Remove the early return from `exitRuntime()`. - Collapse if/else statements in `exit()`.
1 parent dc62be8 commit 094013d

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

src/postamble.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -406,27 +406,30 @@ function exit(status, implicit) {
406406
#endif // ASSERTIONS && !EXIT_RUNTIME
407407

408408
#if USE_PTHREADS
409-
if (!implicit) {
410-
if (ENVIRONMENT_IS_PTHREAD) {
411-
#if PTHREADS_DEBUG
412-
err('Pthread 0x' + _pthread_self().toString(16) + ' called exit(), posting exitOnMainThread.');
413-
#endif
414-
// When running in a pthread we propagate the exit back to the main thread
415-
// where it can decide if the whole process should be shut down or not.
416-
// The pthread may have decided not to exit its own runtime, for example
417-
// because it runs a main loop, but that doesn't affect the main thread.
418-
exitOnMainThread(status);
419-
throw 'unwind';
420-
} else {
409+
if (ENVIRONMENT_IS_PTHREAD) {
410+
#if ASSERTIONS
411+
assert(!implicit, "exit should never be called implicitly in a pthread");
412+
#endif
421413
#if PTHREADS_DEBUG
422-
err('main thread called exit: keepRuntimeAlive=' + keepRuntimeAlive() + ' (counter=' + runtimeKeepaliveCounter + ')');
414+
err('Pthread 0x' + _pthread_self().toString(16) + ' called exit(), posting exitOnMainThread.');
415+
#endif
416+
// When running in a pthread we propagate the exit back to the main thread
417+
// where it can decide if the whole process should be shut down or not.
418+
// The pthread may have decided not to exit its own runtime, for example
419+
// because it runs a main loop, but that doesn't affect the main thread.
420+
exitOnMainThread(status);
421+
throw 'unwind';
422+
#if ASSERTIONS
423+
} else if (!implicit) {
424+
err('main thread called exit: keepRuntimeAlive=' + keepRuntimeAlive() + ' (counter=' + runtimeKeepaliveCounter + ')');
423425
#endif
424-
}
425426
}
426-
#endif
427+
#endif // USE_PTHREADS
427428

428-
if (keepRuntimeAlive()) {
429+
if (!keepRuntimeAlive()) {
430+
exitRuntime();
429431
#if ASSERTIONS
432+
} else {
430433
// if exit() was called, we may warn the user if the runtime isn't actually being shut down
431434
if (!implicit) {
432435
#if EXIT_RUNTIME == 0
@@ -440,8 +443,6 @@ function exit(status, implicit) {
440443
err(msg);
441444
}
442445
#endif // ASSERTIONS
443-
} else {
444-
exitRuntime();
445446
}
446447

447448
procExit(status);

src/preamble.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,6 @@ function exitRuntime() {
426426
#if STACK_OVERFLOW_CHECK
427427
checkStackCookie();
428428
#endif
429-
#if USE_PTHREADS
430-
if (ENVIRONMENT_IS_PTHREAD) return; // PThreads reuse the runtime from the main thread.
431-
#endif
432429
#if EXIT_RUNTIME
433430
#if !STANDALONE_WASM
434431
___funcs_on_exit(); // Native atexit() functions

src/worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ self.onmessage = (e) => {
259259
}
260260
#if ASSERTIONS
261261
} else {
262-
// else e == 'unwind', and we should fall through here and keep the pthread alive for asynchronous events.
262+
// else ex == 'unwind', and we should fall through here and keep the pthread alive for asynchronous events.
263263
err('Pthread 0x' + Module['_pthread_self']().toString(16) + ' completed its main entry point with an `unwind`, keeping the worker alive for asynchronous operation.');
264264
#endif
265265
}

0 commit comments

Comments
 (0)