From d85893954dccc7833e3d954db641d9fd2a3c7451 Mon Sep 17 00:00:00 2001 From: Chris Heyes Date: Tue, 16 Jul 2019 22:27:58 +0100 Subject: Impl time for macOS --- core/os/os_osx.odin | 32 ++++++++++++++++++++++++++++++++ core/time/time_osx.odin | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/core/os/os_osx.odin b/core/os/os_osx.odin index 302cba76a..e0950dee1 100644 --- a/core/os/os_osx.odin +++ b/core/os/os_osx.odin @@ -126,6 +126,14 @@ W_OK :: 2; // Test for write permission X_OK :: 1; // Test for execute permission F_OK :: 0; // Test for file existance +TimeSpec :: struct { + tv_sec : i64, /* seconds */ + tv_nsec : i64, /* nanoseconds */ +}; + +CLOCK_SYSTEM :: 0; +CLOCK_CALENDAR :: 1; + foreign libc { @(link_name="open") _unix_open :: proc(path: cstring, flags: int, #c_vararg mode: ..any) -> Handle ---; @(link_name="close") _unix_close :: proc(handle: Handle) ---; @@ -142,6 +150,10 @@ foreign libc { @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---; @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---; + @(link_name="clock_gettime") _unix_clock_gettime :: proc(clock_id: u64, timespec: ^TimeSpec) ---; + @(link_name="nanosleep") _unix_nanosleep :: proc(requested: ^TimeSpec, remaining: ^TimeSpec) -> int ---; + @(link_name="sleep") _unix_sleep :: proc(seconds: u64) -> int ---; + @(link_name="exit") _unix_exit :: proc(status: int) ---; } @@ -259,6 +271,26 @@ exit :: inline proc(code: int) -> ! { } +clock_gettime :: proc(clock_id: u64) -> TimeSpec { + ts : TimeSpec; + _unix_clock_gettime(clock_id, &ts); + return ts; +} + +sleep :: proc(seconds: u64) -> int { + + return _unix_sleep(seconds); +} + +nanosleep :: proc(nanoseconds: i64) -> int { + assert(nanoseconds <= 999999999); + requested, remaining : TimeSpec; + requested = TimeSpec{tv_nsec = nanoseconds}; + + return _unix_nanosleep(&requested, &remaining); +} + + current_thread_id :: proc "contextless" () -> int { // return int(_unix_gettid()); return 0; diff --git a/core/time/time_osx.odin b/core/time/time_osx.odin index 7d1a1441c..fd499d1ce 100644 --- a/core/time/time_osx.odin +++ b/core/time/time_osx.odin @@ -1,3 +1,34 @@ package time -IS_SUPPORTED :: false; \ No newline at end of file +import "core:os"; + +IS_SUPPORTED :: true; + +now :: proc() -> Time { + + time_spec_now := os.clock_gettime(os.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 := os.clock_gettime(os.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 os.sleep(seconds); + if nanoseconds > 0 do os.nanosleep(nanoseconds); +} + +nanosleep :: proc(d: Duration) { + // NOTE(Jeroen): os.nanosleep returns -1 on failure, 0 on success + // duration needs to be [0, 999999999] nanoseconds. + os.nanosleep(i64(d)); +} -- cgit v1.2.3 From adca5b57cd91b6692274b186f1bd991e12d541a5 Mon Sep 17 00:00:00 2001 From: Chris Heyes Date: Fri, 1 Nov 2019 19:33:48 +0000 Subject: Move time code from os to time package --- core/os/os_darwin.odin | 33 --------------------------------- core/time/time_darwin.odin | 40 ++++++++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 43 deletions(-) diff --git a/core/os/os_darwin.odin b/core/os/os_darwin.odin index f48a547b6..77c455164 100644 --- a/core/os/os_darwin.odin +++ b/core/os/os_darwin.odin @@ -126,14 +126,6 @@ W_OK :: 2; // Test for write permission X_OK :: 1; // Test for execute permission F_OK :: 0; // Test for file existance -TimeSpec :: struct { - tv_sec : i64, /* seconds */ - tv_nsec : i64, /* nanoseconds */ -}; - -CLOCK_SYSTEM :: 0; -CLOCK_CALENDAR :: 1; - foreign libc { @(link_name="open") _unix_open :: proc(path: cstring, flags: int, #c_vararg mode: ..any) -> Handle ---; @(link_name="close") _unix_close :: proc(handle: Handle) ---; @@ -150,10 +142,6 @@ foreign libc { @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---; @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---; - @(link_name="clock_gettime") _unix_clock_gettime :: proc(clock_id: u64, timespec: ^TimeSpec) ---; - @(link_name="nanosleep") _unix_nanosleep :: proc(requested: ^TimeSpec, remaining: ^TimeSpec) -> int ---; - @(link_name="sleep") _unix_sleep :: proc(seconds: u64) -> int ---; - @(link_name="exit") _unix_exit :: proc(status: int) ---; } @@ -270,27 +258,6 @@ exit :: inline proc(code: int) -> ! { _unix_exit(code); } - -clock_gettime :: proc(clock_id: u64) -> TimeSpec { - ts : TimeSpec; - _unix_clock_gettime(clock_id, &ts); - return ts; -} - -sleep :: proc(seconds: u64) -> int { - - return _unix_sleep(seconds); -} - -nanosleep :: proc(nanoseconds: i64) -> int { - assert(nanoseconds <= 999999999); - requested, remaining : TimeSpec; - requested = TimeSpec{tv_nsec = nanoseconds}; - - return _unix_nanosleep(&requested, &remaining); -} - - 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 fd499d1ce..f731776b1 100644 --- a/core/time/time_darwin.odin +++ b/core/time/time_darwin.odin @@ -1,19 +1,37 @@ package time -import "core:os"; +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 := os.clock_gettime(os.CLOCK_SYSTEM); + 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 := os.clock_gettime(os.CLOCK_SYSTEM); + ts_boottime := clock_gettime(CLOCK_SYSTEM); return f64(ts_boottime.tv_sec) + f64(ts_boottime.tv_nsec) / 1e9; } @@ -23,12 +41,14 @@ sleep :: proc(d: Duration) { seconds := u64(ds); nanoseconds := i64((ds - f64(seconds)) * 1e9); - if seconds > 0 do os.sleep(seconds); - if nanoseconds > 0 do os.nanosleep(nanoseconds); + if seconds > 0 do _sleep(seconds); + if nanoseconds > 0 do nanosleep(nanoseconds); } -nanosleep :: proc(d: Duration) { - // NOTE(Jeroen): os.nanosleep returns -1 on failure, 0 on success - // duration needs to be [0, 999999999] nanoseconds. - os.nanosleep(i64(d)); -} +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 -- cgit v1.2.3 From 044e64beb002d11fa8e1b5c2d2153c3891b1bbd4 Mon Sep 17 00:00:00 2001 From: Chris Heyes Date: Fri, 1 Nov 2019 19:39:26 +0000 Subject: Add missing foreign import to time_darwin --- core/time/time_darwin.odin | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/time/time_darwin.odin b/core/time/time_darwin.odin index f731776b1..91acd8ae7 100644 --- a/core/time/time_darwin.odin +++ b/core/time/time_darwin.odin @@ -1,5 +1,7 @@ package time +foreign import libc "system:c" + TimeSpec :: struct { tv_sec : i64, /* seconds */ tv_nsec : i64, /* nanoseconds */ -- cgit v1.2.3