@@ -63,9 +63,12 @@ import Swift
63
63
/// This reflects the fact that a task can be canceled for many reasons,
64
64
/// and additional reasons can accrue during the cancellation process.
65
65
@available ( SwiftStdlib 5 . 5 , * )
66
+ @frozen
66
67
public struct Task < Success, Failure: Error > : Sendable {
68
+ @usableFromInline
67
69
internal let _task : Builtin . NativeObject
68
70
71
+ @_alwaysEmitIntoClient
69
72
internal init ( _ task: Builtin . NativeObject ) {
70
73
self . _task = task
71
74
}
@@ -409,109 +412,32 @@ struct JobFlags {
409
412
410
413
// ==== Task Creation Flags --------------------------------------------------
411
414
412
- /// Flags for schedulable jobs.
413
- ///
414
- /// This is a port of the C++ FlagSet.
415
+ /// Form task creation flags for use with the createAsyncTask builtins.
415
416
@available ( SwiftStdlib 5 . 5 , * )
416
- struct TaskCreateFlags {
417
- /// The actual bit representation of these flags.
418
- var bits : Int = 0
419
-
420
- /// The priority given to the job.
421
- var priority : TaskPriority ? {
422
- get {
423
- let value = Int ( bits) & 0xFF
424
-
425
- if value == 0 {
426
- return nil
427
- }
428
-
429
- return TaskPriority ( rawValue: UInt8 ( value) )
430
- }
431
-
432
- set {
433
- bits = ( bits & ~ 0xFF ) | Int ( newValue? . rawValue ?? 0 )
434
- }
435
- }
436
-
437
- /// Whether this is a child task.
438
- var isChildTask : Bool {
439
- get {
440
- ( bits & ( 1 << 8 ) ) != 0
441
- }
442
-
443
- set {
444
- if newValue {
445
- bits = bits | 1 << 8
446
- } else {
447
- bits = ( bits & ~ ( 1 << 8 ) )
448
- }
449
- }
417
+ @_alwaysEmitIntoClient
418
+ func taskCreateFlags(
419
+ priority: TaskPriority ? , isChildTask: Bool , copyTaskLocals: Bool ,
420
+ inheritContext: Bool , enqueueJob: Bool ,
421
+ addPendingGroupTaskUnconditionally: Bool
422
+ ) -> Int {
423
+ var bits = 0
424
+ bits |= ( bits & ~ 0xFF ) | Int ( priority? . rawValue ?? 0 )
425
+ if isChildTask {
426
+ bits |= 1 << 8
450
427
}
451
-
452
- /// Whether to copy thread locals from the currently-executing task into the
453
- /// newly-created task.
454
- var copyTaskLocals : Bool {
455
- get {
456
- ( bits & ( 1 << 10 ) ) != 0
457
- }
458
-
459
- set {
460
- if newValue {
461
- bits = bits | 1 << 10
462
- } else {
463
- bits = ( bits & ~ ( 1 << 10 ) )
464
- }
465
- }
428
+ if copyTaskLocals {
429
+ bits |= 1 << 10
466
430
}
467
-
468
- /// Whether this task should inherit as much context from the
469
- /// currently-executing task as it can.
470
- var inheritContext : Bool {
471
- get {
472
- ( bits & ( 1 << 11 ) ) != 0
473
- }
474
-
475
- set {
476
- if newValue {
477
- bits = bits | 1 << 11
478
- } else {
479
- bits = ( bits & ~ ( 1 << 11 ) )
480
- }
481
- }
431
+ if inheritContext {
432
+ bits |= 1 << 11
482
433
}
483
-
484
- /// Whether to enqueue the newly-created task on the given executor (if
485
- /// specified) or the global executor.
486
- var enqueueJob : Bool {
487
- get {
488
- ( bits & ( 1 << 12 ) ) != 0
489
- }
490
-
491
- set {
492
- if newValue {
493
- bits = bits | 1 << 12
494
- } else {
495
- bits = ( bits & ~ ( 1 << 12 ) )
496
- }
497
- }
434
+ if enqueueJob {
435
+ bits |= 1 << 12
498
436
}
499
-
500
- /// Whether to add a pending group task unconditionally as part of creating
501
- /// the task.
502
- var addPendingGroupTaskUnconditionally : Bool {
503
- get {
504
- ( bits & ( 1 << 13 ) ) != 0
505
- }
506
-
507
- set {
508
- if newValue {
509
- bits = bits | 1 << 13
510
- } else {
511
- bits = ( bits & ~ ( 1 << 13 ) )
512
- }
513
- }
437
+ if addPendingGroupTaskUnconditionally {
438
+ bits |= 1 << 13
514
439
}
440
+ return bits
515
441
}
516
442
517
443
// ==== Task Creation ----------------------------------------------------------
@@ -534,20 +460,20 @@ extension Task where Failure == Never {
534
460
/// Task.currentPriority.
535
461
/// - operation: the operation to execute
536
462
@discardableResult
463
+ @_alwaysEmitIntoClient
537
464
public init (
538
465
priority: TaskPriority ? = nil ,
539
466
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping ( ) async -> Success
540
467
) {
541
468
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
542
469
// Set up the job flags for a new task.
543
- var flags = TaskCreateFlags ( )
544
- flags. priority = priority
545
- flags. inheritContext = true
546
- flags. copyTaskLocals = true
547
- flags. enqueueJob = true
470
+ let flags = taskCreateFlags (
471
+ priority: priority, isChildTask: false , copyTaskLocals: true ,
472
+ inheritContext: true , enqueueJob: true ,
473
+ addPendingGroupTaskUnconditionally: false )
548
474
549
475
// Create the asynchronous task.
550
- let ( task, _) = Builtin . createAsyncTask ( Int ( flags. bits ) , operation)
476
+ let ( task, _) = Builtin . createAsyncTask ( flags, operation)
551
477
552
478
self . _task = task
553
479
#else
@@ -571,20 +497,21 @@ extension Task where Failure == Error {
571
497
/// Task.currentPriority.
572
498
/// - operation: the operation to execute
573
499
@discardableResult
500
+ @_alwaysEmitIntoClient
574
501
public init (
575
502
priority: TaskPriority ? = nil ,
576
503
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping ( ) async throws -> Success
577
504
) {
578
505
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
579
- // Set up the job flags for a new task.
580
- var flags = TaskCreateFlags ( )
581
- flags . priority = priority
582
- flags . inheritContext = true
583
- flags . copyTaskLocals = true
584
- flags . enqueueJob = true
506
+ // Set up the task flags for a new task.
507
+ let flags = taskCreateFlags (
508
+ priority: priority , isChildTask : false , copyTaskLocals : true ,
509
+ inheritContext: true , enqueueJob : true ,
510
+ addPendingGroupTaskUnconditionally : false
511
+ )
585
512
586
513
// Create the asynchronous task future.
587
- let ( task, _) = Builtin . createAsyncTask ( Int ( flags. bits ) , operation)
514
+ let ( task, _) = Builtin . createAsyncTask ( flags, operation)
588
515
589
516
self . _task = task
590
517
#else
@@ -627,18 +554,20 @@ extension Task where Failure == Never {
627
554
/// tasks result or `cancel` it. If the operation fails the handle will
628
555
/// throw the error the operation has thrown when awaited on.
629
556
@discardableResult
557
+ @_alwaysEmitIntoClient
630
558
public static func detached(
631
559
priority: TaskPriority ? = nil ,
632
560
operation: __owned @Sendable @escaping ( ) async -> Success
633
561
) -> Task < Success , Failure > {
634
562
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
635
563
// Set up the job flags for a new task.
636
- var flags = TaskCreateFlags ( )
637
- flags. priority = priority
638
- flags. enqueueJob = true
564
+ let flags = taskCreateFlags (
565
+ priority: priority, isChildTask: false , copyTaskLocals: false ,
566
+ inheritContext: false , enqueueJob: true ,
567
+ addPendingGroupTaskUnconditionally: false )
639
568
640
569
// Create the asynchronous task future.
641
- let ( task, _) = Builtin . createAsyncTask ( Int ( flags. bits ) , operation)
570
+ let ( task, _) = Builtin . createAsyncTask ( flags, operation)
642
571
643
572
return Task ( task)
644
573
#else
@@ -682,18 +611,21 @@ extension Task where Failure == Error {
682
611
/// tasks result or `cancel` it. If the operation fails the handle will
683
612
/// throw the error the operation has thrown when awaited on.
684
613
@discardableResult
614
+ @_alwaysEmitIntoClient
685
615
public static func detached(
686
616
priority: TaskPriority ? = nil ,
687
617
operation: __owned @Sendable @escaping ( ) async throws -> Success
688
618
) -> Task < Success , Failure > {
689
619
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
690
620
// Set up the job flags for a new task.
691
- var flags = TaskCreateFlags ( )
692
- flags. priority = priority
693
- flags. enqueueJob = true
621
+ let flags = taskCreateFlags (
622
+ priority: priority, isChildTask: false , copyTaskLocals: false ,
623
+ inheritContext: false , enqueueJob: true ,
624
+ addPendingGroupTaskUnconditionally: false
625
+ )
694
626
695
627
// Create the asynchronous task future.
696
- let ( task, _) = Builtin . createAsyncTask ( Int ( flags. bits ) , operation)
628
+ let ( task, _) = Builtin . createAsyncTask ( flags, operation)
697
629
698
630
return Task ( task)
699
631
#else
0 commit comments