@@ -55,12 +55,6 @@ import Swift
55
55
/// like the reason for cancellation.
56
56
/// This reflects the fact that a task can be canceled for many reasons,
57
57
/// and additional reasons can accrue during the cancellation process.
58
- /// For example,
59
- /// if it takes the task too long to exit after being canceled,
60
- /// it could also miss a deadline.
61
- /// ◊FIXME: Replace above example -- deadlines aren't part of the API yet
62
- /// Cancellation is a lightweight way to stop a task before it completes,
63
- /// not a general mechanism for inter-task communication.
64
58
@available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
65
59
public struct Task {
66
60
internal let _task : Builtin . NativeObject
@@ -418,9 +412,8 @@ extension Task {
418
412
}
419
413
}
420
414
421
- /// Whether this is a task created by the 'async' operation, which
422
- /// conceptually continues the work of the synchronous code that invokes
423
- /// it.
415
+ /// Whether this is a task created by an asynchronous operation,
416
+ /// continuing the work of the synchronous code that invokes it.
424
417
var isContinuingAsyncTask : Bool {
425
418
get {
426
419
( bits & ( 1 << 27 ) ) != 0
@@ -453,7 +446,8 @@ extension Task {
453
446
454
447
}
455
448
456
- /// Run the given non-throwing operation as part of a new top-level task.
449
+ /// Runs the given nonthrowing operation asynchronously
450
+ /// as part of a new top-level task.
457
451
///
458
452
/// Avoid using a detached task unless it isn't possible
459
453
/// to model the operation using structured concurrency features like child tasks.
@@ -493,7 +487,8 @@ public func detach<T>(
493
487
return Task . Handle < T , Never > ( task)
494
488
}
495
489
496
- /// Run given throwing `operation` as part of a new top-level task.
490
+ /// Runs the given throwing operation asynchronously
491
+ /// as part of a new top-level task.
497
492
///
498
493
/// If the operation throws an error, this method propogates that error.
499
494
///
@@ -535,6 +530,26 @@ public func detach<T>(
535
530
return Task . Handle < T , Error > ( task)
536
531
}
537
532
533
+ /// Runs the given nonthrowing operation asynchronously
534
+ /// as part of a new top-level task.
535
+ ///
536
+ /// Avoid using a detached task unless it isn't possible
537
+ /// to model the operation using structured concurrency features like child tasks.
538
+ /// Child tasks inherit the parent task's priority and task-local storage,
539
+ /// and canceling a parent task automatically cancels all of its child tasks.
540
+ /// You need to handle these considerations manually with a detached task.
541
+ ///
542
+ /// A detached task runs to completion
543
+ /// unless it is explicitly canceled by calling the `Task.Handle.cancel()` method.
544
+ /// Specifically, dropping a detached task's handle
545
+ /// doesn't cancel that task.
546
+ ///
547
+ /// - Parameters:
548
+ /// - priority: The priority of the task.
549
+ /// - executor: The executor that the detached closure should start executing on.
550
+ /// - operation: The operation to perform.
551
+ ///
552
+ /// - Returns: A handle to the task.
538
553
@discardableResult
539
554
@available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
540
555
public func asyncDetached< T> (
@@ -544,6 +559,28 @@ public func asyncDetached<T>(
544
559
return detach ( priority: priority ?? . unspecified, operation: operation)
545
560
}
546
561
562
+ /// Runs the given throwing operation asynchronously
563
+ /// as part of a new top-level task.
564
+ ///
565
+ /// If the operation throws an error, this method propogates that error.
566
+ ///
567
+ /// Avoid using a detached task unless it isn't possible
568
+ /// to model the operation using structured concurrency features like child tasks.
569
+ /// Child tasks inherit the parent task's priority and task-local storage,
570
+ /// and canceling a parent task automatically cancels all of its child tasks.
571
+ /// You need to handle these considerations manually with a detached task.
572
+ ///
573
+ /// A detached task runs to completion
574
+ /// unless it is explicitly canceled by calling the `Task.Handle.cancel()` method.
575
+ /// Specifically, dropping a detached task's handle
576
+ /// doesn't cancel that task.
577
+ ///
578
+ /// - Parameters:
579
+ /// - priority: The priority of the task.
580
+ /// - executor: The executor that the detached closure should start executing on.
581
+ /// - operation: The operation to perform.
582
+ ///
583
+ /// - Returns: A handle to the task.
547
584
@discardableResult
548
585
@available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
549
586
public func asyncDetached< T> (
@@ -553,7 +590,27 @@ public func asyncDetached<T>(
553
590
return detach ( priority: priority ?? . unspecified, operation: operation)
554
591
}
555
592
556
- /// ABI stub while we stage in the new signatures
593
+
594
+ // ABI stub while we stage in the new signatures
595
+ /// Runs the given nonthrowing operation asynchronously
596
+ /// as part of a new top-level task on behalf of the current actor.
597
+ ///
598
+ /// Use this function when creating asynchronous work
599
+ /// that operates on behalf of the synchronous function that calls it.
600
+ /// Like `detach(priority:operation:)`,
601
+ /// this function creates a separate, top-level task.
602
+ /// Unlike `detach(priority:operation:)`,
603
+ /// the task creating by `async(priority:operation:)`
604
+ /// inherits the priority and actor context of the caller,
605
+ /// so the operation is treated more like an asynchronous extension
606
+ /// to the synchronous operation.
607
+ ///
608
+ /// - Parameters:
609
+ /// - priority: The priority of the task.
610
+ /// Pass `nil` to use the priority from `Task.currentPriority`.
611
+ /// - operation: The operation to perform.
612
+ ///
613
+ /// - Returns: A handle to the task.
557
614
@available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
558
615
@usableFromInline
559
616
func async (
@@ -569,20 +626,25 @@ func async(
569
626
let _: Task . Handle = async ( priority: adjustedPriority, operation: operation)
570
627
}
571
628
572
- /// Run given `operation` as asynchronously in its own top-level task.
629
+ /// Runs the given nonthrowing operation asynchronously
630
+ /// as part of a new top-level task on behalf of the current actor.
573
631
///
574
- /// The `async` function should be used when creating asynchronous work
632
+ /// Use this function when creating asynchronous work
575
633
/// that operates on behalf of the synchronous function that calls it.
576
- /// Like `detach`, the async function creates a separate, top-level task.
577
- /// Unlike `detach`, the task creating by `async` inherits the priority and
578
- /// actor context of the caller, so the `operation` is treated more like an
579
- /// asynchronous extension to the synchronous operation. Additionally, `async`
580
- /// does not return a handle to refer to the task.
634
+ /// Like `detach(priority:operation:)`,
635
+ /// this function creates a separate, top-level task.
636
+ /// Unlike `detach(priority:operation:)`,
637
+ /// the task creating by `async(priority:operation:)`
638
+ /// inherits the priority and actor context of the caller,
639
+ /// so the operation is treated more like an asynchronous extension
640
+ /// to the synchronous operation.
581
641
///
582
642
/// - Parameters:
583
- /// - priority: priority of the task. If nil, the priority will come from
584
- /// Task.currentPriority.
585
- /// - operation: the operation to execute
643
+ /// - priority: The priority of the task.
644
+ /// Pass `nil` to use the priority from `Task.currentPriority`.
645
+ /// - operation: The operation to perform.
646
+ ///
647
+ /// - Returns: A handle to the task.
586
648
@available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
587
649
@discardableResult
588
650
public func async < T> (
@@ -605,20 +667,25 @@ public func async<T>(
605
667
return Task . Handle ( task)
606
668
}
607
669
608
- /// Run given `operation` as asynchronously in its own top-level task.
670
+ /// Runs the given throwing operation asynchronously
671
+ /// as part of a new top-level task on behalf of the current actor.
609
672
///
610
- /// The `async` function should be used when creating asynchronous work
673
+ /// Use this function when creating asynchronous work
611
674
/// that operates on behalf of the synchronous function that calls it.
612
- /// Like `detach`, the async function creates a separate, top-level task.
613
- /// Unlike `detach`, the task creating by `async` inherits the priority and
614
- /// actor context of the caller, so the `operation` is treated more like an
615
- /// asynchronous extension to the synchronous operation. Additionally, `async`
616
- /// does not return a handle to refer to the task.
675
+ /// Like `detach(priority:operation:)`,
676
+ /// this function creates a separate, top-level task.
677
+ /// Unlike `detach(priority:operation:)`,
678
+ /// the task creating by `async(priority:operation:)`
679
+ /// inherits the priority and actor context of the caller,
680
+ /// so the operation is treated more like an asynchronous extension
681
+ /// to the synchronous operation.
617
682
///
618
683
/// - Parameters:
619
- /// - priority: priority of the task. If nil, the priority will come from
620
- /// Task.currentPriority.
621
- /// - operation: the operation to execute
684
+ /// - priority: The priority of the task.
685
+ /// Pass `nil` to use the priority from `Task.currentPriority`.
686
+ /// - operation: The operation to perform.
687
+ ///
688
+ /// - Returns: A handle to the task.
622
689
@available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
623
690
@discardableResult
624
691
public func async < T> (
0 commit comments