Skip to content

Commit ede4010

Browse files
committed
[BackDeployConcurrency] Fix to use the new threading library.
A copy of the older Concurrency sources was just added to BackDeployConcurrency, replacing the `add_subdirectory(../Concurrency)` trick it was using previously. These need to be adjusted to work with the threading library. Fortunately, the necessary adjustments are the same as those required for the existing Concurrency library, give or take. rdar://90776105
1 parent c46d4af commit ede4010

16 files changed

+154
-127
lines changed

stdlib/public/BackDeployConcurrency/Actor.cpp

Lines changed: 25 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,14 @@
1717

1818
#include "ConcurrencyRuntime.h"
1919

20-
#ifdef _WIN32
21-
// On Windows, an include below triggers an indirect include of minwindef.h
22-
// which contains a definition of the `max` macro, generating an error in our
23-
// use of std::max in this file. This define prevents those macros from being
24-
// defined.
25-
#define NOMINMAX
26-
#endif
27-
2820
#include "CompatibilityOverride.h"
21+
#include "swift/Basic/ListMerger.h"
2922
#include "swift/Runtime/Atomic.h"
3023
#include "swift/Runtime/Casting.h"
31-
#include "swift/Runtime/Once.h"
32-
#include "swift/Runtime/Mutex.h"
33-
#include "swift/Runtime/ThreadLocal.h"
34-
#include "swift/Runtime/ThreadLocalStorage.h"
35-
#include "swift/Basic/ListMerger.h"
24+
#include "swift/Threading/Once.h"
25+
#include "swift/Threading/Mutex.h"
26+
#include "swift/Threading/Thread.h"
27+
#include "swift/Threading/ThreadLocalStorage.h"
3628
#ifndef SWIFT_CONCURRENCY_BACK_DEPLOYMENT
3729
#include "llvm/Config/config.h"
3830
#else
@@ -71,19 +63,8 @@
7163
#include <sys/syscall.h>
7264
#endif
7365

74-
#if HAVE_PTHREAD_H
75-
#include <pthread.h>
76-
77-
// Only use __has_include since HAVE_PTHREAD_NP_H is not provided.
78-
#if __has_include(<pthread_np.h>)
79-
#include <pthread_np.h>
80-
#endif
81-
#endif
82-
8366
#if defined(_WIN32)
8467
#include <io.h>
85-
#include <handleapi.h>
86-
#include <processthreadsapi.h>
8768
#endif
8869

