@@ -36,10 +36,24 @@ public protocol Executor: AnyObject, Sendable {
36
36
func enqueue( _ job: consuming ExecutorJob )
37
37
#endif // !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
38
38
39
+ #if !$Embedded
40
+ /// `true` if this is the main executor.
41
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
42
+ var isMainExecutor : Bool { get }
43
+
44
+ /// Return this executable as a SchedulingExecutor, or nil if that is
45
+ /// unsupported.
46
+ ///
47
+ /// Executors that implement SchedulingExecutor should provide their
48
+ /// own copy of this method, which will allow the compiler to avoid a
49
+ /// potentially expensive runtime cast.
50
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
51
+ var asSchedulingExecutor : ( any SchedulingExecutor ) ? { get }
52
+ #endif
39
53
}
40
54
41
55
@available ( StdlibDeploymentTarget 6 . 2 , * )
42
- protocol SchedulingExecutor : Executor {
56
+ public protocol SchedulingExecutor : Executor {
43
57
44
58
#if !$Embedded && !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
45
59
@@ -58,6 +72,7 @@ protocol SchedulingExecutor: Executor {
58
72
/// - tolerance: The maximum additional delay permissible before the
59
73
/// job is executed. `nil` means no limit.
60
74
/// - clock: The clock used for the delay.
75
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
61
76
func enqueue< C: Clock > ( _ job: consuming ExecutorJob ,
62
77
after delay: C . Duration ,
63
78
tolerance: C . Duration ? ,
@@ -77,6 +92,7 @@ protocol SchedulingExecutor: Executor {
77
92
/// - tolerance: The maximum additional delay permissible before the
78
93
/// job is executed. `nil` means no limit.
79
94
/// - clock: The clock used for the delay..
95
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
80
96
func enqueue< C: Clock > ( _ job: consuming ExecutorJob ,
81
97
at instant: C . Instant ,
82
98
tolerance: C . Duration ? ,
@@ -113,7 +129,7 @@ extension Executor {
113
129
/// own copy of this method, which will allow the compiler to avoid a
114
130
/// potentially expensive runtime cast.
115
131
@available ( StdlibDeploymentTarget 6 . 2 , * )
116
- var asSchedulingExecutor : ( any SchedulingExecutor ) ? {
132
+ public var asSchedulingExecutor : ( any SchedulingExecutor ) ? {
117
133
return self as? SchedulingExecutor
118
134
}
119
135
#endif
@@ -136,7 +152,7 @@ extension Executor {
136
152
137
153
#if !$Embedded
138
154
@available ( StdlibDeploymentTarget 6 . 2 , * )
139
- var isMainExecutor : Bool { false }
155
+ public var isMainExecutor : Bool { false }
140
156
#endif
141
157
142
158
}
@@ -147,20 +163,22 @@ extension SchedulingExecutor {
147
163
148
164
#if !$Embedded && !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
149
165
150
- func enqueue< C: Clock > ( _ job: consuming ExecutorJob ,
151
- after delay: C . Duration ,
152
- tolerance: C . Duration ? = nil ,
153
- clock: C ) {
166
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
167
+ public func enqueue< C: Clock > ( _ job: consuming ExecutorJob ,
168
+ after delay: C . Duration ,
169
+ tolerance: C . Duration ? = nil ,
170
+ clock: C ) {
154
171
// If you crash here with a mutual recursion, it's because you didn't
155
172
// implement one of these two functions
156
173
enqueue ( job, at: clock. now. advanced ( by: delay) ,
157
174
tolerance: tolerance, clock: clock)
158
175
}
159
176
160
- func enqueue< C: Clock > ( _ job: consuming ExecutorJob ,
161
- at instant: C . Instant ,
162
- tolerance: C . Duration ? = nil ,
163
- clock: C ) {
177
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
178
+ public func enqueue< C: Clock > ( _ job: consuming ExecutorJob ,
179
+ at instant: C . Instant ,
180
+ tolerance: C . Duration ? = nil ,
181
+ clock: C ) {
164
182
// If you crash here with a mutual recursion, it's because you didn't
165
183
// implement one of these two functions
166
184
enqueue ( job, after: clock. now. duration ( to: instant) ,
@@ -355,7 +373,7 @@ extension SerialExecutor {
355
373
356
374
#if !$Embedded && !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
357
375
@available ( StdlibDeploymentTarget 6 . 2 , * )
358
- var isMainExecutor : Bool { return MainActor . executor. _isSameExecutor ( self ) }
376
+ public var isMainExecutor : Bool { return MainActor . executor. _isSameExecutor ( self ) }
359
377
#endif
360
378
361
379
@available ( StdlibDeploymentTarget 6 . 0 , * )
@@ -510,7 +528,8 @@ extension SerialExecutor where Self: Equatable {
510
528
/// The idea here is that some executors may work by running a loop
511
529
/// that processes events of some sort; we want a way to enter that loop,
512
530
/// and we would also like a way to trigger the loop to exit.
513
- protocol RunLoopExecutor : Executor {
531
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
532
+ public protocol RunLoopExecutor : Executor {
514
533
/// Run the executor's run loop.
515
534
///
516
535
/// This method will synchronously block the calling thread. Nested calls to
@@ -540,9 +559,10 @@ protocol RunLoopExecutor: Executor {
540
559
func stop( )
541
560
}
542
561
562
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
543
563
extension RunLoopExecutor {
544
564
545
- func runUntil( _ condition: ( ) -> Bool ) throws {
565
+ public func runUntil( _ condition: ( ) -> Bool ) throws {
546
566
fatalError ( " run(until condition:) not supported on this executor " )
547
567
}
548
568
@@ -551,14 +571,15 @@ extension RunLoopExecutor {
551
571
552
572
/// The main executor must conform to these two protocols; we have to
553
573
/// make this a protocol for compatibility with Embedded Swift.
554
- protocol MainExecutor : RunLoopExecutor , SerialExecutor {
574
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
575
+ public protocol MainExecutor : RunLoopExecutor , SerialExecutor {
555
576
}
556
577
557
578
558
579
/// An ExecutorFactory is used to create the default main and task
559
580
/// executors.
560
581
@available ( StdlibDeploymentTarget 6 . 2 , * )
561
- protocol ExecutorFactory {
582
+ public protocol ExecutorFactory {
562
583
#if !$Embedded
563
584
/// Constructs and returns the main executor, which is started implicitly
564
585
/// by the `async main` entry point and owns the "main" thread.
@@ -575,7 +596,7 @@ typealias DefaultExecutorFactory = PlatformExecutorFactory
575
596
576
597
@available ( StdlibDeploymentTarget 6 . 2 , * )
577
598
@_silgen_name ( " swift_createExecutors " )
578
- func _createExecutors< F: ExecutorFactory > ( factory: F . Type ) {
599
+ public func _createExecutors< F: ExecutorFactory > ( factory: F . Type ) {
579
600
#if !$Embedded && !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
580
601
MainActor . _executor = factory. mainExecutor
581
602
#endif
@@ -600,7 +621,8 @@ extension MainActor {
600
621
///
601
622
/// Attempting to set this after the first `enqueue` on the main
602
623
/// executor is a fatal error.
603
- static var executor : any MainExecutor {
624
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
625
+ public static var executor : any MainExecutor {
604
626
// It would be good if there was a Swift way to do this
605
627
_createDefaultExecutorsOnce ( )
606
628
return _executor!
@@ -624,11 +646,19 @@ extension Task where Success == Never, Failure == Never {
624
646
///
625
647
/// Attempting to set this after the first `enqueue` on the global
626
648
/// executor is a fatal error.
627
- static var defaultExecutor : any TaskExecutor {
649
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
650
+ public static var defaultExecutor : any TaskExecutor {
628
651
// It would be good if there was a Swift way to do this
629
652
_createDefaultExecutorsOnce ( )
630
653
return _defaultExecutor!
631
654
}
655
+
656
+ /// An unowned version of the above, for performance
657
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
658
+ static var unownedDefaultExecutor : UnownedTaskExecutor {
659
+ _createDefaultExecutorsOnce ( )
660
+ return unsafe UnownedTaskExecutor( _defaultExecutor!)
661
+ }
632
662
}
633
663
634
664
@available ( StdlibDeploymentTarget 6 . 2 , * )
@@ -644,8 +674,9 @@ extension Task where Success == Never, Failure == Never {
644
674
/// 3. The task executor for the current thread
645
675
///
646
676
/// If none of these exist, returns the default executor.
677
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
647
678
@_unavailableInEmbedded
648
- static var currentExecutor : any Executor {
679
+ public static var currentExecutor : any Executor {
649
680
if let activeExecutor = unsafe _getActiveExecutor( ) . asSerialExecutor ( ) {
650
681
return activeExecutor
651
682
} else if let taskExecutor = unsafe _getPreferredTaskExecutor( ) . asTaskExecutor ( ) {
@@ -657,7 +688,8 @@ extension Task where Success == Never, Failure == Never {
657
688
}
658
689
659
690
/// Get the preferred executor for the current `Task`, if any.
660
- static var preferredExecutor : ( any TaskExecutor ) ? {
691
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
692
+ public static var preferredExecutor : ( any TaskExecutor ) ? {
661
693
if let taskExecutor = unsafe _getPreferredTaskExecutor( ) . asTaskExecutor ( ) {
662
694
return taskExecutor
663
695
}
@@ -670,7 +702,7 @@ extension Task where Success == Never, Failure == Never {
670
702
/// This follows the same logic as `currentExecutor`, except that it ignores
671
703
/// any executor that isn't a `SchedulingExecutor`.
672
704
@available ( StdlibDeploymentTarget 6 . 2 , * )
673
- static var currentSchedulingExecutor : ( any SchedulingExecutor ) ? {
705
+ public static var currentSchedulingExecutor : ( any SchedulingExecutor ) ? {
674
706
if let activeExecutor = unsafe _getActiveExecutor( ) . asSerialExecutor ( ) ,
675
707
let scheduling = activeExecutor. asSchedulingExecutor {
676
708
return scheduling
@@ -762,7 +794,8 @@ public struct UnownedSerialExecutor: Sendable {
762
794
unsafe _executor_isComplexEquality ( self )
763
795
}
764
796
765
- func asSerialExecutor( ) -> ( any SerialExecutor ) ? {
797
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
798
+ public func asSerialExecutor( ) -> ( any SerialExecutor ) ? {
766
799
return unsafe unsafeBitCast( executor, to: ( any SerialExecutor ) ? . self)
767
800
}
768
801
}
@@ -792,13 +825,14 @@ public struct UnownedTaskExecutor: Sendable {
792
825
unsafe self. executor = Builtin . buildOrdinaryTaskExecutorRef ( executor)
793
826
}
794
827
795
- @available ( SwiftStdlib 6 . 2 , * )
828
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
796
829
@inlinable
797
830
public init < E: TaskExecutor > ( _ executor: __shared E) {
798
831
unsafe self. executor = Builtin . buildOrdinaryTaskExecutorRef ( executor)
799
832
}
800
833
801
- func asTaskExecutor( ) -> ( any TaskExecutor ) ? {
834
+ @available ( StdlibDeploymentTarget 6 . 2 , * )
835
+ public func asTaskExecutor( ) -> ( any TaskExecutor ) ? {
802
836
return unsafe unsafeBitCast( executor, to: ( any TaskExecutor ) ? . self)
803
837
}
804
838
}
0 commit comments