Io module

with_deadline

procedure · 1 overloadSource

The one way to bound a wait. Limits every IO call (and sleep) in body to milliseconds from now: one that is still waiting then returns .TIMED_OUT, and so does a call made after the limit, even when data is ready. 0 is a limit that has already passed. Deadlines nest; the earlier one wins, and the previous deadline is back when body ends. A negative milliseconds adds no limit (NO_DEADLINE), so a timeout setting that may be off needs no second code path. Channel and lock operations are not IO and ignore it.

Example
Io :: #import "Extensions/Io";

main :: () {
    Io.io_run(() {
        client, server := Io.make_pipe();
        buffer: [8] u8;
        error: Io.Io_Error;

        Io.with_deadline(20, #code {
            _, error = Io.read(client, buffer);       // nothing is ever sent
        });

        assert(error == .TIMED_OUT);
    });
}

Doesn't run in the browser (skipped on wasm).

with_deadline :: (milliseconds: s64, body: Code)

The one way to bound a wait. Limits every IO call (and sleep) in body to milliseconds from now: one that is still waiting then returns .TIMED_OUT, and so does a call made after the limit, even when data is ready. 0 is a limit that has already passed. Deadlines nest; the earlier one wins, and the previous deadline is back when body ends. A negative milliseconds adds no limit (NO_DEADLINE), so a timeout setting that may be off needs no second code path. Channel and lock operations are not IO and ignore it.

Parameters

milliseconds: s64
body: Code
Example
Io :: #import "Extensions/Io";

main :: () {
    Io.io_run(() {
        client, server := Io.make_pipe();
        buffer: [8] u8;
        error: Io.Io_Error;

        Io.with_deadline(20, #code {
            _, error = Io.read(client, buffer);       // nothing is ever sent
        });

        assert(error == .TIMED_OUT);
    });
}

Doesn't run in the browser (skipped on wasm).

See also