Skip to content

[Concurrency] Make initial executor construction fully thread safe. #81326

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 12, 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
18 changes: 12 additions & 6 deletions stdlib/public/Concurrency/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,14 @@ public func _createExecutors<F: ExecutorFactory>(factory: F.Type) {
Task._defaultExecutor = factory.defaultExecutor
}

@available(SwiftStdlib 6.2, *)
@_silgen_name("swift_createDefaultExecutors")
func _createDefaultExecutors() {
if Task._defaultExecutor == nil {
_createExecutors(factory: DefaultExecutorFactory.self)
}
}

#if !$Embedded && !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
extension MainActor {
@available(SwiftStdlib 6.2, *)
Expand All @@ -606,9 +614,8 @@ extension MainActor {
/// executor is a fatal error.
@available(SwiftStdlib 6.2, *)
public static var executor: any MainExecutor {
if _executor == nil {
_executor = DefaultExecutorFactory.mainExecutor
}
// It would be good if there was a Swift way to do this
_createDefaultExecutorsOnce()
return _executor!
}
}
Expand All @@ -625,9 +632,8 @@ extension Task where Success == Never, Failure == Never {
/// executor is a fatal error.
@available(SwiftStdlib 6.2, *)
public static var defaultExecutor: any TaskExecutor {
if _defaultExecutor == nil {
_defaultExecutor = DefaultExecutorFactory.defaultExecutor
}
// It would be good if there was a Swift way to do this
_createDefaultExecutorsOnce()
return _defaultExecutor!
}
}
Expand Down
9 changes: 9 additions & 0 deletions stdlib/public/Concurrency/ExecutorBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <dispatch/dispatch.h>
#endif

#include "swift/Threading/Once.h"

#include "Error.h"
#include "ExecutorBridge.h"
#include "TaskPrivate.h"
Expand All @@ -28,6 +30,13 @@ void _swift_exit(int result) {
exit(result);
}

extern "C" SWIFT_CC(swift)
void swift_createDefaultExecutorsOnce() {
static swift::once_t createExecutorsOnce;

swift::once(createExecutorsOnce, swift_createDefaultExecutors);
}

#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
extern "C" SWIFT_CC(swift)
SerialExecutorRef swift_getMainExecutor() {
Expand Down
8 changes: 7 additions & 1 deletion stdlib/public/Concurrency/ExecutorBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ void swift_destroyDispatchEventC(void *event);
extern "C" SWIFT_CC(swift)
void swift_signalDispatchEvent(void *event);
#endif // !SWIFT_CONCURRENCY_EMBEDDED

extern "C" SWIFT_CC(swift) __attribute__((noreturn))
void swift_dispatchMain();

extern "C" SWIFT_CC(swift)
void swift_createDefaultExecutors();

extern "C" SWIFT_CC(swift)
void swift_createDefaultExecutorsOnce();

#pragma clang diagnostic pop

} // namespace swift
Expand Down
3 changes: 3 additions & 0 deletions stdlib/public/Concurrency/ExecutorBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,6 @@ internal func _dispatchAssertMainQueue()
internal func _getDispatchQueueForExecutor(
_ executor: UnownedSerialExecutor
) -> OpaquePointer?

@_silgen_name("swift_createDefaultExecutorsOnce")
func _createDefaultExecutorsOnce()