Switch from OnceCell to LazyLock in bevy_tasks (#18506)

Remove `critical-section` from required dependencies, allowing linking
without any features.

- Switched from `OnceCell` to `LazyLock`
- Removed `std` feature from `bevy_dylib` (proof that it works)

- CI
This commit is contained in:
Zachary Harrold 2025-03-24 18:43:22 +11:00 committed by François Mockers
parent ff189cf4ef
commit ca3a5c0c6e
3 changed files with 8 additions and 21 deletions

View File

@ -12,11 +12,7 @@ keywords = ["bevy"]
crate-type = ["dylib"]
[dependencies]
# feature std is needed to avoid an issue when linking critical_section
# bevy_dylib is not expected to work in no_std
bevy_internal = { path = "../bevy_internal", version = "0.16.0-rc.1", default-features = false, features = [
"std",
] }
bevy_internal = { path = "../bevy_internal", version = "0.16.0-rc.1", default-features = false }
[lints]
workspace = true

View File

@ -26,12 +26,7 @@ async_executor = ["std", "dep:async-executor"]
## Allows access to the `std` crate. Enabling this feature will prevent compilation
## on `no_std` targets, but provides access to certain additional features on
## supported platforms.
std = [
"futures-lite/std",
"async-task/std",
"bevy_platform_support/std",
"once_cell/std",
]
std = ["futures-lite/std", "async-task/std", "bevy_platform_support/std"]
## `critical-section` provides the building blocks for synchronization primitives
## on all platforms, including `no_std`.
@ -65,9 +60,6 @@ async-channel = { version = "2.3.0", optional = true }
async-io = { version = "2.0.0", optional = true }
concurrent-queue = { version = "2.0.0", optional = true }
atomic-waker = { version = "1", default-features = false }
once_cell = { version = "1.18", default-features = false, features = [
"critical-section",
] }
crossbeam-queue = { version = "0.3", default-features = false, features = [
"alloc",
] }

View File

@ -22,9 +22,8 @@ use core::{
use async_task::{Runnable, Task};
use atomic_waker::AtomicWaker;
use bevy_platform_support::sync::Arc;
use bevy_platform_support::sync::{Arc, LazyLock};
use futures_lite::FutureExt;
use once_cell::sync::OnceCell;
/// An async executor.
///
@ -51,7 +50,7 @@ use once_cell::sync::OnceCell;
/// }));
/// ```
pub struct Executor<'a, const C: usize = 64> {
state: OnceCell<Arc<State<C>>>,
state: LazyLock<Arc<State<C>>>,
_invariant: PhantomData<core::cell::UnsafeCell<&'a ()>>,
}
@ -67,7 +66,7 @@ impl<'a, const C: usize> Executor<'a, C> {
/// ```
pub const fn new() -> Self {
Self {
state: OnceCell::new(),
state: LazyLock::new(|| Arc::new(State::new())),
_invariant: PhantomData,
}
}
@ -284,7 +283,7 @@ impl<'a, const C: usize> Executor<'a, C> {
/// Returns a reference to the inner state.
fn state(&self) -> &Arc<State<C>> {
self.state.get_or_init(|| Arc::new(State::new()))
&self.state
}
}
@ -526,15 +525,15 @@ mod drop_tests {
use core::task::{Poll, Waker};
use std::sync::Mutex;
use bevy_platform_support::sync::LazyLock;
use futures_lite::future;
use once_cell::sync::Lazy;
use super::{Executor, Task};
#[test]
fn leaked_executor_leaks_everything() {
static DROP: AtomicUsize = AtomicUsize::new(0);
static WAKER: Lazy<Mutex<Option<Waker>>> = Lazy::new(Default::default);
static WAKER: LazyLock<Mutex<Option<Waker>>> = LazyLock::new(Default::default);
let ex: Executor = Default::default();