Io module

with_grace

procedure · 1 overloadsince 0.27.0Source

Runs body clear of the running fiber's deadline and cancel, for cleanup that has to happen after one of them fired: telling a server to stop a query, a goodbye message. body has its own limit of milliseconds (negative: none), and with_deadline inside it nests as usual. When body ends, the outer deadline is back, and the fiber is canceled if it was before or a cancel came during body.

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

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

        Io.with_deadline(0, #code {
            _, error = Io.read(client, buffer);                // .TIMED_OUT: the deadline has passed
            Io.with_grace(1000, #code { sent, _ = Io.write(client, "stop"); });
        });

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

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

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

Runs body clear of the running fiber's deadline and cancel, for cleanup that has to happen after one of them fired: telling a server to stop a query, a goodbye message. body has its own limit of milliseconds (negative: none), and with_deadline inside it nests as usual. When body ends, the outer deadline is back, and the fiber is canceled if it was before or a cancel came during body.

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;
        sent: s64;

        Io.with_deadline(0, #code {
            _, error = Io.read(client, buffer);                // .TIMED_OUT: the deadline has passed
            Io.with_grace(1000, #code { sent, _ = Io.write(client, "stop"); });
        });

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

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

See also