@@ -2,31 +2,25 @@ use crate::cell::UnsafeCell;
22use crate :: ptr;
33use crate :: sync:: atomic:: AtomicPtr ;
44use crate :: sync:: atomic:: Ordering :: Relaxed ;
5- use crate :: sys:: sync:: { Mutex , mutex } ;
5+ use crate :: sys:: sync:: { Mutex , OnceBox } ;
66#[ cfg( not( target_os = "nto" ) ) ]
77use crate :: sys:: time:: TIMESPEC_MAX ;
88#[ cfg( target_os = "nto" ) ]
99use crate :: sys:: time:: TIMESPEC_MAX_CAPPED ;
10- use crate :: sys_common:: lazy_box:: { LazyBox , LazyInit } ;
1110use crate :: time:: Duration ;
1211
1312struct AllocatedCondvar ( UnsafeCell < libc:: pthread_cond_t > ) ;
1413
1514pub struct Condvar {
16- inner : LazyBox < AllocatedCondvar > ,
15+ inner : OnceBox < AllocatedCondvar > ,
1716 mutex : AtomicPtr < libc:: pthread_mutex_t > ,
1817}
1918
20- #[ inline]
21- fn raw ( c : & Condvar ) -> * mut libc:: pthread_cond_t {
22- c. inner . 0 . get ( )
23- }
24-
2519unsafe impl Send for AllocatedCondvar { }
2620unsafe impl Sync for AllocatedCondvar { }
2721
28- impl LazyInit for AllocatedCondvar {
29- fn init ( ) -> Box < Self > {
22+ impl AllocatedCondvar {
23+ fn new ( ) -> Box < Self > {
3024 let condvar = Box :: new ( AllocatedCondvar ( UnsafeCell :: new ( libc:: PTHREAD_COND_INITIALIZER ) ) ) ;
3125
3226 cfg_if:: cfg_if! {
@@ -37,7 +31,7 @@ impl LazyInit for AllocatedCondvar {
3731 target_vendor = "apple" ,
3832 ) ) ] {
3933 // `pthread_condattr_setclock` is unfortunately not supported on these platforms.
40- } else if #[ cfg( any( target_os = "espidf" , target_os = "horizon" ) ) ] {
34+ } else if #[ cfg( any( target_os = "espidf" , target_os = "horizon" , target_os = "teeos" ) ) ] {
4135 // NOTE: ESP-IDF's PTHREAD_COND_INITIALIZER support is not released yet
4236 // So on that platform, init() should always be called
4337 // Moreover, that platform does not have pthread_condattr_setclock support,
@@ -82,7 +76,11 @@ impl Drop for AllocatedCondvar {
8276
8377impl Condvar {
8478 pub const fn new ( ) -> Condvar {
85- Condvar { inner : LazyBox :: new ( ) , mutex : AtomicPtr :: new ( ptr:: null_mut ( ) ) }
79+ Condvar { inner : OnceBox :: new ( ) , mutex : AtomicPtr :: new ( ptr:: null_mut ( ) ) }
80+ }
81+
82+ fn get ( & self ) -> * mut libc:: pthread_cond_t {
83+ self . inner . get_or_init ( AllocatedCondvar :: new) . 0 . get ( )
8684 }
8785
8886 #[ inline]
@@ -98,21 +96,21 @@ impl Condvar {
9896
9997 #[ inline]
10098 pub fn notify_one ( & self ) {
101- let r = unsafe { libc:: pthread_cond_signal ( raw ( self ) ) } ;
99+ let r = unsafe { libc:: pthread_cond_signal ( self . get ( ) ) } ;
102100 debug_assert_eq ! ( r, 0 ) ;
103101 }
104102
105103 #[ inline]
106104 pub fn notify_all ( & self ) {
107- let r = unsafe { libc:: pthread_cond_broadcast ( raw ( self ) ) } ;
105+ let r = unsafe { libc:: pthread_cond_broadcast ( self . get ( ) ) } ;
108106 debug_assert_eq ! ( r, 0 ) ;
109107 }
110108
111109 #[ inline]
112110 pub unsafe fn wait ( & self , mutex : & Mutex ) {
113- let mutex = mutex:: raw ( mutex ) ;
111+ let mutex = mutex. get_assert_locked ( ) ;
114112 self . verify ( mutex) ;
115- let r = libc:: pthread_cond_wait ( raw ( self ) , mutex) ;
113+ let r = libc:: pthread_cond_wait ( self . get ( ) , mutex) ;
116114 debug_assert_eq ! ( r, 0 ) ;
117115 }
118116
@@ -129,7 +127,7 @@ impl Condvar {
129127 pub unsafe fn wait_timeout ( & self , mutex : & Mutex , dur : Duration ) -> bool {
130128 use crate :: sys:: time:: Timespec ;
131129
132- let mutex = mutex:: raw ( mutex ) ;
130+ let mutex = mutex. get_assert_locked ( ) ;
133131 self . verify ( mutex) ;
134132
135133 #[ cfg( not( target_os = "nto" ) ) ]
@@ -144,7 +142,7 @@ impl Condvar {
144142 . and_then ( |t| t. to_timespec_capped ( ) )
145143 . unwrap_or ( TIMESPEC_MAX_CAPPED ) ;
146144
147- let r = libc:: pthread_cond_timedwait ( raw ( self ) , mutex, & timeout) ;
145+ let r = libc:: pthread_cond_timedwait ( self . get ( ) , mutex, & timeout) ;
148146 assert ! ( r == libc:: ETIMEDOUT || r == 0 ) ;
149147 r == 0
150148 }
@@ -162,7 +160,7 @@ impl Condvar {
162160 use crate :: sys:: time:: SystemTime ;
163161 use crate :: time:: Instant ;
164162
165- let mutex = mutex:: raw ( mutex ) ;
163+ let mutex = mutex. get_assert_locked ( ) ;
166164 self . verify ( mutex) ;
167165
168166 // OSX implementation of `pthread_cond_timedwait` is buggy
@@ -188,7 +186,7 @@ impl Condvar {
188186 . and_then ( |t| t. to_timespec ( ) )
189187 . unwrap_or ( TIMESPEC_MAX ) ;
190188
191- let r = libc:: pthread_cond_timedwait ( raw ( self ) , mutex, & timeout) ;
189+ let r = libc:: pthread_cond_timedwait ( self . get ( ) , mutex, & timeout) ;
192190 debug_assert ! ( r == libc:: ETIMEDOUT || r == 0 ) ;
193191
194192 // ETIMEDOUT is not a totally reliable method of determining timeout due
0 commit comments