posix: add package

This commit is contained in:
Laytan Laats
2024-08-14 01:44:35 +02:00
parent ac68a9d52c
commit efe68c2e24
83 changed files with 12665 additions and 328 deletions
+12 -21
View File
@@ -5,22 +5,7 @@ package spall
// Only for types.
import "core:os"
when ODIN_OS == .Darwin {
foreign import libc "system:System.framework"
} else {
foreign import libc "system:c"
}
timespec :: struct {
tv_sec: i64, // seconds
tv_nsec: i64, // nanoseconds
}
foreign libc {
__error :: proc() -> ^i32 ---
@(link_name="write") _unix_write :: proc(handle: os.Handle, buffer: rawptr, count: uint) -> int ---
@(link_name="clock_gettime") _unix_clock_gettime :: proc(clock_id: u64, timespec: ^timespec) -> i32 ---
}
import "core:sys/posix"
MAX_RW :: 0x7fffffff
@@ -32,7 +17,7 @@ _write :: proc "contextless" (fd: os.Handle, data: []byte) -> (n: int, err: os.E
for n < len(data) {
chunk := data[:min(len(data), MAX_RW)]
written := _unix_write(fd, raw_data(chunk), len(chunk))
written := posix.write(posix.FD(fd), raw_data(chunk), len(chunk))
if written < 0 {
return n, os.get_last_error()
}
@@ -42,11 +27,17 @@ _write :: proc "contextless" (fd: os.Handle, data: []byte) -> (n: int, err: os.E
return n, nil
}
CLOCK_MONOTONIC_RAW :: 4 // NOTE(tetra): "RAW" means: Not adjusted by NTP.
// NOTE(tetra): "RAW" means: Not adjusted by NTP.
when ODIN_OS == .Darwin {
CLOCK :: posix.Clock(4) // CLOCK_MONOTONIC_RAW
} else {
// It looks like the BSDs don't have a CLOCK_MONOTONIC_RAW equivalent.
CLOCK :: posix.Clock.MONOTONIC
}
@(no_instrumentation)
_tick_now :: proc "contextless" () -> (ns: i64) {
t: timespec
_unix_clock_gettime(CLOCK_MONOTONIC_RAW, &t)
return t.tv_sec*1e9 + t.tv_nsec
t: posix.timespec
posix.clock_gettime(CLOCK, &t)
return i64(t.tv_sec)*1e9 + i64(t.tv_nsec)
}