1- use crate :: cell:: UnsafeCell ;
21use crate :: mem;
32use crate :: sync:: atomic:: { AtomicU32 , Ordering } ;
43use crate :: sys:: cloudabi:: abi;
@@ -12,37 +11,36 @@ extern "C" {
1211}
1312
1413pub struct Condvar {
15- condvar : UnsafeCell < AtomicU32 > ,
14+ condvar : AtomicU32 ,
1615}
1716
1817pub type MovableCondvar = Condvar ;
1918
2019unsafe impl Send for Condvar { }
2120unsafe impl Sync for Condvar { }
2221
23- const NEW : Condvar =
24- Condvar { condvar : UnsafeCell :: new ( AtomicU32 :: new ( abi:: CONDVAR_HAS_NO_WAITERS . 0 ) ) } ;
25-
2622impl Condvar {
2723 pub const fn new ( ) -> Condvar {
28- NEW
24+ Condvar { condvar : AtomicU32 :: new ( abi :: CONDVAR_HAS_NO_WAITERS . 0 ) }
2925 }
3026
3127 pub unsafe fn init ( & mut self ) { }
3228
3329 pub unsafe fn notify_one ( & self ) {
34- let condvar = self . condvar . get ( ) ;
35- if ( * condvar) . load ( Ordering :: Relaxed ) != abi:: CONDVAR_HAS_NO_WAITERS . 0 {
36- let ret = abi:: condvar_signal ( condvar as * mut abi:: condvar , abi:: scope:: PRIVATE , 1 ) ;
30+ if self . condvar . load ( Ordering :: Relaxed ) != abi:: CONDVAR_HAS_NO_WAITERS . 0 {
31+ let ret = abi:: condvar_signal (
32+ & self . condvar as * const AtomicU32 as * mut abi:: condvar ,
33+ abi:: scope:: PRIVATE ,
34+ 1 ,
35+ ) ;
3736 assert_eq ! ( ret, abi:: errno:: SUCCESS , "Failed to signal on condition variable" ) ;
3837 }
3938 }
4039
4140 pub unsafe fn notify_all ( & self ) {
42- let condvar = self . condvar . get ( ) ;
43- if ( * condvar) . load ( Ordering :: Relaxed ) != abi:: CONDVAR_HAS_NO_WAITERS . 0 {
41+ if self . condvar . load ( Ordering :: Relaxed ) != abi:: CONDVAR_HAS_NO_WAITERS . 0 {
4442 let ret = abi:: condvar_signal (
45- condvar as * mut abi:: condvar ,
43+ & self . condvar as * const AtomicU32 as * mut abi:: condvar ,
4644 abi:: scope:: PRIVATE ,
4745 abi:: nthreads:: MAX ,
4846 ) ;
@@ -53,20 +51,19 @@ impl Condvar {
5351 pub unsafe fn wait ( & self , mutex : & Mutex ) {
5452 let mutex = mutex:: raw ( mutex) ;
5553 assert_eq ! (
56- ( * mutex) . load( Ordering :: Relaxed ) & !abi:: LOCK_KERNEL_MANAGED . 0 ,
54+ mutex. load( Ordering :: Relaxed ) & !abi:: LOCK_KERNEL_MANAGED . 0 ,
5755 __pthread_thread_id. 0 | abi:: LOCK_WRLOCKED . 0 ,
5856 "This lock is not write-locked by this thread"
5957 ) ;
6058
6159 // Call into the kernel to wait on the condition variable.
62- let condvar = self . condvar . get ( ) ;
6360 let subscription = abi:: subscription {
6461 type_ : abi:: eventtype:: CONDVAR ,
6562 union : abi:: subscription_union {
6663 condvar : abi:: subscription_condvar {
67- condvar : condvar as * mut abi:: condvar ,
64+ condvar : & self . condvar as * const AtomicU32 as * mut abi:: condvar ,
6865 condvar_scope : abi:: scope:: PRIVATE ,
69- lock : mutex as * mut abi:: lock ,
66+ lock : mutex as * const AtomicU32 as * mut abi:: lock ,
7067 lock_scope : abi:: scope:: PRIVATE ,
7168 } ,
7269 } ,
@@ -86,23 +83,22 @@ impl Condvar {
8683 pub unsafe fn wait_timeout ( & self , mutex : & Mutex , dur : Duration ) -> bool {
8784 let mutex = mutex:: raw ( mutex) ;
8885 assert_eq ! (
89- ( * mutex) . load( Ordering :: Relaxed ) & !abi:: LOCK_KERNEL_MANAGED . 0 ,
86+ mutex. load( Ordering :: Relaxed ) & !abi:: LOCK_KERNEL_MANAGED . 0 ,
9087 __pthread_thread_id. 0 | abi:: LOCK_WRLOCKED . 0 ,
9188 "This lock is not write-locked by this thread"
9289 ) ;
9390
9491 // Call into the kernel to wait on the condition variable.
95- let condvar = self . condvar . get ( ) ;
9692 let timeout =
9793 checked_dur2intervals ( & dur) . expect ( "overflow converting duration to nanoseconds" ) ;
9894 let subscriptions = [
9995 abi:: subscription {
10096 type_ : abi:: eventtype:: CONDVAR ,
10197 union : abi:: subscription_union {
10298 condvar : abi:: subscription_condvar {
103- condvar : condvar as * mut abi:: condvar ,
99+ condvar : & self . condvar as * const AtomicU32 as * mut abi:: condvar ,
104100 condvar_scope : abi:: scope:: PRIVATE ,
105- lock : mutex as * mut abi:: lock ,
101+ lock : mutex as * const AtomicU32 as * mut abi:: lock ,
106102 lock_scope : abi:: scope:: PRIVATE ,
107103 } ,
108104 } ,
@@ -124,7 +120,7 @@ impl Condvar {
124120 let mut nevents: mem:: MaybeUninit < usize > = mem:: MaybeUninit :: uninit ( ) ;
125121 let ret = abi:: poll (
126122 subscriptions. as_ptr ( ) ,
127- mem:: MaybeUninit :: first_ptr_mut ( & mut events) ,
123+ mem:: MaybeUninit :: slice_as_mut_ptr ( & mut events) ,
128124 2 ,
129125 nevents. as_mut_ptr ( ) ,
130126 ) ;
@@ -144,9 +140,8 @@ impl Condvar {
144140 }
145141
146142 pub unsafe fn destroy ( & self ) {
147- let condvar = self . condvar . get ( ) ;
148143 assert_eq ! (
149- ( * condvar) . load( Ordering :: Relaxed ) ,
144+ self . condvar. load( Ordering :: Relaxed ) ,
150145 abi:: CONDVAR_HAS_NO_WAITERS . 0 ,
151146 "Attempted to destroy a condition variable with blocked threads"
152147 ) ;
0 commit comments