Skip to content
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

Fix JitterRng on Mac OS #57

Merged
merged 1 commit into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ matrix:
rust: nightly

# Testing other channels
# Don't run nightly tests on darwin: JitterRng bench fails due to low-res timer
- env: TARGET=x86_64-unknown-linux-gnu NIGHTLY=1
rust: nightly
- env: TARGET=x86_64-apple-darwin NIGHTLY=1
os: osx
rust: nightly

before_install:
- set -e
Expand Down
16 changes: 14 additions & 2 deletions src/jitter_rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ impl JitterRng {
/// times. This measures the absolute worst-case, and gives a lower bound
/// for the available entropy.
///
/// ```no_run
/// ```rust,no_run
/// use rand::JitterRng;
///
/// # use std::error::Error;
Expand Down Expand Up @@ -670,7 +670,7 @@ impl JitterRng {
}
}

#[cfg(feature="std")]
#[cfg(all(feature="std", not(any(target_os = "macos", target_os = "ios"))))]
fn get_nstime() -> u64 {
use std::time::{SystemTime, UNIX_EPOCH};

Expand All @@ -682,6 +682,18 @@ fn get_nstime() -> u64 {
dur.as_secs() << 30 | dur.subsec_nanos() as u64
}

#[cfg(all(feature="std", any(target_os = "macos", target_os = "ios")))]
fn get_nstime() -> u64 {
extern crate libc;
// On Mac OS and iOS std::time::SystemTime only has 1000ns resolution.
// We use `mach_absolute_time` instead. This provides a CPU dependent unit,
// to get real nanoseconds the result should by multiplied by numer/denom
// from `mach_timebase_info`.
// But we are not interested in the exact nanoseconds, just entropy. So we
// use the raw result.
unsafe { libc::mach_absolute_time() }
}

// A function that is opaque to the optimizer to assist in avoiding dead-code
// elimination. Taken from `bencher`.
fn black_box<T>(dummy: T) -> T {
Expand Down