-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
std: Redesign Duration, implementing RFC 1040 #24920
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,14 +129,9 @@ impl Thread { | |
} | ||
|
||
pub fn sleep(dur: Duration) { | ||
if dur < Duration::zero() { | ||
return Thread::yield_now() | ||
} | ||
let seconds = dur.num_seconds(); | ||
let ns = dur - Duration::seconds(seconds); | ||
let mut ts = libc::timespec { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should exist There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately this can't be provided in liblibc because |
||
tv_sec: seconds as libc::time_t, | ||
tv_nsec: ns.num_nanoseconds().unwrap() as libc::c_long, | ||
tv_sec: dur.secs() as libc::time_t, | ||
tv_nsec: dur.extra_nanos() as libc::c_long, | ||
}; | ||
|
||
// If we're awoken with a signal then the return value will be -1 and | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ use libc; | |
use num::Zero; | ||
use os::windows::ffi::{OsStrExt, OsStringExt}; | ||
use path::PathBuf; | ||
use time::Duration; | ||
|
||
pub mod backtrace; | ||
pub mod c; | ||
|
@@ -151,6 +152,27 @@ fn cvt<I: PartialEq + Zero>(i: I) -> io::Result<I> { | |
} | ||
} | ||
|
||
fn dur2timeout(dur: Duration) -> libc::DWORD { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, this doesn't quite match any of the proposals floated in e.g. the timeout RFC (where zero was special-cased). I wonder if just rounding up would be simpler? We should discuss. |
||
// Note that a duration is a (u64, u32) (seconds, nanoseconds) pair, and the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hopefully nobody should have to take care of this themselves. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you elaborate a bit on this? I'm not quite sure what you're implying. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m implying that there should eventually exist a method to do such conversion on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes that definitely makes sense. We don't currently have much interoperation between low-level types and standard library types in terms of conversions like this, but I can definitely see this being a windows extension trait, for example. |
||
// timeouts in windows APIs are typically u32 milliseconds. To translate, we | ||
// have two pieces to take care of: | ||
// | ||
// * Nanosecond precision is rounded up | ||
// * Greater than u32::MAX milliseconds (50 days) is rounded up to INFINITE | ||
// (never time out). | ||
dur.secs().checked_mul(1000).and_then(|ms| { | ||
ms.checked_add((dur.extra_nanos() as u64) / 1_000_000) | ||
}).and_then(|ms| { | ||
ms.checked_add(if dur.extra_nanos() % 1_000_000 > 0 {1} else {0}) | ||
}).map(|ms| { | ||
if ms > <libc::DWORD>::max_value() as u64 { | ||
libc::INFINITE | ||
} else { | ||
ms as libc::DWORD | ||
} | ||
}).unwrap_or(libc::INFINITE) | ||
} | ||
|
||
fn ms_to_filetime(ms: u64) -> libc::FILETIME { | ||
// A FILETIME is a count of 100 nanosecond intervals, so we multiply by | ||
// 10000 b/c there are 10000 intervals in 1 ms | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes the behavior for a zero duration; intended?