Skip to content

Commit fccadb4

Browse files
authored
Merge pull request #37803 from compnerd/printf-thread-id
Concurrency: add Windows support to `SWIFT_TASK_PRINTF_DEBUG`
2 parents e44dbfc + d09d4b9 commit fccadb4

File tree

4 files changed

+65
-21
lines changed

4 files changed

+65
-21
lines changed

stdlib/public/Concurrency/Actor.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ static ExecutorRef swift_task_getCurrentExecutorImpl() {
276276
auto result = (currentTracking ? currentTracking->getActiveExecutor()
277277
: ExecutorRef::generic());
278278
#if SWIFT_TASK_PRINTF_DEBUG
279-
fprintf(stderr, "[%p] getting current executor %p\n", pthread_self(), result.getIdentity());
279+
fprintf(stderr, "[%lu] getting current executor %p\n",
280+
_swift_get_thread_id(), result.getIdentity());
280281
#endif
281282
return result;
282283
}
@@ -1181,7 +1182,8 @@ static Job *preprocessQueue(JobRef first,
11811182

11821183
void DefaultActorImpl::giveUpThread(RunningJobInfo runner) {
11831184
#if SWIFT_TASK_PRINTF_DEBUG
1184-
fprintf(stderr, "[%p] giving up thread for actor %p\n", pthread_self(), this);
1185+
fprintf(stderr, "[%lu] giving up thread for actor %p\n",
1186+
_swift_get_thread_id(), this);
11851187
#endif
11861188
auto oldState = CurrentState.load(std::memory_order_acquire);
11871189
assert(oldState.Flags.isAnyRunningStatus());
@@ -1245,8 +1247,10 @@ void DefaultActorImpl::giveUpThread(RunningJobInfo runner) {
12451247
}
12461248

12471249
#if SWIFT_TASK_PRINTF_DEBUG
1248-
# define LOG_STATE_TRANSITION fprintf(stderr, "[%p] actor %p transitioned from %zx to %zx (%s)\n", \
1249-
pthread_self(), this, oldState.Flags.getOpaqueValue(), newState.Flags.getOpaqueValue(), __FUNCTION__)
1250+
# define LOG_STATE_TRANSITION \
1251+
fprintf(stderr, "[%lu] actor %p transitioned from %zx to %zx (%s)\n", \
1252+
_swift_get_thread_id(), this, oldState.Flags.getOpaqueValue(), \
1253+
newState.Flags.getOpaqueValue(), __FUNCTION__)
12501254
#else
12511255
# define LOG_STATE_TRANSITION ((void)0)
12521256
#endif
@@ -1503,7 +1507,8 @@ SWIFT_CC(swiftasync)
15031507
static void processDefaultActor(DefaultActorImpl *currentActor,
15041508
RunningJobInfo runner) {
15051509
#if SWIFT_TASK_PRINTF_DEBUG
1506-
fprintf(stderr, "[%p] processDefaultActor %p\n", pthread_self(), currentActor);
1510+
fprintf(stderr, "[%lu] processDefaultActor %p\n",
1511+
_swift_get_thread_id(), currentActor);
15071512
#endif
15081513
DefaultActorImpl *actor = currentActor;
15091514

@@ -1529,7 +1534,8 @@ static void processDefaultActor(DefaultActorImpl *currentActor,
15291534
runner);
15301535

15311536
#if SWIFT_TASK_PRINTF_DEBUG
1532-
fprintf(stderr, "[%p] processDefaultActor %p claimed job %p\n", pthread_self(), currentActor, job);
1537+
fprintf(stderr, "[%lu] processDefaultActor %p claimed job %p\n",
1538+
_swift_get_thread_id(), currentActor, job);
15331539
#endif
15341540

15351541
// If we failed to claim a job, we have nothing to do.
@@ -1551,7 +1557,8 @@ static void processDefaultActor(DefaultActorImpl *currentActor,
15511557
auto currentExecutor = trackingInfo.getActiveExecutor();
15521558

15531559
#if SWIFT_TASK_PRINTF_DEBUG
1554-
fprintf(stderr, "[%p] processDefaultActor %p current executor now %p\n", pthread_self(), currentActor, currentExecutor.getIdentity());
1560+
fprintf(stderr, "[%lu] processDefaultActor %p current executor now %p\n",
1561+
_swift_get_thread_id(), currentActor, currentExecutor.getIdentity());
15551562
#endif
15561563

15571564
if (!currentExecutor.isDefaultActor()) {
@@ -1872,7 +1879,8 @@ static void runOnAssumedThread(AsyncTask *task, ExecutorRef executor,
18721879
trackingInfo.leave();
18731880

18741881
#if SWIFT_TASK_PRINTF_DEBUG
1875-
fprintf(stderr, "[%p] leaving assumed thread, current executor is %p\n", pthread_self(), executor.getIdentity());
1882+
fprintf(stderr, "[%lu] leaving assumed thread, current executor is %p\n",
1883+
_swift_get_thread_id(), executor.getIdentity());
18761884
#endif
18771885

18781886
if (executor.isDefaultActor())
@@ -1888,7 +1896,9 @@ static void swift_task_switchImpl(SWIFT_ASYNC_CONTEXT AsyncContext *resumeContex
18881896
(trackingInfo ? trackingInfo->getActiveExecutor()
18891897
: ExecutorRef::generic());
18901898
#if SWIFT_TASK_PRINTF_DEBUG
1891-
fprintf(stderr, "[%p] trying to switch from executor %p to %p\n", pthread_self(), currentExecutor.getIdentity(), newExecutor.getIdentity());
1899+
fprintf(stderr, "[%lu] trying to switch from executor %p to %p\n",
1900+
_swift_get_thread_id(), currentExecutor.getIdentity(),
1901+
newExecutor.getIdentity());
18921902
#endif
18931903

18941904
// If the current executor is compatible with running the new executor,
@@ -1916,7 +1926,8 @@ static void swift_task_switchImpl(SWIFT_ASYNC_CONTEXT AsyncContext *resumeContex
19161926
!shouldYieldThread() &&
19171927
tryAssumeThreadForSwitch(newExecutor, runner)) {
19181928
#if SWIFT_TASK_PRINTF_DEBUG
1919-
fprintf(stderr, "[%p] switch succeeded, task %p assumed thread for executor %p\n", pthread_self(), task, newExecutor.getIdentity());
1929+
fprintf(stderr, "[%lu] switch succeeded, task %p assumed thread for executor %p\n",
1930+
_swift_get_thread_id(), task, newExecutor.getIdentity());
19201931
#endif
19211932
giveUpThreadForSwitch(currentExecutor, runner);
19221933
// 'return' forces tail call
@@ -1926,7 +1937,8 @@ static void swift_task_switchImpl(SWIFT_ASYNC_CONTEXT AsyncContext *resumeContex
19261937
// Otherwise, just asynchronously enqueue the task on the given
19271938
// executor.
19281939
#if SWIFT_TASK_PRINTF_DEBUG
1929-
fprintf(stderr, "[%p] switch failed, task %p enqueued on executor %p\n", pthread_self(), task, newExecutor.getIdentity());
1940+
fprintf(stderr, "[%lu] switch failed, task %p enqueued on executor %p\n",
1941+
_swift_get_thread_id(), task, newExecutor.getIdentity());
19301942
#endif
19311943
swift_task_enqueue(task, newExecutor);
19321944
}
@@ -1946,7 +1958,8 @@ void _swift_task_enqueueOnExecutor(Job *job, HeapObject *executor,
19461958
SWIFT_CC(swift)
19471959
static void swift_task_enqueueImpl(Job *job, ExecutorRef executor) {
19481960
#if SWIFT_TASK_PRINTF_DEBUG
1949-
fprintf(stderr, "[%p] enqueue job %p on executor %p\n", pthread_self(), job, executor.getIdentity());
1961+
fprintf(stderr, "[%lu] enqueue job %p on executor %p\n",
1962+
_swift_get_thread_id(), job, executor.getIdentity());
19501963
#endif
19511964

19521965
assert(job && "no job provided");

stdlib/public/Concurrency/AsyncLet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ static void swift_asyncLet_endImpl(AsyncLet *alet) {
163163
assert(parent && "async-let must have a parent task");
164164

165165
#if SWIFT_TASK_PRINTF_DEBUG
166-
fprintf(stderr, "[%p] async let end of task %p, parent: %p\n", pthread_self(), task, parent);
166+
fprintf(stderr, "[%lu] async let end of task %p, parent: %p\n",
167+
_swift_get_thread_id(), task, parent);
167168
#endif
168169
_swift_task_dealloc_specific(parent, task);
169170
}

stdlib/public/Concurrency/Task.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,17 @@ FutureFragment::Status AsyncTask::waitFuture(AsyncTask *waitingTask) {
6666
case Status::Error:
6767
case Status::Success:
6868
#if SWIFT_TASK_PRINTF_DEBUG
69-
fprintf(stderr, "[%p] task %p waiting on task %p, completed immediately\n", pthread_self(), waitingTask, this);
69+
fprintf(stderr, "[%lu] task %p waiting on task %p, completed immediately\n",
70+
_swift_get_thread_id(), waitingTask, this);
7071
#endif
7172
_swift_tsan_acquire(static_cast<Job *>(this));
7273
// The task is done; we don't need to wait.
7374
return queueHead.getStatus();
7475

7576
case Status::Executing:
7677
#if SWIFT_TASK_PRINTF_DEBUG
77-
fprintf(stderr, "[%p] task %p waiting on task %p, going to sleep\n", pthread_self(), waitingTask, this);
78+
fprintf(stderr, "[%lu] task %p waiting on task %p, going to sleep\n",
79+
_swift_get_thread_id(), waitingTask, this);
7880
#endif
7981
_swift_tsan_release(static_cast<Job *>(waitingTask));
8082
// Task is now complete. We'll need to add ourselves to the queue.
@@ -151,7 +153,8 @@ void AsyncTask::completeFuture(AsyncContext *context) {
151153

152154
#if SWIFT_TASK_PRINTF_DEBUG
153155
if (!waitingTask)
154-
fprintf(stderr, "[%p] task %p had no waiting tasks\n", pthread_self(), this);
156+
fprintf(stderr, "[%lu] task %p had no waiting tasks\n",
157+
_swift_get_thread_id(), this);
155158
#endif
156159

157160
while (waitingTask) {
@@ -160,7 +163,8 @@ void AsyncTask::completeFuture(AsyncContext *context) {
160163
auto nextWaitingTask = waitingTask->getNextWaitingTask();
161164

162165
#if SWIFT_TASK_PRINTF_DEBUG
163-
fprintf(stderr, "[%p] waking task %p from future of task %p\n", pthread_self(), waitingTask, this);
166+
fprintf(stderr, "[%lu] waking task %p from future of task %p\n",
167+
_swift_get_thread_id(), waitingTask, this);
164168
#endif
165169

166170
// Fill in the return context.
@@ -210,7 +214,7 @@ static void destroyTask(SWIFT_CONTEXT HeapObject *obj) {
210214
// here.
211215

212216
#if SWIFT_TASK_PRINTF_DEBUG
213-
fprintf(stderr, "[%p] destroy task %p\n", pthread_self(), task);
217+
fprintf(stderr, "[%lu] destroy task %p\n", _swift_get_thread_id(), task);
214218
#endif
215219
free(task);
216220
}
@@ -286,7 +290,7 @@ static void completeTaskImpl(AsyncTask *task,
286290
task->Private.complete(task);
287291

288292
#if SWIFT_TASK_PRINTF_DEBUG
289-
fprintf(stderr, "[%p] task %p completed\n", pthread_self(), task);
293+
fprintf(stderr, "[%lu] task %p completed\n", _swift_get_thread_id(), task);
290294
#endif
291295

292296
// Complete the future.
@@ -434,7 +438,8 @@ static AsyncTaskAndContext swift_task_create_group_future_commonImpl(
434438
allocation = malloc(amountToAllocate);
435439
}
436440
#if SWIFT_TASK_PRINTF_DEBUG
437-
fprintf(stderr, "[%p] allocate task %p, parent = %p\n", pthread_self(), allocation, parent);
441+
fprintf(stderr, "[%lu] allocate task %p, parent = %p\n",
442+
_swift_get_thread_id(), allocation, parent);
438443
#endif
439444

440445
AsyncContext *initialContext =
@@ -511,7 +516,8 @@ static AsyncTaskAndContext swift_task_create_group_future_commonImpl(
511516
}
512517

513518
#if SWIFT_TASK_PRINTF_DEBUG
514-
fprintf(stderr, "[%p] creating task %p with parent %p\n", pthread_self(), task, parent);
519+
fprintf(stderr, "[%lu] creating task %p with parent %p\n",
520+
_swift_get_thread_id(), task, parent);
515521
#endif
516522

517523
// Initialize the task-local allocator.

stdlib/public/Concurrency/TaskPrivate.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,35 @@
2727
#define SWIFT_FATAL_ERROR swift_Concurrency_fatalError
2828
#include "../runtime/StackAllocator.h"
2929

30+
#if HAVE_PTHREAD_H
31+
#include <pthread.h>
32+
#endif
33+
#if defined(_WIN32)
34+
#define WIN32_LEAN_AND_MEAN
35+
#define VC_EXTRA_LEAN
36+
#define NOMINMAX
37+
#include <Windows.h>
38+
#endif
39+
3040
namespace swift {
3141

3242
// Uncomment to enable helpful debug spew to stderr
3343
//#define SWIFT_TASK_PRINTF_DEBUG 1
3444

45+
#if defined(_WIN32)
46+
using ThreadID = decltype(GetCurrentThreadId());
47+
#else
48+
using ThreadID = decltype(pthread_self());
49+
#endif
50+
51+
inline ThreadID _swift_get_thread_id() {
52+
#if defined(_WIN32)
53+
return GetCurrentThreadId();
54+
#else
55+
return pthread_self();
56+
#endif
57+
}
58+
3559
class AsyncTask;
3660
class TaskGroup;
3761

0 commit comments

Comments
 (0)