|
1 | 1 | use super::Mutex; |
2 | 2 | use crate::cell::UnsafeCell; |
3 | 3 | use crate::pin::Pin; |
| 4 | +#[cfg(not(target_os = "nto"))] |
| 5 | +use crate::sys::pal::time::TIMESPEC_MAX; |
| 6 | +#[cfg(target_os = "nto")] |
| 7 | +use crate::sys::pal::time::TIMESPEC_MAX_CAPPED; |
4 | 8 | use crate::time::Duration; |
5 | 9 |
|
6 | 10 | pub struct Condvar { |
@@ -51,10 +55,6 @@ impl Condvar { |
51 | 55 | /// * `mutex` must be locked by the current thread. |
52 | 56 | /// * This condition variable may only be used with the same mutex. |
53 | 57 | pub unsafe fn wait_timeout(&self, mutex: Pin<&Mutex>, dur: Duration) -> bool { |
54 | | - #[cfg(not(target_os = "nto"))] |
55 | | - use crate::sys::pal::time::TIMESPEC_MAX; |
56 | | - #[cfg(target_os = "nto")] |
57 | | - use crate::sys::pal::time::TIMESPEC_MAX_CAPPED; |
58 | 58 | use crate::sys::pal::time::Timespec; |
59 | 59 |
|
60 | 60 | let mutex = mutex.raw(); |
@@ -118,10 +118,12 @@ impl Condvar { |
118 | 118 |
|
119 | 119 | let (dur, clamped) = if dur <= MAX_DURATION { (dur, false) } else { (MAX_DURATION, true) }; |
120 | 120 |
|
121 | | - let timeout = libc::timespec { |
122 | | - // This cannot overflow because of the clamping above. |
123 | | - tv_sec: dur.as_secs() as i64, |
124 | | - tv_nsec: dur.subsec_nanos() as i64, |
| 121 | + // This can overflow on 32-bit platforms, but not on 64-bit because of the clamping above. |
| 122 | + let timeout = if let Ok(tv_sec) = dur.as_secs().try_into() { |
| 123 | + libc::timespec { tv_sec, tv_nsec: dur.subsec_nanos() as _ } |
| 124 | + } else { |
| 125 | + // This is less than `MAX_DURATION` on 32-bit platforms. |
| 126 | + TIMESPEC_MAX |
125 | 127 | }; |
126 | 128 |
|
127 | 129 | let r = unsafe { libc::pthread_cond_timedwait_relative_np(self.raw(), mutex, &timeout) }; |
|
0 commit comments