mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-15 15:41:26 -07:00
merge from upstream and convert to ^File types
This commit is contained in:
@@ -81,3 +81,16 @@ PTHREAD_MUTEX_NORMAL :: 0
|
||||
PTHREAD_MUTEX_RECURSIVE :: 1
|
||||
PTHREAD_MUTEX_ERRORCHECK :: 2
|
||||
|
||||
PTHREAD_CANCEL_ENABLE :: 0
|
||||
PTHREAD_CANCEL_DISABLE :: 1
|
||||
PTHREAD_CANCEL_DEFERRED :: 0
|
||||
PTHREAD_CANCEL_ASYNCHRONOUS :: 1
|
||||
|
||||
foreign import pthread "System.framework"
|
||||
|
||||
@(default_calling_convention="c")
|
||||
foreign pthread {
|
||||
pthread_setcancelstate :: proc (state: c.int, old_state: ^c.int) -> c.int ---
|
||||
pthread_setcanceltype :: proc (type: c.int, old_type: ^c.int) -> c.int ---
|
||||
pthread_cancel :: proc (thread: pthread_t) -> c.int ---
|
||||
}
|
||||
@@ -92,6 +92,11 @@ sem_t :: struct {
|
||||
_padding: u32,
|
||||
}
|
||||
|
||||
PTHREAD_CANCEL_ENABLE :: 0
|
||||
PTHREAD_CANCEL_DISABLE :: 1
|
||||
PTHREAD_CANCEL_DEFERRED :: 0
|
||||
PTHREAD_CANCEL_ASYNCHRONOUS :: 1
|
||||
|
||||
foreign import "system:pthread"
|
||||
|
||||
@(default_calling_convention="c")
|
||||
@@ -110,5 +115,8 @@ foreign pthread {
|
||||
// NOTE: unclear whether pthread_yield is well-supported on Linux systems,
|
||||
// see https://linux.die.net/man/3/pthread_yield
|
||||
pthread_yield :: proc() ---
|
||||
}
|
||||
|
||||
pthread_setcancelstate :: proc (state: c.int, old_state: ^c.int) -> c.int ---
|
||||
pthread_setcanceltype :: proc (type: c.int, old_type: ^c.int) -> c.int ---
|
||||
pthread_cancel :: proc (thread: pthread_t) -> c.int ---
|
||||
}
|
||||
@@ -94,6 +94,11 @@ when size_of(int) == 8 {
|
||||
SEM_T_SIZE :: 16
|
||||
}
|
||||
|
||||
PTHREAD_CANCEL_ENABLE :: 0
|
||||
PTHREAD_CANCEL_DISABLE :: 1
|
||||
PTHREAD_CANCEL_DEFERRED :: 0
|
||||
PTHREAD_CANCEL_ASYNCHRONOUS :: 1
|
||||
|
||||
foreign import "system:pthread"
|
||||
|
||||
@(default_calling_convention="c")
|
||||
@@ -112,4 +117,8 @@ foreign pthread {
|
||||
// NOTE: unclear whether pthread_yield is well-supported on Linux systems,
|
||||
// see https://linux.die.net/man/3/pthread_yield
|
||||
pthread_yield :: proc() -> c.int ---
|
||||
|
||||
pthread_setcancelstate :: proc (state: c.int, old_state: ^c.int) -> c.int ---
|
||||
pthread_setcanceltype :: proc (type: c.int, old_type: ^c.int) -> c.int ---
|
||||
pthread_cancel :: proc (thread: pthread_t) -> c.int ---
|
||||
}
|
||||
|
||||
@@ -46,6 +46,11 @@ sched_param :: struct {
|
||||
|
||||
sem_t :: distinct rawptr
|
||||
|
||||
PTHREAD_CANCEL_ENABLE :: 0
|
||||
PTHREAD_CANCEL_DISABLE :: 1
|
||||
PTHREAD_CANCEL_DEFERRED :: 0
|
||||
PTHREAD_CANCEL_ASYNCHRONOUS :: 1
|
||||
|
||||
foreign import libc "system:c"
|
||||
|
||||
@(default_calling_convention="c")
|
||||
@@ -62,4 +67,8 @@ foreign libc {
|
||||
// NOTE: unclear whether pthread_yield is well-supported on Linux systems,
|
||||
// see https://linux.die.net/man/3/pthread_yield
|
||||
pthread_yield :: proc() ---
|
||||
}
|
||||
|
||||
pthread_setcancelstate :: proc (state: c.int, old_state: ^c.int) -> c.int ---
|
||||
pthread_setcanceltype :: proc (type: c.int, old_type: ^c.int) -> c.int ---
|
||||
pthread_cancel :: proc (thread: pthread_t) -> c.int ---
|
||||
}
|
||||
@@ -4,7 +4,6 @@ package unix
|
||||
foreign import "system:pthread"
|
||||
|
||||
import "core:c"
|
||||
import "core:time"
|
||||
|
||||
//
|
||||
// On success, these functions return 0.
|
||||
@@ -72,7 +71,7 @@ foreign pthread {
|
||||
|
||||
// assumes the mutex is pre-locked
|
||||
pthread_cond_wait :: proc(cond: ^pthread_cond_t, mutex: ^pthread_mutex_t) -> c.int ---
|
||||
pthread_cond_timedwait :: proc(cond: ^pthread_cond_t, mutex: ^pthread_mutex_t, timeout: ^time.TimeSpec) -> c.int ---
|
||||
pthread_cond_timedwait :: proc(cond: ^pthread_cond_t, mutex: ^pthread_mutex_t, timeout: ^timespec) -> c.int ---
|
||||
|
||||
pthread_condattr_init :: proc(attrs: ^pthread_condattr_t) -> c.int ---
|
||||
pthread_condattr_destroy :: proc(attrs: ^pthread_condattr_t) -> c.int ---
|
||||
@@ -95,7 +94,7 @@ foreign pthread {
|
||||
|
||||
pthread_mutex_lock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
|
||||
|
||||
pthread_mutex_timedlock :: proc(mutex: ^pthread_mutex_t, timeout: ^time.TimeSpec) -> c.int ---
|
||||
pthread_mutex_timedlock :: proc(mutex: ^pthread_mutex_t, timeout: ^timespec) -> c.int ---
|
||||
|
||||
pthread_mutex_unlock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
|
||||
|
||||
|
||||
@@ -1114,7 +1114,7 @@ when ODIN_ARCH == .amd64 {
|
||||
SYS_landlock_add_rule : uintptr : 445
|
||||
SYS_landlock_restrict_self : uintptr : 446
|
||||
SYS_memfd_secret : uintptr : 447
|
||||
} else when ODIN_ARCH == .arm {
|
||||
} else when ODIN_ARCH == .arm32 { // TODO
|
||||
SYS_restart_syscall : uintptr : 0
|
||||
SYS_exit : uintptr : 1
|
||||
SYS_fork : uintptr : 2
|
||||
@@ -1567,7 +1567,7 @@ sys_gettid :: proc "contextless" () -> int {
|
||||
return cast(int)intrinsics.syscall(SYS_gettid)
|
||||
}
|
||||
|
||||
sys_getrandom :: proc "contextless" (buf: ^byte, buflen: int, flags: uint) -> int {
|
||||
sys_getrandom :: proc "contextless" (buf: [^]byte, buflen: int, flags: uint) -> int {
|
||||
return cast(int)intrinsics.syscall(SYS_getrandom, buf, cast(uintptr)(buflen), cast(uintptr)(flags))
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
//+build linux, darwin, freebsd, openbsd
|
||||
package unix
|
||||
|
||||
when ODIN_OS == .Darwin {
|
||||
foreign import libc "System.framework"
|
||||
} else {
|
||||
foreign import libc "system:c"
|
||||
}
|
||||
|
||||
import "core:c"
|
||||
|
||||
@(default_calling_convention="c")
|
||||
foreign libc {
|
||||
clock_gettime :: proc(clock_id: u64, timespec: ^timespec) -> c.int ---
|
||||
sleep :: proc(seconds: c.uint) -> c.int ---
|
||||
nanosleep :: proc(requested, remaining: ^timespec) -> c.int ---
|
||||
}
|
||||
|
||||
timespec :: struct {
|
||||
tv_sec: i64, // seconds
|
||||
tv_nsec: i64, // nanoseconds
|
||||
}
|
||||
|
||||
when ODIN_OS == .OpenBSD {
|
||||
CLOCK_REALTIME :: 0
|
||||
CLOCK_PROCESS_CPUTIME_ID :: 2
|
||||
CLOCK_MONOTONIC :: 3
|
||||
CLOCK_THREAD_CPUTIME_ID :: 4
|
||||
CLOCK_UPTIME :: 5
|
||||
CLOCK_BOOTTIME :: 6
|
||||
|
||||
// CLOCK_MONOTONIC_RAW doesn't exist, use CLOCK_MONOTONIC
|
||||
CLOCK_MONOTONIC_RAW :: CLOCK_MONOTONIC
|
||||
} else {
|
||||
CLOCK_REALTIME :: 0 // NOTE(tetra): May jump in time, when user changes the system time.
|
||||
CLOCK_MONOTONIC :: 1 // NOTE(tetra): May stand still while system is asleep.
|
||||
CLOCK_PROCESS_CPUTIME_ID :: 2
|
||||
CLOCK_THREAD_CPUTIME_ID :: 3
|
||||
CLOCK_MONOTONIC_RAW :: 4 // NOTE(tetra): "RAW" means: Not adjusted by NTP.
|
||||
CLOCK_REALTIME_COARSE :: 5 // NOTE(tetra): "COARSE" clocks are apparently much faster, but not "fine-grained."
|
||||
CLOCK_MONOTONIC_COARSE :: 6
|
||||
CLOCK_BOOTTIME :: 7 // NOTE(tetra): Same as MONOTONIC, except also including time system was asleep.
|
||||
CLOCK_REALTIME_ALARM :: 8
|
||||
CLOCK_BOOTTIME_ALARM :: 9
|
||||
}
|
||||
|
||||
// TODO(tetra, 2019-11-05): The original implementation of this package for Darwin used this constants.
|
||||
// I do not know if Darwin programmers are used to the existance of these constants or not, so
|
||||
// I'm leaving aliases to them for now.
|
||||
CLOCK_SYSTEM :: CLOCK_REALTIME
|
||||
CLOCK_CALENDAR :: CLOCK_MONOTONIC
|
||||
|
||||
boot_time_in_nanoseconds :: proc "c" () -> i64 {
|
||||
ts_now, ts_boottime: timespec
|
||||
clock_gettime(CLOCK_REALTIME, &ts_now)
|
||||
clock_gettime(CLOCK_BOOTTIME, &ts_boottime)
|
||||
|
||||
ns := (ts_now.tv_sec - ts_boottime.tv_sec) * 1e9 + ts_now.tv_nsec - ts_boottime.tv_nsec
|
||||
return i64(ns)
|
||||
}
|
||||
|
||||
seconds_since_boot :: proc "c" () -> f64 {
|
||||
ts_boottime: timespec
|
||||
clock_gettime(CLOCK_BOOTTIME, &ts_boottime)
|
||||
return f64(ts_boottime.tv_sec) + f64(ts_boottime.tv_nsec) / 1e9
|
||||
}
|
||||
|
||||
|
||||
inline_nanosleep :: proc "c" (nanoseconds: i64) -> (remaining: timespec, res: i32) {
|
||||
s, ns := nanoseconds / 1e9, nanoseconds % 1e9
|
||||
requested := timespec{tv_sec=s, tv_nsec=ns}
|
||||
res = nanosleep(&requested, &remaining)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user