Change the logic for adding new task status records to a task

This change has two parts to it:

1. Add in a new interface (addStatusRecordWithChecks) for adding task
status records that also takes in a function ref. This function ref will
be used to evaluate if current state of the parent task has any changes
that need to be propagated to the child task that has been created.

This is necessary to prevent the following race between task creation
and concurrent cancellation and escalation:

a. Parent task create child task. It does lazy relaxed loads on its own
   state while doing so and propagates this state to the child.
b. Child task is created but has not been attached to the parent
   task/task group.
c. Parent task gets cancelled by another thread.
d. Child task gets linked into the parent’s task status records but no
   reevaluation has happened to account for changes that might have happened to
   the parent after (a).

2. Move status record management functions from the
Runtime/Concurrency.h to TaskPrivate.h. Remove any corresponding
overrides that are no longer needed. Remove unused tryAddStatusRecord
method whose functionality is provided by addStatusRecordWithChecks.

Radar-Id: rdar://problem/86347801
This commit is contained in:
Rokhini Prabhu
2021-12-16 19:28:11 -08:00
parent d1bb98b11e
commit 1947102ebd
11 changed files with 154 additions and 271 deletions

View File

@@ -142,8 +142,14 @@ void swift::asyncLet_addImpl(AsyncTask *task, AsyncLet *asyncLet,
auto record = impl->getTaskRecord();
assert(impl == record && "the async-let IS the task record");
// ok, now that the group actually is initialized: attach it to the task
swift_task_addStatusRecord(record);
// ok, now that the async let task actually is initialized: attach it to the
// current task
bool addedRecord =
addStatusRecord(record, [&](ActiveTaskStatus parentStatus) {
updateNewChildWithParentAndGroupState(task, parentStatus, NULL);
return true;
});
assert(addedRecord);
}
// =============================================================================
@@ -309,7 +315,7 @@ static void swift_asyncLet_endImpl(AsyncLet *alet) {
// Remove the child record from the parent task
auto record = asImpl(alet)->getTaskRecord();
swift_task_removeStatusRecord(record);
removeStatusRecord(record);
// TODO: we need to implicitly await either before the end or here somehow.
@@ -337,7 +343,7 @@ static void asyncLet_finish_after_task_completion(SWIFT_ASYNC_CONTEXT AsyncConte
// Remove the child record from the parent task
auto record = asImpl(alet)->getTaskRecord();
swift_task_removeStatusRecord(record);
removeStatusRecord(record);
// and finally, release the task and destroy the async-let
assert(swift_task_getCurrent() && "async-let must have a parent task");