Skip to content

Commit fda7f53

Browse files
authored
Reapply "Task names" (swiftlang#79562) (swiftlang#79600)
1 parent b6918a3 commit fda7f53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1224
-1132
lines changed

Runtimes/Core/Concurrency/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_subdirectory(InternalShims)
22

3+
gyb_expand(TaskGroup+addTask.swift.gyb TaskGroup+addTask.swift)
34
gyb_expand(Task+startSynchronously.swift.gyb Task+startSynchronously.swift)
45

56
add_library(swift_Concurrency
@@ -79,10 +80,10 @@ add_library(swift_Concurrency
7980
Task+TaskExecutor.swift
8081
TaskCancellation.swift
8182
TaskGroup.swift
82-
TaskGroup+TaskExecutor.swift
8383
TaskLocal.swift
8484
TaskSleep.swift
8585
TaskSleepDuration.swift
86+
"${CMAKE_CURRENT_BINARY_DIR}/TaskGroup+addTask.swift"
8687
"${CMAKE_CURRENT_BINARY_DIR}/Task+startSynchronously.swift")
8788

8889
include(${SwiftCore_CONCURRENCY_GLOBAL_EXECUTOR}.cmake)

include/swift/ABI/MetadataValues.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,6 +2737,7 @@ class JobFlags : public FlagSet<uint32_t> {
27372737
// 27 is currently unused
27382738
Task_IsAsyncLetTask = 28,
27392739
Task_HasInitialTaskExecutorPreference = 29,
2740+
Task_HasInitialTaskName = 30,
27402741
};
27412742
// clang-format on
27422743

@@ -2773,6 +2774,9 @@ class JobFlags : public FlagSet<uint32_t> {
27732774
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_HasInitialTaskExecutorPreference,
27742775
task_hasInitialTaskExecutorPreference,
27752776
task_setHasInitialTaskExecutorPreference)
2777+
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_HasInitialTaskName,
2778+
task_hasInitialTaskName,
2779+
task_setHasInitialTaskName)
27762780
};
27772781

27782782
/// Kinds of task status record.
@@ -2802,6 +2806,9 @@ enum class TaskStatusRecordKind : uint8_t {
28022806
/// enqueued on.
28032807
TaskExecutorPreference = 5,
28042808

2809+
/// A human-readable task name.
2810+
TaskName = 6,
2811+
28052812
// Kinds >= 192 are private to the implementation.
28062813
First_Reserved = 192,
28072814
Private_RecordLock = 192
@@ -2825,6 +2832,8 @@ enum class TaskOptionRecordKind : uint8_t {
28252832
/// Set the initial task executor preference of the task.
28262833
InitialTaskExecutorUnowned = 5,
28272834
InitialTaskExecutorOwned = 6,
2835+
// Set a human-readable task name.
2836+
InitialTaskName = 7,
28282837
/// Request a child task for swift_task_run_inline.
28292838
RunInline = UINT8_MAX,
28302839
};

include/swift/ABI/Task.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,28 @@ class AsyncTask : public Job {
421421
/// Checking this is, of course, inherently race-prone on its own.
422422
bool isCancelled() const;
423423

424+
// ==== INITIAL TASK RECORDS =================================================
425+
// A task may have a number of "initial" records set, they MUST be set in the
426+
// following order to make the task-local allocation/deallocation's stack
427+
// discipline easy to work out at the tasks destruction:
428+
//
429+
// - Initial TaskName
430+
// - Initial ExecutorPreference
431+
432+
// ==== Task Naming ----------------------------------------------------------
433+
434+
/// At task creation a task may be assigned a name.
435+
void pushInitialTaskName(const char* taskName);
436+
void dropInitialTaskNameRecord();
437+
438+
/// Get the initial task name that was given to this task during creation,
439+
/// or nullptr if the task has no name
440+
const char* getTaskName();
441+
442+
bool hasInitialTaskNameRecord() const {
443+
return Flags.task_hasInitialTaskName();
444+
}
445+
424446
// ==== Task Executor Preference ---------------------------------------------
425447

426448
/// Get the preferred task executor reference if there is one set for this

include/swift/ABI/TaskOptions.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ class InitialTaskExecutorOwnedPreferenceTaskOptionRecord
131131
}
132132
};
133133

134+
class InitialTaskNameTaskOptionRecord
135+
: public TaskOptionRecord {
136+
137+
const char* TaskName;
138+
139+
public:
140+
InitialTaskNameTaskOptionRecord(
141+
const char* taskName)
142+
: TaskOptionRecord(TaskOptionRecordKind::InitialTaskName),
143+
TaskName(taskName) {}
144+
145+
const char* getTaskName() const {
146+
return TaskName;
147+
}
148+
149+
static bool classof(const TaskOptionRecord *record) {
150+
return record->getKind() == TaskOptionRecordKind::InitialTaskName;
151+
}
152+
};
153+
134154
/// Task option to specify the initial serial executor for the task.
135155
class InitialSerialExecutorTaskOptionRecord : public TaskOptionRecord {
136156
const SerialExecutorRef Executor;

include/swift/ABI/TaskStatus.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,22 @@ class TaskExecutorPreferenceStatusRecord : public TaskStatusRecord {
325325
}
326326
};
327327

