Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gstd): Introduce critical hook #3503

Merged
merged 41 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
37c0a25
Initial commit
ark0f Nov 10, 2023
e6ed3af
Move `critical.rs` to root of `gstd`
ark0f Nov 17, 2023
af4f5b1
Update doc
ark0f Nov 17, 2023
106a722
Replace BTreeMap with HashMap
ark0f Nov 18, 2023
6d44bb1
Fix `execute_all` doc
ark0f Nov 18, 2023
55f3869
Fix module doc
ark0f Nov 18, 2023
fdd387c
Rename function in module docs
ark0f Nov 19, 2023
7af6af0
Redesign
ark0f Nov 28, 2023
5e35803
Move `pin-project` dep to workspace deps
ark0f Nov 28, 2023
e620f56
Merge remote-tracking branch 'origin/master' into al/critical-section
ark0f Nov 28, 2023
41eb5f7
Fix docs
ark0f Nov 28, 2023
d9088f2
Format docs
ark0f Nov 28, 2023
8a4a44d
Per-call sections
ark0f Nov 28, 2023
70256d4
Use `async-runtime` code style
ark0f Nov 28, 2023
97c28be
Fix test
ark0f Nov 28, 2023
57d0747
Fix module doc
ark0f Nov 29, 2023
4f168b2
Register section during `.await`
ark0f Nov 29, 2023
97e7843
Update docs
ark0f Nov 29, 2023
865867d
Redesign
ark0f Nov 30, 2023
3bc7a2b
Update doc
ark0f Nov 30, 2023
e05ccd8
Remove `pin-project` dep
ark0f Nov 30, 2023
eca363f
Fix module doc
ark0f Nov 30, 2023
1578713
Docs and move static to critical.rs
ark0f Nov 30, 2023
331d167
Remove obsolete doc
ark0f Nov 30, 2023
079de67
Update module doc
ark0f Nov 30, 2023
ee11e31
Merge remote-tracking branch 'origin/master' into al/critical-section
ark0f Dec 1, 2023
f4d22a1
Remove async init
ark0f Dec 4, 2023
b35aa9e
Check dispatch status of message in test
ark0f Dec 4, 2023
5bced2b
Remove hook reset test
ark0f Dec 11, 2023
0548c10
Merge remote-tracking branch 'origin/master' into al/critical-section
ark0f Dec 11, 2023
0cb1459
Add TODO
ark0f Dec 11, 2023
cb96641
Forbid `set_hook` in `handle_signal` and `handle_reply`
ark0f Dec 11, 2023
f7b8566
Rename tests
ark0f Dec 11, 2023
0cfe986
Remove hook on successful execution
ark0f Dec 11, 2023
19cbd17
Update module doc
ark0f Dec 11, 2023
18f3b92
Fix missing `mut` attr
ark0f Dec 11, 2023
948e15a
Update docs
ark0f Dec 11, 2023
80b95a9
Fix `take_hook` wrong call
ark0f Dec 11, 2023
53823ee
Merge remote-tracking branch 'origin/master' into al/critical-section
ark0f Jan 10, 2024
3876e34
Don't call `gr_source` in other entry points
ark0f Jan 10, 2024
56757cf
Remove accidental symbol
ark0f Jan 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
"core-processor",
"core-errors",
"examples/async",
"examples/async-critical",
"examples/async-custom-entry",
"examples/async-init",
"examples/async-signal-entry",
Expand Down Expand Up @@ -367,6 +368,7 @@ try-runtime-cli = { version = "0.10.0-dev", git = "https://github.com/gear-tech/
# Examples
test-syscalls = { path = "examples/syscalls", default-features = false }
demo-async = { path = "examples/async" }
demo-async-critical = { path = "examples/async-critical" }
demo-async-custom-entry = { path = "examples/async-custom-entry" }
demo-async-init = { path = "examples/async-init" }
demo-async-recursion = { path = "examples/async-recursion" }
Expand Down
21 changes: 21 additions & 0 deletions examples/async-critical/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "demo-async-critical"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
gstd.workspace = true
parity-scale-codec.workspace = true
futures.workspace = true

[build-dependencies]
gear-wasm-builder.workspace = true

[features]
debug = ["gstd/debug"]
default = ["std"]
std = []
21 changes: 21 additions & 0 deletions examples/async-critical/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This file is part of Gear.

// Copyright (C) 2021-2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

fn main() {
gear_wasm_builder::build();
}
40 changes: 40 additions & 0 deletions examples/async-critical/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This file is part of Gear.

// Copyright (C) 2021-2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![no_std]

#[cfg(feature = "std")]
mod code {
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
}

#[cfg(feature = "std")]
pub use code::WASM_BINARY_OPT as WASM_BINARY;

use gstd::{Decode, Encode};

#[derive(Debug, Encode, Decode)]
pub enum HandleAction {
Simple,
Panic,
InHandleReply,
InHandleSignal,
}

#[cfg(target_arch = "wasm32")]
mod wasm;
111 changes: 111 additions & 0 deletions examples/async-critical/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// This file is part of Gear.

// Copyright (C) 2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::HandleAction;
use gstd::{critical, exec, msg, prelude::*, ActorId};

static mut REPLY_SET_HOOK: bool = false;
static mut SIGNAL_SET_HOOK: bool = false;
static mut INITIATOR: ActorId = ActorId::zero();

#[gstd::async_main(handle_reply = my_handle_reply, handle_signal = my_handle_signal)]
async fn main() {
ark0f marked this conversation as resolved.
Show resolved Hide resolved
unsafe { INITIATOR = msg::source() };

let action: HandleAction = msg::load().expect("Failed to read handle action");

match action {
HandleAction::Simple => {
// call `gr_source` outside because it is forbidden in `handle_signal`
let source = msg::source();

// should not send anything because execution will be completed
critical::set_hook(move || {
msg::send_bytes(source, b"critical", 0).unwrap();
});

// wait occurs inside so hook is saved
gstd::msg::send_bytes_for_reply(source, b"for_reply", 0, 0)
.expect("Failed to send message")
.await
.expect("Received error reply");
}
HandleAction::Panic => {
// call `gr_source` outside because it is forbidden in `handle_signal`
let source = msg::source();

// should send message because panic occurs below
critical::set_hook(move || {
msg::send_bytes(source, b"critical", 0).unwrap();
});

// wait occurs inside so hook is saved
gstd::msg::send_bytes_for_reply(msg::source(), b"for_reply", 0, 0)
ark0f marked this conversation as resolved.
Show resolved Hide resolved
.expect("Failed to send message")
.await
.expect("Received error reply");

// panic occurs so `handle_signal` will execute hook
panic!();
}
HandleAction::InHandleReply => {
unsafe {
REPLY_SET_HOOK = true;
}

gstd::msg::send_bytes_for_reply(msg::source(), b"for_reply", 0, 0)
.expect("Failed to send message")
.await
.expect("Received error reply");
}
HandleAction::InHandleSignal => {
unsafe {
SIGNAL_SET_HOOK = true;
}

gstd::msg::send_bytes_for_reply(msg::source(), b"for_reply", 0, 0)
.expect("Failed to send message")
.await
.expect("Received error reply");

panic!()
}
}
}

fn my_handle_reply() {
unsafe {
if REPLY_SET_HOOK {
// should panic in this entrypoint
critical::set_hook(move || {
msg::send_bytes(INITIATOR, b"from_handle_reply", 0).unwrap();
});
}
}
}

fn my_handle_signal() {
unsafe {
if SIGNAL_SET_HOOK {
// should panic in this entrypoint
critical::set_hook(move || {
msg::send_bytes(INITIATOR, b"from_handle_signal", 0).unwrap();
});
}
}
}
3 changes: 2 additions & 1 deletion gstd/src/async_runtime/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

//! Module for future-management.

use crate::{prelude::Box, MessageId};
use crate::{critical, prelude::Box, MessageId};
use core::{
future::Future,
pin::Pin,
Expand Down Expand Up @@ -87,6 +87,7 @@ where
if Pin::new(&mut task.future).poll(&mut cx).is_ready() {
super::futures().remove(&msg_id);
super::locks().remove_message_entry(msg_id);
let _ = critical::take_hook();
} else {
super::locks().wait(msg_id);
}
Expand Down
8 changes: 6 additions & 2 deletions gstd/src/async_runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ mod signals;
mod waker;

pub use self::futures::message_loop;
pub(crate) use locks::Lock;
pub(crate) use signals::ReplyPoll;

use self::futures::FuturesMap;
use crate::critical;
use hashbrown::HashMap;
pub(crate) use locks::Lock;
use locks::LocksMap;
pub(crate) use signals::ReplyPoll;
use signals::WakeSignals;

static mut FUTURES: Option<FuturesMap> = None;
Expand Down Expand Up @@ -58,6 +59,9 @@ pub fn handle_signal() {
let msg_id = crate::msg::signal_from().expect(
"`gstd::async_runtime::handle_signal()` must be called only in `handle_signal` entrypoint",
);

critical::take_and_execute();

futures().remove(&msg_id);
locks().remove_message_entry(msg_id);
}
Loading