Files
ziran/std/timer_linux.zi
waozi 2b49d83b60 Build on macOS again, fix the package and playground CI jobs
macOS: the self-hosted compiler backend and its file, process and timer
modules called Linux-only libc (__errno_location, pipe2, statx,
clock_nanosleep, sched_getaffinity, Linux open flags and clock ids), and
the checked bootstrap C was generated for Linux only. std/process_run_linux,
std/file_tree_linux, std/timer_linux and cmd/compiler_backend_process gain
Darwin branches (pipe + FD_CLOEXEC, __error, fstatat with the 64-bit-inode
record, nanosleep, CLOCK_MONOTONIC 6, Darwin open flags and sysconf name),
and open/fcntl take their mode as a variadic argument as Apple's arm64 ABI
requires. Darwin's $INODE64 symbols are resolved at run time, so one seed
serves Apple Silicon and Intel. bootstrap/darwin holds the Darwin copies of
the three platform modules, generated with the Darwin defines; the Makefile
selects them on a Darwin host, passes ZIRAN_DEFINES when generating those
modules, and check-bootstrap/update-bootstrap cover them.

Package jobs: check.yml ran the deleted test_native_package.sh wrapper; it
now runs scripts/test_native_package.zi directly.

Site: the browser playground linked the deleted cmd/zi2zx/zir_asm_lower.c
and missed ZxExecBuild's debug argument; it now compiles the checked Ziran
lowering from bootstrap/compiler_zir_lower.

The widget table generator measures native ZX, the default on Linux
x86-64 and AArch64, labels the column accordingly and records host load.
2026-11-16 00:25:27 -03:00

154 lines
5.6 KiB
Ziran

#if #defined(_WIN32) {
// Monotonic timing and bounded sleeps on 64-bit Windows.
#scope_file
kernel32 :: #system_library "kernel32";
CounterRaw :: (value: *s64) -> s32 #foreign kernel32 "QueryPerformanceCounter";
FrequencyRaw :: (value: *s64) -> s32 #foreign kernel32 "QueryPerformanceFrequency";
SleepRaw :: (milliseconds: u32) #foreign kernel32 "Sleep";
#scope_export
#program_export
MonotonicNanoseconds :: () -> s64 {
counter: s64
frequency: s64
if CounterRaw(*counter) == 0 || FrequencyRaw(*frequency) == 0 ||
counter < 0 || frequency <= 0 {
return -1
}
seconds := counter / frequency
// Scale only the fractional second, avoiding counter * 1e9 overflow.
fraction := cast(s64)(cast(float64)(counter % frequency) /
cast(float64)frequency * 1000000000.0)
if seconds > 9223372036 ||
seconds == 9223372036 && fraction > 854775807 {
return -1
}
return seconds * 1000000000 + fraction
}
#program_export
MonotonicMilliseconds :: () -> s64 {
counter: s64
frequency: s64
if CounterRaw(*counter) == 0 || FrequencyRaw(*frequency) == 0 ||
counter < 0 || frequency <= 0 {
return -1
}
seconds := counter / frequency
if seconds > 9223372036854775 {
return -1
}
fraction := cast(s64)(cast(float64)(counter % frequency) /
cast(float64)frequency * 1000.0)
if seconds == 9223372036854775 && fraction > 807 {
return -1
}
return seconds * 1000 + fraction
}
#program_export
SleepMilliseconds :: (milliseconds: s64) -> bool {
if milliseconds < 0 || milliseconds > 86400000 {
return false
}
SleepRaw(cast(u32)milliseconds)
return true
}
} else {
// Monotonic timing and interruptible sleeps for native glibc Linux programs.
// Wall-clock time is intentionally unavailable from this module.
libc :: #system_library "libc";
// struct timespec: time_t seconds and long nanoseconds. Both are the size
// of a pointer on Linux and Android, so 32-bit on armeabi-v7a; 64-bit fields
// read the clock as garbage there. Emscripten's time_t is 64-bit on wasm32
// while long stays 32-bit, so its timespec is 64-bit seconds, 32-bit
// nanoseconds and padding.
#if OS == .WEB {
ClockValue :: struct {
seconds: s64
nanoseconds: s32
padding: s32
}
} else {
ClockValue :: struct {
seconds: isize
nanoseconds: isize
}
}
ClockGetTimeRaw :: (clock: s32, value: *ClockValue) -> s32 #foreign libc "clock_gettime";
#if #defined(__APPLE__) {
// Darwin numbers CLOCK_MONOTONIC 6 and has no clock_nanosleep;
// nanosleep reports EINTR through errno instead of its result.
MONOTONIC_CLOCK :: 6;
SleepRaw :: (request: *ClockValue, remaining: *ClockValue) -> s32 #foreign libc "nanosleep";
ErrnoRaw :: () -> *s32 #foreign libc "__error";
ClockSleepRaw :: (clock: s32, flags: s32, request: *ClockValue, remaining: *ClockValue) -> s32 {
if SleepRaw(request, remaining) == 0 { return 0 }
return <<ErrnoRaw()
}
} else {
MONOTONIC_CLOCK :: 1;
ClockSleepRaw :: (clock: s32, flags: s32, request: *ClockValue,
remaining: *ClockValue) -> s32 #foreign libc "clock_nanosleep";
}
// Nanosecond resolution for profiling short operations. -1 reports a failed
// clock or a value outside the signed nanosecond range.
#program_export
MonotonicNanoseconds :: () -> s64 {
value: ClockValue
if ClockGetTimeRaw(MONOTONIC_CLOCK, *value) != 0 { return -1 }
seconds := cast(s64)value.seconds; nanoseconds := cast(s64)value.nanoseconds
if seconds < 0 || nanoseconds < 0 || nanoseconds > 999999999 ||
seconds > 9223372036 ||
(seconds == 9223372036 && nanoseconds > 854775807) { return -1 }
return seconds * 1000000000 + nanoseconds
}
#program_export
MonotonicMilliseconds :: () -> s64 {
value: [1]ClockValue
if ClockGetTimeRaw(MONOTONIC_CLOCK, *value[0]) != 0 { return -1 }
seconds := cast(s64)value[0].seconds; nanoseconds := cast(s64)value[0].nanoseconds
if nanoseconds < 0 || nanoseconds > 999999999 {
return -1
}
if seconds > 9223372036854775 || seconds < 0 {
return -1
}
return seconds * 1000 + nanoseconds / 1000000
}
MakeClockValue :: (milliseconds: s64) -> ClockValue {
value: ClockValue
if milliseconds < 0 { return value }
#if OS == .WEB {
value.seconds = milliseconds / 1000
value.nanoseconds = cast(s32)((milliseconds % 1000) * 1000000)
}
#if OS != .WEB {
value.seconds = cast(isize)(milliseconds / 1000)
value.nanoseconds = cast(isize)((milliseconds % 1000) * 1000000)
}
return value
}
#program_export
SleepMilliseconds :: (milliseconds: s64) -> bool {
if milliseconds < 0 || milliseconds > 86400000 { return false }
request: [1]ClockValue
remaining: [1]ClockValue
request[0] = MakeClockValue(milliseconds)
while request[0].seconds > 0 || request[0].nanoseconds > 0 {
status: s32 = ClockSleepRaw(MONOTONIC_CLOCK, 0, *request[0], *remaining[0])
if status == 0 { return true }
if status != 4 { return false }
request[0] = remaining[0]
}
return true
}
}