| 1 |
// GStreamer |
| 2 |
// |
| 3 |
// Copyright (C) 2015-2023 Sebastian Dröge <[email protected]> |
| 4 |
// |
| 5 |
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0. |
| 6 |
// If a copy of the MPL was not distributed with this file, You can obtain one at |
| 7 |
// <https://mozilla.org/MPL/2.0/>. |
| 8 |
// |
| 9 |
// SPDX-License-Identifier: MPL-2.0 |
| 10 |
|
| 11 |
#[cfg(target_os = "macos")] |
| 12 |
/// Returns the monotonic system clock in nanoseconds or 0 on error. |
| 13 |
pub fn time() -> u64 { |
| 14 |
use std::{ |
| 15 |
mem, |
| 16 |
sync::atomic::{self, AtomicU32}, |
| 17 |
}; |
| 18 |
|
| 19 |
use crate::ffi::unix::macos::*; |
| 20 |
|
| 21 |
static TIMEBASE_N: AtomicU32 = AtomicU32::new(0); |
| 22 |
static TIMEBASE_D: AtomicU32 = AtomicU32::new(0); |
| 23 |
|
| 24 |
let mut timebase_n = TIMEBASE_N.load(atomic::Ordering::Relaxed); |
| 25 |
let mut timebase_d = TIMEBASE_D.load(atomic::Ordering::Relaxed); |
| 26 |
if timebase_n == 0 || timebase_d == 0 { |
| 27 |
// SAFETY: This is safe to call at any time and returns the timebase. Returns 0 on success. |
| 28 |
unsafe { |
| 29 |
let mut timebase = mem::MaybeUninit::uninit(); |
| 30 |
if mach_timebase_info(timebase.as_mut_ptr()) != 0 { |
| 31 |
return 0; |
| 32 |
} |
| 33 |
let timebase = timebase.assume_init(); |
| 34 |
timebase_n = timebase.numer; |
| 35 |
timebase_d = timebase.denom; |
| 36 |
|
| 37 |
TIMEBASE_N.store(timebase_n, atomic::Ordering::Relaxed); |
| 38 |
TIMEBASE_D.store(timebase_d, atomic::Ordering::Relaxed); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// SAFETY: This is safe to call at any time. |
| 43 |
let time = unsafe { mach_absolute_time() }; |
| 44 |
|
| 45 |
(time as u128 * timebase_n as u128 / timebase_d as u128) as u64 |
| 46 |
} |
| 47 |
|
| 48 |
#[cfg(target_os = "windows")] |
| 49 |
/// Returns the monotonic system clock in nanoseconds or 0 on error. |
| 50 |
pub fn time() -> u64 { |
| 51 |
use std::{ |
| 52 |
mem, |
| 53 |
sync::atomic::{self, AtomicI64}, |
| 54 |
}; |
| 55 |
|
| 56 |
use crate::ffi::windows::*; |
| 57 |
|
| 58 |
static FREQUENCY: AtomicI64 = AtomicI64::new(0); |
| 59 |
|
| 60 |
let mut freq = FREQUENCY.load(atomic::Ordering::Relaxed); |
| 61 |
if freq == 0 { |
| 62 |
// SAFETY: This is safe to call at any time and will never fail on Windows XP or newer. |
| 63 |
unsafe { |
| 64 |
QueryPerformanceFrequency(&mut freq); |
| 65 |
} |
| 66 |
FREQUENCY.store(freq, atomic::Ordering::Relaxed); |
| 67 |
} |
| 68 |
|
| 69 |
// SAFETY: This is safe to call at any time and will never fail on Windows XP or newer. |
| 70 |
let time = unsafe { |
| 71 |
let mut time = mem::MaybeUninit::uninit(); |
| 72 |
QueryPerformanceCounter(time.as_mut_ptr()); |
| 73 |
time.assume_init() |
| 74 |
}; |
| 75 |
|
| 76 |
(time as u128 * 1_000_000_000 / freq as u128) as u64 |
| 77 |
} |
| 78 |
|
| 79 |
#[cfg(any( |
| 80 |
target_os = "linux", |
| 81 |
target_os = "freebsd", |
| 82 |
target_os = "openbsd", |
| 83 |
target_os = "netbsd", |
| 84 |
target_os = "dragonfly", |
| 85 |
target_os = "solaris", |
| 86 |
target_os = "illumos", |
| 87 |
target_os = "hurd", |
| 88 |
))] |
| 89 |
/// Returns the monotonic system clock in nanoseconds or 0 on error. |
| 90 |
pub fn time() -> u64 { |
| 91 |
use std::mem; |
| 92 |
|
| 93 |
use crate::ffi::unix::clock_gettime::*; |
| 94 |
|
| 95 |
// SAFETY: This is safe to call at any time. 0 will be returned on success, any other value on |
| 96 |
// error. |
| 97 |
unsafe { |
| 98 |
let mut timespec = mem::MaybeUninit::uninit(); |
| 99 |
let res = clock_gettime(CLOCK_MONOTONIC, timespec.as_mut_ptr()); |
| 100 |
if res == 0 { |
| 101 |
let timespec = timespec.assume_init(); |
| 102 |
|
| 103 |
timespec.tv_sec as u64 * 1_000_000_000 + timespec.tv_nsec as u64 |
| 104 |
} else { |
| 105 |
0 |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#[cfg(test)] |
| 111 |
mod test { |
| 112 |
#[test] |
| 113 |
fn test_clock() { |
| 114 |
// Check that this doesn't return 0 as that's very likely an indication of things going |
| 115 |
// wrong. |
| 116 |
let now = super::time(); |
| 117 |
assert_ne!(now, 0); |
| 118 |
} |
| 119 |
} |