File tree 3 files changed +25
-9
lines changed
library/std/src/sys/hermit
3 files changed +25
-9
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ use crate::ptr;
3
3
use crate :: sync:: atomic:: { AtomicUsize , Ordering :: SeqCst } ;
4
4
use crate :: sys:: hermit:: abi;
5
5
use crate :: sys:: locks:: Mutex ;
6
+ use crate :: sys_common:: lazy_box:: { LazyBox , LazyInit } ;
6
7
use crate :: time:: Duration ;
7
8
8
9
// The implementation is inspired by Andrew D. Birrell's paper
@@ -14,14 +15,26 @@ pub struct Condvar {
14
15
sem2 : * const c_void ,
15
16
}
16
17
17
- pub type MovableCondvar = Condvar ;
18
+ pub ( crate ) type MovableCondvar = LazyBox < Condvar > ;
19
+
20
+ impl LazyInit for Condvar {
21
+ fn init ( ) -> Box < Self > {
22
+ Box :: new ( Self :: new ( ) )
23
+ }
24
+ }
18
25
19
26
unsafe impl Send for Condvar { }
20
27
unsafe impl Sync for Condvar { }
21
28
22
29
impl Condvar {
23
- pub const fn new ( ) -> Condvar {
24
- Condvar { counter : AtomicUsize :: new ( 0 ) , sem1 : ptr:: null ( ) , sem2 : ptr:: null ( ) }
30
+ pub fn new ( ) -> Self {
31
+ let mut condvar =
32
+ Self { counter : AtomicUsize :: new ( 0 ) , sem1 : ptr:: null ( ) , sem2 : ptr:: null ( ) } ;
33
+ unsafe {
34
+ let _ = abi:: sem_init ( & mut condvar. sem1 , 0 ) ;
35
+ let _ = abi:: sem_init ( & mut condvar. sem2 , 0 ) ;
36
+ }
37
+ condvar
25
38
}
26
39
27
40
pub unsafe fn notify_one ( & self ) {
Original file line number Diff line number Diff line change @@ -175,9 +175,7 @@ impl Mutex {
175
175
}
176
176
177
177
#[ inline]
178
- pub unsafe fn init ( & mut self ) {
179
- self . inner = Spinlock :: new ( MutexInner :: new ( ) ) ;
180
- }
178
+ pub unsafe fn init ( & mut self ) { }
181
179
182
180
#[ inline]
183
181
pub unsafe fn lock ( & self ) {
Original file line number Diff line number Diff line change 1
1
use crate :: cell:: UnsafeCell ;
2
- use crate :: sys:: locks:: { Condvar , Mutex } ;
2
+ use crate :: sys:: locks:: { MovableCondvar , Mutex } ;
3
+ use crate :: sys_common:: lazy_box:: { LazyBox , LazyInit } ;
3
4
4
5
pub struct RwLock {
5
6
lock : Mutex ,
6
- cond : Condvar ,
7
+ cond : MovableCondvar ,
7
8
state : UnsafeCell < State > ,
8
9
}
9
10
@@ -28,7 +29,11 @@ unsafe impl Sync for RwLock {}
28
29
29
30
impl RwLock {
30
31
pub const fn new ( ) -> RwLock {
31
- RwLock { lock : Mutex :: new ( ) , cond : Condvar :: new ( ) , state : UnsafeCell :: new ( State :: Unlocked ) }
32
+ RwLock {
33
+ lock : Mutex :: new ( ) ,
34
+ cond : MovableCondvar :: new ( ) ,
35
+ state : UnsafeCell :: new ( State :: Unlocked ) ,
36
+ }
32
37
}
33
38
34
39
#[ inline]
You can’t perform that action at this time.
0 commit comments