From 4c647b5fb5be063b06e07c420ccb7de93c0b6082 Mon Sep 17 00:00:00 2001 From: playX18 Date: Tue, 1 Oct 2024 12:18:36 +0700 Subject: [PATCH 1/2] refactor(gstd): use waker-fn instead of unsafe code --- Cargo.lock | 5 +++-- gstd/Cargo.toml | 1 + gstd/src/async_runtime/waker.rs | 16 ++-------------- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index edcefd2611e..9103b72a56b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7471,6 +7471,7 @@ dependencies = [ "hex", "parity-scale-codec", "scale-info", + "waker-fn", ] [[package]] @@ -19026,9 +19027,9 @@ dependencies = [ [[package]] name = "waker-fn" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" +checksum = "317211a0dc0ceedd78fb2ca9a44aed3d7b9b26f81870d485c07122b4350673b7" [[package]] name = "walkdir" diff --git a/gstd/Cargo.toml b/gstd/Cargo.toml index 46e61e53c68..5882831aacc 100644 --- a/gstd/Cargo.toml +++ b/gstd/Cargo.toml @@ -25,6 +25,7 @@ hex = { workspace = true, features = ["alloc"] } parity-scale-codec = { workspace = true, features = ["derive"] } scale-info = { workspace = true, features = ["derive"] } futures = { workspace = true, features = ["alloc"] } +waker-fn = "1.1.1" [features] #! ## Default features diff --git a/gstd/src/async_runtime/waker.rs b/gstd/src/async_runtime/waker.rs index e16f56c908b..e24abc9b624 100644 --- a/gstd/src/async_runtime/waker.rs +++ b/gstd/src/async_runtime/waker.rs @@ -18,20 +18,8 @@ //! Module for Gear programs asynchronous waker. -use core::{ - ptr, - task::{RawWaker, RawWakerVTable, Waker}, -}; - -const VTABLE: RawWakerVTable = RawWakerVTable::new(clone_waker, wake, wake_by_ref, drop_waker); +use core::task::Waker; pub(crate) fn empty() -> Waker { - unsafe { Waker::from_raw(RawWaker::new(ptr::null(), &VTABLE)) } -} - -unsafe fn clone_waker(ptr: *const ()) -> RawWaker { - RawWaker::new(ptr, &VTABLE) + waker_fn::waker_fn(|| {}) } -unsafe fn wake(_ptr: *const ()) {} -unsafe fn wake_by_ref(_ptr: *const ()) {} -unsafe fn drop_waker(_ptr: *const ()) {} From 7516f3bdf02fe7c58053c0119871ef207d0164f4 Mon Sep 17 00:00:00 2001 From: playX18 Date: Wed, 2 Oct 2024 06:08:15 +0700 Subject: [PATCH 2/2] remove waker module; bump waker_fn version --- gstd/Cargo.toml | 2 +- gstd/src/async_runtime/futures.rs | 2 +- gstd/src/async_runtime/mod.rs | 1 - gstd/src/async_runtime/waker.rs | 25 ------------------------- 4 files changed, 2 insertions(+), 28 deletions(-) delete mode 100644 gstd/src/async_runtime/waker.rs diff --git a/gstd/Cargo.toml b/gstd/Cargo.toml index 5882831aacc..b9c6645045b 100644 --- a/gstd/Cargo.toml +++ b/gstd/Cargo.toml @@ -25,7 +25,7 @@ hex = { workspace = true, features = ["alloc"] } parity-scale-codec = { workspace = true, features = ["derive"] } scale-info = { workspace = true, features = ["derive"] } futures = { workspace = true, features = ["alloc"] } -waker-fn = "1.1.1" +waker-fn = "1.2.0" [features] #! ## Default features diff --git a/gstd/src/async_runtime/futures.rs b/gstd/src/async_runtime/futures.rs index 43fb8a1b05f..2e4c88e4d99 100644 --- a/gstd/src/async_runtime/futures.rs +++ b/gstd/src/async_runtime/futures.rs @@ -47,7 +47,7 @@ impl Task { F: Future + 'static, { Self { - waker: super::waker::empty(), + waker: waker_fn::waker_fn(|| {}), future: future.boxed_local(), lock_exceeded: false, } diff --git a/gstd/src/async_runtime/mod.rs b/gstd/src/async_runtime/mod.rs index 9a0c07ee76c..ceb8bd3eb9c 100644 --- a/gstd/src/async_runtime/mod.rs +++ b/gstd/src/async_runtime/mod.rs @@ -19,7 +19,6 @@ mod futures; mod locks; mod signals; -mod waker; #[cfg(not(feature = "ethexe"))] mod reply_hooks; diff --git a/gstd/src/async_runtime/waker.rs b/gstd/src/async_runtime/waker.rs deleted file mode 100644 index e24abc9b624..00000000000 --- a/gstd/src/async_runtime/waker.rs +++ /dev/null @@ -1,25 +0,0 @@ -// This file is part of Gear. - -// Copyright (C) 2021-2024 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 . - -//! Module for Gear programs asynchronous waker. - -use core::task::Waker; - -pub(crate) fn empty() -> Waker { - waker_fn::waker_fn(|| {}) -}