@@ -28,11 +28,14 @@ impl Mutex {
2828
2929 pub unsafe fn lock ( & self ) {
3030 while !self . try_lock ( ) {
31- let val = wasm32:: memory_atomic_wait32 (
32- self . ptr ( ) ,
33- 1 , // we expect our mutex is locked
34- -1 , // wait infinitely
35- ) ;
31+ // SAFETY: the caller must uphold the safety contract for `memory_atomic_wait32`.
32+ let val = unsafe {
33+ wasm32:: memory_atomic_wait32 (
34+ self . ptr ( ) ,
35+ 1 , // we expect our mutex is locked
36+ -1 , // wait infinitely
37+ )
38+ } ;
3639 // we should have either woke up (0) or got a not-equal due to a
3740 // race (1). We should never time out (2)
3841 debug_assert ! ( val == 0 || val == 1 ) ;
@@ -47,7 +50,7 @@ impl Mutex {
4750
4851 #[ inline]
4952 pub unsafe fn try_lock ( & self ) -> bool {
50- self . locked . compare_exchange ( 0 , 1 , SeqCst , SeqCst ) . is_ok ( )
53+ unsafe { self . locked . compare_exchange ( 0 , 1 , SeqCst , SeqCst ) . is_ok ( ) }
5154 }
5255
5356 #[ inline]
@@ -83,7 +86,7 @@ unsafe impl Sync for ReentrantMutex {}
8386
8487impl ReentrantMutex {
8588 pub const unsafe fn uninitialized ( ) -> ReentrantMutex {
86- ReentrantMutex { owner : AtomicU32 :: new ( 0 ) , recursions : UnsafeCell :: new ( 0 ) }
89+ unsafe { ReentrantMutex { owner : AtomicU32 :: new ( 0 ) , recursions : UnsafeCell :: new ( 0 ) } }
8790 }
8891
8992 pub unsafe fn init ( & self ) {
@@ -93,19 +96,20 @@ impl ReentrantMutex {
9396 pub unsafe fn lock ( & self ) {
9497 let me = thread:: my_id ( ) ;
9598 while let Err ( owner) = self . _try_lock ( me) {
96- let val = wasm32:: memory_atomic_wait32 ( self . ptr ( ) , owner as i32 , -1 ) ;
99+ // SAFETY: the caller must gurantee that `self.ptr()` and `owner` are valid i32.
100+ let val = unsafe { wasm32:: memory_atomic_wait32 ( self . ptr ( ) , owner as i32 , -1 ) } ;
97101 debug_assert ! ( val == 0 || val == 1 ) ;
98102 }
99103 }
100104
101105 #[ inline]
102106 pub unsafe fn try_lock ( & self ) -> bool {
103- self . _try_lock ( thread:: my_id ( ) ) . is_ok ( )
107+ unsafe { self . _try_lock ( thread:: my_id ( ) ) . is_ok ( ) }
104108 }
105109
106110 #[ inline]
107111 unsafe fn _try_lock ( & self , id : u32 ) -> Result < ( ) , u32 > {
108- let id = id. checked_add ( 1 ) . unwrap ( ) ; // make sure `id` isn't 0
112+ let id = id. checked_add ( 1 ) . unwrap ( ) ;
109113 match self . owner . compare_exchange ( 0 , id, SeqCst , SeqCst ) {
110114 // we transitioned from unlocked to locked
111115 Ok ( _) => {
@@ -132,7 +136,10 @@ impl ReentrantMutex {
132136 match * self . recursions . get ( ) {
133137 0 => {
134138 self . owner . swap ( 0 , SeqCst ) ;
135- wasm32:: memory_atomic_notify ( self . ptr ( ) as * mut i32 , 1 ) ; // wake up one waiter, if any
139+ // SAFETY: the caller must gurantee that `self.ptr()` is valid i32.
140+ unsafe {
141+ wasm32:: atomic_notify ( self . ptr ( ) as * mut i32 , 1 ) ;
142+ } // wake up one waiter, if any
136143 }
137144 ref mut n => * n -= 1 ,
138145 }
0 commit comments