Skip to content

Commit 6bb7573

Browse files
Switch from OnceCell to LazyLock in bevy_tasks (#18506)
# Objective Remove `critical-section` from required dependencies, allowing linking without any features. ## Solution - Switched from `OnceCell` to `LazyLock` - Removed `std` feature from `bevy_dylib` (proof that it works) ## Testing - CI
1 parent 08d3b11 commit 6bb7573

File tree

3 files changed

+8
-21
lines changed

3 files changed

+8
-21
lines changed

crates/bevy_dylib/Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ keywords = ["bevy"]
1212
crate-type = ["dylib"]
1313

1414
[dependencies]
15-
# feature std is needed to avoid an issue when linking critical_section
16-
# bevy_dylib is not expected to work in no_std
17-
bevy_internal = { path = "../bevy_internal", version = "0.16.0-dev", default-features = false, features = [
18-
"std",
19-
] }
15+
bevy_internal = { path = "../bevy_internal", version = "0.16.0-dev", default-features = false }
2016

2117
[lints]
2218
workspace = true

crates/bevy_tasks/Cargo.toml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ async_executor = ["std", "dep:async-executor"]
2626
## Allows access to the `std` crate. Enabling this feature will prevent compilation
2727
## on `no_std` targets, but provides access to certain additional features on
2828
## supported platforms.
29-
std = [
30-
"futures-lite/std",
31-
"async-task/std",
32-
"bevy_platform_support/std",
33-
"once_cell/std",
34-
]
29+
std = ["futures-lite/std", "async-task/std", "bevy_platform_support/std"]
3530

3631
## `critical-section` provides the building blocks for synchronization primitives
3732
## on all platforms, including `no_std`.
@@ -65,9 +60,6 @@ async-channel = { version = "2.3.0", optional = true }
6560
async-io = { version = "2.0.0", optional = true }
6661
concurrent-queue = { version = "2.0.0", optional = true }
6762
atomic-waker = { version = "1", default-features = false }
68-
once_cell = { version = "1.18", default-features = false, features = [
69-
"critical-section",
70-
] }
7163
crossbeam-queue = { version = "0.3", default-features = false, features = [
7264
"alloc",
7365
] }

crates/bevy_tasks/src/edge_executor.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ use core::{
2222

2323
use async_task::{Runnable, Task};
2424
use atomic_waker::AtomicWaker;
25-
use bevy_platform_support::sync::Arc;
25+
use bevy_platform_support::sync::{Arc, LazyLock};
2626
use futures_lite::FutureExt;
27-
use once_cell::sync::OnceCell;
2827

2928
/// An async executor.
3029
///
@@ -51,7 +50,7 @@ use once_cell::sync::OnceCell;
5150
/// }));
5251
/// ```
5352
pub struct Executor<'a, const C: usize = 64> {
54-
state: OnceCell<Arc<State<C>>>,
53+
state: LazyLock<Arc<State<C>>>,
5554
_invariant: PhantomData<core::cell::UnsafeCell<&'a ()>>,
5655
}
5756

@@ -67,7 +66,7 @@ impl<'a, const C: usize> Executor<'a, C> {
6766
/// ```
6867
pub const fn new() -> Self {
6968
Self {
70-
state: OnceCell::new(),
69+
state: LazyLock::new(|| Arc::new(State::new())),
7170
_invariant: PhantomData,
7271
}
7372
}
@@ -284,7 +283,7 @@ impl<'a, const C: usize> Executor<'a, C> {
284283

285284
/// Returns a reference to the inner state.
286285
fn state(&self) -> &Arc<State<C>> {
287-
self.state.get_or_init(|| Arc::new(State::new()))
286+
&self.state
288287
}
289288
}
290289

@@ -526,15 +525,15 @@ mod drop_tests {
526525
use core::task::{Poll, Waker};
527526
use std::sync::Mutex;
528527

528+
use bevy_platform_support::sync::LazyLock;
529529
use futures_lite::future;
530-
use once_cell::sync::Lazy;
531530

532531
use super::{Executor, Task};
533532

534533
#[test]
535534
fn leaked_executor_leaks_everything() {
536535
static DROP: AtomicUsize = AtomicUsize::new(0);
537-
static WAKER: Lazy<Mutex<Option<Waker>>> = Lazy::new(Default::default);
536+
static WAKER: LazyLock<Mutex<Option<Waker>>> = LazyLock::new(Default::default);
538537

539538
let ex: Executor = Default::default();
540539

0 commit comments

Comments
 (0)