Skip to content

Commit c072992

Browse files
authored
Merge pull request #81201 from al45tair/eng/PR-150310927-6.2
[Concurrency] Fix issue with using Dispatch queues as executors.
2 parents 5643f53 + 7f5f45c commit c072992

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

stdlib/public/Concurrency/Executor.swift

+15
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,26 @@ extension SerialExecutor {
340340
#endif
341341
}
342342

343+
#if SWIFT_CONCURRENCY_USES_DISPATCH
344+
@available(SwiftStdlib 6.2, *)
345+
private var _dispatchQueue: OpaquePointer? {
346+
return _getDispatchQueueForExecutor(self.asUnownedSerialExecutor())
347+
}
348+
#endif
349+
343350
@available(SwiftStdlib 6.2, *)
344351
internal func _isSameExecutor(_ rhs: some SerialExecutor) -> Bool {
345352
if rhs === self {
346353
return true
347354
}
355+
#if SWIFT_CONCURRENCY_USES_DISPATCH
356+
if let rhsQueue = rhs._dispatchQueue {
357+
if let ourQueue = _dispatchQueue, ourQueue == rhsQueue {
358+
return true
359+
}
360+
return false
361+
}
362+
#endif
348363
if let rhs = rhs as? Self {
349364
return isSameExclusiveExecutionContext(other: rhs)
350365
}

stdlib/public/Concurrency/ExecutorBridge.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "Error.h"
1818
#include "ExecutorBridge.h"
19+
#include "TaskPrivate.h"
1920

2021
using namespace swift;
2122

@@ -136,6 +137,15 @@ extern "C" SWIFT_CC(swift)
136137
void swift_dispatchAssertMainQueue() {
137138
dispatch_assert_queue(dispatch_get_main_queue());
138139
}
139-
#endif // SWIFT_CONCURRENCY_ENABLE_DISPATCH
140+
141+
extern "C" SWIFT_CC(swift)
142+
void *swift_getDispatchQueueForExecutor(SerialExecutorRef executor) {
143+
if (executor.getRawImplementation() == (uintptr_t)_swift_task_getDispatchQueueSerialExecutorWitnessTable()) {
144+
return executor.getIdentity();
145+
}
146+
return nullptr;
147+
}
148+
149+
#endif // SWIFT_CONCURRENCY_USES_DISPATCH
140150

141151
#pragma clang diagnostic pop

stdlib/public/Concurrency/ExecutorBridge.swift

+5
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,8 @@ internal func _dispatchEnqueueWithDeadline(_ global: CBool,
119119
@available(SwiftStdlib 6.2, *)
120120
@_silgen_name("swift_dispatchAssertMainQueue")
121121
internal func _dispatchAssertMainQueue()
122+
123+
@_silgen_name("swift_getDispatchQueueForExecutor")
124+
internal func _getDispatchQueueForExecutor(
125+
_ executor: UnownedSerialExecutor
126+
) -> OpaquePointer?

stdlib/public/Concurrency/Task.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ static SerialExecutorRef executorForEnqueuedJob(Job *job) {
382382
return swift_task_getMainExecutor();
383383
}
384384

385+
if (auto identity = reinterpret_cast<HeapObject *>(jobQueue)) {
386+
return SerialExecutorRef::forOrdinary(
387+
identity, _swift_task_getDispatchQueueSerialExecutorWitnessTable());
388+
}
389+
385390
return SerialExecutorRef::generic();
386391
#endif
387392
}

0 commit comments

Comments
 (0)