Skip to content

[Concurrency] Fix issue with using Dispatch queues as executors. #81201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions stdlib/public/Concurrency/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,26 @@ extension SerialExecutor {
#endif
}

#if SWIFT_CONCURRENCY_USES_DISPATCH
@available(SwiftStdlib 6.2, *)
private var _dispatchQueue: OpaquePointer? {
return _getDispatchQueueForExecutor(self.asUnownedSerialExecutor())
}
#endif

@available(SwiftStdlib 6.2, *)
internal func _isSameExecutor(_ rhs: some SerialExecutor) -> Bool {
if rhs === self {
return true
}
#if SWIFT_CONCURRENCY_USES_DISPATCH
if let rhsQueue = rhs._dispatchQueue {
if let ourQueue = _dispatchQueue, ourQueue == rhsQueue {
return true
}
return false
}
#endif
if let rhs = rhs as? Self {
return isSameExclusiveExecutionContext(other: rhs)
}
Expand Down
12 changes: 11 additions & 1 deletion stdlib/public/Concurrency/ExecutorBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "Error.h"
#include "ExecutorBridge.h"
#include "TaskPrivate.h"

using namespace swift;

Expand Down Expand Up @@ -136,6 +137,15 @@ extern "C" SWIFT_CC(swift)
void swift_dispatchAssertMainQueue() {
dispatch_assert_queue(dispatch_get_main_queue());
}
#endif // SWIFT_CONCURRENCY_ENABLE_DISPATCH

extern "C" SWIFT_CC(swift)
void *swift_getDispatchQueueForExecutor(SerialExecutorRef executor) {
if (executor.getRawImplementation() == (uintptr_t)_swift_task_getDispatchQueueSerialExecutorWitnessTable()) {
return executor.getIdentity();
}
return nullptr;
}

#endif // SWIFT_CONCURRENCY_USES_DISPATCH

#pragma clang diagnostic pop
5 changes: 5 additions & 0 deletions stdlib/public/Concurrency/ExecutorBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,8 @@ internal func _dispatchEnqueueWithDeadline(_ global: CBool,
@available(SwiftStdlib 6.2, *)
@_silgen_name("swift_dispatchAssertMainQueue")
internal func _dispatchAssertMainQueue()

@_silgen_name("swift_getDispatchQueueForExecutor")
internal func _getDispatchQueueForExecutor(
_ executor: UnownedSerialExecutor
) -> OpaquePointer?
5 changes: 5 additions & 0 deletions stdlib/public/Concurrency/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ static SerialExecutorRef executorForEnqueuedJob(Job *job) {
return swift_task_getMainExecutor();
}

if (auto identity = reinterpret_cast<HeapObject *>(jobQueue)) {
return SerialExecutorRef::forOrdinary(
identity, _swift_task_getDispatchQueueSerialExecutorWitnessTable());
}

return SerialExecutorRef::generic();
#endif
}
Expand Down