switch from slab to slotmap for tasks to fix that ABA problem (#2488)
This commit is contained in:
1 parent
5024139e01
commit
e4764f2bba
5 files changed
+39
-18
No files matched your search
Generated
+11
@@ -2253,6 +2253,7 @@ dependencies = [
|
|||||||
"rustversion",
|
"rustversion",
|
||||||
"serde",
|
"serde",
|
||||||
"slab",
|
"slab",
|
||||||
|
"slotmap",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-fluent-assertions",
|
"tracing-fluent-assertions",
|
||||||
@@ -8343,6 +8344,16 @@ dependencies = [
|
|||||||
"rustc-hash",
|
"rustc-hash",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "slotmap"
|
||||||
|
version = "1.0.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dbff4acf519f630b3a3ddcfaea6c06b42174d9a44bc70c620e9ed1649d58b82a"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
"version_check",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "smallvec"
|
name = "smallvec"
|
||||||
version = "1.13.2"
|
version = "1.13.2"
|
||||||
|
|||||||
@@ -86,6 +86,7 @@ tracing-futures = "0.2.5"
|
|||||||
toml = "0.8"
|
toml = "0.8"
|
||||||
tokio = "1.28"
|
tokio = "1.28"
|
||||||
slab = "0.4.2"
|
slab = "0.4.2"
|
||||||
|
slotmap = { version = "1.0.7", features = ["serde"] }
|
||||||
futures-channel = "0.3.21"
|
futures-channel = "0.3.21"
|
||||||
futures-util = { version = "0.3", default-features = false }
|
futures-util = { version = "0.3", default-features = false }
|
||||||
rustc-hash = "1.1.0"
|
rustc-hash = "1.1.0"
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ futures-util = { workspace = true, default-features = false, features = [
|
|||||||
"std",
|
"std",
|
||||||
] }
|
] }
|
||||||
slab = { workspace = true }
|
slab = { workspace = true }
|
||||||
|
slotmap = { workspace = true }
|
||||||
futures-channel = { workspace = true }
|
futures-channel = { workspace = true }
|
||||||
tracing = { workspace = true }
|
tracing = { workspace = true }
|
||||||
serde = { version = "1", features = ["derive"], optional = true }
|
serde = { version = "1", features = ["derive"], optional = true }
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use slotmap::DefaultKey;
|
||||||
|
|
||||||
use crate::innerlude::Effect;
|
use crate::innerlude::Effect;
|
||||||
use crate::{
|
use crate::{
|
||||||
innerlude::{LocalTask, SchedulerMsg},
|
innerlude::{LocalTask, SchedulerMsg},
|
||||||
@@ -27,7 +29,7 @@ pub struct Runtime {
|
|||||||
pub(crate) current_task: Cell<Option<Task>>,
|
pub(crate) current_task: Cell<Option<Task>>,
|
||||||
|
|
||||||
/// Tasks created with cx.spawn
|
/// Tasks created with cx.spawn
|
||||||
pub(crate) tasks: RefCell<slab::Slab<Rc<LocalTask>>>,
|
pub(crate) tasks: RefCell<slotmap::SlotMap<DefaultKey, Rc<LocalTask>>>,
|
||||||
|
|
||||||
// Currently suspended tasks
|
// Currently suspended tasks
|
||||||
pub(crate) suspended_tasks: Cell<usize>,
|
pub(crate) suspended_tasks: Cell<usize>,
|
||||||
|
|||||||
+23
-17
@@ -3,6 +3,7 @@ use crate::innerlude::ScopeOrder;
|
|||||||
use crate::innerlude::{remove_future, spawn, Runtime};
|
use crate::innerlude::{remove_future, spawn, Runtime};
|
||||||
use crate::ScopeId;
|
use crate::ScopeId;
|
||||||
use futures_util::task::ArcWake;
|
use futures_util::task::ArcWake;
|
||||||
|
use slotmap::DefaultKey;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::task::Waker;
|
use std::task::Waker;
|
||||||
use std::{cell::Cell, future::Future};
|
use std::{cell::Cell, future::Future};
|
||||||
@@ -14,7 +15,7 @@ use std::{pin::Pin, task::Poll};
|
|||||||
/// `Task` is a unique identifier for a task that has been spawned onto the runtime. It can be used to cancel the task
|
/// `Task` is a unique identifier for a task that has been spawned onto the runtime. It can be used to cancel the task
|
||||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
||||||
pub struct Task(pub(crate) usize);
|
pub struct Task(pub(crate) slotmap::DefaultKey);
|
||||||
|
|
||||||
impl Task {
|
impl Task {
|
||||||
/// Start a new future on the same thread as the rest of the VirtualDom.
|
/// Start a new future on the same thread as the rest of the VirtualDom.
|
||||||
@@ -139,24 +140,29 @@ impl Runtime {
|
|||||||
let (task, task_id) = {
|
let (task, task_id) = {
|
||||||
let mut tasks = self.tasks.borrow_mut();
|
let mut tasks = self.tasks.borrow_mut();
|
||||||
|
|
||||||
let entry = tasks.vacant_entry();
|
let mut task_id = Task(DefaultKey::default());
|
||||||
let task_id = Task(entry.key());
|
let mut local_task = None;
|
||||||
|
tasks.insert_with_key(|key| {
|
||||||
|
task_id = Task(key);
|
||||||
|
|
||||||
let task = Rc::new(LocalTask {
|
let new_task = Rc::new(LocalTask {
|
||||||
scope,
|
scope,
|
||||||
active: Cell::new(true),
|
active: Cell::new(true),
|
||||||
parent: self.current_task(),
|
parent: self.current_task(),
|
||||||
task: RefCell::new(Box::pin(task)),
|
task: RefCell::new(Box::pin(task)),
|
||||||
waker: futures_util::task::waker(Arc::new(LocalTaskHandle {
|
waker: futures_util::task::waker(Arc::new(LocalTaskHandle {
|
||||||
id: task_id,
|
id: task_id,
|
||||||
tx: self.sender.clone(),
|
tx: self.sender.clone(),
|
||||||
})),
|
})),
|
||||||
ty: Cell::new(ty),
|
ty: Cell::new(ty),
|
||||||
|
});
|
||||||
|
|
||||||
|
local_task = Some(new_task.clone());
|
||||||
|
|
||||||
|
new_task
|
||||||
});
|
});
|
||||||
|
|
||||||
entry.insert(task.clone());
|
(local_task.unwrap(), task_id)
|
||||||
|
|
||||||
(task, task_id)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get a borrow on the task, holding no borrows on the tasks map
|
// Get a borrow on the task, holding no borrows on the tasks map
|
||||||
@@ -244,7 +250,7 @@ impl Runtime {
|
|||||||
///
|
///
|
||||||
/// This does not abort the task, so you'll want to wrap it in an abort handle if that's important to you
|
/// This does not abort the task, so you'll want to wrap it in an abort handle if that's important to you
|
||||||
pub(crate) fn remove_task(&self, id: Task) -> Option<Rc<LocalTask>> {
|
pub(crate) fn remove_task(&self, id: Task) -> Option<Rc<LocalTask>> {
|
||||||
let task = self.tasks.borrow_mut().try_remove(id.0);
|
let task = self.tasks.borrow_mut().remove(id.0);
|
||||||
if let Some(task) = &task {
|
if let Some(task) = &task {
|
||||||
if task.suspended() {
|
if task.suspended() {
|
||||||
self.suspended_tasks.set(self.suspended_tasks.get() - 1);
|
self.suspended_tasks.set(self.suspended_tasks.get() - 1);
|
||||||
|
|||||||
Reference in new issue
Block a user