diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2019-11-01 19:44:50 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-11-01 19:44:50 +0000 |
| commit | b5b085914aa7d8672111c9589cd535a8de647ee9 (patch) | |
| tree | e3d7f3a0306e35c98952852eebfdd9a1aa2aff50 | |
| parent | 3b898e5224872a507bd4a8e8be75c80643943d2a (diff) | |
| parent | 044e64beb002d11fa8e1b5c2d2153c3891b1bbd4 (diff) | |
Merge pull request #404 from hazeycode/master
Impl time for macOS
| -rw-r--r-- | core/os/os_darwin.odin | 1 | ||||
| -rw-r--r-- | core/time/time_darwin.odin | 55 |
2 files changed, 54 insertions, 2 deletions
diff --git a/core/os/os_darwin.odin b/core/os/os_darwin.odin index 7b183b3aa..77c455164 100644 --- a/core/os/os_darwin.odin +++ b/core/os/os_darwin.odin @@ -258,7 +258,6 @@ exit :: inline proc(code: int) -> ! { _unix_exit(code); } - current_thread_id :: proc "contextless" () -> int { // return int(_unix_gettid()); return 0; diff --git a/core/time/time_darwin.odin b/core/time/time_darwin.odin index 7d1a1441c..91acd8ae7 100644 --- a/core/time/time_darwin.odin +++ b/core/time/time_darwin.odin @@ -1,3 +1,56 @@ package time -IS_SUPPORTED :: false;
\ No newline at end of file +foreign import libc "system:c" + +TimeSpec :: struct { + tv_sec : i64, /* seconds */ + tv_nsec : i64, /* nanoseconds */ +}; + +CLOCK_SYSTEM :: 0; +CLOCK_CALENDAR :: 1; + +IS_SUPPORTED :: true; + +foreign libc { + @(link_name="clock_gettime") _clock_gettime :: proc(clock_id: u64, timespec: ^TimeSpec) ---; + @(link_name="nanosleep") _nanosleep :: proc(requested: ^TimeSpec, remaining: ^TimeSpec) -> int ---; + @(link_name="sleep") _sleep :: proc(seconds: u64) -> int ---; +} + +clock_gettime :: proc(clock_id: u64) -> TimeSpec { + ts : TimeSpec; + _clock_gettime(clock_id, &ts); + return ts; +} + +now :: proc() -> Time { + + time_spec_now := clock_gettime(CLOCK_SYSTEM); + ns := time_spec_now.tv_sec * 1e9 + time_spec_now.tv_nsec; + return Time{_nsec=ns}; +} + +seconds_since_boot :: proc() -> f64 { + + ts_boottime := clock_gettime(CLOCK_SYSTEM); + return f64(ts_boottime.tv_sec) + f64(ts_boottime.tv_nsec) / 1e9; +} + +sleep :: proc(d: Duration) { + + ds := duration_seconds(d); + seconds := u64(ds); + nanoseconds := i64((ds - f64(seconds)) * 1e9); + + if seconds > 0 do _sleep(seconds); + if nanoseconds > 0 do nanosleep(nanoseconds); +} + +nanosleep :: proc(nanoseconds: i64) -> int { + assert(nanoseconds <= 999999999); + requested, remaining : TimeSpec; + requested = TimeSpec{tv_nsec = nanoseconds}; + + return _nanosleep(&requested, &remaining); +}
\ No newline at end of file |