add clock_gettime and time

This commit is contained in:
Marat Safin
2018-03-22 14:11:45 +03:00
parent 66fa211a05
commit 58b2b64183
8 changed files with 73 additions and 4 deletions
+4
View File
@@ -170,3 +170,7 @@ pub fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t {
pub fn write(fildes: c_int, buf: &[u8]) -> ssize_t {
e(unsafe { syscall!(WRITE, fildes, buf.as_ptr(), buf.len()) }) as ssize_t
}
pub fn clock_gettime(clk_id: clockid_t, tp: *mut timespec) -> c_int {
e(unsafe { syscall!(CLOCK_GETTIME, clk_id, tp) }) as c_int
}
+14
View File
@@ -220,3 +220,17 @@ pub fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t {
pub fn write(fd: c_int, buf: &[u8]) -> ssize_t {
e(syscall::write(fd as usize, buf)) as ssize_t
}
pub fn clock_gettime(clk_id: clockid_t, tp: *mut timespec) -> c_int {
let mut redox_tp = unsafe { redox_timespec::from(&*tp) };
match e(syscall::clock_gettime(clk_id as usize, &mut redox_tp)) as c_int {
-1 => -1,
_ => {
unsafe {
(*tp).tv_sec = redox_tp.tv_sec;
(*tp).tv_nsec = redox_tp.tv_nsec as i64;
};
0
}
}
}
+1
View File
@@ -68,6 +68,7 @@ pub type clockid_t = i32;
pub type timer_t = c_void;
#[repr(C)]
#[derive(Default)]
pub struct timespec {
pub tv_sec: time_t,
pub tv_nsec: c_long,
+4
View File
@@ -4,3 +4,7 @@ language = "C"
[enum]
prefix_with_name = true
[defines]
"target_os = linux" = "__linux__"
"target_os = redox" = "__redox__"
+36 -2
View File
@@ -6,6 +6,33 @@ extern crate platform;
use platform::types::*;
#[cfg(target_os = "redox")]
pub const CLOCK_REALTIME: c_int = 1;
#[cfg(target_os = "redox")]
pub const CLOCK_MONOTONIC: c_int = 4;
#[cfg(target_os = "linux")]
pub const CLOCK_REALTIME: c_int = 0;
#[cfg(target_os = "linux")]
pub const CLOCK_MONOTONIC: c_int = 1;
#[cfg(target_os = "linux")]
pub const CLOCK_PROCESS_CPUTIME_ID: c_int = 2;
#[cfg(target_os = "linux")]
pub const CLOCK_THREAD_CPUTIME_ID: c_int = 3;
#[cfg(target_os = "linux")]
pub const CLOCK_MONOTONIC_RAW: c_int = 4;
#[cfg(target_os = "linux")]
pub const CLOCK_REALTIME_COARSE: c_int = 5;
#[cfg(target_os = "linux")]
pub const CLOCK_MONOTONIC_COARSE: c_int = 6;
#[cfg(target_os = "linux")]
pub const CLOCK_BOOTTIME: c_int = 7;
#[cfg(target_os = "linux")]
pub const CLOCK_REALTIME_ALARM: c_int = 8;
#[cfg(target_os = "linux")]
pub const CLOCK_BOOTTIME_ALARM: c_int = 9;
#[cfg(target_os = "linux")]
pub const CLOCK_TAI: c_int = 11;
/*
*#[repr(C)]
*pub struct timespec {
@@ -59,7 +86,7 @@ pub extern "C" fn clock_getres(clock_id: clockid_t, res: *mut timespec) -> c_int
#[no_mangle]
pub extern "C" fn clock_gettime(clock_id: clockid_t, tp: *mut timespec) -> c_int {
unimplemented!();
platform::clock_gettime(clock_id, tp)
}
#[no_mangle]
@@ -134,7 +161,14 @@ pub extern "C" fn strptime(buf: *const c_char, format: *const c_char, tm: *mut t
#[no_mangle]
pub extern "C" fn time(tloc: *mut time_t) -> time_t {
unimplemented!();
let mut ts: timespec = Default::default();
platform::clock_gettime(CLOCK_REALTIME, &mut ts);
unsafe {
if !tloc.is_null() {
*tloc = ts.tv_sec
};
}
ts.tv_sec
}
#[no_mangle]
+1 -1
View File
@@ -47,4 +47,4 @@
/unlink
/waitpid
/write
/time
+2 -1
View File
@@ -41,7 +41,8 @@ EXPECT_BINS=\
unistd/getopt \
unlink \
waitpid \
write
write \
time
# Binaries that may generate varied output
BINS=\
+11
View File
@@ -0,0 +1,11 @@
#include <time.h>
#include <stdio.h>
int main(int argc, char** argv) {
timespec tm = {0, 0};
clock_gettime(CLOCK_REALTIME, &tm);
perror("clock_gettime");
time(NULL);
perror("time");
return 0;
}