File tree 1 file changed +32
-7
lines changed
library/std/src/sys/sync/condvar 1 file changed +32
-7
lines changed Original file line number Diff line number Diff line change
1
+ use crate :: cell:: RefCell ;
1
2
use crate :: sys:: sync:: Mutex ;
3
+ use crate :: thread:: sleep;
2
4
use crate :: time:: Duration ;
3
5
4
- pub struct Condvar { }
6
+ pub struct Condvar {
7
+ value : RefCell < u32 > ,
8
+ }
9
+
10
+ unsafe impl Send for Condvar { }
11
+ unsafe impl Sync for Condvar { } // no threads on this platform
5
12
6
13
impl Condvar {
7
14
#[ inline]
8
15
pub const fn new ( ) -> Condvar {
9
- Condvar { }
16
+ Condvar { value : RefCell :: new ( 0 ) }
10
17
}
11
18
12
19
#[ inline]
13
- pub fn notify_one ( & self ) { }
20
+ pub fn notify_one ( & self ) {
21
+ * self . value . borrow_mut ( ) += 1 ;
22
+ }
14
23
15
24
#[ inline]
16
- pub fn notify_all ( & self ) { }
25
+ pub fn notify_all ( & self ) {
26
+ * self . value . borrow_mut ( ) += 1 ;
27
+ }
17
28
18
29
pub unsafe fn wait ( & self , _mutex : & Mutex ) {
19
- panic ! ( "condvar wait not supported" )
30
+ let mut value = self . value . borrow_mut ( ) ;
31
+ if * value == 0 {
32
+ // Since the target does not have threads,
33
+ // no notification can ever arrive.
34
+ loop { }
35
+ } else {
36
+ * value -= 1 ;
37
+ }
20
38
}
21
39
22
- pub unsafe fn wait_timeout ( & self , _mutex : & Mutex , _dur : Duration ) -> bool {
23
- panic ! ( "condvar wait not supported" ) ;
40
+ pub unsafe fn wait_timeout ( & self , _mutex : & Mutex , dur : Duration ) -> bool {
41
+ let mut value = self . value . borrow_mut ( ) ;
42
+ if * value == 0 {
43
+ sleep ( dur) ;
44
+ false
45
+ } else {
46
+ * value -= 1 ;
47
+ true
48
+ }
24
49
}
25
50
}
You can’t perform that action at this time.
0 commit comments