time.rs 810 B

12345678910111213141516171819202122232425
  1. use syscall;
  2. use syscall::data::TimeSpec;
  3. use syscall::error::{Error, EFAULT};
  4. use libc::{c_int, c_void};
  5. use types::timeval;
  6. libc_fn!(unsafe clock_gettime(clk_id: c_int, tp: *mut TimeSpec) -> Result<c_int> {
  7. Ok(syscall::clock_gettime(clk_id as usize, &mut *tp)? as c_int)
  8. });
  9. libc_fn!(unsafe _gettimeofday(tv: *mut timeval, _tz: *const c_void) -> Result<c_int> {
  10. if !tv.is_null() {
  11. let mut tp = TimeSpec::default();
  12. syscall::clock_gettime(syscall::flag::CLOCK_REALTIME, &mut tp)?;
  13. (*tv).tv_sec = tp.tv_sec;
  14. (*tv).tv_usec = (tp.tv_nsec / 1000) as i64;
  15. Ok(0)
  16. } else {
  17. Err(Error::new(EFAULT))
  18. }
  19. });
  20. libc_fn!(unsafe nanosleep(req: *const TimeSpec, rem: *mut TimeSpec) -> Result<c_int> {
  21. Ok(syscall::nanosleep(&*req, &mut *rem)? as c_int)
  22. });