time_unix.odin 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //+private
  2. //+build darwin, freebsd, openbsd, netbsd
  3. package time
  4. import "core:sys/posix"
  5. _IS_SUPPORTED :: true
  6. _now :: proc "contextless" () -> Time {
  7. time_spec_now: posix.timespec
  8. posix.clock_gettime(.REALTIME, &time_spec_now)
  9. ns := i64(time_spec_now.tv_sec) * 1e9 + time_spec_now.tv_nsec
  10. return Time{_nsec=ns}
  11. }
  12. _sleep :: proc "contextless" (d: Duration) {
  13. ds := duration_seconds(d)
  14. seconds := posix.time_t(ds)
  15. nanoseconds := i64((ds - f64(seconds)) * 1e9)
  16. ts := posix.timespec{
  17. tv_sec = seconds,
  18. tv_nsec = nanoseconds,
  19. }
  20. for {
  21. res := posix.nanosleep(&ts, &ts)
  22. if res == .OK || posix.errno() != .EINTR {
  23. break
  24. }
  25. }
  26. }
  27. when ODIN_OS == .Darwin {
  28. TICK_CLOCK :: posix.Clock(4) // CLOCK_MONOTONIC_RAW
  29. } else {
  30. // It looks like the BSDs don't have a CLOCK_MONOTONIC_RAW equivalent.
  31. TICK_CLOCK :: posix.Clock.MONOTONIC
  32. }
  33. _tick_now :: proc "contextless" () -> Tick {
  34. t: posix.timespec
  35. posix.clock_gettime(TICK_CLOCK, &t)
  36. return Tick{_nsec = i64(t.tv_sec)*1e9 + t.tv_nsec}
  37. }
  38. _yield :: proc "contextless" () {
  39. posix.sched_yield()
  40. }