Skip to content

Conversation

@eyebrowsoffire
Copy link
Contributor

This switches skwasm over from the emscripten pthreads implementation to emscripten's "wasm workers" threading implementation. The pthreads implementation simply will not run at all in a non-crossOriginIsolated context, but the wasm workers implementation only fails if we actually attempt to spawn a thread. This means we can actually choose whether to use a single-threaded or multi-threaded strategy at runtime, which means we don't have to build two variants of skwasm for single- vs multi-threaded.

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@github-actions github-actions bot added engine flutter/engine related. See also e: labels. platform-web Web applications specifically labels Mar 6, 2025
@eyebrowsoffire
Copy link
Contributor Author

This change doesn't require additional unit tests, because there is no change in functionality. The existing unit tests should be sufficient to confirm that we didn't break any existing functionality with this change.

const url = resolveUrlWithSegments(baseUrl, filename);
return URL.createObjectURL(new Blob(
[`
"use strict";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see randomly missing spaces, e.g. sometimes it's a = b but sometimes a=b, or ) => { vs )=>{, etc. Is that to save over-the-wire payload size, or just hand-formatting errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I took the original file generated by emscripten and modified it until I got it working properly, and then forgot to go back and clean it up (remove debug print statements, use consistent spacing and const and stuff). So I need to go back and clean this up.

const pendingMessages = [];
const d = message.data;
d["instantiateWasm"] = (info,receiveInstance) => {
var instance=new WebAssembly.Instance(d["wasm"],info);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like instance can be const.

console.log("adding queuing listener");
addEventListener("message", eventListener);
};
console.log("adding initial listener");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how useful the console.logs are to the user. This will generate a lot of noise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid noise here except in exceptional cases – or for time-bound debugging on our side.

assert(emscripten_is_main_browser_thread());

_thread = emscripten_malloc_wasm_worker(65536);
emscripten_wasm_worker_post_function_v(_thread, []() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, how does this work? Line 22 asserts emscripten_is_main_browser_thread(), which means the worker doesn't execute this code. If so, then the worker doesn't have this closure that's supposed to connect to the main thread. Is this some emscripten magic? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I can break down how this works. So this C++ lambda is not actually a closure, since it doesn't actually close over any variables (there are no captures). As such, a stateless lambda with no closures is special-cased in C++, and it decays to a normal vanilla C function pointer. So if this lambda did capture any variables, this wouldn't work at all, the lambda wouldn't be able to decay to a C function pointer. But with no captures, you can essentially think of the compiler just treating it as a scoped static C function.

The emscripten_wasm_worker_post_function_v takes a pointer to a C function as an argument. Whenever the address of a function is taken, emscripten/llvm know to add that function to the global function table, and the pointer to that C function is essentially just an index into that global function table. The emscripten wasm workers runtime has JS support code that basically takes this function index and ships it across to the worker via a postMessage. When the worker processes this message, it looks up the function in its own global function table by index, and the global function tables should be identical between the instance on the main thread and the instance on the worker.

@eyebrowsoffire eyebrowsoffire added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 10, 2025
@auto-submit auto-submit bot added this pull request to the merge queue Mar 10, 2025
Merged via the queue into flutter:master with commit b2a4a05 Mar 10, 2025
174 of 175 checks passed
@flutter-dashboard flutter-dashboard bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Mar 10, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 11, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 11, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 11, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 11, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 11, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 11, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 26, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 27, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 27, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 27, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 27, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 27, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 27, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 27, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 27, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 28, 2025
eyebrowsoffire added a commit to eyebrowsoffire/flutter that referenced this pull request Apr 2, 2025
This is a reland of flutter#164748, which was
reverted here: flutter#165350

It was reverted due to some issues in the flutter packages roller: flutter#165347

The unit test in flutter/packages was modified to be more deterministic and less affected by minor timing differences: flutter/packages#8920

So this is basically being relanded without any significant changes, since the downstream tests have been fixed.
github-merge-queue bot pushed a commit that referenced this pull request Apr 2, 2025
This is a reland of #164748,
which was reverted here: #165350

It was reverted due to some issues in the flutter packages roller:
#165347

The unit test in flutter/packages was modified to be more deterministic
and less affected by minor timing differences:
flutter/packages#8920

So this is basically being relanded without any significant changes,
since the downstream tests have been fixed.
eyebrowsoffire added a commit to eyebrowsoffire/flutter that referenced this pull request Apr 5, 2025
This is a reland of flutter#164748, which was
reverted here: flutter#165350

It was reverted due to some issues in the flutter packages roller: flutter#165347

The unit test in flutter/packages was modified to be more deterministic and less affected by minor timing differences: flutter/packages#8920

So this is basically being relanded without any significant changes, since the downstream tests have been fixed.
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 20, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 20, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 21, 2025
zhangyuang pushed a commit to zhangyuang/flutter-fork that referenced this pull request Jun 9, 2025
This is a reland of flutter#164748,
which was reverted here: flutter#165350

It was reverted due to some issues in the flutter packages roller:
flutter#165347

The unit test in flutter/packages was modified to be more deterministic
and less affected by minor timing differences:
flutter/packages#8920

So this is basically being relanded without any significant changes,
since the downstream tests have been fixed.
romanejaquez pushed a commit to romanejaquez/flutter that referenced this pull request Aug 14, 2025
This switches skwasm over from the emscripten pthreads implementation to
emscripten's "wasm workers" threading implementation. The pthreads
implementation simply will not run at all in a non-crossOriginIsolated
context, but the wasm workers implementation only fails if we actually
attempt to spawn a thread. This means we can actually choose whether to
use a single-threaded or multi-threaded strategy at runtime, which means
we don't have to build two variants of skwasm for single- vs
multi-threaded.
romanejaquez pushed a commit to romanejaquez/flutter that referenced this pull request Aug 14, 2025
This reverts commit b2a4a05.

This has been causing issues when rolling to flutter/packages repo. See
flutter#165347.
romanejaquez pushed a commit to romanejaquez/flutter that referenced this pull request Aug 14, 2025
This is a reland of flutter#164748,
which was reverted here: flutter#165350

It was reverted due to some issues in the flutter packages roller:
flutter#165347

The unit test in flutter/packages was modified to be more deterministic
and less affected by minor timing differences:
flutter/packages#8920

So this is basically being relanded without any significant changes,
since the downstream tests have been fixed.
@eyebrowsoffire eyebrowsoffire deleted the skwasm_dynamic_threading branch December 12, 2025 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

engine flutter/engine related. See also e: labels. platform-web Web applications specifically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants