revert: bring back task abstraction

This commit is contained in:
Nova
2025-10-11 15:51:26 -07:00
parent ba5415653e
commit 6742caa967
7 changed files with 66 additions and 49 deletions

View File

@@ -3,7 +3,7 @@ use super::{
scenegraph::Scenegraph,
};
use crate::{
core::registry::OwnedRegistry,
core::{registry::OwnedRegistry, task},
nodes::{
Node, audio, drawable, fields, input, items,
root::{ClientState, Root},
@@ -143,11 +143,9 @@ impl Client {
})
.unwrap_or_else(|| "??".to_string());
let _ = client.dispatch_join_handle.get_or_init(|| {
tokio::task::Builder::new()
.name(&format!(
"Stardust client \"{exe_printable}\" dispatch, pid={pid_printable}",
))
.spawn({
task::new(
|| format!("Stardust client \"{exe_printable}\" dispatch, pid={pid_printable}"),
{
let client = client.clone();
async move {
loop {
@@ -157,15 +155,14 @@ impl Client {
let _ = message_time_tx.send(Instant::now());
}
}
})
.unwrap()
},
)
.unwrap()
});
let _ = client.flush_join_handle.get_or_init(|| {
tokio::task::Builder::new()
.name(&format!(
"Stardust client \"{exe_printable}\" flush, pid={pid_printable}",
))
.spawn({
task::new(
|| format!("Stardust client \"{exe_printable}\" flush, pid={pid_printable}"),
{
let client = client.clone();
async move {
loop {
@@ -174,8 +171,9 @@ impl Client {
}
}
}
})
.unwrap()
},
)
.unwrap()
});
Ok(client)

View File

@@ -8,3 +8,4 @@ pub mod error;
pub mod registry;
pub mod resource;
pub mod scenegraph;
pub mod task;

21
src/core/task.rs Normal file
View File

@@ -0,0 +1,21 @@
use std::future::Future;
use tokio::task::JoinHandle;
#[allow(unused_variables)]
pub fn new<
F: FnOnce() -> S,
S: AsRef<str>,
A: Future<Output = O> + Send + 'static,
O: Send + 'static,
>(
name_fn: F,
async_future: A,
) -> std::io::Result<JoinHandle<O>> {
#[cfg(not(feature = "profile_tokio"))]
let result = Ok(tokio::task::spawn(async_future));
#[cfg(feature = "profile_tokio")]
let result = tokio::task::Builder::new()
.name(name_fn().as_ref())
.spawn(async_future);
result
}