]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/vxworks/condvar.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys / vxworks / condvar.rs
1 use crate::cell::UnsafeCell;
2 use crate::sys::mutex::{self, Mutex};
3 use crate::time::Duration;
4
5 pub struct Condvar {
6 inner: UnsafeCell<libc::pthread_cond_t>,
7 }
8
9 unsafe impl Send for Condvar {}
10 unsafe impl Sync for Condvar {}
11
12 const TIMESPEC_MAX: libc::timespec =
13 libc::timespec { tv_sec: <libc::time_t>::max_value(), tv_nsec: 1_000_000_000 - 1 };
14
15 fn saturating_cast_to_time_t(value: u64) -> libc::time_t {
16 if value > <libc::time_t>::max_value() as u64 {
17 <libc::time_t>::max_value()
18 } else {
19 value as libc::time_t
20 }
21 }
22
23 impl Condvar {
24 pub const fn new() -> Condvar {
25 // Might be moved and address is changing it is better to avoid
26 // initialization of potentially opaque OS data before it landed
27 Condvar { inner: UnsafeCell::new(libc::PTHREAD_COND_INITIALIZER) }
28 }
29
30 pub unsafe fn init(&mut self) {
31 use crate::mem::MaybeUninit;
32 let mut attr = MaybeUninit::<libc::pthread_condattr_t>::uninit();
33 let r = libc::pthread_condattr_init(attr.as_mut_ptr());
34 assert_eq!(r, 0);
35 let r = libc::pthread_condattr_setclock(attr.as_mut_ptr(), libc::CLOCK_MONOTONIC);
36 assert_eq!(r, 0);
37 let r = libc::pthread_cond_init(self.inner.get(), attr.as_ptr());
38 assert_eq!(r, 0);
39 let r = libc::pthread_condattr_destroy(attr.as_mut_ptr());
40 assert_eq!(r, 0);
41 }
42
43 #[inline]
44 pub unsafe fn notify_one(&self) {
45 let r = libc::pthread_cond_signal(self.inner.get());
46 debug_assert_eq!(r, 0);
47 }
48
49 #[inline]
50 pub unsafe fn notify_all(&self) {
51 let r = libc::pthread_cond_broadcast(self.inner.get());
52 debug_assert_eq!(r, 0);
53 }
54
55 #[inline]
56 pub unsafe fn wait(&self, mutex: &Mutex) {
57 let r = libc::pthread_cond_wait(self.inner.get(), mutex::raw(mutex));
58 debug_assert_eq!(r, 0);
59 }
60
61 // This implementation is used on systems that support pthread_condattr_setclock
62 // where we configure condition variable to use monotonic clock (instead of
63 // default system clock). This approach avoids all problems that result
64 // from changes made to the system time.
65 pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
66 use crate::mem;
67
68 let mut now: libc::timespec = mem::zeroed();
69 let r = libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut now);
70 assert_eq!(r, 0);
71
72 // Nanosecond calculations can't overflow because both values are below 1e9.
73 let nsec = dur.subsec_nanos() + now.tv_nsec as u32;
74
75 let sec = saturating_cast_to_time_t(dur.as_secs())
76 .checked_add((nsec / 1_000_000_000) as libc::time_t)
77 .and_then(|s| s.checked_add(now.tv_sec));
78 let nsec = nsec % 1_000_000_000;
79
80 let timeout =
81 sec.map(|s| libc::timespec { tv_sec: s, tv_nsec: nsec as _ }).unwrap_or(TIMESPEC_MAX);
82
83 let r = libc::pthread_cond_timedwait(self.inner.get(), mutex::raw(mutex), &timeout);
84 assert!(r == libc::ETIMEDOUT || r == 0);
85 r == 0
86 }
87
88 #[inline]
89 pub unsafe fn destroy(&self) {
90 let r = libc::pthread_cond_destroy(self.inner.get());
91 debug_assert_eq!(r, 0);
92 }
93 }