Skip to content

Commit ff6b67c

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.
1 parent 5a42c27 commit ff6b67c

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
@@ -225,6 +225,8 @@ static void destroyJob(SWIFT_CONTEXT HeapObject *obj) {
225225
}
226226

227227
AsyncTask::~AsyncTask() {
228+
flagAsCompleted();
229+
228230
// For a future, destroy the result.
229231
if (isFuture()) {
230232
futureFragment()->destroy();

stdlib/public/Concurrency/TaskPrivate.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,16 @@ inline void AsyncTask::flagAsSuspended() {
384384
}
385385
}
386386

387+
// READ ME: This is not a dead function! Do not remove it! This is a function
388+
// that can be used when debugging locally to instrument when a task actually is
389+
// dealloced.
390+
inline void AsyncTask::flagAsCompleted() {
391+
#if SWIFT_TASK_PRINTF_DEBUG
392+
fprintf(stderr, "[%lu] task completed %p\n",
393+
_swift_get_thread_id(), this);
394+
#endif
395+
}
396+
387397
inline void AsyncTask::localValuePush(const HeapObject *key,
388398
/* +1 */ OpaqueValue *value,
389399
const Metadata *valueType) {

0 commit comments

Comments
 (0)