Skip to content

Commit 4b96536

Browse files
committed
[concurrency] Add AsyncTask::flagAsCompleted() and behind SWIFT_TASK_PRINTF_DEBUG allow it to be used to log Task completion.
This should let people debug Task lifetime easier. (cherry picked from commit ff6b67c)
1 parent 821ae4f commit 4b96536

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

include/swift/ABI/Task.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ class AsyncTask : public Job {
279279
void flagAsSuspended();
280280
void flagAsSuspended_slow();
281281

282+
/// Flag that this task is now completed. This normally does not do anything
283+
/// but can be used to locally insert logging.
284+
void flagAsCompleted();
285+
282286
/// Check whether this task has been cancelled.
283287
/// Checking this is, of course, inherently race-prone on its own.
284288
bool isCancelled() const;

stdlib/public/Concurrency/Task.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ static void destroyJob(SWIFT_CONTEXT HeapObject *obj) {
219219
}
220220

221221
AsyncTask::~AsyncTask() {
222+
flagAsCompleted();
223+
222224
// For a future, destroy the result.
223225
if (isFuture()) {
224226
futureFragment()->destroy();

stdlib/public/Concurrency/TaskPrivate.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,16 @@ inline void AsyncTask::flagAsSuspended() {
357357
}
358358
}
359359

360+
// READ ME: This is not a dead function! Do not remove it! This is a function
361+
// that can be used when debugging locally to instrument when a task actually is
362+
// dealloced.
363+
inline void AsyncTask::flagAsCompleted() {
364+
#if SWIFT_TASK_PRINTF_DEBUG
365+
fprintf(stderr, "[%lu] task completed %p\n",
366+
_swift_get_thread_id(), this);
367+
#endif
368+
}
369+
360370
inline void AsyncTask::localValuePush(const HeapObject *key,
361371
/* +1 */ OpaqueValue *value,
362372
const Metadata *valueType) {

0 commit comments

Comments
 (0)