Skip to content

Commit 7ea2b3d

Browse files
authored
Merge pull request #77241 from nickolas-pohilets/mpokhylets/refactor-task-local-items
2 parents d0506e1 + 2acf5b6 commit 7ea2b3d

File tree

3 files changed

+206
-335
lines changed

3 files changed

+206
-335
lines changed

stdlib/public/Concurrency/Task.cpp

+12-35
Original file line numberDiff line numberDiff line change
@@ -1027,48 +1027,25 @@ swift_task_create_commonImpl(size_t rawTaskCreateFlags,
10271027
if ((group && group->isCancelled()) || swift_task_isCancelled(parent))
10281028
swift_task_cancel(task);
10291029

1030-
// Initialize task locals storage
1031-
bool taskLocalStorageInitialized = false;
1032-
10331030
// Inside a task group, we may have to perform some defensive copying,
10341031
// check if doing so is necessary, and initialize storage using partial
10351032
// defensive copies if necessary.
10361033
if (group) {
10371034
assert(parent && "a task created in a group must be a child task");
1038-
// We are a child task in a task group; and it may happen that we are calling
1039-
// addTask specifically in such shape:
1040-
//
1041-
// $local.withValue(theValue) { addTask {} }
1042-
//
1043-
// If this is the case, we MUST copy `theValue` (and any other such directly
1044-
// wrapping the addTask value bindings), because those values will be popped
1045-
// when withValue returns - breaking our structured concurrency guarantees
1046-
// that we rely on for the "link directly to parent's task local Item".
1047-
//
1048-
// Values set outside the task group are not subject to this problem, as
1049-
// their structural lifetime guarantee is upheld by the group scope
1050-
// out-living any addTask created tasks.
1051-
auto ParentLocal = parent->_private().Local;
1052-
// If we were going to copy ALL values anyway, we don't need to
1053-
// perform this defensive partial copying. In practice, we currently
1054-
// do not have child tasks which force copying, but we could.
1055-
assert(!taskCreateFlags.copyTaskLocals() &&
1056-
"Currently we don't have child tasks which force copying task "
1057-
"locals; unexpected attempt to combine the two!");
1058-
1059-
if (auto taskLocalHeadLinkType = ParentLocal.peekHeadLinkType()) {
1060-
if (taskLocalHeadLinkType ==
1061-
swift::TaskLocal::NextLinkType::IsNextCreatedInTaskGroupBody) {
1062-
ParentLocal.copyToOnlyOnlyFromCurrentGroup(task);
1063-
taskLocalStorageInitialized = true;
1064-
}
1065-
}
10661035
}
10671036

1068-
if (!taskLocalStorageInitialized) {
1069-
// just initialize the storage normally
1070-
task->_private().Local.initializeLinkParent(task, parent);
1071-
}
1037+
// Initialize task locals with a link to the parent task.
1038+
//
1039+
// Inside a task group, we may have to perform some defensive copying,
1040+
// and initialize storage using partial defensive copies if necessary.
1041+
//
1042+
// If we were going to copy ALL values anyway, we don't need to
1043+
// perform this defensive partial copying. In practice, we currently
1044+
// do not have child tasks which force copying, but we could.
1045+
assert(!taskCreateFlags.copyTaskLocals() &&
1046+
"Currently we don't have child tasks which force copying task "
1047+
"locals; unexpected attempt to combine the two!");
1048+
task->_private().Local.initializeLinkParent(task, parent);
10721049
}
10731050

10741051
// Configure the initial context.

0 commit comments

Comments
 (0)