File tree Expand file tree Collapse file tree 3 files changed +25
-9
lines changed
library/std/src/sys/hermit Expand file tree Collapse file tree 3 files changed +25
-9
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ use crate::ptr;
33use crate :: sync:: atomic:: { AtomicUsize , Ordering :: SeqCst } ;
44use crate :: sys:: hermit:: abi;
55use crate :: sys:: locks:: Mutex ;
6+ use crate :: sys_common:: lazy_box:: { LazyBox , LazyInit } ;
67use crate :: time:: Duration ;
78
89// The implementation is inspired by Andrew D. Birrell's paper
@@ -14,14 +15,26 @@ pub struct Condvar {
1415 sem2 : * const c_void ,
1516}
1617
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+ }
1825
1926unsafe impl Send for Condvar { }
2027unsafe impl Sync for Condvar { }
2128
2229impl 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
2538 }
2639
2740 pub unsafe fn notify_one ( & self ) {
Original file line number Diff line number Diff line change @@ -175,9 +175,7 @@ impl Mutex {
175175 }
176176
177177 #[ inline]
178- pub unsafe fn init ( & mut self ) {
179- self . inner = Spinlock :: new ( MutexInner :: new ( ) ) ;
180- }
178+ pub unsafe fn init ( & mut self ) { }
181179
182180 #[ inline]
183181 pub unsafe fn lock ( & self ) {
Original file line number Diff line number Diff line change 11use 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 } ;
34
45pub struct RwLock {
56 lock : Mutex ,
6- cond : Condvar ,
7+ cond : MovableCondvar ,
78 state : UnsafeCell < State > ,
89}
910
@@ -28,7 +29,11 @@ unsafe impl Sync for RwLock {}
2829
2930impl RwLock {
3031 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+ }
3237 }
3338
3439 #[ inline]
You can’t perform that action at this time.
0 commit comments