Skip to content

Commit d713aef

Browse files
committed
minor cleanups and renames
1 parent e270da9 commit d713aef

File tree

9 files changed

+42
-15
lines changed

9 files changed

+42
-15
lines changed

include/swift/ABI/TaskLocal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ class TaskLocal {
130130
}
131131

132132
void relinkNext(Item* nextOverride) {
133-
fprintf(stderr, "[%s:%d](%s) try relink item:%p\n", __FILE_NAME__, __LINE__, __FUNCTION__, this);
134-
fprintf(stderr, "[%s:%d](%s) try relink to target:%p\n", __FILE_NAME__, __LINE__, __FUNCTION__, nextOverride);
135133
assert(!getNext() &&
136134
"Can only relink task local item that was not pointing at anything yet");
137135
assert(nextOverride->isNextLinkPointer() ||

include/swift/Runtime/Concurrency.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,17 @@ void swift_task_localValuePop();
668668
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
669669
void swift_task_localsCopyTo(AsyncTask* target);
670670

671-
// FIXME no need for runtime API
671+
/// Some task local bindings must be copied defensively when a child task is
672+
/// created in a task group. See task creation (swift_task_create_common) for
673+
/// a detailed discussion how and when this is used.
674+
///
675+
/// Its Swift signature is
676+
///
677+
/// \code
678+
/// func swift_task_localsCopyToTaskGroupChildTaskDefensively<Key>(AsyncTask* task)
679+
/// \endcode
672680
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
673-
void swift_task_localsOnlyCurrentCopyTo(AsyncTask* target);
681+
void swift_task_localsCopyToTaskGroupChildTaskDefensively(AsyncTask* target);
674682

675683
/// Switch the current task to a new executor if we aren't already
676684
/// running on a compatible executor.

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ static FuncDecl *createSameSignatureDistributedThunkDecl(DeclContext *DC,
693693
/*parameterNameLoc=*/SourceLoc(), paramName, DC);
694694

695695
paramDecl->setImplicit(true);
696+
paramDecl->setSending();
696697
paramDecl->setSpecifier(funcParam->getSpecifier());
697698
paramDecl->setInterfaceType(funcParam->getInterfaceType());
698699

stdlib/public/CompatibilityOverride/CompatibilityOverrideConcurrency.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ OVERRIDE_TASK_LOCAL(task_localsCopyTo, void,
378378
(AsyncTask *target),
379379
(target))
380380

381-
OVERRIDE_TASK_LOCAL(task_localsOnlyCurrentCopyTo, void,
381+
OVERRIDE_TASK_LOCAL(task_localsCopyToTaskGroupChildTaskDefensively, void,
382382
SWIFT_EXPORT_FROM(swift_Concurrency), SWIFT_CC(swift),
383383
swift::,
384384
(AsyncTask *target),

stdlib/public/Concurrency/Task.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,10 +1024,17 @@ swift_task_create_commonImpl(size_t rawTaskCreateFlags,
10241024
// their structural lifetime guarantee is upheld by the group scope
10251025
// out-living any addTask created tasks.
10261026
auto ParentLocal = parent->_private().Local;
1027+
// If we were going to copy ALL values anyway, we don't need to
1028+
// perform this defensive partial copying. In practice, we currently
1029+
// do not have child tasks which force copying, but we could.
1030+
assert(!taskCreateFlags.copyTaskLocals() &&
1031+
"Currently we don't have child tasks which force copying task "
1032+
"locals; unexpected attempt to combine the two!");
1033+
10271034
if (auto taskLocalHeadLinkType = ParentLocal.peekHeadLinkType()) {
10281035
if (taskLocalHeadLinkType ==
10291036
swift::TaskLocal::NextLinkType::IsNextCreatedInTaskGroupBody) {
1030-
swift_task_localsOnlyCurrentCopyTo(task);
1037+
swift_task_localsCopyToTaskGroupChildTaskDefensively(task);
10311038
taskLocalStorageInitialized = true;
10321039
}
10331040
}

stdlib/public/Concurrency/TaskLocal.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static void swift_task_localsCopyToImpl(AsyncTask *target) {
164164
}
165165

166166
SWIFT_CC(swift)
167-
static void swift_task_localsOnlyCurrentCopyToImpl(AsyncTask *target) {
167+
static void swift_task_localsCopyToTaskGroupChildTaskDefensivelyImpl(AsyncTask *target) {
168168
TaskLocal::Storage *Local = nullptr;
169169

170170
if (AsyncTask *task = swift_task_getCurrent()) {
@@ -521,7 +521,6 @@ void TaskLocal::Storage::copyToOnlyOnlyFromCurrent(AsyncTask *target) {
521521
if (copied.emplace(item->key).second) {
522522

523523
if (!item->isNextLinkPointerCreatedInTaskGroupBody() && copiedHead) {
524-
SWIFT_TASK_LOCAL_DEBUG_LOG(item->key, "break out, next item is not within body, item:%p", item);
525524
// The next item is not the "risky one" so we can directly link to it,
526525
// as we would have within normal child task relationships. E.g. this is
527526
// a parent or next pointer to a "safe" (withValue { withTaskGroup { ... } })
@@ -532,18 +531,12 @@ void TaskLocal::Storage::copyToOnlyOnlyFromCurrent(AsyncTask *target) {
532531

533532
auto copy = item->copyTo(target);
534533
if (!copiedHead) {
535-
SWIFT_TASK_LOCAL_DEBUG_LOG(item->key, "store copied head item:%p",
536-
copiedHead);
537534
copiedHead = copy;
538535
}
539536

540-
SWIFT_TASK_LOCAL_DEBUG_LOG(item->key, "copy value [%p] to target:%p, item:%p, copied:%p",
541-
item->getStoragePtr(), target, item, copiedHead);
542-
543537
// If we didn't copy an item, e.g. because it was a pointer to parent,
544538
// break out of the loop and keep pointing at parent still.
545539
if (!copy) {
546-
SWIFT_TASK_LOCAL_DEBUG_LOG(item->key, "break out, next is %p", 0);
547540
break;
548541
}
549542
} else {

test/Concurrency/Runtime/async_task_locals_in_task_group_may_need_to_copy.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ enum TL {
1818
// ==== ------------------------------------------------------------------------
1919

2020
func test() async {
21+
// no outer task locals
22+
await withTaskGroup(of: Void.self) { group in
23+
TL.$two.withValue(2222) {
24+
// should not have any effect on reads below
25+
}
26+
await TL.$two.withValue(22) {
27+
group.addTask { // will have to copy the `22`
28+
print("Survived, one: \(TL.one) @ \(#fileID):\(#line)") // CHECK: Survived, one: 1
29+
print("Survived, two: \(TL.two) @ \(#fileID):\(#line)") // CHECK: Survived, two: 22
30+
}
31+
}
32+
}
33+
2134
await TL.$one.withValue(11) {
2235
await TL.$one.withValue(1111) {
2336
await withTaskGroup(of: Void.self) { group in

test/abi/macOS/arm64/concurrency.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ Added: _$ss26withTaskExecutorPreference_9isolation9operationxSch_pSg_ScA_pSgYixy
284284
// async function pointer to Swift.withTaskExecutorPreference<A, B where B: Swift.Error>(_: Swift.TaskExecutor?, isolation: isolated Swift.Actor?, operation: () async throws(B) -> A) async throws(B) -> A
285285
Added: _$ss26withTaskExecutorPreference_9isolation9operationxSch_pSg_ScA_pSgYixyYaq_YKXEtYaq_YKs5ErrorR_r0_lFTu
286286

287+
// === Task groups can now handle (copy) task locals set directly around an addTask
288+
Added: _swift_task_localsCopyToTaskGroupChildTaskDefensively
289+
287290
// === Add #isolation to next() and waitForAll() in task groups
288291
// Swift.TaskGroup.awaitAllRemainingTasks(isolation: isolated Swift.Actor?) async -> ()
289292
Added: _$sScG22awaitAllRemainingTasks9isolationyScA_pSgYi_tYaF
@@ -318,4 +321,5 @@ Added: _$ss9TaskLocalC13withValueImpl_9operation9isolation4file4lineqd__xn_qd__y
318321
Added: _$ss9TaskLocalC13withValueImpl_9operation9isolation4file4lineqd__xn_qd__yYaKXEScA_pSgYiSSSutYaKlFTu
319322
// Swift.TaskLocal.withValue<A>(_: A, operation: () async throws -> A1, isolation: isolated Swift.Actor?, file: Swift.String, line: Swift.UInt) async throws -> A1
320323
Added: _$ss9TaskLocalC9withValue_9operation9isolation4file4lineqd__x_qd__yYaKXEScA_pSgYiSSSutYaKlF
321-
Added: _$ss9TaskLocalC9withValue_9operation9isolation4file4lineqd__x_qd__yYaKXEScA_pSgYiSSSutYaKlFTu
324+
Added: _$ss9TaskLocalC9withValue_9operation9isolation4file4lineqd__x_qd__yYaKXEScA_pSgYiSSSutYaKlFTu
325+

test/abi/macOS/x86_64/concurrency.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ Added: _$ss26withTaskExecutorPreference_9isolation9operationxSch_pSg_ScA_pSgYixy
284284
// async function pointer to Swift.withTaskExecutorPreference<A, B where B: Swift.Error>(_: Swift.TaskExecutor?, isolation: isolated Swift.Actor?, operation: () async throws(B) -> A) async throws(B) -> A
285285
Added: _$ss26withTaskExecutorPreference_9isolation9operationxSch_pSg_ScA_pSgYixyYaq_YKXEtYaq_YKs5ErrorR_r0_lFTu
286286

287+
// === Task groups can now handle (copy) task locals set directly around an addTask
288+
Added: _swift_task_localsCopyToTaskGroupChildTaskDefensively
289+
287290
// === Add #isolation to next() and waitForAll() in task groups
288291
// Swift.TaskGroup.awaitAllRemainingTasks(isolation: isolated Swift.Actor?) async -> ()
289292
Added: _$sScG22awaitAllRemainingTasks9isolationyScA_pSgYi_tYaF

0 commit comments

Comments
 (0)