@@ -28,11 +28,14 @@ impl Mutex {
28
28
29
29
pub unsafe fn lock ( & self ) {
30
30
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
+ } ;
36
39
// we should have either woke up (0) or got a not-equal due to a
37
40
// race (1). We should never time out (2)
38
41
debug_assert ! ( val == 0 || val == 1 ) ;
@@ -47,7 +50,7 @@ impl Mutex {
47
50
48
51
#[ inline]
49
52
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 ( ) }
51
54
}
52
55
53
56
#[ inline]
@@ -83,7 +86,7 @@ unsafe impl Sync for ReentrantMutex {}
83
86
84
87
impl ReentrantMutex {
85
88
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 ) } }
87
90
}
88
91
89
92
pub unsafe fn init ( & self ) {
@@ -93,19 +96,20 @@ impl ReentrantMutex {
93
96
pub unsafe fn lock ( & self ) {
94
97
let me = thread:: my_id ( ) ;
95
98
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 ) } ;
97
101
debug_assert ! ( val == 0 || val == 1 ) ;
98
102
}
99
103
}
100
104
101
105
#[ inline]
102
106
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 ( ) }
104
108
}
105
109
106
110
#[ inline]
107
111
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 ( ) ;
109
113
match self . owner . compare_exchange ( 0 , id, SeqCst , SeqCst ) {
110
114
// we transitioned from unlocked to locked
111
115
Ok ( _) => {
@@ -132,7 +136,10 @@ impl ReentrantMutex {
132
136
match * self . recursions . get ( ) {
133
137
0 => {
134
138
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
136
143
}
137
144
ref mut n => * n -= 1 ,
138
145
}
0 commit comments