Skip to content

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

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
Apr 22, 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 @@ -546,6 +546,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 @@ -558,9 +566,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 @@ -577,9 +584,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
Copy link
Contributor

@beccadax beccadax Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't something like this get thread-safe lazy initialization? I believe we do that for static lets.

public static let defaultExector: any TaskExecutor = DefaultExecutorFactory.defaultExecutor

edit: come to think of it this might need to be moved into a backing global, but the point stands.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit more complicated than it looks :-)

Yes, initialising a static variable does happen in a thread-safe manner, but we don't actually want to initialise these to the default executor (since that means constructing the default executor always, even when we don't actually want it, and the act of constructing it might have side effects).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could leverage static variable initialisation, but I think to do so we'd need to add an extra variable and make the static initialisation be a closure call. I'm not sure that's really an improvement overall. What would be an improvement is the ability to do a once explicitly and directly in Swift, since that's what we really want here.

_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"

Expand All @@ -27,6 +29,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 @@ -119,3 +119,6 @@ internal func _dispatchEnqueueWithDeadline(_ global: CBool,
@available(SwiftStdlib 6.2, *)
@_silgen_name("swift_dispatchAssertMainQueue")
internal func _dispatchAssertMainQueue()

@_silgen_name("swift_createDefaultExecutorsOnce")
func _createDefaultExecutorsOnce()