Skip to content

[WIP] Experiment with atomic builtins on Node #15740

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

Conversation

kleisauke
Copy link
Collaborator

This PR experiments with the use of atomic builtins on non-web environments (for e.g. Node.js). I did a quick test on the POSIX test suite, to check if this is worthwhile:
Before:

Ran 383 tests in 213.139s

OK (skipped=23, expected failures=53)

After:

Ran 383 tests in 212.284s

OK (skipped=23, expected failures=53)

But benchmarking this on the test suite is probably not a good measurement.

This PR is currently easiest to review per commit, as it depends upon PR #15739.
Context: #15727.

} else {
// Can wait in one go.
#ifdef __EMSCRIPTEN_ATOMIC_BUILTINS__
__builtin_wasm_memory_atomic_wait32((int*)addr, val, -1);
#else // !defined(__EMSCRIPTEN_ATOMIC_BUILTINS__)
emscripten_futex_wait((void*)addr, val, INFINITY);
Copy link
Collaborator

Choose a reason for hiding this comment

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

But doesn't emscripten_futex_wait already used __builtin_wasm_memory_atomic_wait32 in places where this is legal (i.e. off the main web thread?)

I think the decision on whether we can use the atomic wait needs to be dynamic doesn't it ? We don't actually want to produce two different binaries do we?

Copy link
Collaborator Author

@kleisauke kleisauke Dec 10, 2021

Choose a reason for hiding this comment

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

emscripten_futex_wait is implemented in JS and calls directly Atomics.wait for non-web environments. So, this PR is NFC in that respect.

This does indeed produce a different binary if you only target Node, so that would be a breaking change. Perhaps it would be better to implement emscripten_futex_wait dynamically in native code. But as far as I know, that is currently not possible since a JS equivalent of ENVIRONMENT_IS_WEB is not available (PR #15659 is also relevant here).

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can/should implement something like emscripten_thread_can_block in native code.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I decided to have a go addingemscripten_thread_can_block along with native version of emscripten_futex_wait: #15742

Copy link
Collaborator Author

@kleisauke kleisauke Dec 11, 2021

Choose a reason for hiding this comment

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

Great! Do you plan to move emscripten_futex_wake to native code as well (which could use the __builtin_wasm_memory_atomic_notify builtin)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes I hope we can move all the wait/wait to native code but I'm doing it one step at a time,

@kleisauke
Copy link
Collaborator Author

Closing in favor of #15742, which is a better approach.

@kleisauke kleisauke closed this Dec 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants