1
1
use crate :: cell:: UnsafeCell ;
2
2
use crate :: sys:: locks:: { pthread_mutex, Mutex } ;
3
+ use crate :: sys_common:: lazy_box:: { LazyBox , LazyInit } ;
3
4
use crate :: time:: Duration ;
4
5
5
6
pub struct Condvar {
6
7
inner : UnsafeCell < libc:: pthread_cond_t > ,
7
8
}
8
9
9
- pub type MovableCondvar = Box < Condvar > ;
10
+ pub ( crate ) type MovableCondvar = LazyBox < Condvar > ;
10
11
11
12
unsafe impl Send for Condvar { }
12
13
unsafe impl Sync for Condvar { }
@@ -18,6 +19,14 @@ fn saturating_cast_to_time_t(value: u64) -> libc::time_t {
18
19
if value > <libc:: time_t >:: MAX as u64 { <libc:: time_t >:: MAX } else { value as libc:: time_t }
19
20
}
20
21
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
+
21
30
impl Condvar {
22
31
pub const fn new ( ) -> Condvar {
23
32
// Might be moved and address is changing it is better to avoid
@@ -32,14 +41,14 @@ impl Condvar {
32
41
target_os = "android" ,
33
42
target_os = "redox"
34
43
) ) ]
35
- pub unsafe fn init ( & mut self ) { }
44
+ unsafe fn init ( & mut self ) { }
36
45
37
46
// NOTE: ESP-IDF's PTHREAD_COND_INITIALIZER support is not released yet
38
47
// So on that platform, init() should always be called
39
48
// Moreover, that platform does not have pthread_condattr_setclock support,
40
49
// hence that initialization should be skipped as well
41
50
#[ cfg( target_os = "espidf" ) ]
42
- pub unsafe fn init ( & mut self ) {
51
+ unsafe fn init ( & mut self ) {
43
52
let r = libc:: pthread_cond_init ( self . inner . get ( ) , crate :: ptr:: null ( ) ) ;
44
53
assert_eq ! ( r, 0 ) ;
45
54
}
@@ -52,7 +61,7 @@ impl Condvar {
52
61
target_os = "redox" ,
53
62
target_os = "espidf"
54
63
) ) ) ]
55
- pub unsafe fn init ( & mut self ) {
64
+ unsafe fn init ( & mut self ) {
56
65
use crate :: mem:: MaybeUninit ;
57
66
let mut attr = MaybeUninit :: < libc:: pthread_condattr_t > :: uninit ( ) ;
58
67
let r = libc:: pthread_condattr_init ( attr. as_mut_ptr ( ) ) ;
0 commit comments