8970
#if SWIFT_OBJC_INTEROP
@@ -129,9 +110,9 @@ class ExecutorTrackingInfo {
129110
/// the right executor. It would make sense for that to be a
130111
/// separate thread-local variable (or whatever is most efficient
131112
/// on the target platform).
132-
static SWIFT_RUNTIME_DECLARE_THREAD_LOCAL(
133-
Pointer<ExecutorTrackingInfo>, ActiveInfoInThread,
134-
SWIFT_CONCURRENCY_EXECUTOR_TRACKING_INFO_KEY);
113+
static SWIFT_THREAD_LOCAL_TYPE(Pointer<ExecutorTrackingInfo>,
114+
tls_key::concurrency_executor_tracking_info)
115+
ActiveInfoInThread;
135116

136117
/// The active executor.
137118
ExecutorRef ActiveExecutor = ExecutorRef::generic();
@@ -154,7 +135,7 @@ class ExecutorTrackingInfo {
154135

155136
/// Unconditionally initialize a fresh tracking state on the
156137
/// current state, shadowing any previous tracking state.
157-
/// leave() must be called beforet the object goes out of scope.
138+
/// leave() must be called before the object goes out of scope.
158139
void enterAndShadow(ExecutorRef currentExecutor) {
159140
ActiveExecutor = currentExecutor;
160141
SavedInfo = ActiveInfoInThread.get();
@@ -197,24 +178,21 @@ class ExecutorTrackingInfo {
197178
class ActiveTask {
198179
/// A thread-local variable pointing to the active tracking
199180
/// information about the current thread, if any.
200-
static SWIFT_RUNTIME_DECLARE_THREAD_LOCAL(Pointer<AsyncTask>, Value,
201-
SWIFT_CONCURRENCY_TASK_KEY);
181+
static SWIFT_THREAD_LOCAL_TYPE(Pointer<AsyncTask>,
182+
tls_key::concurrency_task) Value;
202183

203184
public:
204185
static void set(AsyncTask *task) { Value.set(task); }
205186
static AsyncTask *get() { return Value.get(); }
206187
};
207188

208189
/// Define the thread-locals.
209-
SWIFT_RUNTIME_DECLARE_THREAD_LOCAL(
210-
Pointer<AsyncTask>,
211-
ActiveTask::Value,
212-
SWIFT_CONCURRENCY_TASK_KEY);
190+
SWIFT_THREAD_LOCAL_TYPE(Pointer<AsyncTask>, tls_key::concurrency_task)
191+
ActiveTask::Value;
213192

214-
SWIFT_RUNTIME_DECLARE_THREAD_LOCAL(
215-
Pointer<ExecutorTrackingInfo>,
216-
ExecutorTrackingInfo::ActiveInfoInThread,
217-
SWIFT_CONCURRENCY_EXECUTOR_TRACKING_INFO_KEY);
193+
SWIFT_THREAD_LOCAL_TYPE(Pointer<ExecutorTrackingInfo>,
194+
tls_key::concurrency_executor_tracking_info)
195+
ExecutorTrackingInfo::ActiveInfoInThread;
218196

219197
} // end anonymous namespace
220198

@@ -238,7 +216,7 @@ void swift::runJobInEstablishedExecutorContext(Job *job) {
238216
task->runInFullyEstablishedContext();
239217

240218
assert(ActiveTask::get() == nullptr &&
241-
"active task wasn't cleared before susspending?");
219+
"active task wasn't cleared before suspending?");
242220
} else {
243221
// There's no extra bookkeeping to do for simple jobs besides swapping in
244222
// the voucher.
@@ -281,30 +259,20 @@ static ExecutorRef swift_task_getCurrentExecutorImpl() {
281259
return result;
282260
}
283261

284-
#if defined(_WIN32)
285-
static HANDLE __initialPthread = INVALID_HANDLE_VALUE;
286-
#endif
287-
288262
/// Determine whether we are currently executing on the main thread
289263
/// independently of whether we know that we are on the main actor.
290264
static bool isExecutingOnMainThread() {
291-
#if defined(__linux__)
292-
return syscall(SYS_gettid) == getpid();
293-
#elif defined(_WIN32)
294-
if (__initialPthread == INVALID_HANDLE_VALUE) {
295-
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
296-
GetCurrentProcess(), &__initialPthread, 0, FALSE,
297-
DUPLICATE_SAME_ACCESS);
298-
}
299-
300-
return __initialPthread == GetCurrentThread();
265+
#if SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY
266+
return true;
301267
#else
302-
return pthread_main_np() == 1;
268+
return Thread::onMainThread();
303269
#endif
304270
}
305271

306272
JobPriority swift::swift_task_getCurrentThreadPriority() {
307-
#if defined(__APPLE__)
273+
#if SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY
274+
return JobPriority::UserInitiated;
275+
#elif defined(__APPLE__)
308276
return static_cast<JobPriority>(qos_class_self());
309277
#else
310278
if (isExecutingOnMainThread())
@@ -344,8 +312,8 @@ void swift::swift_task_reportUnexpectedExecutor(
344312
const unsigned char *file, uintptr_t fileLength, bool fileIsASCII,
345313
uintptr_t line, ExecutorRef executor) {
346314
// Make sure we have an appropriate log level.
347-
static swift_once_t logLevelToken;
348-
swift_once(&logLevelToken, checkUnexpectedExecutorLogLevel, nullptr);
315+
static swift::once_t logLevelToken;
316+
swift::once(logLevelToken, checkUnexpectedExecutorLogLevel, nullptr);
349317

350318
bool isFatalError = false;
351319
switch (unexpectedExecutorLogLevel) {

stdlib/public/BackDeployConcurrency/AsyncLet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
#include "CompatibilityOverride.h"
1818
#include "ConcurrencyRuntime.h"
1919
#include "swift/ABI/Metadata.h"
20-
#include "swift/Runtime/Mutex.h"
20+
#include "swift/Runtime/Heap.h"
2121
#include "swift/Runtime/HeapObject.h"
22+
#include "swift/Threading/Mutex.h"
2223
#include "llvm/ADT/PointerIntPair.h"
2324
#include "AsyncLet.h"
2425
#include "Debug.h"

stdlib/public/BackDeployConcurrency/AsyncStream.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,24 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "swift/Runtime/Mutex.h"
13+
#include <new>
14+
15+
#include "swift/Threading/Mutex.h"
1416

1517
namespace swift {
1618
// return the size in words for the given mutex primitive
1719
extern "C"
1820
size_t _swift_async_stream_lock_size() {
19-
size_t words = sizeof(MutexHandle) / sizeof(void *);
21+
size_t words = sizeof(Mutex) / sizeof(void *);
2022
if (words < 1) { return 1; }
2123
return words;
2224
}
2325

24-
extern "C"
25-
void _swift_async_stream_lock_init(MutexHandle &lock) {
26-
MutexPlatformHelper::init(lock);
26+
extern "C" void _swift_async_stream_lock_init(Mutex &lock) {
27+
new (&lock) Mutex();
2728
}
2829

29-
extern "C"
30-
void _swift_async_stream_lock_lock(MutexHandle &lock) {
31-
MutexPlatformHelper::lock(lock);
32-
}
33-
34-
extern "C"
35-
void _swift_async_stream_lock_unlock(MutexHandle &lock) {
36-
MutexPlatformHelper::unlock(lock);
37-
}
30+
extern "C" void _swift_async_stream_lock_lock(Mutex &lock) { lock.lock(); }
3831

32+
extern "C" void _swift_async_stream_lock_unlock(Mutex &lock) { lock.unlock(); }
3933
}

stdlib/public/BackDeployConcurrency/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ add_swift_target_library(swift_Concurrency ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
9999
AsyncThrowingFlatMapSequence.swift
100100
AsyncThrowingMapSequence.swift
101101
AsyncThrowingPrefixWhileSequence.swift
102+
ConditionVariable.cpp
102103
GlobalActor.swift
103104
MainActor.swift
104105
PartialAsyncTask.swift
@@ -114,7 +115,7 @@ add_swift_target_library(swift_Concurrency ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
114115
TaskLocal.swift
115116
TaskSleep.swift
116117
ThreadSanitizer.cpp
117-
Mutex.cpp
118+
ThreadingError.cpp
118119
AsyncStreamBuffer.swift
119120
AsyncStream.swift
120121
AsyncThrowingStream.swift
@@ -123,6 +124,8 @@ add_swift_target_library(swift_Concurrency ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
123124
${swift_concurrency_extra_sources}
124125
../Concurrency/linker-support/magic-symbols-for-install-name.c
125126

127+
INCORPORATE_OBJECT_LIBRARIES_SHARED_ONLY
128+
swiftThreading
126129
LINK_LIBRARIES ${swift_concurrency_link_libraries}
127130

128131
C_COMPILE_FLAGS

stdlib/public/BackDeployConcurrency/Mutex.cpp renamed to stdlib/public/BackDeployConcurrency/ConditionVariable.cpp

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- Mutex.cpp - Mutex support code -----------------------------------===//
1+
//===--- ConditionVariable.cpp - A condition variable ---------------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -11,21 +11,51 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "Error.h"
14-
15-
#define SWIFT_FATAL_ERROR swift_Concurrency_fatalError
16-
17-
// Include the runtime's mutex support code.
18-
// FIXME: figure out some reasonable way to share this stuff
19-
2014
#include "ConditionVariable.h"
21-
#include "../runtime/MutexPThread.cpp"
22-
#include "../runtime/MutexWin32.cpp"
23-
#ifdef SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
24-
#include "swift/Runtime/MutexSingleThreaded.h"
25-
#endif
2615

2716
using namespace swift;
2817

18+
#define fatalError swift_Concurrency_fatalError
19+
20+
#define reportError(PThreadFunction) \
21+
do { \
22+
int errorcode = PThreadFunction; \
23+
if (errorcode != 0) { \
24+
fatalError(/* flags = */ 0, "'%s' failed with error '%s'(%d)\n", \
25+
#PThreadFunction, errorName(errorcode), errorcode); \
26+
} \
27+
} while (false)
28+
29+
#define returnTrueOrReportError(PThreadFunction, returnFalseOnEBUSY) \
30+
do { \
31+
int errorcode = PThreadFunction; \
32+
if (errorcode == 0) \
33+
return true; \
34+
if (returnFalseOnEBUSY && errorcode == EBUSY) \
35+
return false; \
36+
fatalError(/* flags = */ 0, "'%s' failed with error '%s'(%d)\n", \
37+
#PThreadFunction, errorName(errorcode), errorcode); \
38+
} while (false)
39+
40+
static const char *errorName(int errorcode) {
41+
switch (errorcode) {
42+
case EINVAL:
43+
return "EINVAL";
44+
case EPERM:
45+
return "EPERM";
46+
case EDEADLK:
47+
return "EDEADLK";
48+
case ENOMEM:
49+
return "ENOMEM";
50+
case EAGAIN:
51+
return "EAGAIN";
52+
case EBUSY:
53+
return "EBUSY";
54+
default:
55+
return "<unknown>";
56+
}
57+
}
58+
2959
void ConditionPlatformHelper::init(pthread_cond_t &condition) {
3060
reportError(pthread_cond_init(&condition, nullptr));
3161
}

stdlib/public/BackDeployConcurrency/ConditionVariable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#ifndef SWIFT_RUNTIME_CONDITION_VARIABLE_H
1919
#define SWIFT_RUNTIME_CONDITION_VARIABLE_H
2020

21-
#include "swift/Runtime/Mutex.h"
21+
#include "swift/Threading/Mutex.h"
2222
#include <type_traits>
2323
#include <utility>
2424

stdlib/public/BackDeployConcurrency/DispatchGlobalExecutor.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ extern "C" void dispatch_queue_set_width(dispatch_queue_t dq, long width);
118118
static dispatch_queue_t getGlobalQueue(JobPriority priority) {
119119
size_t numericPriority = static_cast<size_t>(priority);
120120
if (numericPriority >= globalQueueCacheCount)
121-
swift_Concurrency_fatalError(0, "invalid job priority %#zx");
121+
swift_Concurrency_fatalError(0, "invalid job priority %#zx", numericPriority);
122122

123123
#ifdef SWIFT_CONCURRENCY_BACK_DEPLOYMENT
124124
std::memory_order loadOrder = std::memory_order_acquire;

stdlib/public/BackDeployConcurrency/Error.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,27 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "swift/Threading/Errors.h"
14+
#include <cstdio>
15+
1316
#include "Error.h"
1417

1518
// swift::fatalError is not exported from libswiftCore and not shared, so define another
1619
// internal function instead.
17-
SWIFT_NORETURN void swift::swift_Concurrency_fatalError(uint32_t flags, const char *format, ...) {
20+
SWIFT_NORETURN
21+
SWIFT_VFORMAT(2)
22+
void swift::swift_Concurrency_fatalErrorv(uint32_t flags, const char *format,
23+
va_list val) {
24+
vfprintf(stderr, format, val);
1825
abort();
1926
}
27+
28+
SWIFT_NORETURN
29+
SWIFT_FORMAT(2, 3)
30+
void swift::swift_Concurrency_fatalError(uint32_t flags, const char *format,
31+
...) {
32+
va_list val;
33+
34+
va_start(val, format);
35+
swift_Concurrency_fatalErrorv(flags, format, val);
36+
}

stdlib/public/BackDeployConcurrency/Error.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@
1717
#ifndef SWIFT_CONCURRENCY_ERRORS_H
1818
#define SWIFT_CONCURRENCY_ERRORS_H
1919

20+
#include "swift/Basic/Compiler.h"
21+
2022
#include "../SwiftShims/Visibility.h"
23+
#include <cstdarg>
2124
#include <cstdint>
22-
#include <stdlib.h>
25+
#include <cstdlib>
2326

2427
namespace swift {
2528

26-
SWIFT_NORETURN void swift_Concurrency_fatalError(uint32_t flags, const char *format, ...);
29+
SWIFT_NORETURN SWIFT_FORMAT(2, 3) void swift_Concurrency_fatalError(
30+
uint32_t flags, const char *format, ...);
31+
SWIFT_NORETURN SWIFT_VFORMAT(2) void swift_Concurrency_fatalErrorv(
32+
uint32_t flags, const char *format, va_list val);
2733

2834
} // namespace swift
2935

stdlib/public/BackDeployConcurrency/Executor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func _checkExpectedExecutor(_filenameStart: Builtin.RawPointer,
9393
_filenameStart, _filenameLength, _filenameIsASCII, _line, _executor)
9494
}
9595

96-
#if !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
96+
#if !SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY
9797
// This must take a DispatchQueueShim, not something like AnyObject,
9898
// or else SILGen will emit a retain/release in unoptimized builds,
9999
// which won't work because DispatchQueues aren't actually

stdlib/public/BackDeployConcurrency/Task.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#include "CompatibilityOverride.h"
1818
#include "ConcurrencyRuntime.h"
1919
#include "swift/ABI/Metadata.h"
20-
#include "swift/Runtime/Mutex.h"
2120
#include "swift/Runtime/HeapObject.h"
21+
#include "swift/Threading/Mutex.h"
2222
#include "Task.h"
2323
#include "TaskGroupPrivate.h"
2424
#include "TaskLocal.h"

0 commit comments

Comments
 (0)