Module gstd::critical

source ·
Expand description

Critical hook that guarantees code section execution.

Hook is set on per-message basis.

Code is executed in handle_signal entry point in case of failure only across exec::wait() calls because hook has to be saved.

use gstd::{critical, msg};

// get source outside of critical hook
// because `gr_source` sys-call is forbidden inside `handle_signal` entry point
let source = msg::source();

critical::set_hook(move || {
    msg::send(source, "sends failed", 0).expect("Failed to send emergency message");
});

let msg = msg::send_for_reply(source, "send_for_reply", 0, 0)
    .expect("Failed to send message")
    // await on `MessageFuture` which calls `exec::wait()` inside
    // so program state will be saved and thus hook will too
    .await
    .expect("Received error reply");

// if some code fails (panic, out of gas, etc) after `exec::wait()` and friends
// then saved hook will be executed in `handle_signal`

// your code
// ...

Functions§