aboutsummaryrefslogtreecommitdiff
path: root/core/src/async_util/task_group.rs
diff options
context:
space:
mode:
authorhozan23 <hozan23@proton.me>2024-03-20 15:20:21 +0100
committerhozan23 <hozan23@proton.me>2024-03-20 15:20:21 +0100
commit379dca552ca91d22ee007b42f93803ad3dc2b274 (patch)
tree1ba10573b6ec5213baf46f6ab9c3767e0daa4eed /core/src/async_util/task_group.rs
parent340957fec147f4429796413f27bbd9b84ba6f141 (diff)
core: add the option to create a new task group without providing an
executor & remove GlobalExecutor type
Diffstat (limited to 'core/src/async_util/task_group.rs')
-rw-r--r--core/src/async_util/task_group.rs48
1 files changed, 45 insertions, 3 deletions
diff --git a/core/src/async_util/task_group.rs b/core/src/async_util/task_group.rs
index 7129af2..0fb4855 100644
--- a/core/src/async_util/task_group.rs
+++ b/core/src/async_util/task_group.rs
@@ -2,9 +2,7 @@ use std::{future::Future, sync::Arc, sync::Mutex};
use async_task::FallibleTask;
-use crate::Executor;
-
-use super::{select, CondWait, Either};
+use super::{executor::global_executor, select, CondWait, Either, Executor};
/// TaskGroup is a group of spawned tasks.
///
@@ -35,6 +33,19 @@ pub struct TaskGroup<'a> {
executor: Executor<'a>,
}
+impl TaskGroup<'static> {
+ /// Creates a new task group without providing an executor
+ ///
+ /// This will Spawn a task onto a global executor (single-threaded by default).
+ pub fn new_without_executor() -> Self {
+ Self {
+ tasks: Mutex::new(Vec::new()),
+ stop_signal: Arc::new(CondWait::new()),
+ executor: global_executor(),
+ }
+ }
+}
+
impl<'a> TaskGroup<'a> {
/// Creates a new task group
pub fn new(executor: Executor<'a>) -> Self {
@@ -191,4 +202,35 @@ mod tests {
group.cancel().await;
}));
}
+
+ #[test]
+ fn test_task_group_without_executor() {
+ smol::block_on(async {
+ let group = Arc::new(TaskGroup::new_without_executor());
+
+ group.spawn(future::ready(0), |res| async move {
+ assert!(matches!(res, TaskResult::Completed(0)));
+ });
+
+ group.spawn(future::pending::<()>(), |res| async move {
+ assert!(matches!(res, TaskResult::Cancelled));
+ });
+
+ let groupc = group.clone();
+ group.spawn(
+ async move {
+ groupc.spawn(future::pending::<()>(), |res| async move {
+ assert!(matches!(res, TaskResult::Cancelled));
+ });
+ },
+ |res| async move {
+ assert!(matches!(res, TaskResult::Completed(_)));
+ },
+ );
+
+ // Do something
+ smol::Timer::after(std::time::Duration::from_millis(50)).await;
+ group.cancel().await;
+ });
+ }
}