-
Notifications
You must be signed in to change notification settings - Fork 276
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
Add wasm32 atomic intrinsics #561
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//! Intrinsics associated with WebAssembly's upcoming threads proposal. | ||
//! | ||
//! These intrinsics are all unstable because they're not actually stable in | ||
//! WebAssembly itself yet. The signatures may change as [the | ||
//! specification][spec] is updated. | ||
//! | ||
//! [spec]: https://github.com/WebAssembly/threads | ||
|
||
#![cfg(target_feature = "atomics")] | ||
|
||
#[cfg(test)] | ||
use stdsimd_test::assert_instr; | ||
#[cfg(test)] | ||
use wasm_bindgen_test::wasm_bindgen_test; | ||
|
||
extern "C" { | ||
#[link_name = "llvm.wasm.atomic.wait.i32"] | ||
fn llvm_atomic_wait_i32(ptr: *mut i32, exp: i32, timeout: i64) -> i32; | ||
#[link_name = "llvm.wasm.atomic.wait.i64"] | ||
fn llvm_atomic_wait_i64(ptr: *mut i64, exp: i64, timeout: i64) -> i32; | ||
#[link_name = "llvm.wasm.atomic.notify"] | ||
fn llvm_atomic_notify(ptr: *mut i32, cnt: i32) -> i32; | ||
} | ||
|
||
/// Corresponding intrinsic to wasm's [`i32.atomic.wait` instruction][instr] | ||
/// | ||
/// This function, when called, will block the current thread if the memory | ||
/// pointed to by `ptr` is equal to `expression` (performing this action | ||
/// atomically). | ||
/// | ||
/// The argument `timeout_ns` is a maxinum number of nanoseconds the calling | ||
/// thread will be blocked for, if it blocks. If the timeout is negative then | ||
/// the calling thread will be blocked forever. | ||
/// | ||
/// The calling thread can only be woken up with a call to the `wake` intrinsic | ||
/// once it has been blocked. Changing the memory behind `ptr` will not wake the | ||
/// thread once it's blocked. | ||
/// | ||
/// # Return value | ||
/// | ||
/// * 0 - indicates that the thread blocked and then was woken up | ||
/// * 1 - the loaded value from `ptr` didn't match `expression`, the thread | ||
/// didn't block | ||
/// * 2 - the thread blocked, but the timeout expired. | ||
/// | ||
/// [instr]: https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md#wait | ||
#[inline] | ||
#[cfg_attr(test, assert_instr("i32.atomic.wait"))] | ||
pub unsafe fn wait_i32(ptr: *mut i32, expression: i32, timeout_ns: i64) -> i32 { | ||
llvm_atomic_wait_i32(ptr, expression, timeout_ns) | ||
} | ||
|
||
/// Corresponding intrinsic to wasm's [`i64.atomic.wait` instruction][instr] | ||
/// | ||
/// This function, when called, will block the current thread if the memory | ||
/// pointed to by `ptr` is equal to `expression` (performing this action | ||
/// atomically). | ||
/// | ||
/// The argument `timeout_ns` is a maxinum number of nanoseconds the calling | ||
/// thread will be blocked for, if it blocks. If the timeout is negative then | ||
/// the calling thread will be blocked forever. | ||
/// | ||
/// The calling thread can only be woken up with a call to the `wake` intrinsic | ||
/// once it has been blocked. Changing the memory behind `ptr` will not wake the | ||
/// thread once it's blocked. | ||
/// | ||
/// # Return value | ||
/// | ||
/// * 0 - indicates that the thread blocked and then was woken up | ||
/// * 1 - the loaded value from `ptr` didn't match `expression`, the thread | ||
/// didn't block | ||
/// * 2 - the thread blocked, but the timeout expired. | ||
/// | ||
/// [instr]: https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md#wait | ||
#[inline] | ||
#[cfg_attr(test, assert_instr("i64.atomic.wait"))] | ||
pub unsafe fn wait_i64(ptr: *mut i64, expression: i64, timeout_ns: i64) -> i32 { | ||
llvm_atomic_wait_i64(ptr, expression, timeout_ns) | ||
} | ||
|
||
/// Corresponding intrinsic to wasm's [`atomic.wake` instruction][instr] | ||
/// | ||
/// This function will wake up a number of threads blocked on the address | ||
/// indicated by `ptr`. Threads previously blocked with the `wait_i32` and | ||
/// `wait_i64` functions above will be woken up. | ||
/// | ||
/// The `waiters` argument indicates how many waiters should be woken up (a | ||
/// maximum). If the value is negative all waiters are woken up, and if the | ||
/// value is zero no waiters are woken up. | ||
/// | ||
/// # Return value | ||
/// | ||
/// Returns the number of waiters which were actually woken up. | ||
/// | ||
/// [instr]: https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md#wake | ||
#[inline] | ||
#[cfg_attr(test, assert_instr("atomic.wake"))] | ||
pub unsafe fn wake(ptr: *mut i32, waiters: i32) -> i32 { | ||
llvm_atomic_notify(ptr, waiters) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be in the parent hiding the module, and the re-export of these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah wait, I see you did the same in the memory module, do we want to export the modules themselves (
atomic
,memory
,simd128
, etc.) ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure yeah, do you mean just the attribute or the functions too?
On a possibly random tangent about function names...
I'm starting to get a little torn on the naming of the functions here. Technically the function name should be
wasm32::i32::atomic::wait
but that's a lot of::
. Currentlywasm32::atomic::i32_wait
is sort of weirdly inconsistent. What do you think ofwasm32::i32_atomic_wait
? Basically translating all.
to_
and we have toplevel functions for all the operations? (this'd affect the SIMD proposal slightly)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me too, in the simd module we also had problems with this. Maybe we should just open an issue to discuss this, e.g., show examples of the wasm names, and how could we make them consistent in Rust.
In the mean time I think we can do "whatever". The other PR puts the intrinsics behind a
memory::
so I don't mind them here being behindatomic::
and the simd module puts them behind e.g.i32x4::
IIRC and the other vector type names, so at least it remains consistent for now.About the best way to do this, I am not a super huge fan of
i32_atomic_wait
, but I don't really like the many::
either :/ What does clang do?__builtin_...
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing, done! - #562