File tree Expand file tree Collapse file tree 8 files changed +39
-26
lines changed Expand file tree Collapse file tree 8 files changed +39
-26
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,11 @@ use crate::ptr::null;
33use crate :: sync:: atomic:: AtomicU32 ;
44use crate :: time:: Duration ;
55
6+ /// An atomic for use as a futex that is at least 8-bits but may be larger.
7+ pub type SmallAtomic = AtomicU32 ;
8+ /// Must be the underlying type of SmallAtomic
9+ pub type SmallPrimitive = u32 ;
10+
611pub fn futex_wait ( futex : & AtomicU32 , expected : u32 , timeout : Option < Duration > ) -> bool {
712 // Calculate the timeout as a relative timespec.
813 //
Original file line number Diff line number Diff line change 1111use crate :: sync:: atomic:: AtomicU32 ;
1212use crate :: time:: Duration ;
1313
14+ /// An atomic for use as a futex that is at least 8-bits but may be larger.
15+ pub type SmallAtomic = AtomicU32 ;
16+ /// Must be the underlying type of SmallAtomic
17+ pub type SmallPrimitive = u32 ;
18+
1419/// Wait for a futex_wake operation to wake us.
1520///
1621/// Returns directly if the futex doesn't hold the expected value.
Original file line number Diff line number Diff line change @@ -6,6 +6,11 @@ use core::arch::wasm64 as wasm;
66use crate :: sync:: atomic:: AtomicU32 ;
77use crate :: time:: Duration ;
88
9+ /// An atomic for use as a futex that is at least 8-bits but may be larger.
10+ pub type SmallAtomic = AtomicU32 ;
11+ /// Must be the underlying type of SmallAtomic
12+ pub type SmallPrimitive = u32 ;
13+
914/// Wait for a futex_wake operation to wake us.
1015///
1116/// Returns directly if the futex doesn't hold the expected value.
Original file line number Diff line number Diff line change @@ -10,6 +10,11 @@ use core::sync::atomic::{
1010} ;
1111use core:: time:: Duration ;
1212
13+ /// An atomic for use as a futex that is at least 8-bits but may be larger.
14+ pub type SmallAtomic = AtomicU8 ;
15+ /// Must be the underlying type of SmallAtomic
16+ pub type SmallPrimitive = u8 ;
17+
1318pub unsafe trait Waitable {
1419 type Atomic ;
1520}
Original file line number Diff line number Diff line change 1- use crate :: sync:: atomic:: {
2- self ,
3- Ordering :: { Acquire , Relaxed , Release } ,
4- } ;
5- use crate :: sys:: futex:: { futex_wait, futex_wake} ;
6-
7- cfg_if:: cfg_if! {
8- if #[ cfg( windows) ] {
9- // On Windows we can have a smol futex
10- type Atomic = atomic:: AtomicU8 ;
11- type State = u8 ;
12- } else {
13- type Atomic = atomic:: AtomicU32 ;
14- type State = u32 ;
15- }
16- }
1+ use crate :: sync:: atomic:: Ordering :: { Acquire , Relaxed , Release } ;
2+ use crate :: sys:: futex:: { self , futex_wait, futex_wake} ;
3+
4+ type Atomic = futex:: SmallAtomic ;
5+ type State = futex:: SmallPrimitive ;
176
187pub struct Mutex {
198 futex : Atomic ,
Original file line number Diff line number Diff line change 1+ #![ forbid( unsafe_op_in_unsafe_fn) ]
12use crate :: pin:: Pin ;
2- use crate :: sync:: atomic:: AtomicU32 ;
33use crate :: sync:: atomic:: Ordering :: { Acquire , Release } ;
4- use crate :: sys:: futex:: { futex_wait, futex_wake} ;
4+ use crate :: sys:: futex:: { self , futex_wait, futex_wake} ;
55use crate :: time:: Duration ;
66
7- const PARKED : u32 = u32:: MAX ;
8- const EMPTY : u32 = 0 ;
9- const NOTIFIED : u32 = 1 ;
7+ type Atomic = futex:: SmallAtomic ;
8+ type State = futex:: SmallPrimitive ;
9+
10+ const PARKED : State = State :: MAX ;
11+ const EMPTY : State = 0 ;
12+ const NOTIFIED : State = 1 ;
1013
1114pub struct Parker {
12- state : AtomicU32 ,
15+ state : Atomic ,
1316}
1417
1518// Notes about memory ordering:
@@ -36,7 +39,7 @@ impl Parker {
3639 /// Construct the futex parker. The UNIX parker implementation
3740 /// requires this to happen in-place.
3841 pub unsafe fn new_in_place ( parker : * mut Parker ) {
39- parker. write ( Self { state : AtomicU32 :: new ( EMPTY ) } ) ;
42+ unsafe { parker. write ( Self { state : Atomic :: new ( EMPTY ) } ) } ;
4043 }
4144
4245 // Assumes this is only called by the thread that owns the Parker,
Original file line number Diff line number Diff line change 11cfg_if:: cfg_if! {
22 if #[ cfg( any(
3+ all( target_os = "windows" , not( target_vendor = "win7" ) ) ,
34 target_os = "linux" ,
45 target_os = "android" ,
56 all( target_arch = "wasm32" , target_feature = "atomics" ) ,
@@ -18,9 +19,9 @@ cfg_if::cfg_if! {
1819 ) ) ] {
1920 mod id;
2021 pub use id:: Parker ;
21- } else if #[ cfg( target_os = "windows " ) ] {
22- mod windows ;
23- pub use windows :: Parker ;
22+ } else if #[ cfg( target_vendor = "win7 " ) ] {
23+ mod windows7 ;
24+ pub use windows7 :: Parker ;
2425 } else if #[ cfg( all( target_vendor = "apple" , not( miri) ) ) ] {
2526 mod darwin;
2627 pub use darwin:: Parker ;
File renamed without changes.
You can’t perform that action at this time.
0 commit comments