Skip to content

Commit 9e7b313

Browse files
committed
Improve exit logic. NFC.
- Don't call `exitRuntime()` when `ENVIRONMENT_IS_PTHREAD` is true. - Only obtain `runtimeKeepaliveCounter` when `EXIT_RUNTIME` is true. - Remove the early return from `exitRuntime()`. - Collapse if/else statements in `exit()`.
1 parent 4ce34cc commit 9e7b313

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

src/postamble.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -411,24 +411,25 @@ function exit(status, implicit) {
411411
#endif // ASSERTIONS && !EXIT_RUNTIME
412412

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

433434
#if EXIT_RUNTIME
434435
if (!keepRuntimeAlive()) {
@@ -437,12 +438,13 @@ function exit(status, implicit) {
437438
#endif
438439

439440
#if ASSERTIONS
441+
#if EXIT_RUNTIME
440442
// if exit() was called explicitly, warn the user if the runtime isn't actually being shut down
443+
else if (!implicit) {
444+
var msg = 'program exited (with status: ' + status + '), but keepRuntimeAlive() is set (counter=' + runtimeKeepaliveCounter + ') due to an async operation, so halting execution but not exiting the runtime or preventing further async execution (you can use emscripten_force_exit, if you want to force a true shutdown)';
445+
#else // !EXIT_RUNTIME
441446
if (keepRuntimeAlive() && !implicit) {
442-
#if !EXIT_RUNTIME
443447
var msg = 'program exited (with status: ' + status + '), but EXIT_RUNTIME is not set, so halting execution but not exiting the runtime or preventing further async execution (build with EXIT_RUNTIME=1, if you want a true shutdown)';
444-
#else
445-
var msg = 'program exited (with status: ' + status + '), but keepRuntimeAlive() is set (counter=' + runtimeKeepaliveCounter + ') due to an async operation, so halting execution but not exiting the runtime or preventing further async execution (you can use emscripten_force_exit, if you want to force a true shutdown)';
446448
#endif // EXIT_RUNTIME
447449
#if MODULARIZE
448450
readyPromiseReject(msg);

src/preamble.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,6 @@ function exitRuntime() {
407407
#if STACK_OVERFLOW_CHECK
408408
checkStackCookie();
409409
#endif
410-
#if USE_PTHREADS
411-
if (ENVIRONMENT_IS_PTHREAD) return; // PThreads reuse the runtime from the main thread.
412-
#endif
413410
#if !STANDALONE_WASM
414411
___funcs_on_exit(); // Native atexit() functions
415412
#endif

src/worker.js

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

0 commit comments

Comments
 (0)