-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gstd): Introduce critical hook (#3503)
- Loading branch information
Showing
12 changed files
with
579 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
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) | ||
.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(); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.