Skip to content

Commit 30e1c69

Browse files
committed
minor cleanups and renames
1 parent 421e5e5 commit 30e1c69

File tree

9 files changed

+48
-33
lines changed

9 files changed

+48
-33
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.

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 & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -735,22 +735,6 @@ swift_task_create_commonImpl(size_t rawTaskCreateFlags,
735735
/// FIXME: this is the fail path
736736
if (group) {
737737
assert(parent && "a task created in a group must be a child task");
738-
//
739-
// // Prevent task-local misuse;
740-
// // We must not allow an addTask {} wrapped immediately with a withValue {}
741-
// auto ParentLocal = parent->_private().Local;
742-
// if (auto taskLocalHeadLinkType = ParentLocal.peekHeadLinkType()) {
743-
// if (taskLocalHeadLinkType ==
744-
// swift::TaskLocal::NextLinkType::IsNextCreatedInTaskGroupBody) {
745-
//#if !SWIFT_CONCURRENCY_EMBEDDED
746-
// swift_task_reportIllegalTaskLocalBindingWithinWithTaskGroup(
747-
// nullptr, 0, true, 0);
748-
//#endif
749-
// // TODO(ktoso): If we were to keep this crash mode; offer a better failure for embedded swift
750-
// abort();
751-
// }
752-
// }
753-
//
754738
// Add to the task group, if requested.
755739
if (taskCreateFlags.addPendingGroupTaskUnconditionally()) {
756740
assert(group && "Missing group");
@@ -1024,10 +1008,17 @@ swift_task_create_commonImpl(size_t rawTaskCreateFlags,
10241008
// their structural lifetime guarantee is upheld by the group scope
10251009
// out-living any addTask created tasks.
10261010
auto ParentLocal = parent->_private().Local;
1011+
// If we were going to copy ALL values anyway, we don't need to
1012+
// perform this defensive partial copying. In practice, we currently
1013+
// do not have child tasks which force copying, but we could.
1014+
assert(!taskCreateFlags.copyTaskLocals() &&
1015+
"Currently we don't have child tasks which force copying task "
1016+
"locals; unexpected attempt to combine the two!");
1017+
10271018
if (auto taskLocalHeadLinkType = ParentLocal.peekHeadLinkType()) {
10281019
if (taskLocalHeadLinkType ==
10291020
swift::TaskLocal::NextLinkType::IsNextCreatedInTaskGroupBody) {
1030-
swift_task_localsOnlyCurrentCopyTo(task);
1021+
swift_task_localsCopyToTaskGroupChildTaskDefensively(task);
10311022
taskLocalStorageInitialized = true;
10321023
}
10331024
}

stdlib/public/Concurrency/TaskLocal.cpp

Lines changed: 4 additions & 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()) {
@@ -289,6 +289,9 @@ TaskLocal::Item::copyTo(AsyncTask *target) {
289289
// =============================================================================
290290
// ==== checks -----------------------------------------------------------------
291291

292+
/// UNUSED: This is effectively not used anymore by new runtimes because we will
293+
/// defensively copy in this situation since Swift 6, rather than crash as a
294+
/// means of defence.
292295
SWIFT_CC(swift)
293296
static void swift_task_reportIllegalTaskLocalBindingWithinWithTaskGroupImpl(
294297
const unsigned char *_unused_file, uintptr_t _unused_fileLength,
@@ -521,7 +524,6 @@ void TaskLocal::Storage::copyToOnlyOnlyFromCurrent(AsyncTask *target) {
521524
if (copied.emplace(item->key).second) {
522525

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

533535
auto copy = item->copyTo(target);
534536
if (!copiedHead) {
535-
SWIFT_TASK_LOCAL_DEBUG_LOG(item->key, "store copied head item:%p",
536-
copiedHead);
537537
copiedHead = copy;
538538
}
539539

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-
543540
// If we didn't copy an item, e.g. because it was a pointer to parent,
544541
// break out of the loop and keep pointing at parent still.
545542
if (!copy) {
546-
SWIFT_TASK_LOCAL_DEBUG_LOG(item->key, "break out, next is %p", 0);
547543
break;
548544
}
549545
} else {

stdlib/public/Concurrency/TaskLocal.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,9 @@ func _taskLocalsCopy(
362362

363363
// ==== Checks -----------------------------------------------------------------
364364

365-
@available(SwiftStdlib 5.1, *)
366365
@usableFromInline
366+
@available(SwiftStdlib 5.1, *)
367+
@available(*, deprecated, message: "The situation diagnosed by this is not handled gracefully rather than by crashing")
367368
func _checkIllegalTaskLocalBindingWithinWithTaskGroup(file: String, line: UInt) {
368369
if _taskHasTaskGroupStatusRecord() {
369370
file.withCString { _fileStart in
@@ -373,8 +374,9 @@ func _checkIllegalTaskLocalBindingWithinWithTaskGroup(file: String, line: UInt)
373374
}
374375
}
375376

376-
@available(SwiftStdlib 5.1, *)
377377
@usableFromInline
378+
@available(SwiftStdlib 5.1, *)
379+
@available(*, deprecated, message: "The situation diagnosed by this is not handled gracefully rather than by crashing")
378380
@_silgen_name("swift_task_reportIllegalTaskLocalBindingWithinWithTaskGroup")
379381
func _reportIllegalTaskLocalBindingWithinWithTaskGroup(
380382
_ _filenameStart: UnsafePointer<Int8>,

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)