10
10
////
11
11
////===----------------------------------------------------------------------===//
12
12
13
+ import Dispatch
14
+ #if canImport(Darwin)
15
+ import Darwin
16
+ #elseif canImport(Glibc)
17
+ import Glibc
18
+ #elseif os(Windows)
19
+ import CRT
20
+ #endif
21
+
22
+
13
23
import Swift
14
24
@_implementationOnly import _SwiftConcurrencyShims
15
25
@@ -31,7 +41,7 @@ import Swift
31
41
/// These partial periods towards the task's completion are `PartialAsyncTask`.
32
42
/// Partial tasks are generally not interacted with by end-users directly,
33
43
/// unless implementing a scheduler.
34
- public struct Task : TaskOperations {
44
+ public struct Task {
35
45
internal let _task : Builtin . NativeObject
36
46
37
47
// May only be created by the standard library.
@@ -69,7 +79,12 @@ extension Task {
69
79
Task . unsafeCurrent? . priority ?? Priority . default
70
80
}
71
81
72
- // Docs inherited from `TaskOperations`.
82
+ /// Returns the `current` task's priority.
83
+ ///
84
+ /// If no current `Task` is available, returns `Priority.default`.
85
+ ///
86
+ /// - SeeAlso: `Task.Priority`
87
+ /// - SeeAlso: `Task.currentPriority`
73
88
public var priority : Priority {
74
89
getJobFlags ( _task) . priority
75
90
}
@@ -220,51 +235,29 @@ extension Task.Handle where Failure == Never {
220
235
221
236
extension Task . Handle : Hashable {
222
237
public func hash( into hasher: inout Hasher ) {
223
- unsafeBitCast ( _task, to: UInt64 . self) . hash ( into: & hasher)
238
+ unsafeBitCast ( _task, to: size_t . self) . hash ( into: & hasher)
224
239
}
225
240
}
226
241
227
242
extension Task . Handle : Equatable {
228
243
public static func == ( lhs: Self , rhs: Self ) -> Bool {
229
- unsafeBitCast ( lhs. _task, to: UInt64 . self) ==
230
- unsafeBitCast ( rhs. _task, to: UInt64 . self)
244
+ unsafeBitCast ( lhs. _task, to: size_t . self) ==
245
+ unsafeBitCast ( rhs. _task, to: size_t . self)
231
246
}
232
247
}
233
248
234
249
// ==== Conformances -----------------------------------------------------------
235
250
236
- /// Protocol for those operations which are safe to be invoked on any `Task`
237
- /// instance regardless if the caller is running in the same task or a different
238
- /// one.
239
- ///
240
- /// This protocol also lists the functions and computed properties shared between
241
- /// `Task` and `UnsafeCurrentTask`.
242
- public protocol TaskOperations {
243
-
244
- /// Returns `true` if the task is cancelled, and should stop executing.
245
- ///
246
- /// - SeeAlso: `checkCancellation()`
247
- var isCancelled : Bool { get }
248
-
249
- /// Returns the `current` task's priority.
250
- ///
251
- /// If no current `Task` is available, returns `Priority.default`.
252
- ///
253
- /// - SeeAlso: `Task.Priority`
254
- /// - SeeAlso: `Task.currentPriority`
255
- var priority : Task . Priority { get }
256
- }
257
-
258
251
extension Task : Hashable {
259
252
public func hash( into hasher: inout Hasher ) {
260
- unsafeBitCast ( _task, to: UInt64 . self) . hash ( into: & hasher)
253
+ unsafeBitCast ( _task, to: size_t . self) . hash ( into: & hasher)
261
254
}
262
255
}
263
256
264
257
extension Task : Equatable {
265
258
public static func == ( lhs: Self , rhs: Self ) -> Bool {
266
- unsafeBitCast ( lhs. _task, to: UInt64 . self) ==
267
- unsafeBitCast ( rhs. _task, to: UInt64 . self)
259
+ unsafeBitCast ( lhs. _task, to: size_t . self) ==
260
+ unsafeBitCast ( rhs. _task, to: size_t . self)
268
261
}
269
262
}
270
263
@@ -502,7 +495,7 @@ extension Task {
502
495
///
503
496
/// The returned value must not be accessed from tasks other than the current one.
504
497
public static var unsafeCurrent : UnsafeCurrentTask ? {
505
- // FIXME: implement this once getCurrentAsyncTask can be called from sync funcs
498
+ // FIXME: rdar://70546948 implement this once getCurrentAsyncTask can be called from sync funcs
506
499
// guard let _task = Builtin.getCurrentAsyncTask() else {
507
500
// return nil
508
501
// }
@@ -531,7 +524,7 @@ extension Task {
531
524
/// represented by this handle itself. Doing so may result in undefined behavior,
532
525
/// and most certainly will break invariants in other places of the program
533
526
/// actively running on this task.
534
- public struct UnsafeCurrentTask : TaskOperations {
527
+ public struct UnsafeCurrentTask {
535
528
private let _task : Builtin . NativeObject
536
529
537
530
// May only be created by the standard library.
@@ -547,12 +540,19 @@ public struct UnsafeCurrentTask: TaskOperations {
547
540
Task ( _task)
548
541
}
549
542
550
- // Docs inherited from `TaskOperations`.
543
+ /// Returns `true` if the task is cancelled, and should stop executing.
544
+ ///
545
+ /// - SeeAlso: `checkCancellation()`
551
546
public var isCancelled : Bool {
552
547
_taskIsCancelled ( _task)
553
548
}
554
549
555
- // Docs inherited from `TaskOperations`.
550
+ /// Returns the `current` task's priority.
551
+ ///
552
+ /// If no current `Task` is available, returns `Priority.default`.
553
+ ///
554
+ /// - SeeAlso: `Task.Priority`
555
+ /// - SeeAlso: `Task.currentPriority`
556
556
public var priority : Task . Priority {
557
557
getJobFlags ( _task) . priority
558
558
}
@@ -572,7 +572,7 @@ func _enqueueJobGlobal(_ task: Builtin.Job)
572
572
func isTaskCancelled( _ task: Builtin . NativeObject ) -> Bool
573
573
574
574
@_silgen_name ( " swift_task_runAndBlockThread " )
575
- func runAsyncAndBlock( _ asyncFun: @escaping ( ) async -> ( ) )
575
+ public func runAsyncAndBlock( _ asyncFun: @escaping ( ) async -> ( ) )
576
576
577
577
@_silgen_name ( " swift_task_asyncMainDrainQueue " )
578
578
public func _async MainDrainQueue( ) -> Never
0 commit comments