Skip to content
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
22 changes: 20 additions & 2 deletions stdlib/public/Concurrency/CFExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,41 @@ enum CoreFoundation {

static let CFRunLoopRun: @convention(c) () -> () =
symbol("CFRunLoopRun")
static let CFRunLoopRunInMode:
@convention(c) (AnyObject, Double, CBool) -> Int32 =
symbol("CFRunLoopRunInMode")
static let CFRunLoopGetMain: @convention(c) () -> OpaquePointer =
unsafe symbol("CFRunLoopGetMain")
static let CFRunLoopStop: @convention(c) (OpaquePointer) -> () =
unsafe symbol("CFRunLoopStop")

private static let _kCFRunLoopCommonModes: UnsafePointer<AnyObject> =
unsafe symbol("kCFRunLoopCommonModes")
static var kCFRunLoopCommonModes: AnyObject {
return unsafe _kCFRunLoopCommonModes.pointee
}
}

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

/// A CFRunLoop-based main executor (Apple platforms only)
@available(StdlibDeploymentTarget 6.2, *)
final class CFMainExecutor: DispatchMainExecutor, @unchecked Sendable {
final class CFMainExecutor: DispatchMainExecutor, RunLoopExecutor,
@unchecked Sendable {

override public func run() throws {
CoreFoundation.CFRunLoopRun()
}

override public func stop() {
public func runUntil(_ condition: () -> Bool) throws {
while !condition() {
CoreFoundation.CFRunLoopRunInMode(CoreFoundation.kCFRunLoopCommonModes,
0,
false)
}
}

public func stop() {
unsafe CoreFoundation.CFRunLoopStop(CoreFoundation.CFRunLoopGetMain())
}

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

/// A Dispatch-based main executor.
@available(StdlibDeploymentTarget 6.2, *)
class DispatchMainExecutor: RunLoopExecutor, SchedulingExecutor,
class DispatchMainExecutor: ThreadDonationExecutor, SchedulingExecutor,
@unchecked Sendable {
var threaded = false

Expand All @@ -40,10 +40,6 @@ class DispatchMainExecutor: RunLoopExecutor, SchedulingExecutor,
_dispatchMain()
}

public func stop() {
fatalError("DispatchMainExecutor cannot be stopped")
}

var asScheduling: (any SchedulingExecutor)? {
return self
}
Expand Down
52 changes: 35 additions & 17 deletions stdlib/public/Concurrency/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,22 @@ extension SerialExecutor where Self: Equatable {

}

/// An executor that can take over a thread.
@available(StdlibDeploymentTarget 6.2, *)
public protocol ThreadDonationExecutor: Executor {
/// Donate the calling thread to this executor.
///
/// This method will synchronously block the calling thread.
func run() throws
}

/// An executor that is backed by some kind of run loop.
///
/// 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.
@available(StdlibDeploymentTarget 6.2, *)
public protocol RunLoopExecutor: Executor {
public protocol RunLoopExecutor: SerialExecutor, ThreadDonationExecutor {
/// Run the executor's run loop.
///
/// This method will synchronously block the calling thread. Nested calls to
Expand All @@ -552,10 +561,6 @@ public protocol RunLoopExecutor: Executor {

/// Run the executor's run loop until a condition is satisfied.
///
/// Not every `RunLoopExecutor` will support this method; you must not call
/// it unless you *know* that it is supported. The default implementation
/// generates a fatal error.
///
/// Parameters:
///
/// - condition: A closure that returns `true` if the run loop should
Expand All @@ -572,20 +577,17 @@ public protocol RunLoopExecutor: Executor {
func stop()
}

@available(StdlibDeploymentTarget 6.2, *)
extension RunLoopExecutor {

public func runUntil(_ condition: () -> Bool) throws {
fatalError("run(until condition:) not supported on this executor")
}

}


/// The main executor must conform to these two protocols; we have to
/// make this a protocol for compatibility with Embedded Swift.
/// The main executor protocol only includes the `run` method; some main
/// executors may not support `runUntil` or `stop`.
@available(StdlibDeploymentTarget 6.2, *)
public protocol MainExecutor: RunLoopExecutor, SerialExecutor {
public protocol MainExecutor: SerialExecutor, ThreadDonationExecutor {
/// Run the executor's run loop.
///
/// This method will synchronously block the calling thread. Nested calls to
/// `run()` may be permitted, however it is not permitted to call `run()` on a
/// single executor instance from more than one thread.
func run() throws
}


Expand All @@ -604,6 +606,22 @@ public protocol ExecutorFactory {
static var defaultExecutor: any TaskExecutor { get }
}

// Provide default implementations so you can override just one of the two
@available(StdlibDeploymentTarget 6.2, *)
public extension ExecutorFactory {
#if os(WASI) || !$Embedded
@available(StdlibDeploymentTarget 6.2, *)
public static var mainExecutor: any MainExecutor {
return DefaultExecutorFactory.mainExecutor
}
#endif

@available(StdlibDeploymentTarget 6.2, *)
public static var defaultExecutor: any TaskExecutor {
return DefaultExecutorFactory.defaultExecutor
}
}

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

Expand Down
18 changes: 10 additions & 8 deletions stdlib/public/Concurrency/PartialAsyncTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ public struct ExecutorJob: Sendable, ~Copyable {
/// referenced from the private data area are cleared up prior to running the
/// job.
///
/// The size and alignment of the private data buffer are both twice the
/// machine word size (i.e. `2 * sizeof(void *)`, or `2 * MemoryLayout<UInt>`).
///
/// Parameters:
///
/// - body: The closure to execute.
Expand All @@ -337,28 +340,27 @@ public struct ExecutorJob: Sendable, ~Copyable {

/// Kinds of schedulable jobs
@available(StdlibDeploymentTarget 6.2, *)
@frozen
public struct Kind: Sendable, RawRepresentable {
public typealias RawValue = UInt8
struct Kind: Sendable, RawRepresentable {
typealias RawValue = UInt8

/// The raw job kind value.
public var rawValue: RawValue
var rawValue: RawValue

/// Creates a new instance with the specified raw value.
public init?(rawValue: Self.RawValue) {
init?(rawValue: Self.RawValue) {
self.rawValue = rawValue
}

/// A task.
public static let task = Kind(rawValue: RawValue(0))!
static let task = Kind(rawValue: RawValue(0))!

// Job kinds >= 192 are private to the implementation.
public static let firstReserved = Kind(rawValue: RawValue(192))!
static let firstReserved = Kind(rawValue: RawValue(192))!
}

/// What kind of job this is.
@available(StdlibDeploymentTarget 6.2, *)
public var kind: Kind {
var kind: Kind {
return Kind(rawValue: _jobGetKind(self.context))!
}

Expand Down
35 changes: 14 additions & 21 deletions test/abi/Inputs/macOS/arm64/concurrency/baseline
Original file line number Diff line number Diff line change
Expand Up @@ -421,20 +421,6 @@ _$ss11ExecutorJobV14LocalAllocatorVMa
_$ss11ExecutorJobV14LocalAllocatorVMn
_$ss11ExecutorJobV14LocalAllocatorVN
_$ss11ExecutorJobV16createTrampoline2toABx_tScFRzlF
_$ss11ExecutorJobV4KindV13firstReservedADvgZ
_$ss11ExecutorJobV4KindV13firstReservedADvpZMV
_$ss11ExecutorJobV4KindV4taskADvgZ
_$ss11ExecutorJobV4KindV4taskADvpZMV
_$ss11ExecutorJobV4KindV8rawValueADSgs5UInt8V_tcfC
_$ss11ExecutorJobV4KindV8rawValues5UInt8VvM
_$ss11ExecutorJobV4KindV8rawValues5UInt8Vvg
_$ss11ExecutorJobV4KindV8rawValues5UInt8VvpMV
_$ss11ExecutorJobV4KindV8rawValues5UInt8Vvs
_$ss11ExecutorJobV4KindVMa
_$ss11ExecutorJobV4KindVMn
_$ss11ExecutorJobV4KindVN
_$ss11ExecutorJobV4KindVSYsMc
_$ss11ExecutorJobV4kindAB4KindVvg
_$ss11ExecutorJobV7contextABBjn_tcfC
_$ss11ExecutorJobV8prioritys0B8PriorityVvg
_$ss11ExecutorJobV9allocatorAB14LocalAllocatorVSgvg
Expand Down Expand Up @@ -472,7 +458,7 @@ _$ss11JobPriorityVSQsMc
_$ss11JobPriorityVyABScPcfC
_$ss12MainExecutorMp
_$ss12MainExecutorPScfTb
_$ss12MainExecutorPs07RunLoopB0Tb
_$ss12MainExecutorPs014ThreadDonationB0Tb
_$ss12MainExecutorTL
_$ss13_runAsyncMainyyyyYaKcF
_$ss13withTaskGroup2of9returning4bodyq_xm_q_mq_ScGyxGzYaXEtYar0_lF
Expand Down Expand Up @@ -516,16 +502,18 @@ _$ss15ExecutorFactoryP04mainA0s04MainA0_pvgZTj
_$ss15ExecutorFactoryP04mainA0s04MainA0_pvgZTq
_$ss15ExecutorFactoryP07defaultA0Sch_pvgZTj
_$ss15ExecutorFactoryP07defaultA0Sch_pvgZTq
_$ss15ExecutorFactoryPsE04mainA0s04MainA0_pvgZ
_$ss15ExecutorFactoryPsE04mainA0s04MainA0_pvpZMV
_$ss15ExecutorFactoryPsE07defaultA0Sch_pvgZ
_$ss15ExecutorFactoryPsE07defaultA0Sch_pvpZMV
_$ss15ExecutorFactoryTL
_$ss15RunLoopExecutorMp
_$ss15RunLoopExecutorP3runyyKFTj
_$ss15RunLoopExecutorP3runyyKFTq
_$ss15RunLoopExecutorP4stopyyFTj
_$ss15RunLoopExecutorP4stopyyFTq
_$ss15RunLoopExecutorP8runUntilyySbyXEKFTj
_$ss15RunLoopExecutorP8runUntilyySbyXEKFTq
_$ss15RunLoopExecutorPScFTb
_$ss15RunLoopExecutorPsE8runUntilyySbyXEKF
_$ss15RunLoopExecutorPScfTb
_$ss15RunLoopExecutorPs014ThreadDonationC0Tb
_$ss15RunLoopExecutorTL
_$ss15SuspendingClockV17minimumResolutions8DurationVvg
_$ss15SuspendingClockV17minimumResolutions8DurationVvpMV
Expand Down Expand Up @@ -734,6 +722,11 @@ _$ss22AsyncDropWhileSequenceVMa
_$ss22AsyncDropWhileSequenceVMn
_$ss22AsyncDropWhileSequenceV_9predicateAByxGx_Sb7ElementQzYactcfC
_$ss22AsyncDropWhileSequenceVyxGScisMc
_$ss22ThreadDonationExecutorMp
_$ss22ThreadDonationExecutorP3runyyKFTj
_$ss22ThreadDonationExecutorP3runyyKFTq
_$ss22ThreadDonationExecutorPScFTb
_$ss22ThreadDonationExecutorTL
_$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF
_$ss23AsyncCompactMapSequenceV04makeA8IteratorAB0F0Vyxq__GyF
_$ss23AsyncCompactMapSequenceV4basexvg
Expand Down Expand Up @@ -841,8 +834,8 @@ _$ss25UnimplementedMainExecutorCScfsMc
_$ss25UnimplementedMainExecutorCScfsWP
_$ss25UnimplementedMainExecutorCfD
_$ss25UnimplementedMainExecutorCfd
_$ss25UnimplementedMainExecutorCs07RunLoopC0sMc
_$ss25UnimplementedMainExecutorCs07RunLoopC0sWP
_$ss25UnimplementedMainExecutorCs014ThreadDonationC0sMc
_$ss25UnimplementedMainExecutorCs014ThreadDonationC0sWP
_$ss25UnimplementedMainExecutorCs0bC0sMc
_$ss25UnimplementedMainExecutorCs0bC0sWP
_$ss25UnimplementedTaskExecutorC06isMainC0Sbvg
Expand Down
35 changes: 14 additions & 21 deletions test/abi/Inputs/macOS/arm64/concurrency/baseline-asserts
Original file line number Diff line number Diff line change
Expand Up @@ -421,20 +421,6 @@ _$ss11ExecutorJobV14LocalAllocatorVMa
_$ss11ExecutorJobV14LocalAllocatorVMn
_$ss11ExecutorJobV14LocalAllocatorVN
_$ss11ExecutorJobV16createTrampoline2toABx_tScFRzlF
_$ss11ExecutorJobV4KindV13firstReservedADvgZ
_$ss11ExecutorJobV4KindV13firstReservedADvpZMV
_$ss11ExecutorJobV4KindV4taskADvgZ
_$ss11ExecutorJobV4KindV4taskADvpZMV
_$ss11ExecutorJobV4KindV8rawValueADSgs5UInt8V_tcfC
_$ss11ExecutorJobV4KindV8rawValues5UInt8VvM
_$ss11ExecutorJobV4KindV8rawValues5UInt8Vvg
_$ss11ExecutorJobV4KindV8rawValues5UInt8VvpMV
_$ss11ExecutorJobV4KindV8rawValues5UInt8Vvs
_$ss11ExecutorJobV4KindVMa
_$ss11ExecutorJobV4KindVMn
_$ss11ExecutorJobV4KindVN
_$ss11ExecutorJobV4KindVSYsMc
_$ss11ExecutorJobV4kindAB4KindVvg
_$ss11ExecutorJobV7contextABBjn_tcfC
_$ss11ExecutorJobV8prioritys0B8PriorityVvg
_$ss11ExecutorJobV9allocatorAB14LocalAllocatorVSgvg
Expand Down Expand Up @@ -472,7 +458,7 @@ _$ss11JobPriorityVSQsMc
_$ss11JobPriorityVyABScPcfC
_$ss12MainExecutorMp
_$ss12MainExecutorPScfTb
_$ss12MainExecutorPs07RunLoopB0Tb
_$ss12MainExecutorPs014ThreadDonationB0Tb
_$ss12MainExecutorTL
_$ss13_runAsyncMainyyyyYaKcF
_$ss13withTaskGroup2of9returning4bodyq_xm_q_mq_ScGyxGzYaXEtYar0_lF
Expand Down Expand Up @@ -516,16 +502,18 @@ _$ss15ExecutorFactoryP04mainA0s04MainA0_pvgZTj
_$ss15ExecutorFactoryP04mainA0s04MainA0_pvgZTq
_$ss15ExecutorFactoryP07defaultA0Sch_pvgZTj
_$ss15ExecutorFactoryP07defaultA0Sch_pvgZTq
_$ss15ExecutorFactoryPsE04mainA0s04MainA0_pvgZ
_$ss15ExecutorFactoryPsE04mainA0s04MainA0_pvpZMV
_$ss15ExecutorFactoryPsE07defaultA0Sch_pvgZ
_$ss15ExecutorFactoryPsE07defaultA0Sch_pvpZMV
_$ss15ExecutorFactoryTL
_$ss15RunLoopExecutorMp
_$ss15RunLoopExecutorP3runyyKFTj
_$ss15RunLoopExecutorP3runyyKFTq
_$ss15RunLoopExecutorP4stopyyFTj
_$ss15RunLoopExecutorP4stopyyFTq
_$ss15RunLoopExecutorP8runUntilyySbyXEKFTj
_$ss15RunLoopExecutorP8runUntilyySbyXEKFTq
_$ss15RunLoopExecutorPScFTb
_$ss15RunLoopExecutorPsE8runUntilyySbyXEKF
_$ss15RunLoopExecutorPScfTb
_$ss15RunLoopExecutorPs014ThreadDonationC0Tb
_$ss15RunLoopExecutorTL
_$ss15SuspendingClockV17minimumResolutions8DurationVvg
_$ss15SuspendingClockV17minimumResolutions8DurationVvpMV
Expand Down Expand Up @@ -734,6 +722,11 @@ _$ss22AsyncDropWhileSequenceVMa
_$ss22AsyncDropWhileSequenceVMn
_$ss22AsyncDropWhileSequenceV_9predicateAByxGx_Sb7ElementQzYactcfC
_$ss22AsyncDropWhileSequenceVyxGScisMc
_$ss22ThreadDonationExecutorMp
_$ss22ThreadDonationExecutorP3runyyKFTj
_$ss22ThreadDonationExecutorP3runyyKFTq
_$ss22ThreadDonationExecutorPScFTb
_$ss22ThreadDonationExecutorTL
_$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF
_$ss23AsyncCompactMapSequenceV04makeA8IteratorAB0F0Vyxq__GyF
_$ss23AsyncCompactMapSequenceV4basexvg
Expand Down Expand Up @@ -841,8 +834,8 @@ _$ss25UnimplementedMainExecutorCScfsMc
_$ss25UnimplementedMainExecutorCScfsWP
_$ss25UnimplementedMainExecutorCfD
_$ss25UnimplementedMainExecutorCfd
_$ss25UnimplementedMainExecutorCs07RunLoopC0sMc
_$ss25UnimplementedMainExecutorCs07RunLoopC0sWP
_$ss25UnimplementedMainExecutorCs014ThreadDonationC0sMc
_$ss25UnimplementedMainExecutorCs014ThreadDonationC0sWP
_$ss25UnimplementedMainExecutorCs0bC0sMc
_$ss25UnimplementedMainExecutorCs0bC0sWP
_$ss25UnimplementedTaskExecutorC06isMainC0Sbvg
Expand Down
Loading