Skip to content

Commit e7342eb

Browse files
authored
Merge pull request swiftlang#39365 from kateinoigakukun/katei/fix-coop-executor-part-2
[Concurrency] repair cooperative executor and its tests
2 parents 012f8bb + 0b70235 commit e7342eb

File tree

11 files changed

+31
-11
lines changed

11 files changed

+31
-11
lines changed

cmake/modules/AddSwiftUnittests.cmake

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ function(add_swift_unittest test_dirname)
4949
_ENABLE_EXTENDED_ALIGNED_STORAGE)
5050
endif()
5151

52+
# some headers switch their inline implementations based on
53+
# SWIFT_STDLIB_SINGLE_THREADED_RUNTIME definition
54+
if(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
55+
target_compile_definitions("${test_dirname}" PRIVATE
56+
SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
57+
endif()
58+
5259
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
5360
if(SWIFT_USE_LINKER)
5461
target_link_options(${test_dirname} PRIVATE

stdlib/public/Concurrency/GlobalExecutor.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static DelayedJob *DelayedJobQueue = nullptr;
103103

104104
/// Get the next-in-queue storage slot.
105105
static Job *&nextInQueue(Job *cur) {
106-
return reinterpret_cast<Job*&>(&cur->SchedulerPrivate[NextWaitingTaskIndex]);
106+
return reinterpret_cast<Job*&>(cur->SchedulerPrivate[Job::NextWaitingTaskIndex]);
107107
}
108108

109109
/// Insert a job into the cooperative global queue.
@@ -448,13 +448,9 @@ void swift::swift_task_enqueueOnDispatchQueue(Job *job,
448448
}
449449
#endif
450450

451-
#if SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR
452-
static HeapObject _swift_mainExecutorIdentity;
453-
#endif
454-
455451
ExecutorRef swift::swift_task_getMainExecutor() {
456452
#if SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR
457-
return ExecutorRef::forOrdinary(&_swift_mainExecutorIdentity, nullptr);
453+
return ExecutorRef::generic();
458454
#else
459455
return ExecutorRef::forOrdinary(
460456
reinterpret_cast<HeapObject*>(&_dispatch_main_q),
@@ -464,7 +460,7 @@ ExecutorRef swift::swift_task_getMainExecutor() {
464460

465461
bool ExecutorRef::isMainExecutor() const {
466462
#if SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR
467-
return Identity == &_swift_mainExecutorIdentity;
463+
return isGeneric();
468464
#else
469465
return Identity == reinterpret_cast<HeapObject*>(&_dispatch_main_q);
470466
#endif

stdlib/public/Concurrency/Mutex.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@
1919

2020
#include "../runtime/MutexPThread.cpp"
2121
#include "../runtime/MutexWin32.cpp"
22+
#ifdef SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
23+
#include "swift/Runtime/MutexSingleThreaded.h"
24+
#endif

test/Concurrency/Runtime/async_task_locals_copy_to_sync.swift

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// rdar://76038845
88
// REQUIRES: concurrency_runtime
99
// UNSUPPORTED: back_deployment_runtime
10+
// Disable on cooperative executor because it can't dispatch jobs before the end of main function
11+
// UNSUPPORTED: single_threaded_runtime
1012

1113
import Dispatch
1214

test/Concurrency/Runtime/cancellation_handler.swift

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// rdar://76038845
66
// REQUIRES: concurrency_runtime
77
// UNSUPPORTED: back_deployment_runtime
8+
// UNSUPPORTED: single_threaded_runtime
89

910
// for sleep
1011
#if canImport(Darwin)

test/Concurrency/Runtime/data_race_detection.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// rdar://76038845
99
// REQUIRES: concurrency_runtime
1010
// UNSUPPORTED: back_deployment_runtime
11+
// UNSUPPORTED: single_threaded_runtime
1112

1213
import _Concurrency
1314
import Dispatch
@@ -58,14 +59,14 @@ actor MyActor {
5859
struct Runner {
5960
static func main() async {
6061
print("Launching a main-actor task")
61-
// CHECK: warning: data race detected: @MainActor function at main/data_race_detection.swift:22 was not called on the main thread
62+
// CHECK: warning: data race detected: @MainActor function at main/data_race_detection.swift:23 was not called on the main thread
6263
launchFromMainThread()
6364
sleep(1)
6465

6566
let actor = MyActor()
6667
let actorFn = await actor.getTaskOnMyActor()
6768
print("Launching an actor-instance task")
68-
// CHECK: warning: data race detected: actor-isolated function at main/data_race_detection.swift:51 was not called on the same actor
69+
// CHECK: warning: data race detected: actor-isolated function at main/data_race_detection.swift:52 was not called on the same actor
6970
launchTask(actorFn)
7071

7172
sleep(1)

test/Concurrency/Runtime/mainactor.swift

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// rdar://76038845
88
// REQUIRES: concurrency_runtime
99
// UNSUPPORTED: back_deployment_runtime
10+
// UNSUPPORTED: single_threaded_runtime
1011

1112
import Dispatch
1213

test/Interpreter/enforce_exclusive_access.swift

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// RUN: %target-codesign %t/a.out
55
// RUN: %target-run %t/a.out
66
// REQUIRES: executable_test
7+
// UNSUPPORTED: single_threaded_runtime
78

89
// Tests for traps at run time when enforcing exclusive access.
910

unittests/runtime/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND
8484
endif()
8585
endif()
8686

87+
if(NOT SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
88+
list(APPEND PLATFORM_SOURCES Mutex.cpp)
89+
endif()
90+
8791
if(SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED)
8892
# list(APPEND PLATFORM_SOURCES
8993
# DistributedActor.cpp
@@ -98,15 +102,15 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND
98102
weak.mm
99103
Refcounting.mm
100104
Actor.cpp
101-
TaskStatus.cpp)
105+
TaskStatus.cpp
106+
Mutex.cpp)
102107

103108
add_swift_unittest(SwiftRuntimeTests
104109
Array.cpp
105110
CompatibilityOverrideRuntime.cpp
106111
CompatibilityOverrideConcurrency.cpp
107112
Concurrent.cpp
108113
Metadata.cpp
109-
Mutex.cpp
110114
Enum.cpp
111115
Refcounting.cpp
112116
Stdlib.cpp

unittests/runtime/Concurrent.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ TEST(ConcurrentReadableArrayTest, SingleThreaded) {
4545
check();
4646
}
4747

48+
#ifndef SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
49+
4850
TEST(ConcurrentReadableArrayTest, MultiThreaded) {
4951
const int insertCount = 100000;
5052

@@ -542,3 +544,4 @@ TEST(ConcurrentReadableHashMapTest, MultiThreaded4) {
542544
runTest(16, 1);
543545
runTest(16, 8);
544546
}
547+
#endif // !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME

validation-test/Runtime/ConcurrentMetadata.swift

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %target-run-simple-swift
22
// REQUIRES: executable_test
3+
// UNSUPPORTED: single_threaded_runtime
34

45
// Exercise the metadata cache from multiple threads to shake out any
56
// concurrency bugs.

0 commit comments

Comments
 (0)