Skip to content

Commit e124253

Browse files
committed
Track whether a task is actively running.
Tracking this as a single bit is actually largely uninteresting to the runtime. To handle priority escalation properly, we really need to track this at a finer grain of detail: recording that the task is running on a specific thread, enqueued on a specific actor, or so on. But starting by tracking a single bit is important for two reasons: - First, it's more realistic about the performance overheads of tasks: we're going to be doing this tracking eventually, and the cost of that tracking will be dominated by the atomic access, so doing that access now sets the baseline about right. - Second, it ensures that we've actually got runtime involvement in all the right places to do this tracking. A propos of the latter: there was no runtime involvement with awaiting a continuation, which is a point at which the task potentially transitions from running to suspended. We must do the tracking as part of this transition, rather than recognizing in the run-loops that a task is still active and treating it as having suspended, because the latter point potentially races with the resumption of the task. To do this, I've had to introduce a runtime function, swift_continuation_await, to do this awaiting rather than inlining the atomic operation on the continuation. As part of doing this work, I've also fixed a bug where we failed to load-acquire in swift_task_escalate before walking the task status records to invoke escalation actions. I've also fixed several places where the handling of task statuses may have accidentally allowed the task to revert to uncancelled.
1 parent 56c54d9 commit e124253

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

include/swift/Runtime/Concurrency.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,14 @@ void swift_defaultActor_enqueue(Job *job, DefaultActor *actor);
588588
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
589589
AsyncTask *swift_task_suspend();
590590

591+
/// Do a primitive suspension of the current task, as if part of
592+
/// a continuation, although this does not provide any of the
593+
/// higher-level continuation semantics. The current task is returned;
594+
/// its ResumeFunction and ResumeContext will need to be initialized,
595+
/// and then it will need to be enqueued or run as a job later.
596+
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
597+
AsyncTask *swift_task_suspend();
598+
591599
/// Prepare a continuation in the current task.
592600
///
593601
/// The caller should initialize the Parent, ResumeParent,

0 commit comments

Comments
 (0)