Skip to content

Commit cfcf661

Browse files
committed
Improve exit logic. NFC
- Don't call `exitRuntime()` when `ENVIRONMENT_IS_PTHREAD` is true. - Remove the early return from `exitRuntime()`. - Rework if/else statements in `$exitJS()`.
1 parent 1a05b75 commit cfcf661

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

src/library.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,29 @@ mergeInto(LibraryManager.library, {
6868
#endif // ASSERTIONS && !EXIT_RUNTIME
6969

7070
#if USE_PTHREADS
71-
if (!implicit) {
72-
if (ENVIRONMENT_IS_PTHREAD) {
71+
if (ENVIRONMENT_IS_PTHREAD) {
72+
#if ASSERTIONS
73+
assert(!implicit, "exit should never be called implicitly in a pthread");
74+
#endif
7375
#if PTHREADS_DEBUG
74-
err('Pthread 0x' + _pthread_self().toString(16) + ' called exit(), posting exitOnMainThread.');
75-
#endif
76-
// When running in a pthread we propagate the exit back to the main thread
77-
// where it can decide if the whole process should be shut down or not.
78-
// The pthread may have decided not to exit its own runtime, for example
79-
// because it runs a main loop, but that doesn't affect the main thread.
80-
exitOnMainThread(status);
81-
throw 'unwind';
82-
} else {
76+
err('Pthread 0x' + _pthread_self().toString(16) + ' called exit(), posting exitOnMainThread.');
77+
#endif
78+
// When running in a pthread we propagate the exit back to the main thread
79+
// where it can decide if the whole process should be shut down or not.
80+
// The pthread may have decided not to exit its own runtime, for example
81+
// because it runs a main loop, but that doesn't affect the main thread.
82+
exitOnMainThread(status);
83+
throw 'unwind';
8384
#if PTHREADS_DEBUG
85+
} else if (!implicit) {
8486
#if EXIT_RUNTIME
85-
err('main thread called exit: keepRuntimeAlive=' + keepRuntimeAlive() + ' (counter=' + runtimeKeepaliveCounter + ')');
87+
err('main thread called exit: keepRuntimeAlive=' + keepRuntimeAlive() + ' (counter=' + runtimeKeepaliveCounter + ')');
8688
#else
87-
err('main thread called exit: keepRuntimeAlive=' + keepRuntimeAlive());
88-
#endif
89-
#endif
90-
}
89+
err('main thread called exit: keepRuntimeAlive=' + keepRuntimeAlive());
90+
#endif // EXIT_RUNTIME
91+
#endif // PTHREADS_DEBUG
9192
}
92-
#endif
93+
#endif // USE_PTHREADS
9394

9495
#if EXIT_RUNTIME
9596
if (!keepRuntimeAlive()) {
@@ -100,10 +101,10 @@ mergeInto(LibraryManager.library, {
100101
#if ASSERTIONS
101102
// if exit() was called explicitly, warn the user if the runtime isn't actually being shut down
102103
if (keepRuntimeAlive() && !implicit) {
103-
#if !EXIT_RUNTIME
104-
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)';
105-
#else
104+
#if EXIT_RUNTIME
106105
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)';
106+
#else
107+
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)';
107108
#endif // EXIT_RUNTIME
108109
#if MODULARIZE
109110
readyPromiseReject(msg);

src/preamble.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,6 @@ function exitRuntime() {
295295
#if STACK_OVERFLOW_CHECK
296296
checkStackCookie();
297297
#endif
298-
#if USE_PTHREADS
299-
if (ENVIRONMENT_IS_PTHREAD) return; // PThreads reuse the runtime from the main thread.
300-
#endif
301298
#if !STANDALONE_WASM
302299
___funcs_on_exit(); // Native atexit() functions
303300
#endif

src/worker.js

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

0 commit comments

Comments
 (0)