Skip to content

Commit 777f712

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 0385985 commit 777f712

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
@@ -84,28 +84,29 @@ mergeInto(LibraryManager.library, {
8484
#endif // ASSERTIONS && !EXIT_RUNTIME
8585

8686
#if USE_PTHREADS
87-
if (!implicit) {
88-
if (ENVIRONMENT_IS_PTHREAD) {
87+
if (ENVIRONMENT_IS_PTHREAD) {
88+
#if ASSERTIONS
89+
assert(!implicit, "exit should never be called implicitly in a pthread");
90+
#endif
8991
#if PTHREADS_DEBUG
90-
err('Pthread 0x' + _pthread_self().toString(16) + ' called exit(), posting exitOnMainThread.');
91-
#endif
92-
// When running in a pthread we propagate the exit back to the main thread
93-
// where it can decide if the whole process should be shut down or not.
94-
// The pthread may have decided not to exit its own runtime, for example
95-
// because it runs a main loop, but that doesn't affect the main thread.
96-
exitOnMainThread(status);
97-
throw 'unwind';
98-
} else {
92+
err('Pthread 0x' + _pthread_self().toString(16) + ' called exit(), posting exitOnMainThread.');
93+
#endif
94+
// When running in a pthread we propagate the exit back to the main thread
95+
// where it can decide if the whole process should be shut down or not.
96+
// The pthread may have decided not to exit its own runtime, for example
97+
// because it runs a main loop, but that doesn't affect the main thread.
98+
exitOnMainThread(status);
99+
throw 'unwind';
99100
#if PTHREADS_DEBUG
101+
} else if (!implicit) {
100102
#if EXIT_RUNTIME
101-
err('main thread called exit: keepRuntimeAlive=' + keepRuntimeAlive() + ' (counter=' + runtimeKeepaliveCounter + ')');
103+
err('main thread called exit: keepRuntimeAlive=' + keepRuntimeAlive() + ' (counter=' + runtimeKeepaliveCounter + ')');
102104
#else
103-
err('main thread called exit: keepRuntimeAlive=' + keepRuntimeAlive());
104-
#endif
105-
#endif
106-
}
105+
err('main thread called exit: keepRuntimeAlive=' + keepRuntimeAlive());
106+
#endif // EXIT_RUNTIME
107+
#endif // PTHREADS_DEBUG
107108
}
108-
#endif
109+
#endif // USE_PTHREADS
109110

110111
#if EXIT_RUNTIME
111112
if (!keepRuntimeAlive()) {
@@ -116,10 +117,10 @@ mergeInto(LibraryManager.library, {
116117
#if ASSERTIONS
117118
// if exit() was called explicitly, warn the user if the runtime isn't actually being shut down
118119
if (keepRuntimeAlive() && !implicit) {
119-
#if !EXIT_RUNTIME
120-
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)';
121-
#else
120+
#if EXIT_RUNTIME
122121
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)';
122+
#else
123+
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)';
123124
#endif // EXIT_RUNTIME
124125
#if MODULARIZE
125126
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
@@ -257,7 +257,7 @@ self.onmessage = (e) => {
257257
}
258258
#if ASSERTIONS
259259
} else {
260-
// else e == 'unwind', and we should fall through here and keep the pthread alive for asynchronous events.
260+
// else ex == 'unwind', and we should fall through here and keep the pthread alive for asynchronous events.
261261
err('Pthread 0x' + Module['_pthread_self']().toString(16) + ' completed its main entry point with an `unwind`, keeping the worker alive for asynchronous operation.');
262262
#endif
263263
}

0 commit comments

Comments
 (0)