From c8a3e75d75dd7e9f7d5fdc1f60d8f89f38f6a14b Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 6 Jul 2024 08:12:08 +0000 Subject: [PATCH 1/2] Concurrency: Guard TG state with a mutex even with cooperative executor `SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY` just means that the global executor is cooperative, but it doesn't mean that the target platform is always single-threaded. For example, on wasm32-unknown-wasip1-threads, the global executor is cooperative, but users can still set up their own TaskExecutor with multiple threads. This patch guards the `TaskGroup` state with a mutex even with a cooperative executor by respecting threading package instead. This change effectively affects only wasm32-unknown-wasip1-threads. --- stdlib/public/Concurrency/TaskGroup.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/public/Concurrency/TaskGroup.cpp b/stdlib/public/Concurrency/TaskGroup.cpp index 0aa525d3cbc30..d284c64a1fabf 100644 --- a/stdlib/public/Concurrency/TaskGroup.cpp +++ b/stdlib/public/Concurrency/TaskGroup.cpp @@ -292,7 +292,7 @@ class TaskGroupBase : public TaskGroupTaskStatusRecord { }; protected: -#if SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY || SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL +#if SWIFT_THREADING_NONE || SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL // Synchronization is simple here. In a single threaded mode, all swift tasks // run on a single thread so no coordination is needed. In a task-to-thread // model, only the parent task which created the task group can From 9a6dd7e1914ecb45800802ce0527fa8576dc468c Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Mon, 8 Jul 2024 10:49:38 +0900 Subject: [PATCH 2/2] Add comment explaining #if guard --- stdlib/public/Concurrency/TaskGroup.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/stdlib/public/Concurrency/TaskGroup.cpp b/stdlib/public/Concurrency/TaskGroup.cpp index d284c64a1fabf..b887fc02c94c1 100644 --- a/stdlib/public/Concurrency/TaskGroup.cpp +++ b/stdlib/public/Concurrency/TaskGroup.cpp @@ -292,6 +292,11 @@ class TaskGroupBase : public TaskGroupTaskStatusRecord { }; protected: +// Guard with SWIFT_THREADING_NONE and not just SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY +// because the latter just means that the global executor is cooperative, +// but it doesn't mean that the target platform is always single-threaded. For example, on +// wasm32-unknown-wasip1-threads, the global executor is cooperative, but users can still set up their +// own TaskExecutor with multiple threads. #if SWIFT_THREADING_NONE || SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL // Synchronization is simple here. In a single threaded mode, all swift tasks // run on a single thread so no coordination is needed. In a task-to-thread