DoxigAlpha

sleep

Spurious wakeups are possible and no precision of timing is guaranteed.

Function parameters

Parameters

#
nanoseconds:u64

Type definitions in this namespace

Types

#
SpawnConfig
Configuration options for hints on how to spawn threads.

Spurious wakeups are possible and no precision of timing is guaranteed.

Functions

#
sleep
Spurious wakeups are possible and no precision of timing is guaranteed.
getName
On Windows, the result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
getCurrentId
Returns the platform ID of the callers thread.
getCpuCount
Returns the platforms view on the number of logical CPU cores available.
spawn
Spawns a new thread which executes `function` using `args` and returns a handle to the spawned thread.
getHandle
Returns the handle of this thread
detach
Release the obligation of the caller to call `join()` and have the thread clean up its own resources on completion.
join
Waits for the thread to complete, then deallocates any resources created on `spawn()`.
yield
Yields the current thread potentially allowing other threads to run.

Error sets in this namespace

Error Sets

#

= native_os != .windows and native_os != .wasi and builtin.link_libc

Values

#
use_pthreads
= native_os != .windows and native_os != .wasi and builtin.link_libc
max_name_len
= switch (native_os) { .linux => 15, .windows => 31, .macos, .ios, .watchos, .tvos, .visionos => 63, .netbsd => 31, .freebsd => 15, .openbsd => 23, .dragonfly => 1023, .solaris, .illumos => 31, // https://github.com/SerenityOS/serenity/blob/6b4c300353da49d3508b5442cf61da70bd04d757/Kernel/Tasks/Thread.h#L102 .serenity => 63, else => 0, }
Handle
Represents a kernel thread handle.

Source

Implementation

#
pub fn sleep(nanoseconds: u64) void {
    if (builtin.os.tag == .windows) {
        const big_ms_from_ns = nanoseconds / std.time.ns_per_ms;
        const ms = math.cast(windows.DWORD, big_ms_from_ns) orelse math.maxInt(windows.DWORD);
        windows.kernel32.Sleep(ms);
        return;
    }

    if (builtin.os.tag == .wasi) {
        const w = std.os.wasi;
        const userdata: w.userdata_t = 0x0123_45678;
        const clock: w.subscription_clock_t = .{
            .id = .MONOTONIC,
            .timeout = nanoseconds,
            .precision = 0,
            .flags = 0,
        };
        const in: w.subscription_t = .{
            .userdata = userdata,
            .u = .{
                .tag = .CLOCK,
                .u = .{ .clock = clock },
            },
        };

        var event: w.event_t = undefined;
        var nevents: usize = undefined;
        _ = w.poll_oneoff(&in, &event, 1, &nevents);
        return;
    }

    if (builtin.os.tag == .uefi) {
        const boot_services = std.os.uefi.system_table.boot_services.?;
        const us_from_ns = nanoseconds / std.time.ns_per_us;
        const us = math.cast(usize, us_from_ns) orelse math.maxInt(usize);
        boot_services.stall(us) catch unreachable;
        return;
    }

    const s = nanoseconds / std.time.ns_per_s;
    const ns = nanoseconds % std.time.ns_per_s;

    // Newer kernel ports don't have old `nanosleep()` and `clock_nanosleep()` has been around
    // since Linux 2.6 and glibc 2.1 anyway.
    if (builtin.os.tag == .linux) {
        const linux = std.os.linux;

        var req: linux.timespec = .{
            .sec = std.math.cast(linux.time_t, s) orelse std.math.maxInt(linux.time_t),
            .nsec = std.math.cast(linux.time_t, ns) orelse std.math.maxInt(linux.time_t),
        };
        var rem: linux.timespec = undefined;

        while (true) {
            switch (linux.E.init(linux.clock_nanosleep(.MONOTONIC, .{ .ABSTIME = false }, &req, &rem))) {
                .SUCCESS => return,
                .INTR => {
                    req = rem;
                    continue;
                },
                .FAULT => unreachable,
                .INVAL => unreachable,
                .OPNOTSUPP => unreachable,
                else => return,
            }
        }
    }

    posix.nanosleep(s, ns);
}