Skip to content

Improve & simplify time sync in pthreads #18267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ See docs/process.md for more on how version tagging works.
applications that wish to know where emscripten is installed looking for
`emcc` in the `PATH` has long been the recommended method (i.e. `which emcc`).
(#18279)
- More accurate synchronisation of `emscripten_get_now` clocks between main
thread and pthreads.
This also changes the absolute value returned by the function, but it shouldn't
affect correct usages as the function has always returned different values on
different platforms, and is clearly documented as "only meaningful in
comparison to other calls to this function". (#18267)

3.1.27 - 11/29/22
-----------------
Expand Down
11 changes: 5 additions & 6 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -2328,12 +2328,10 @@ mergeInto(LibraryManager.library, {
"} else " +
#endif
#if USE_PTHREADS
// Pthreads need their clocks synchronized to the execution of the main thread, so give them a special form of the function.
// N.b. Wasm workers do not provide this kind of clock synchronization.
"if (ENVIRONMENT_IS_PTHREAD) {\n" +
" _emscripten_get_now = () => performance.now() - Module['__performance_now_clock_drift'];\n" +
"} else " +
#endif
// Pthreads need their clocks synchronized to the execution of the main thread, so, when using them,
// make sure to adjust all timings to the respective time origins.
"_emscripten_get_now = () => performance.timeOrigin + performance.now();\n",
#else
#if ENVIRONMENT_MAY_BE_SHELL
"if (typeof dateNow != 'undefined') {\n" +
" _emscripten_get_now = dateNow;\n" +
Expand All @@ -2349,6 +2347,7 @@ mergeInto(LibraryManager.library, {
// Modern environment where performance.now() is supported:
// N.B. a shorter form "_emscripten_get_now = return performance.now;" is unfortunately not allowed even in current browsers (e.g. FF Nightly 75).
"_emscripten_get_now = () => performance.now();\n",
#endif
#endif

emscripten_get_now_res: function() { // return resolution of get_now, in nanoseconds
Expand Down
1 change: 0 additions & 1 deletion src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,6 @@ var LibraryPThread = {
worker.ref();
}
#endif
msg.time = performance.now();
worker.postMessage(msg, threadParams.transferList);
delete worker.runPthread;
};
Expand Down
12 changes: 0 additions & 12 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,6 @@ self.onmessage = (e) => {
#endif
#endif // MODULARIZE && EXPORT_ES6
} else if (e.data.cmd === 'run') {
// This worker was idle, and now should start executing its pthread entry
// point.
// performance.now() is specced to return a wallclock time in msecs since
// that Web Worker/main thread launched. However for pthreads this can
// cause subtle problems in emscripten_get_now() as this essentially
// would measure time from pthread_create(), meaning that the clocks
// between each threads would be wildly out of sync. Therefore sync all
// pthreads to the clock on the main browser thread, so that different
// threads see a somewhat coherent clock across each of them
// (+/- 0.1msecs in testing).
Module['__performance_now_clock_drift'] = performance.now() - e.data.time;

// Pass the thread address to wasm to store it for fast access.
Module['__emscripten_thread_init'](e.data.pthread_ptr, /*isMainBrowserThread=*/0, /*isMainRuntimeThread=*/0, /*canBlock=*/1);

Expand Down