Skip to content

Commit 14a4bd4

Browse files
committed
[Concurrency][Threading] Remove use of platform thread functions.
The concurrency library can use the new threading library functions, which avoids the problem of including <windows.h>. rdar://90776105
1 parent 66f6eb6 commit 14a4bd4

File tree

6 files changed

+71
-60
lines changed

6 files changed

+71
-60
lines changed

include/swift/Threading/Thread.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ namespace swift {
2525

2626
/// Identifies a thread
2727
class Thread {
28+
public:
29+
using Id = threading_impl::thread_id;
30+
2831
private:
29-
threading_impl::thread_id id_;
32+
Id id_;
3033

3134
public:
3235
Thread() {}
36+
explicit Thread(Id platformId) : id_(platformId) {}
3337
Thread(const Thread& other) : id_(other.id_) {}
3438
Thread(Thread&& other) : id_(std::move(other.id_)) {}
3539

@@ -43,6 +47,9 @@ class Thread {
4347
return *this;
4448
}
4549

50+
/// Returns the platform specific thread ID
51+
Id platformThreadId() const { return id_; }
52+
4653
/// Returns the currently executing thread
4754
static Thread current() {
4855
return Thread(threading_impl::thread_get_current());

lib/Threading/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_swift_host_library(swiftThreading STATIC
22
C11.cpp
3+
Darwin.cpp
34
Linux.cpp
45
Nothreads.cpp
56
Pthreads.cpp

lib/Threading/Darwin.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//==--- Darwin.cpp - Threading abstraction implementation ------ -*-C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Implements threading support for Darwin
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#if SWIFT_THREADING_DARWIN
18+
19+
#include <pthread.h>
20+
21+
#include "swift/Threading/Errors.h"
22+
#include "swift/Threading/Impl/Darwin.h"
23+
24+
namespace {
25+
26+
#pragma clang diagnostic push
27+
#pragma GCC diagnostic ignored "-Wglobal-constructors"
28+
29+
class MainThreadRememberer {
30+
private:
31+
pthread_t mainThread_;
32+
public:
33+
MainThreadRememberer() { mainThread_ = pthread_self(); }
34+
35+
pthread_t main_thread() const { return mainThread_; }
36+
};
37+
38+
MainThreadRememberer rememberer;
39+
40+
#pragma clang diagnostic pop
41+
42+
}
43+
44+
using namespace swift;
45+
using namespace threading_impl;
46+
47+
thread_id
48+
swift::threading_impl::thread_get_main() {
49+
return rememberer.main_thread();
50+
}
51+
52+
bool
53+
swift::threading_impl::thread_is_main() {
54+
return pthread_main_np();
55+
}
56+
57+
#endif // SWIFT_THREADING_DARWIN

stdlib/public/Concurrency/Actor.cpp

+2-32
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "swift/Threading/Once.h"
3636
#include "swift/Threading/Mutex.h"
3737
#include "swift/Threading/ThreadLocalStorage.h"
38+
#include "swift/Threading/Thread.h"
3839
#include "swift/ABI/Task.h"
3940
#include "swift/ABI/Actor.h"
4041
#include "swift/Basic/ListMerger.h"
@@ -67,21 +68,6 @@
6768
#include <sys/syscall.h>
6869
#endif
6970

70-
#if defined(_POSIX_THREADS)
71-
#include <pthread.h>
72-
73-
// Only use __has_include since HAVE_PTHREAD_NP_H is not provided.
74-
#if __has_include(<pthread_np.h>)
75-
#include <pthread_np.h>
76-
#endif
77-
#endif
78-
79-
#if defined(_WIN32)
80-
#include <io.h>
81-
#include <handleapi.h>
82-
#include <processthreadsapi.h>
83-
#endif
84-
8571
#if SWIFT_OBJC_INTEROP
8672
extern "C" void *objc_autoreleasePoolPush();
8773
extern "C" void objc_autoreleasePoolPop(void *);
@@ -278,29 +264,13 @@ static ExecutorRef swift_task_getCurrentExecutorImpl() {
278264
return result;
279265
}
280266

281-
#if defined(_WIN32)
282-
static HANDLE __initialPthread = INVALID_HANDLE_VALUE;
283-
#endif
284-
285267
/// Determine whether we are currently executing on the main thread
286268
/// independently of whether we know that we are on the main actor.
287269
static bool isExecutingOnMainThread() {
288270
#if SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY
289271
return true;
290-
#elif defined(__linux__)
291-
return syscall(SYS_gettid) == getpid();
292-
#elif defined(_WIN32)
293-
if (__initialPthread == INVALID_HANDLE_VALUE) {
294-
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
295-
GetCurrentProcess(), &__initialPthread, 0, FALSE,
296-
DUPLICATE_SAME_ACCESS);
297-
}
298-
299-
return __initialPthread == GetCurrentThread();
300-
#elif defined(__wasi__)
301-
return true;
302272
#else
303-
return pthread_main_np() == 1;
273+
return Thread::onMainThread();
304274
#endif
305275
}
306276

stdlib/public/Concurrency/TaskPrivate.h

+2-27
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717
#ifndef SWIFT_CONCURRENCY_TASKPRIVATE_H
1818
#define SWIFT_CONCURRENCY_TASKPRIVATE_H
1919

20-
#if defined(_WIN32)
21-
#define WIN32_LEAN_AND_MEAN
22-
#define VC_EXTRA_LEAN
23-
#define NOMINMAX
24-
#include <Windows.h>
25-
#endif
26-
2720
#include "Error.h"
2821
#include "Tracing.h"
2922
#include "swift/ABI/Metadata.h"
@@ -34,41 +27,23 @@
3427
#include "swift/Runtime/Error.h"
3528
#include "swift/Runtime/Exclusivity.h"
3629
#include "swift/Runtime/HeapObject.h"
30+
#include "swift/Threading/Thread.h"
3731
#include <atomic>
3832
#include <new>
3933

4034
#define SWIFT_FATAL_ERROR swift_Concurrency_fatalError
4135
#include "../runtime/StackAllocator.h"
4236

43-
#if HAVE_PTHREAD_H
44-
#include <pthread.h>
45-
#endif
46-
4737
namespace swift {
4838

4939
// Set to 1 to enable helpful debug spew to stderr
5040
// If this is enabled, tests with `swift_task_debug_log` requirement can run.
5141
#if 0
5242
#define SWIFT_TASK_DEBUG_LOG(fmt, ...) \
5343
fprintf(stderr, "[%lu] [%s:%d](%s) " fmt "\n", \
54-
(unsigned long)_swift_get_thread_id(), \
44+
(unsigned long)Thread::current()::platformThreadId(), \
5545
__FILE__, __LINE__, __FUNCTION__, \
5646
__VA_ARGS__)
57-
58-
#if defined(_WIN32)
59-
using ThreadID = decltype(GetCurrentThreadId());
60-
#else
61-
using ThreadID = decltype(pthread_self());
62-
#endif
63-
64-
inline ThreadID _swift_get_thread_id() {
65-
#if defined(_WIN32)
66-
return GetCurrentThreadId();
67-
#else
68-
return pthread_self();
69-
#endif
70-
}
71-
7247
#else
7348
#define SWIFT_TASK_DEBUG_LOG(fmt, ...) (void)0
7449
#endif

stdlib/public/Threading/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_swift_target_library(swiftThreading OBJECT_LIBRARY
22
"${SWIFT_SOURCE_DIR}/lib/Threading/C11.cpp"
3+
"${SWIFT_SOURCE_DIR}/lib/Threading/Darwin.cpp"
34
"${SWIFT_SOURCE_DIR}/lib/Threading/Linux.cpp"
45
"${SWIFT_SOURCE_DIR}/lib/Threading/Nothreads.cpp"
56
"${SWIFT_SOURCE_DIR}/lib/Threading/Pthreads.cpp"

0 commit comments

Comments
 (0)