-
Notifications
You must be signed in to change notification settings - Fork 265
wasm implementation of RawMutex and RawRwlock using UnsafeCell #187
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
Closed
Closed
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,2 @@ | ||
| pub mod raw_mutex; | ||
| pub mod raw_rwlock; |
This file contains hidden or 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,128 @@ | ||
| // Eventually, when access to WASM i32_atomic_wait is stable, this should look more like | ||
| // https://github.com/rust-lang/rust/blob/f51752774bbbe48d2aabe53c86e9e91ed3a73a5d/src/libstd/sys/wasm/mutex_atomics.rs#L81-L160 | ||
| // | ||
| // For now, we essentially do what | ||
| // https://github.com/rust-lang/rust/blob/253fc0ed742c235fa34c5d78814fa7b8a5e5e055/src/libstd/sys/wasm/mutex.rs does. | ||
|
|
||
| use std::cell::UnsafeCell; | ||
|
|
||
| const LOCKED_BIT: u8 = 1; | ||
| const PARKED_BIT: u8 = 2; | ||
|
|
||
| // UnparkToken used to indicate that that the target thread should attempt to | ||
| // lock the mutex again as soon as it is unparked. | ||
| pub(crate) const TOKEN_NORMAL: UnparkToken = UnparkToken(0); | ||
|
|
||
| // UnparkToken used to indicate that the mutex is being handed off to the target | ||
| // thread directly without unlocking it. | ||
| pub(crate) const TOKEN_HANDOFF: UnparkToken = UnparkToken(1); | ||
|
|
||
| /// Raw mutex type backed by the parking lot. | ||
| pub struct RawMutex { | ||
| state: UnsafeCell<u8>, | ||
| } | ||
|
|
||
| unsafe impl Send for RawMutex {} | ||
| unsafe impl Sync for RawMutex {} // no threads on wasm | ||
|
|
||
| use crate::deadlock; | ||
| use core::time::Duration; | ||
| use lock_api::{GuardSend, RawMutex as RawMutexTrait, RawMutexFair, RawMutexTimed}; | ||
| use parking_lot_core::{self, UnparkToken}; | ||
| use std::time::Instant; | ||
|
|
||
| unsafe impl RawMutexTrait for RawMutex { | ||
| const INIT: RawMutex = RawMutex { | ||
| state: UnsafeCell::new(0u8), | ||
| }; | ||
|
|
||
| type GuardMarker = GuardSend; | ||
|
|
||
| #[inline] | ||
| fn lock(&self) { | ||
| unsafe { | ||
| let state = self.state.get(); | ||
| assert!( | ||
| (*state & LOCKED_BIT) == 0, | ||
| "cannot recursively acquire Mutex" | ||
| ); | ||
| *state = *state | LOCKED_BIT; | ||
| deadlock::acquire_resource(self as *const _ as usize); | ||
| }; | ||
| } | ||
|
|
||
| #[inline] | ||
| fn try_lock(&self) -> bool { | ||
| let state = self.state.get(); | ||
| unsafe { | ||
| if *state & LOCKED_BIT > 0 { | ||
| false | ||
| } else { | ||
| *state |= *state; | ||
| deadlock::acquire_resource(self as *const _ as usize); | ||
| true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[inline] | ||
| fn unlock(&self) { | ||
| unsafe { | ||
| deadlock::release_resource(self as *const _ as usize); | ||
| let state = self.state.get(); | ||
| *state &= !LOCKED_BIT; | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| unsafe impl RawMutexTimed for RawMutex { | ||
| type Duration = Duration; | ||
| type Instant = Instant; | ||
|
|
||
| #[inline] | ||
| fn try_lock_until(&self, _timeout: Instant) -> bool { | ||
| self.try_lock() | ||
| } | ||
|
|
||
| #[inline] | ||
| fn try_lock_for(&self, _timeout: Duration) -> bool { | ||
| self.try_lock() | ||
| } | ||
| } | ||
|
|
||
| unsafe impl RawMutexFair for RawMutex { | ||
| #[inline] | ||
| fn unlock_fair(&self) { | ||
| self.unlock() | ||
| } | ||
|
|
||
| #[inline] | ||
| fn bump(&self) {} | ||
| } | ||
|
|
||
| impl RawMutex { | ||
| // Used by Condvar when requeuing threads to us, must be called while | ||
| // holding the queue lock. | ||
| // false if unlocked | ||
| #[inline] | ||
| pub(crate) fn mark_parked_if_locked(&self) -> bool { | ||
| unsafe { | ||
| let state = self.state.get(); | ||
| if *state & LOCKED_BIT > 0 { | ||
| false | ||
| } else { | ||
| *state &= PARKED_BIT; | ||
| true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Used by Condvar when requeuing threads to us, must be called while | ||
| // holding the queue lock. | ||
| #[inline] | ||
| pub(crate) fn mark_parked(&self) { | ||
| unsafe { | ||
| *self.state.get() &= !PARKED_BIT; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
This wont work correctly when using the multi threading wasm extension.
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.
This is true, but neither would the original code without being explicitly designed for the new atomic primitives that aren’t even available on nightly, right? If it needs new code to work I’m not fussed about the exact way it happens to be incorrect now.
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.
Maybe add
Also atomic.notify and atomic.wait are exposed on nightly: https://doc.rust-lang.org/stable/core/arch/wasm32/index.html Normal atomics are accessable using the Atomic* types.
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 was referring mainly to having to compile libstd from source. Regardless, this is probably a good idea.