11use crate :: cell:: UnsafeCell ;
22use crate :: sys:: locks:: { pthread_mutex, Mutex } ;
3+ use crate :: sys_common:: lazy_box:: { LazyBox , LazyInit } ;
34use crate :: time:: Duration ;
45
56pub struct Condvar {
67 inner : UnsafeCell < libc:: pthread_cond_t > ,
78}
89
9- pub type MovableCondvar = Box < Condvar > ;
10+ pub ( crate ) type MovableCondvar = LazyBox < Condvar > ;
1011
1112unsafe impl Send for Condvar { }
1213unsafe impl Sync for Condvar { }
@@ -18,6 +19,14 @@ fn saturating_cast_to_time_t(value: u64) -> libc::time_t {
1819 if value > <libc:: time_t >:: MAX as u64 { <libc:: time_t >:: MAX } else { value as libc:: time_t }
1920}
2021
22+ impl LazyInit for Condvar {
23+ fn init ( ) -> Box < Self > {
24+ let mut condvar = Box :: new ( Self :: new ( ) ) ;
25+ unsafe { condvar. init ( ) } ;
26+ condvar
27+ }
28+ }
29+
2130impl Condvar {
2231 pub const fn new ( ) -> Condvar {
2332 // Might be moved and address is changing it is better to avoid
@@ -32,14 +41,14 @@ impl Condvar {
3241 target_os = "android" ,
3342 target_os = "redox"
3443 ) ) ]
35- pub unsafe fn init ( & mut self ) { }
44+ unsafe fn init ( & mut self ) { }
3645
3746 // NOTE: ESP-IDF's PTHREAD_COND_INITIALIZER support is not released yet
3847 // So on that platform, init() should always be called
3948 // Moreover, that platform does not have pthread_condattr_setclock support,
4049 // hence that initialization should be skipped as well
4150 #[ cfg( target_os = "espidf" ) ]
42- pub unsafe fn init ( & mut self ) {
51+ unsafe fn init ( & mut self ) {
4352 let r = libc:: pthread_cond_init ( self . inner . get ( ) , crate :: ptr:: null ( ) ) ;
4453 assert_eq ! ( r, 0 ) ;
4554 }
@@ -52,7 +61,7 @@ impl Condvar {
5261 target_os = "redox" ,
5362 target_os = "espidf"
5463 ) ) ) ]
55- pub unsafe fn init ( & mut self ) {
64+ unsafe fn init ( & mut self ) {
5665 use crate :: mem:: MaybeUninit ;
5766 let mut attr = MaybeUninit :: < libc:: pthread_condattr_t > :: uninit ( ) ;
5867 let r = libc:: pthread_condattr_init ( attr. as_mut_ptr ( ) ) ;
0 commit comments