mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
Impl time for macOS
This commit is contained in:
@@ -126,6 +126,14 @@ W_OK :: 2; // Test for write permission
|
|||||||
X_OK :: 1; // Test for execute permission
|
X_OK :: 1; // Test for execute permission
|
||||||
F_OK :: 0; // Test for file existance
|
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 {
|
foreign libc {
|
||||||
@(link_name="open") _unix_open :: proc(path: cstring, flags: int, #c_vararg mode: ..any) -> Handle ---;
|
@(link_name="open") _unix_open :: proc(path: cstring, flags: int, #c_vararg mode: ..any) -> Handle ---;
|
||||||
@(link_name="close") _unix_close :: proc(handle: 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="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---;
|
||||||
@(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---;
|
@(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) ---;
|
@(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 {
|
current_thread_id :: proc "contextless" () -> int {
|
||||||
// return int(_unix_gettid());
|
// return int(_unix_gettid());
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+32
-1
@@ -1,3 +1,34 @@
|
|||||||
package time
|
package time
|
||||||
|
|
||||||
IS_SUPPORTED :: false;
|
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));
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user