Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › Io › Threaded › clockResolution Private Function clockResolution Doc Source Private Function std.Io.Threaded.clockResolution
fn clockResolution (userdata : ?*anyopaque , clock : Io .Clock ) Io .Clock .ResolutionError !Io .Duration Params userdata : ?*anyopaque clock : Io .Clock File Io/Threaded.zig: 11727
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765 fn clockResolution (userdata : ?*anyopaque , clock : Io .Clock ) Io .Clock .ResolutionError !Io .Duration {
const t : *Threaded = @ptrCast (@alignCast (userdata ));
_ = t ;
return switch (native_os ) {
.windows => switch (clock ) {
.awake , .boot , .real => {
// We don't need to cache QPF as it's internally just a memory read to KUSER_SHARED_DATA
// (a read-only page of info updated and mapped by the kernel to all processes):
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntddk/ns-ntddk-kuser_shared_data
// https://www.geoffchappell.com/studies/windows/km/ntoskrnl/inc/api/ntexapi_x/kuser_shared_data/index.htm
var qpf : windows .LARGE_INTEGER = undefined ;
if (!windows .ntdll .RtlQueryPerformanceFrequency (&qpf ).toBool ()) {
recoverableOsBugDetected ();
return .zero ;
}
// 10Mhz (1 qpc tick every 100ns) is a common enough QPF value that we can optimize on it.
// https://github.com/microsoft/STL/blob/785143a0c73f030238ef618890fd4d6ae2b3a3a0/stl/inc/chrono#L694-L701
const common_qpf = 10_000_000 ;
if (qpf == common_qpf ) return .fromNanoseconds (std .time .ns_per_s / common_qpf );
// Convert to ns using fixed point.
const scale = @as (u64 , std .time .ns_per_s << 32 ) / @as (u32 , @intCast (qpf ));
const result = scale >> 32 ;
return .fromNanoseconds (result );
},
.cpu_process , .cpu_thread => return error .ClockUnavailable ,
},
.wasi => {
if (builtin .link_libc ) return clockResolutionPosix (clock );
var ns : std .os .wasi .timestamp_t = undefined ;
return switch (std .os .wasi .clock_res_get (clockToWasi (clock ), &ns )) {
.SUCCESS => .fromNanoseconds (ns ),
.INVAL => return error .ClockUnavailable ,
else => |err | return posix .unexpectedErrno (err ),
};
},
else => return clockResolutionPosix (clock ),
};
}