Skip to content

[Concurrency] Mark all of the custom global excecutor API as SPI for now. #82111

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions stdlib/public/Concurrency/CFExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ enum CoreFoundation {

// .. Main Executor ............................................................

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public final class CFMainExecutor: DispatchMainExecutor, @unchecked Sendable {

Expand All @@ -58,6 +59,7 @@ public final class CFMainExecutor: DispatchMainExecutor, @unchecked Sendable {

// .. Task Executor ............................................................

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public final class CFTaskExecutor: DispatchGlobalTaskExecutor,
@unchecked Sendable {
Expand Down
11 changes: 9 additions & 2 deletions stdlib/public/Concurrency/Clock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public protocol Clock<Duration>: Sendable {
#endif

/// The traits associated with this clock instance.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
var traits: ClockTraits { get }

Expand All @@ -59,6 +60,7 @@ public protocol Clock<Duration>: Sendable {
///
/// Returns: A `Swift.Duration` representing the equivalent duration, or
/// `nil` if this function is not supported.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
func convert(from duration: Duration) -> Swift.Duration?

Expand All @@ -70,6 +72,7 @@ public protocol Clock<Duration>: Sendable {
///
/// Returns: A `Duration` representing the equivalent duration, or
/// `nil` if this function is not supported.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
func convert(from duration: Swift.Duration) -> Duration?

Expand All @@ -82,6 +85,7 @@ public protocol Clock<Duration>: Sendable {
///
/// Returns: An `Instant` representing the equivalent instant, or
/// `nil` if this function is not supported.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
func convert<OtherClock: Clock>(instant: OtherClock.Instant,
from clock: OtherClock) -> Instant?
Expand Down Expand Up @@ -140,17 +144,19 @@ extension Clock {
}
}

@available(StdlibDeploymentTarget 6.2, *)
extension Clock {
// For compatibility, return `nil` if this is not implemented
@available(StdlibDeploymentTarget 6.2, *)
public func convert(from duration: Duration) -> Swift.Duration? {
return nil
}

@available(StdlibDeploymentTarget 6.2, *)
public func convert(from duration: Swift.Duration) -> Duration? {
return nil
}

@available(StdlibDeploymentTarget 6.2, *)
public func convert<OtherClock: Clock>(instant: OtherClock.Instant,
from clock: OtherClock) -> Instant? {
let ourNow = now
Expand All @@ -171,8 +177,8 @@ extension Clock {
}
}

@available(StdlibDeploymentTarget 6.2, *)
extension Clock where Duration == Swift.Duration {
@available(StdlibDeploymentTarget 6.2, *)
public func convert(from duration: Duration) -> Duration? {
return duration
}
Expand Down Expand Up @@ -207,6 +213,7 @@ extension Clock {
/// clocks best matches the clock that the user is trying to specify a
/// time or delay in. Executors are expected to do this on a best effort
/// basis.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public struct ClockTraits: OptionSet {
public let rawValue: UInt32
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/Concurrency/ContinuousClock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ extension ContinuousClock: Clock {
}

/// The continuous clock is continuous and monotonic
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public var traits: ClockTraits {
return [.continuous, .monotonic]
Expand Down
8 changes: 7 additions & 1 deletion stdlib/public/Concurrency/CooperativeExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ extension ExecutorJob {

/// A co-operative executor that can be used as the main executor or as a
/// task executor.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
class CooperativeExecutor: Executor, @unchecked Sendable {
public class CooperativeExecutor: Executor, @unchecked Sendable {
var runQueue: PriorityQueue<UnownedJob>
var waitQueue: PriorityQueue<UnownedJob>
var shouldStop: Bool = false
Expand Down Expand Up @@ -179,6 +180,7 @@ class CooperativeExecutor: Executor, @unchecked Sendable {
public var asSchedulable: any SchedulableExecutor { self }
}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
extension CooperativeExecutor: SchedulableExecutor {
var currentTime: Timestamp {
Expand All @@ -202,6 +204,7 @@ extension CooperativeExecutor: SchedulableExecutor {
}
}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
extension CooperativeExecutor: RunLoopExecutor {
public func run() throws {
Expand Down Expand Up @@ -249,12 +252,15 @@ extension CooperativeExecutor: RunLoopExecutor {
}
}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
extension CooperativeExecutor: SerialExecutor {}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
extension CooperativeExecutor: TaskExecutor {}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
extension CooperativeExecutor: MainExecutor {}

Expand Down
7 changes: 7 additions & 0 deletions stdlib/public/Concurrency/DispatchExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Swift

// .. Main Executor ............................................................

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public class DispatchMainExecutor: RunLoopExecutor, @unchecked Sendable {
var threaded = false
Expand All @@ -43,6 +44,7 @@ public class DispatchMainExecutor: RunLoopExecutor, @unchecked Sendable {
}
}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
extension DispatchMainExecutor: SerialExecutor {

Expand All @@ -57,6 +59,7 @@ extension DispatchMainExecutor: SerialExecutor {
}
}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
extension DispatchMainExecutor: SchedulableExecutor {
public var asSchedulable: SchedulableExecutor? {
Expand Down Expand Up @@ -85,11 +88,13 @@ extension DispatchMainExecutor: SchedulableExecutor {
}
}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
extension DispatchMainExecutor: MainExecutor {}

// .. Task Executor ............................................................

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public class DispatchGlobalTaskExecutor: TaskExecutor, SchedulableExecutor,
@unchecked Sendable {
Expand Down Expand Up @@ -201,10 +206,12 @@ extension DispatchExecutorProtocol {

}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
extension DispatchGlobalTaskExecutor: DispatchExecutorProtocol {
}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
extension DispatchMainExecutor: DispatchExecutorProtocol {
}
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/Concurrency/DummyExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Swift

// .. Main Executor ............................................................

@_spi(CustomDefaultExecutors)
@available(SwiftStdlib 6.2, *)
public final class DummyMainExecutor: MainExecutor, @unchecked Sendable {
public init() {}
Expand Down Expand Up @@ -45,6 +46,7 @@ public final class DummyMainExecutor: MainExecutor, @unchecked Sendable {

// .. Task Executor ............................................................

@_spi(CustomDefaultExecutors)
@available(SwiftStdlib 6.2, *)
public final class DummyTaskExecutor: TaskExecutor, @unchecked Sendable {
public init() {}
Expand Down
26 changes: 25 additions & 1 deletion stdlib/public/Concurrency/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public protocol Executor: AnyObject, Sendable {

#if !$Embedded
/// `true` if this is the main executor.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
var isMainExecutor: Bool { get }
#endif
}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public protocol SchedulableExecutor: Executor {

Expand All @@ -63,6 +65,7 @@ public protocol SchedulableExecutor: Executor {
/// - tolerance: The maximum additional delay permissible before the
/// job is executed. `nil` means no limit.
/// - clock: The clock used for the delay.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
func enqueue<C: Clock>(_ job: consuming ExecutorJob,
after delay: C.Duration,
Expand All @@ -83,6 +86,7 @@ public protocol SchedulableExecutor: Executor {
/// - tolerance: The maximum additional delay permissible before the
/// job is executed. `nil` means no limit.
/// - clock: The clock used for the delay..
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
func enqueue<C: Clock>(_ job: consuming ExecutorJob,
at instant: C.Instant,
Expand Down Expand Up @@ -160,6 +164,7 @@ extension SchedulableExecutor {

#if !$Embedded && !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public func enqueue<C: Clock>(_ job: consuming ExecutorJob,
after delay: C.Duration,
Expand All @@ -171,6 +176,7 @@ extension SchedulableExecutor {
tolerance: tolerance, clock: clock)
}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public func enqueue<C: Clock>(_ job: consuming ExecutorJob,
at instant: C.Instant,
Expand Down Expand Up @@ -360,6 +366,7 @@ public protocol SerialExecutor: Executor {
/// The default implementation returns `nil` is used to indicate that it is "unknown" if the current context is
/// isolated by this serial executor. The runtime then _may_ proceed to invoke `checkIsolated()` as a last-resort
/// attempt to verify the isolation of the current context.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
func isIsolatingCurrentContext() -> Bool?

Expand All @@ -369,6 +376,7 @@ public protocol SerialExecutor: Executor {
extension SerialExecutor {

#if !$Embedded && !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public var isMainExecutor: Bool { return MainActor.executor._isSameExecutor(self) }
#endif
Expand Down Expand Up @@ -515,6 +523,7 @@ extension SerialExecutor {
@available(StdlibDeploymentTarget 6.2, *)
extension SerialExecutor where Self: Equatable {

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public func isSameExclusiveExecutionContext(other: Self) -> Bool {
return self == other
Expand All @@ -527,6 +536,7 @@ extension SerialExecutor where Self: Equatable {
/// The idea here is that some executors may work by running a loop
/// that processes events of some sort; we want a way to enter that loop,
/// and we would also like a way to trigger the loop to exit.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public protocol RunLoopExecutor: Executor {
/// Run the executor's run loop.
Expand Down Expand Up @@ -558,6 +568,7 @@ public protocol RunLoopExecutor: Executor {
func stop()
}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
extension RunLoopExecutor {

Expand All @@ -570,13 +581,15 @@ extension RunLoopExecutor {

/// The main executor must conform to these three protocols; we have to
/// make this a protocol for compatibility with Embedded Swift.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public protocol MainExecutor: RunLoopExecutor, SerialExecutor {
}


/// An ExecutorFactory is used to create the default main and task
/// executors.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public protocol ExecutorFactory {
#if !$Embedded
Expand All @@ -590,9 +603,11 @@ public protocol ExecutorFactory {
static var defaultExecutor: any TaskExecutor { get }
}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
typealias DefaultExecutorFactory = PlatformExecutorFactory
public typealias DefaultExecutorFactory = PlatformExecutorFactory

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
@_silgen_name("swift_createExecutors")
public func _createExecutors<F: ExecutorFactory>(factory: F.Type) {
Expand Down Expand Up @@ -620,6 +635,7 @@ extension MainActor {
///
/// Attempting to set this after the first `enqueue` on the main
/// executor is a fatal error.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public static var executor: any MainExecutor {
// It would be good if there was a Swift way to do this
Expand All @@ -638,6 +654,7 @@ extension Task where Success == Never, Failure == Never {
///
/// Attempting to set this after the first `enqueue` on the global
/// executor is a fatal error.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public static var defaultExecutor: any TaskExecutor {
// It would be good if there was a Swift way to do this
Expand All @@ -658,6 +675,7 @@ extension Task where Success == Never, Failure == Never {
/// 3. The task executor for the current thread
///
/// If none of these exist, returns the default executor.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
@_unavailableInEmbedded
public static var currentExecutor: any Executor {
Expand All @@ -672,6 +690,7 @@ extension Task where Success == Never, Failure == Never {
}

/// Get the preferred executor for the current `Task`, if any.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public static var preferredExecutor: (any TaskExecutor)? {
if let taskExecutor = unsafe _getPreferredTaskExecutor().asTaskExecutor() {
Expand All @@ -684,6 +703,7 @@ extension Task where Success == Never, Failure == Never {
///
/// This follows the same logic as `currentExecutor`, except that it ignores
/// any executor that isn't a `SchedulableExecutor`.
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
@_unavailableInEmbedded
public static var currentSchedulableExecutor: (any SchedulableExecutor)? {
Expand Down Expand Up @@ -760,6 +780,7 @@ public struct UnownedSerialExecutor: Sendable {

/// Automatically opt-in to complex equality semantics if the Executor
/// implements `Equatable`.
@_spi(CustomDefaultExecutors)
@available(SwiftStdlib 6.2, *)
@inlinable
public init<E: SerialExecutor>(_ executor: __shared E) {
Expand All @@ -776,6 +797,7 @@ public struct UnownedSerialExecutor: Sendable {
unsafe _executor_isComplexEquality(self)
}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public func asSerialExecutor() -> (any SerialExecutor)? {
return unsafe unsafeBitCast(executor, to: (any SerialExecutor)?.self)
Expand Down Expand Up @@ -807,12 +829,14 @@ public struct UnownedTaskExecutor: Sendable {
unsafe self.executor = Builtin.buildOrdinaryTaskExecutorRef(executor)
}

@_spi(CustomDefaultExecutors)
@available(SwiftStdlib 6.2, *)
@inlinable
public init<E: TaskExecutor>(_ executor: __shared E) {
unsafe self.executor = Builtin.buildOrdinaryTaskExecutorRef(executor)
}

@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public func asTaskExecutor() -> (any TaskExecutor)? {
return unsafe unsafeBitCast(executor, to: (any TaskExecutor)?.self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import Swift

// This platform uses a single, global, CooperativeExecutor
@_spi(CustomDefaultExecutors)
@available(StdlibDeploymentTarget 6.2, *)
public struct PlatformExecutorFactory: ExecutorFactory {
static let executor = CooperativeExecutor()
Expand Down
Loading