From 5da91ff702f95f12e26381688abd546aa3894a77 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 19 Sep 2021 12:47:07 +0000 Subject: [PATCH 1/6] [Concurrency] repair cooperative main executor This patch repairs the build failure for cooperative executor. And also fixed main executor to avoid assertion failure due to `witnessTable == nullptr` --- stdlib/public/Concurrency/GlobalExecutor.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/stdlib/public/Concurrency/GlobalExecutor.cpp b/stdlib/public/Concurrency/GlobalExecutor.cpp index 1fbe16a4dc6ad..c34272576572c 100644 --- a/stdlib/public/Concurrency/GlobalExecutor.cpp +++ b/stdlib/public/Concurrency/GlobalExecutor.cpp @@ -103,7 +103,7 @@ static DelayedJob *DelayedJobQueue = nullptr; /// Get the next-in-queue storage slot. static Job *&nextInQueue(Job *cur) { - return reinterpret_cast(&cur->SchedulerPrivate[NextWaitingTaskIndex]); + return reinterpret_cast(cur->SchedulerPrivate[Job::NextWaitingTaskIndex]); } /// Insert a job into the cooperative global queue. @@ -448,13 +448,9 @@ void swift::swift_task_enqueueOnDispatchQueue(Job *job, } #endif -#if SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR -static HeapObject _swift_mainExecutorIdentity; -#endif - ExecutorRef swift::swift_task_getMainExecutor() { #if SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR - return ExecutorRef::forOrdinary(&_swift_mainExecutorIdentity, nullptr); + return ExecutorRef::generic(); #else return ExecutorRef::forOrdinary( reinterpret_cast(&_dispatch_main_q), @@ -464,7 +460,7 @@ ExecutorRef swift::swift_task_getMainExecutor() { bool ExecutorRef::isMainExecutor() const { #if SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR - return Identity == &_swift_mainExecutorIdentity; + return isGeneric(); #else return Identity == reinterpret_cast(&_dispatch_main_q); #endif From 1b56c037a364672f7ab2ca34ea3b7af902c672c8 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 19 Sep 2021 12:48:36 +0000 Subject: [PATCH 2/6] [test] fix runtime unittests for single threaded runtime Some headers switch their inline implementations based on SWIFT_STDLIB_SINGLE_THREAD_RUNTIME definition. This fixes linking failure while building runtime unittests --- cmake/modules/AddSwiftUnittests.cmake | 7 +++++++ stdlib/public/Concurrency/Mutex.cpp | 3 +++ 2 files changed, 10 insertions(+) diff --git a/cmake/modules/AddSwiftUnittests.cmake b/cmake/modules/AddSwiftUnittests.cmake index ff44121a69f8c..ca0ae47288eef 100644 --- a/cmake/modules/AddSwiftUnittests.cmake +++ b/cmake/modules/AddSwiftUnittests.cmake @@ -49,6 +49,13 @@ function(add_swift_unittest test_dirname) _ENABLE_EXTENDED_ALIGNED_STORAGE) endif() + # some headers switch their inline implementations based on + # SWIFT_STDLIB_SINGLE_THREADED_RUNTIME definition + if(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME) + target_compile_definitions("${test_dirname}" PRIVATE + SWIFT_STDLIB_SINGLE_THREADED_RUNTIME) + endif() + if(NOT SWIFT_COMPILER_IS_MSVC_LIKE) if(SWIFT_USE_LINKER) target_link_options(${test_dirname} PRIVATE diff --git a/stdlib/public/Concurrency/Mutex.cpp b/stdlib/public/Concurrency/Mutex.cpp index d61c6759089c0..ccf4ec939d882 100644 --- a/stdlib/public/Concurrency/Mutex.cpp +++ b/stdlib/public/Concurrency/Mutex.cpp @@ -19,3 +19,6 @@ #include "../runtime/MutexPThread.cpp" #include "../runtime/MutexWin32.cpp" +#ifdef SWIFT_STDLIB_SINGLE_THREADED_RUNTIME + #include "swift/Runtime/MutexSingleThreaded.h" +#endif From 8e2a7db5f25f87939b82a0c4d6f4f418afdec9a1 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 19 Sep 2021 12:51:16 +0000 Subject: [PATCH 3/6] [test] disable async_task_locals_copy_to_sync.swift on single thread --- test/Concurrency/Runtime/async_task_locals_copy_to_sync.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/Concurrency/Runtime/async_task_locals_copy_to_sync.swift b/test/Concurrency/Runtime/async_task_locals_copy_to_sync.swift index fcf0cfd8c67a1..e79429156dbec 100644 --- a/test/Concurrency/Runtime/async_task_locals_copy_to_sync.swift +++ b/test/Concurrency/Runtime/async_task_locals_copy_to_sync.swift @@ -7,6 +7,8 @@ // rdar://76038845 // REQUIRES: concurrency_runtime // UNSUPPORTED: back_deployment_runtime +// Disable on cooperative executor because it can't dispatch jobs before the end of main function +// UNSUPPORTED: single_threaded_runtime import Dispatch From 722d79048066773ced9e60530cad0eb646dae02c Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 19 Sep 2021 12:52:59 +0000 Subject: [PATCH 4/6] [test] mark multi-thread based tests as UNSUPPORTED in single-thread Some of test cases assumes that the runtime is built for multi-threaded platform. --- test/Concurrency/Runtime/cancellation_handler.swift | 1 + test/Concurrency/Runtime/data_race_detection.swift | 5 +++-- test/Concurrency/Runtime/mainactor.swift | 1 + test/Interpreter/enforce_exclusive_access.swift | 1 + validation-test/Runtime/ConcurrentMetadata.swift | 1 + 5 files changed, 7 insertions(+), 2 deletions(-) diff --git a/test/Concurrency/Runtime/cancellation_handler.swift b/test/Concurrency/Runtime/cancellation_handler.swift index 546fb1a69031f..2344d88e48e88 100644 --- a/test/Concurrency/Runtime/cancellation_handler.swift +++ b/test/Concurrency/Runtime/cancellation_handler.swift @@ -5,6 +5,7 @@ // rdar://76038845 // REQUIRES: concurrency_runtime // UNSUPPORTED: back_deployment_runtime +// UNSUPPORTED: single_threaded_runtime // for sleep #if canImport(Darwin) diff --git a/test/Concurrency/Runtime/data_race_detection.swift b/test/Concurrency/Runtime/data_race_detection.swift index df7a670fbad86..81153db0a178e 100644 --- a/test/Concurrency/Runtime/data_race_detection.swift +++ b/test/Concurrency/Runtime/data_race_detection.swift @@ -8,6 +8,7 @@ // rdar://76038845 // REQUIRES: concurrency_runtime // UNSUPPORTED: back_deployment_runtime +// UNSUPPORTED: single_threaded_runtime import _Concurrency import Dispatch @@ -58,14 +59,14 @@ actor MyActor { struct Runner { static func main() async { print("Launching a main-actor task") - // CHECK: warning: data race detected: @MainActor function at main/data_race_detection.swift:22 was not called on the main thread + // CHECK: warning: data race detected: @MainActor function at main/data_race_detection.swift:23 was not called on the main thread launchFromMainThread() sleep(1) let actor = MyActor() let actorFn = await actor.getTaskOnMyActor() print("Launching an actor-instance task") - // CHECK: warning: data race detected: actor-isolated function at main/data_race_detection.swift:51 was not called on the same actor + // CHECK: warning: data race detected: actor-isolated function at main/data_race_detection.swift:52 was not called on the same actor launchTask(actorFn) sleep(1) diff --git a/test/Concurrency/Runtime/mainactor.swift b/test/Concurrency/Runtime/mainactor.swift index bab9b616582a5..061b3ec22b587 100644 --- a/test/Concurrency/Runtime/mainactor.swift +++ b/test/Concurrency/Runtime/mainactor.swift @@ -7,6 +7,7 @@ // rdar://76038845 // REQUIRES: concurrency_runtime // UNSUPPORTED: back_deployment_runtime +// UNSUPPORTED: single_threaded_runtime import Dispatch diff --git a/test/Interpreter/enforce_exclusive_access.swift b/test/Interpreter/enforce_exclusive_access.swift index 321dc5dfcd38b..a900089d1515f 100644 --- a/test/Interpreter/enforce_exclusive_access.swift +++ b/test/Interpreter/enforce_exclusive_access.swift @@ -4,6 +4,7 @@ // RUN: %target-codesign %t/a.out // RUN: %target-run %t/a.out // REQUIRES: executable_test +// UNSUPPORTED: single_threaded_runtime // Tests for traps at run time when enforcing exclusive access. diff --git a/validation-test/Runtime/ConcurrentMetadata.swift b/validation-test/Runtime/ConcurrentMetadata.swift index a006c5afb708b..60e8add4dc2cb 100644 --- a/validation-test/Runtime/ConcurrentMetadata.swift +++ b/validation-test/Runtime/ConcurrentMetadata.swift @@ -1,5 +1,6 @@ // RUN: %target-run-simple-swift // REQUIRES: executable_test +// UNSUPPORTED: single_threaded_runtime // Exercise the metadata cache from multiple threads to shake out any // concurrency bugs. From d64f83aeeb7bb558e76bcd1c335eb4bf8e33ca12 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 19 Sep 2021 14:36:30 +0000 Subject: [PATCH 5/6] [test] disable a part of unittests/runtime/Concurrent.cpp for single-thread runtime --- unittests/runtime/Concurrent.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/unittests/runtime/Concurrent.cpp b/unittests/runtime/Concurrent.cpp index c014fcb207001..f3b3ea9308b1e 100644 --- a/unittests/runtime/Concurrent.cpp +++ b/unittests/runtime/Concurrent.cpp @@ -45,6 +45,8 @@ TEST(ConcurrentReadableArrayTest, SingleThreaded) { check(); } +#ifndef SWIFT_STDLIB_SINGLE_THREADED_RUNTIME + TEST(ConcurrentReadableArrayTest, MultiThreaded) { const int insertCount = 100000; @@ -542,3 +544,4 @@ TEST(ConcurrentReadableHashMapTest, MultiThreaded4) { runTest(16, 1); runTest(16, 8); } +#endif // !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME From 0b7023519f345d9be958c65a13e094172d4d8c7a Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 19 Sep 2021 14:36:59 +0000 Subject: [PATCH 6/6] [test] disable unittests/runtime/Mutex.cpp for single-thread runtime --- unittests/runtime/CMakeLists.txt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/unittests/runtime/CMakeLists.txt b/unittests/runtime/CMakeLists.txt index 17ff1c944f6f2..4e06c42535d6c 100644 --- a/unittests/runtime/CMakeLists.txt +++ b/unittests/runtime/CMakeLists.txt @@ -84,6 +84,10 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND endif() endif() + if(NOT SWIFT_STDLIB_SINGLE_THREADED_RUNTIME) + list(APPEND PLATFORM_SOURCES Mutex.cpp) + endif() + if(SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED) # list(APPEND PLATFORM_SOURCES # DistributedActor.cpp @@ -98,7 +102,8 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND weak.mm Refcounting.mm Actor.cpp - TaskStatus.cpp) + TaskStatus.cpp + Mutex.cpp) add_swift_unittest(SwiftRuntimeTests Array.cpp @@ -106,7 +111,6 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND CompatibilityOverrideConcurrency.cpp Concurrent.cpp Metadata.cpp - Mutex.cpp Enum.cpp Refcounting.cpp Stdlib.cpp