328+
class TaskNameStatusRecord : public TaskStatusRecord {
329+
private:
330+
const char *Name;
331+
332+
public:
333+
TaskNameStatusRecord(const char *name)
334+
: TaskStatusRecord(TaskStatusRecordKind::TaskName),
335+
Name(name) {}
336+
337+
const char *getName() { return Name; }
338+
339+
static bool classof(const TaskStatusRecord *record) {
340+
return record->getKind() == TaskStatusRecordKind::TaskName;
341+
}
342+
};
343+
328344
// This record is allocated for a task to record what it is dependent on before
329345
// the task can make progress again.
330346
class TaskDependencyStatusRecord : public TaskStatusRecord {

include/swift/AST/ASTSynthesis.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ enum SingletonTypeSynthesizer {
5757
_taskExecutor, // the '_Concurrency.TaskExecutor' protocol
5858
_actor, // the '_Concurrency.Actor' protocol
5959
_distributedActor, // the 'Distributed.DistributedActor' protocol
60+
_unsafeRawBufferPointer // UnsafeRawBufferPointer
6061
};
6162
inline Type synthesizeType(SynthesisContext &SC,
6263
SingletonTypeSynthesizer kind) {
@@ -89,6 +90,8 @@ inline Type synthesizeType(SynthesisContext &SC,
8990
case _distributedActor:
9091
return SC.Context.getProtocol(KnownProtocolKind::DistributedActor)
9192
->getDeclaredInterfaceType();
93+
case _unsafeRawBufferPointer:
94+
return SC.Context.getUnsafeRawBufferPointerType();
9295
case _copyable:
9396
return SC.Context.getProtocol(KnownProtocolKind::Copyable)
9497
->getDeclaredInterfaceType();

include/swift/AST/Builtins.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,8 @@ BUILTIN_MISC_OPERATION(UnprotectedAddressOfBorrowOpaque, "unprotectedAddressOfBo
954954
/// createTask<T>(flags: Int,
955955
/// initialSerialExecutor: (Builtin.Executor)? = nil,
956956
/// taskGroup: Builtin.RawPointer? = nil,
957-
/// initialTaskExecutor: (Builtin.Executor)? = nil,
957+
/// initialTaskExecutorDeprecated: (Builtin.Executor)? = nil,
958+
/// initialTaskExecutorOwned: (any TaskExecutor)? = nil,
958959
/// operation: sending @escaping () async throws -> T)
959960
/// -> Builtin.NativeObject, Builtin.RawPointer)
960961
///

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ BASELINE_LANGUAGE_FEATURE(BuiltinBuildExecutor, 0, "Executor-building builtins")
193193
BASELINE_LANGUAGE_FEATURE(BuiltinBuildComplexEqualityExecutor, 0, "Executor-building for 'complexEquality executor' builtins")
194194
BASELINE_LANGUAGE_FEATURE(BuiltinBuildMainExecutor, 0, "MainActor executor building builtin")
195195
BASELINE_LANGUAGE_FEATURE(BuiltinCreateAsyncTaskOwnedTaskExecutor, 0, "Task create with owned TaskExecutor")
196+
BASELINE_LANGUAGE_FEATURE(BuiltinCreateAsyncTaskName, 0, "Task create with a name")
196197
BASELINE_LANGUAGE_FEATURE(BuiltinCreateAsyncTaskInGroup, 0, "Task create in task group builtin with extra flags")
197198
BASELINE_LANGUAGE_FEATURE(BuiltinCreateAsyncTaskInGroupWithExecutor, 0, "Task create in task group builtin with extra flags")
198199
BASELINE_LANGUAGE_FEATURE(BuiltinCreateAsyncDiscardingTaskInGroup, 0, "Task create in discarding task group builtin, accounting for the Void return type")

include/swift/Runtime/Concurrency.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,9 @@ void swift_task_reportUnexpectedExecutor(
10521052
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
10531053
JobPriority swift_task_getCurrentThreadPriority(void);
10541054

1055+
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
1056+
const char *swift_task_getCurrentTaskName(void);
1057+
10551058
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
10561059
void swift_task_startOnMainActor(AsyncTask* job);
10571060

lib/AST/Builtins.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,7 @@ static ValueDecl *getCreateTask(ASTContext &ctx, Identifier id) {
15731573
_existential(_taskExecutor),
15741574
/*else*/ _executor))),
15751575
_nil)),
1576+
_label("taskName", _defaulted(_optional(_rawPointer), _nil)),
15761577
_label("operation",
15771578
_sending(_function(_async(_throws(_thick)), _typeparam(0),
15781579
_parameters())))),
@@ -1597,6 +1598,7 @@ static ValueDecl *getCreateDiscardingTask(ASTContext &ctx, Identifier id) {
15971598
_existential(_taskExecutor),
15981599
/*else*/ _executor))),
15991600
_nil)),
1601+
_label("taskName", _defaulted(_optional(_rawPointer), _nil)),
16001602
_label("operation", _sending(_function(_async(_throws(_thick)), _void,
16011603
_parameters())))),
16021604
_tuple(_nativeObject, _rawPointer));

0 commit comments

Comments
 (0)