Skip to content

Commit 8790016

Browse files
Block task-local values in isolated deinit, instead of copying, following review feedback
1 parent 8970317 commit 8790016

File tree

4 files changed

+11
-31
lines changed

4 files changed

+11
-31
lines changed

include/swift/ABI/TaskLocal.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,6 @@ class TaskLocal {
253253
/// management. If @c task is nil, items will be allocated using malloc(). The
254254
/// same value of @c task should be passed to @c TaskLocal::Storage::destroy() .
255255
static void copyTo(Storage *target, AsyncTask *task);
256-
257-
class AdHocScope {
258-
Storage *oldStorage;
259-
260-
public:
261-
AdHocScope(Storage *storage);
262-
~AdHocScope();
263-
};
264256

265257
class WithResetValuesScope {
266258
bool didPush;

stdlib/public/Concurrency/Actor.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,16 +1791,11 @@ class IsolatedDeinitJob : public Job {
17911791
private:
17921792
void *Object;
17931793
DeinitWorkFunction *Work;
1794-
TaskLocal::Storage Local;
17951794
public:
17961795
IsolatedDeinitJob(JobPriority priority, void *object, DeinitWorkFunction *work)
17971796
: Job({JobKind::IsolatedDeinit, priority}, &process), Object(object),
17981797
Work(work)
1799-
{
1800-
TaskLocal::copyTo(&Local, nullptr);
1801-
}
1802-
1803-
~IsolatedDeinitJob() { Local.destroy(nullptr); }
1798+
{}
18041799

18051800
SWIFT_CC(swiftasync)
18061801
static void process(Job *_job) {
@@ -1810,7 +1805,7 @@ class IsolatedDeinitJob : public Job {
18101805
}
18111806

18121807
inline void runWork() {
1813-
TaskLocal::AdHocScope taskLocalScope(&Local);
1808+
TaskLocal::WithResetValuesScope taskLocalResetScope;
18141809
Work(Object);
18151810
}
18161811

@@ -1831,7 +1826,8 @@ static void swift_task_deinitOnExecutorImpl(void *object,
18311826
// Note that swift_task_isCurrentExecutor() returns true for @MainActor
18321827
// when running on the main thread without any executor
18331828
if (swift_task_isCurrentExecutor(newExecutor)) {
1834-
return work(object); // 'return' forces tail call
1829+
TaskLocal::WithResetValuesScope taskLocalResetScope;
1830+
return work(object);
18351831
}
18361832

18371833
// Optimize deallocation of the default actors
@@ -1853,7 +1849,10 @@ static void swift_task_deinitOnExecutorImpl(void *object,
18531849
trackingInfo.enterAndShadow(newExecutor);
18541850

18551851
// Run the work.
1856-
work(object);
1852+
{
1853+
TaskLocal::WithResetValuesScope taskLocalResetScope;
1854+
work(object);
1855+
}
18571856

18581857
// `work` is a synchronous function, it cannot call swift_task_switch()
18591858
// If it calls any synchronous API that may change executor inside tracking info,

stdlib/public/Concurrency/TaskLocal.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -425,17 +425,6 @@ void TaskLocal::Storage::copyTo(TaskLocal::Storage *target, AsyncTask *task) {
425425
}
426426
}
427427

428-
TaskLocal::AdHocScope::AdHocScope(Storage *storage) {
429-
assert(swift_task_getCurrent() == nullptr &&
430-
"Cannot use ad-hoc scope with a task");
431-
oldStorage = FallbackTaskLocalStorage::get();
432-
FallbackTaskLocalStorage::set(storage);
433-
}
434-
435-
TaskLocal::AdHocScope::~AdHocScope() {
436-
FallbackTaskLocalStorage::set(oldStorage);
437-
}
438-
439428
TaskLocal::WithResetValuesScope::WithResetValuesScope() {
440429
didPush = swift_task_localStopPush();
441430
}

test/Concurrency/Runtime/async_task_locals_isolated_deinit.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ if #available(SwiftStdlib 5.1, *) {
7575
Task {
7676
await TL.$number.withValue(42) {
7777
await AnotherActor.shared.performTesting {
78-
_ = ClassWithIsolatedDeinit(expectedNumber: 42, group: group)
78+
_ = ClassWithIsolatedDeinit(expectedNumber: 0, group: group)
7979
}
8080
}
8181
}
@@ -88,10 +88,10 @@ if #available(SwiftStdlib 5.1, *) {
8888
group.enter()
8989
Task {
9090
TL.$number.withValue(37) {
91-
_ = ActorWithIsolatedDeinit(expectedNumber: 37, group: group)
91+
_ = ActorWithIsolatedDeinit(expectedNumber: 0, group: group)
9292
}
9393
TL.$number.withValue(99) {
94-
_ = ClassWithIsolatedDeinit(expectedNumber: 99, group: group)
94+
_ = ClassWithIsolatedDeinit(expectedNumber: 0, group: group)
9595
}
9696
}
9797
group.wait()

0 commit comments

Comments
 (0)