@@ -33,36 +33,11 @@ import Swift
33
33
/// unless implementing a scheduler.
34
34
@available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
35
35
public struct Task {
36
- internal let _task : Builtin . NativeObject
37
-
38
- // May only be created by the standard library.
39
- internal init ( _ task: Builtin . NativeObject ) {
40
- self . _task = task
41
- }
42
- }
43
-
44
- // ==== Current Task -----------------------------------------------------------
45
-
46
- @available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
47
- extension Task {
48
-
49
- /// Returns 'current' `Task` instance, representing the task from within which
50
- /// this function was called.
51
- ///
52
- /// All functions available on the Task
53
- public static var current : Task ? {
54
- guard let _task = _getCurrentAsyncTask ( ) else {
55
- return nil
56
- }
57
-
58
- // FIXME: This retain seems pretty wrong, however if we don't we WILL crash
59
- // with "destroying a task that never completed" in the task's destroy.
60
- // How do we solve this properly?
61
- Builtin . retain ( _task)
62
-
63
- return Task ( _task)
64
- }
65
-
36
+ // Task instances should not be used as they could be stored away,
37
+ // and sine some tasks may be task-local allocated such stored away
38
+ // references could point at already destroyed task memory (!).
39
+ //
40
+ // If necessary to obtain a task instance, please use withUnsafeCurrentTask.
66
41
}
67
42
68
43
// ==== Task Priority ----------------------------------------------------------
@@ -78,18 +53,12 @@ extension Task {
78
53
/// - SeeAlso: `Task.priority`
79
54
public static var currentPriority : Priority {
80
55
withUnsafeCurrentTask { task in
81
- task? . priority ?? Priority . default
82
- }
83
- }
56
+ guard let task = task else {
57
+ return Priority . default
58
+ }
84
59
85
- /// Returns the `current` task's priority.
86
- ///
87
- /// If no current `Task` is available, returns `Priority.default`.
88
- ///
89
- /// - SeeAlso: `Task.Priority`
90
- /// - SeeAlso: `Task.currentPriority`
91
- public var priority : Priority {
92
- getJobFlags ( _task) . priority
60
+ return getJobFlags ( task. _task) . priority
61
+ }
93
62
}
94
63
95
64
/// Task priority may inform decisions an `Executor` makes about how and when
@@ -150,18 +119,22 @@ extension Task {
150
119
/// i.e. the task will run regardless of the handle still being present or not.
151
120
/// Dropping a handle however means losing the ability to await on the task's result
152
121
/// and losing the ability to cancel it.
122
+ ///
123
+ // Implementation notes:
124
+ // A task handle can ONLY be obtained for a detached task, and as such shares
125
+ // no lifetime concerns with regards to holding and storing the `_task` with
126
+ // the `Task` type, which would have also be obtainable for any task, including
127
+ // a potentially task-local allocated one. I.e. it is always safe to store away
128
+ // a Task.Handle, yet the same is not true for the "current task" which may be
129
+ // a async-let created task, at risk of getting destroyed while the reference
130
+ // lingers around.
153
131
public struct Handle < Success, Failure: Error > : Sendable {
154
132
internal let _task : Builtin . NativeObject
155
133
156
134
internal init ( _ task: Builtin . NativeObject ) {
157
135
self . _task = task
158
136
}
159
137
160
- /// Returns the `Task` that this handle refers to.
161
- public var task : Task {
162
- Task ( _task)
163
- }
164
-
165
138
/// Wait for the task to complete, returning (or throwing) its result.
166
139
///
167
140
/// ### Priority
@@ -253,23 +226,6 @@ extension Task.Handle: Equatable {
253
226
}
254
227
}
255
228
256
- // ==== Conformances -----------------------------------------------------------
257
-
258
- @available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
259
- extension Task : Hashable {
260
- public func hash( into hasher: inout Hasher ) {
261
- UnsafeRawPointer ( Builtin . bridgeToRawPointer ( _task) ) . hash ( into: & hasher)
262
- }
263
- }
264
-
265
- @available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
266
- extension Task : Equatable {
267
- public static func == ( lhs: Self , rhs: Self ) -> Bool {
268
- UnsafeRawPointer ( Builtin . bridgeToRawPointer ( lhs. _task) ) ==
269
- UnsafeRawPointer ( Builtin . bridgeToRawPointer ( rhs. _task) )
270
- }
271
- }
272
-
273
229
// ==== Job Flags --------------------------------------------------------------
274
230
275
231
@available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
@@ -603,7 +559,7 @@ extension Task {
603
559
extension Task {
604
560
605
561
@available ( * , deprecated, message: " `Task.unsafeCurrent` was replaced by `withUnsafeCurrentTask { task in ... }`, and will be removed soon. " )
606
- public static var unsafeCurrent : UnsafeCurrentTask ? {
562
+ public static var unsafeCurrent : UnsafeCurrentTask ? { // TODO: remove as soon as possible
607
563
guard let _task = _getCurrentAsyncTask ( ) else {
608
564
return nil
609
565
}
@@ -667,14 +623,6 @@ public struct UnsafeCurrentTask {
667
623
self . _task = task
668
624
}
669
625
670
- /// Returns `Task` representing the same asynchronous context as this 'UnsafeCurrentTask'.
671
- ///
672
- /// Operations on `Task` (unlike `UnsafeCurrentTask`) are safe to be called
673
- /// from any other task (or thread).
674
- public var task : Task {
675
- Task ( _task)
676
- }
677
-
678
626
/// Returns `true` if the task is cancelled, and should stop executing.
679
627
///
680
628
/// - SeeAlso: `checkCancellation()`
0 commit comments