Skip to content

Commit

Permalink
keep base time instant reference in z_clock_t
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Feb 29, 2024
1 parent 6561fdc commit 3ffe9ca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
50 changes: 25 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ typedef struct z_owned_bytes_map_t {
*/
typedef struct z_clock_t {
uint64_t t;
const void *t_base;
} z_clock_t;
/**
* Represents a Zenoh ID.
Expand Down
7 changes: 6 additions & 1 deletion src/platform/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use chrono::{DateTime, Local};
use libc::c_char;
use std::{
cmp::min,
os::raw::c_void,
slice,
time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};
Expand All @@ -22,20 +23,24 @@ lazy_static! {
#[derive(Clone, Copy)]
pub struct z_clock_t {
t: u64,
t_base: *const c_void,
}

#[no_mangle]
pub extern "C" fn z_clock_now() -> z_clock_t {
z_clock_t {
t: CLOCK_BASE.elapsed().as_nanos() as u64,
t_base: &CLOCK_BASE as *const CLOCK_BASE as *const c_void,
}
}
#[allow(clippy::missing_safety_doc)]
unsafe fn get_elapsed_nanos(time: *const z_clock_t) -> u64 {
if time.is_null() {
return 0;
}
let now_t = z_clock_now().t;
let now_t = (*((*time).t_base as *const CLOCK_BASE))
.elapsed()
.as_nanos() as u64;
if now_t > (*time).t {
now_t - (*time).t
} else {
Expand Down

0 comments on commit 3ffe9ca

Please sign in to comment.