1- use crate :: cell:: UnsafeCell ;
21use crate :: mem;
32use crate :: sync:: atomic:: { AtomicU32 , Ordering } ;
43use crate :: sys:: cloudabi:: abi;
@@ -12,37 +11,32 @@ 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 ( & self . condvar as * const AtomicU32 as * mut abi:: condvar , abi:: scope:: PRIVATE , 1 ) ;
3732 assert_eq ! ( ret, abi:: errno:: SUCCESS , "Failed to signal on condition variable" ) ;
3833 }
3934 }
4035
4136 pub unsafe fn notify_all ( & self ) {
42- let condvar = self . condvar . get ( ) ;
43- if ( * condvar) . load ( Ordering :: Relaxed ) != abi:: CONDVAR_HAS_NO_WAITERS . 0 {
37+ if self . condvar . load ( Ordering :: Relaxed ) != abi:: CONDVAR_HAS_NO_WAITERS . 0 {
4438 let ret = abi:: condvar_signal (
45- condvar as * mut abi:: condvar ,
39+ & self . condvar as * const AtomicU32 as * mut abi:: condvar ,
4640 abi:: scope:: PRIVATE ,
4741 abi:: nthreads:: MAX ,
4842 ) ;
@@ -53,20 +47,19 @@ impl Condvar {
5347 pub unsafe fn wait ( & self , mutex : & Mutex ) {
5448 let mutex = mutex:: raw ( mutex) ;
5549 assert_eq ! (
56- ( * mutex) . load( Ordering :: Relaxed ) & !abi:: LOCK_KERNEL_MANAGED . 0 ,
50+ mutex. load( Ordering :: Relaxed ) & !abi:: LOCK_KERNEL_MANAGED . 0 ,
5751 __pthread_thread_id. 0 | abi:: LOCK_WRLOCKED . 0 ,
5852 "This lock is not write-locked by this thread"
5953 ) ;
6054
6155 // Call into the kernel to wait on the condition variable.
62- let condvar = self . condvar . get ( ) ;
6356 let subscription = abi:: subscription {
6457 type_ : abi:: eventtype:: CONDVAR ,
6558 union : abi:: subscription_union {
6659 condvar : abi:: subscription_condvar {
67- condvar : condvar as * mut abi:: condvar ,
60+ condvar : & self . condvar as * const AtomicU32 as * mut abi:: condvar ,
6861 condvar_scope : abi:: scope:: PRIVATE ,
69- lock : mutex as * mut abi:: lock ,
62+ lock : mutex as * const AtomicU32 as * mut abi:: lock ,
7063 lock_scope : abi:: scope:: PRIVATE ,
7164 } ,
7265 } ,
@@ -86,23 +79,22 @@ impl Condvar {
8679 pub unsafe fn wait_timeout ( & self , mutex : & Mutex , dur : Duration ) -> bool {
8780 let mutex = mutex:: raw ( mutex) ;
8881 assert_eq ! (
89- ( * mutex) . load( Ordering :: Relaxed ) & !abi:: LOCK_KERNEL_MANAGED . 0 ,
82+ mutex. load( Ordering :: Relaxed ) & !abi:: LOCK_KERNEL_MANAGED . 0 ,
9083 __pthread_thread_id. 0 | abi:: LOCK_WRLOCKED . 0 ,
9184 "This lock is not write-locked by this thread"
9285 ) ;
9386
9487 // Call into the kernel to wait on the condition variable.
95- let condvar = self . condvar . get ( ) ;
9688 let timeout =
9789 checked_dur2intervals ( & dur) . expect ( "overflow converting duration to nanoseconds" ) ;
9890 let subscriptions = [
9991 abi:: subscription {
10092 type_ : abi:: eventtype:: CONDVAR ,
10193 union : abi:: subscription_union {
10294 condvar : abi:: subscription_condvar {
103- condvar : condvar as * mut abi:: condvar ,
95+ condvar : & self . condvar as * const AtomicU32 as * mut abi:: condvar ,
10496 condvar_scope : abi:: scope:: PRIVATE ,
105- lock : mutex as * mut abi:: lock ,
97+ lock : mutex as * const AtomicU32 as * mut abi:: lock ,
10698 lock_scope : abi:: scope:: PRIVATE ,
10799 } ,
108100 } ,
@@ -144,9 +136,8 @@ impl Condvar {
144136 }
145137
146138 pub unsafe fn destroy ( & self ) {
147- let condvar = self . condvar . get ( ) ;
148139 assert_eq ! (
149- ( * condvar) . load( Ordering :: Relaxed ) ,
140+ self . condvar. load( Ordering :: Relaxed ) ,
150141 abi:: CONDVAR_HAS_NO_WAITERS . 0 ,
151142 "Attempted to destroy a condition variable with blocked threads"
152143 ) ;
0 commit comments