Skip to content

Commit 63a0900

Browse files
committed
[Threading] Create new threading library and use it.
Moved all the threading code to one place. Added explicit support for Darwin, Linux, Pthreads, C11 threads and Win32 threads, including new implementations of Once for Linux, Pthreads, C11 and Win32. rdar://90776105
1 parent 34af337 commit 63a0900

File tree

98 files changed

+2287
-1411
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+2287
-1411
lines changed

CMakeLists.txt

+5-4
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,13 @@ option(SWIFT_STDLIB_ENABLE_UNICODE_DATA
126126

127127
include(Threading)
128128

129-
threading_package_default(SWIFT_STDLIB_THREADING_PACKAGE_default)
129+
threading_package_default("${SWIFT_HOST_VARIANT_SDK}"
130+
SWIFT_THREADING_PACKAGE_default)
130131

131-
set(SWIFT_STDLIB_THREADING_PACKAGE "${SWIFT_STDLIB_THREADING_PACKAGE_default}"
132+
set(SWIFT_THREADING_PACKAGE "${SWIFT_THREADING_PACKAGE_default}"
132133
CACHE STRING
133134
"The threading package to use. Must be one of 'none', 'pthreads',
134-
'darwin', 'win32', 'c11'.")
135+
'darwin', 'linux', 'win32', 'c11'.")
135136

136137
option(SWIFT_BUILD_DYNAMIC_SDK_OVERLAY
137138
"Build dynamic variants of the Swift SDK overlay"
@@ -1046,7 +1047,7 @@ if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_SDK_OVERLAY)
10461047
message(STATUS " Leak Detection Checker Entrypoints: ${SWIFT_RUNTIME_ENABLE_LEAK_CHECKER}")
10471048
message(STATUS "")
10481049

1049-
message(STATUS "Threading Package: ${SWIFT_STDLIB_THREADING_PACKAGE}")
1050+
message(STATUS "Threading Package: ${SWIFT_THREADING_PACKAGE}")
10501051
message(STATUS "Differentiable Programming Support: ${SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING}")
10511052
message(STATUS "Concurrency Support: ${SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY}")
10521053
message(STATUS "Distributed Support: ${SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED}")

cmake/modules/AddSwift.cmake

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ include(SwiftXcodeSupport)
44
include(SwiftWindowsSupport)
55
include(SwiftAndroidSupport)
66
include(SwiftCXXUtils)
7+
include(Threading)
78

89
function(_swift_gyb_target_sources target scope)
910
file(GLOB GYB_UNICODE_DATA ${SWIFT_SOURCE_DIR}/utils/UnicodeData/*)
@@ -308,6 +309,10 @@ function(_add_host_variant_c_compile_flags target)
308309
$<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS>)
309310
endif()
310311

312+
threading_package_name("${SWIFT_HOST_VARIANT_SDK}" _threading_package)
313+
target_compile_definitions(${target} PRIVATE
314+
"SWIFT_THREADING_${_threading_package}")
315+
311316
if(SWIFT_ANALYZE_CODE_COVERAGE)
312317
target_compile_options(${target} PRIVATE
313318
$<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:-fprofile-instr-generate -fcoverage-mapping>)

cmake/modules/AddSwiftUnittests.cmake

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ function(add_swift_unittest test_dirname)
4949

5050
# some headers switch their inline implementations based on
5151
# SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY and
52-
# SWIFT_STDLIB_THREADING_PACKAGE definitions
52+
# SWIFT_THREADING_PACKAGE definitions
5353
if(SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY)
5454
target_compile_definitions("${test_dirname}" PRIVATE
5555
SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY)
5656
endif()
57-
threading_package_name(_threading_package)
57+
threading_package_name("${SWIFT_HOST_VARIANT_SDK}" _threading_package)
5858
target_compile_definitions("${test_dirname}" PRIVATE
59-
"SWIFT_STDLIB_THREADING_${_threading_package}")
59+
"SWIFT_THREADING_${_threading_package}")
6060

6161
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
6262
if(SWIFT_USE_LINKER)

cmake/modules/Threading.cmake

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Get the default threading package for the platform
2-
function(threading_package_default out_var)
3-
if("${SWIFT_HOST_VARIANT_SDK}" IN_LIST SWIFT_DARWIN_PLATFORMS)
2+
function(threading_package_default sdk out_var)
3+
if(sdk IN_LIST SWIFT_DARWIN_PLATFORMS)
44
set("${out_var}" "darwin" PARENT_SCOPE)
5-
elseif("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WINDOWS")
5+
elseif(sdk STREQUAL "LINUX")
6+
set("${out_var}" "linux" PARENT_SCOPE)
7+
elseif(sdk STREQUAL "WINDOWS")
68
set("${out_var}" "win32" PARENT_SCOPE)
7-
elseif("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASI")
9+
elseif(sdk STREQUAL "WASI")
810
set("${out_var}" "none" PARENT_SCOPE)
911
else()
1012
set("${out_var}" "pthreads" PARENT_SCOPE)
@@ -14,11 +16,11 @@ endfunction()
1416
# Given the threading package, find the name for the preprocessor
1517
# define that we need to make. Also deals with the default platform
1618
# setting.
17-
function(threading_package_name out_var)
19+
function(threading_package_name sdk out_var)
1820
precondition(SWIFT_HOST_VARIANT_SDK)
1921
precondition(SWIFT_DARWIN_PLATFORMS)
2022

21-
string(TOUPPER "${SWIFT_STDLIB_THREADING_PACKAGE}" package)
23+
string(TOUPPER "${SWIFT_THREADING_PACKAGE}" package)
2224
if(package STREQUAL "")
2325
threading_package_default(package)
2426
string(TOUPPER "${package}" package)

include/swift/Basic/Compiler.h

+16
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,20 @@
162162
#define SWIFT_ASM_LABEL_WITH_PREFIX(STRING) \
163163
SWIFT_ASM_LABEL_RAW(SWIFT_SYMBOL_PREFIX_STRING STRING)
164164

165+
// SWIFT_FORMAT(fmt,first) marks a function as taking a format string argument
166+
// at argument `fmt`, with the first argument for the format string as `first`.
167+
#if __has_attribute(format)
168+
#define SWIFT_FORMAT(fmt,first) __attribute__((format(printf, fmt, first)))
169+
#else
170+
#define SWIFT_FORMAT(fmt,first)
171+
#endif
172+
173+
// SWIFT_VFORMAT(fmt) marks a function as taking a format string argument at
174+
// argument `fmt`, with the arguments in a `va_list`.
175+
#if __has_attribute(format)
176+
#define SWIFT_VFORMAT(fmt) __attribute__((format(printf, fmt, 0)))
177+
#else
178+
#define SWIFT_VFORMAT(fmt)
179+
#endif
180+
165181
#endif // SWIFT_BASIC_COMPILER_H

include/swift/Basic/Lazy.h

+6-35
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,26 @@
1515

1616
#include <memory>
1717

18-
#if SWIFT_STDLIB_THREADING_NONE
19-
// No dependencies on single-threaded environments.
20-
#elif SWIFT_STDLIB_THREADING_DARWIN
21-
#include <dispatch/dispatch.h>
22-
#else
23-
#include <mutex>
24-
#endif
25-
2618
#include "swift/Basic/Malloc.h"
2719
#include "swift/Basic/type_traits.h"
20+
#include "swift/Threading/Once.h"
2821

2922
namespace swift {
3023

31-
#if SWIFT_STDLIB_THREADING_NONE
32-
using OnceToken_t = bool;
33-
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
34-
do { if (!TOKEN) { TOKEN = true; (FUNC)(CONTEXT); } } while (0)
35-
#elif SWIFT_STDLIB_THREADING_DARWIN
36-
using OnceToken_t = ::dispatch_once_t;
37-
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
38-
::dispatch_once_f(&TOKEN, CONTEXT, FUNC)
39-
#elif defined(__CYGWIN__)
40-
// _swift_once_f() is declared in Private.h.
41-
// This prototype is copied instead including the header file.
42-
void _swift_once_f(uintptr_t *predicate, void *context,
43-
void (*function)(void *));
44-
using OnceToken_t = unsigned long;
45-
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
46-
_swift_once_f(&TOKEN, CONTEXT, FUNC)
47-
#else
48-
using OnceToken_t = std::once_flag;
49-
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
50-
::std::call_once(TOKEN, FUNC, CONTEXT)
51-
#endif
52-
5324
/// A template for lazily-constructed, zero-initialized, leaked-on-exit
5425
/// global objects.
5526
template <class T> class Lazy {
5627
alignas(T) char Value[sizeof(T)] = { 0 };
5728

58-
OnceToken_t OnceToken = {};
29+
swift::once_t OnceToken = {};
5930

6031
static void defaultInitCallback(void *ValueAddr) {
6132
::new (ValueAddr) T();
6233
}
63-
34+
6435
public:
6536
using Type = T;
66-
37+
6738
T &get(void (*initCallback)(void *) = defaultInitCallback);
6839

6940
template<typename Arg1>
@@ -87,7 +58,7 @@ template <typename T> inline T &Lazy<T>::get(void (*initCallback)(void*)) {
8758
static_assert(std::is_literal_type<Lazy<T>>::value,
8859
"Lazy<T> must be a literal type");
8960

90-
SWIFT_ONCE_F(OnceToken, initCallback, &Value);
61+
swift::once(OnceToken, initCallback, &Value);
9162
return unsafeGetAlreadyInitialized();
9263
}
9364

@@ -103,7 +74,7 @@ template <typename Arg1> inline T &Lazy<T>::getWithInit(Arg1 &&arg1) {
10374
}
10475
} data{&Value, static_cast<Arg1&&>(arg1)};
10576

106-
SWIFT_ONCE_F(OnceToken, &Data::init, &data);
77+
swift::once(OnceToken, &Data::init, &data);
10778
return unsafeGetAlreadyInitialized();
10879
}
10980

include/swift/Runtime/AtomicWaitQueue.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include "swift/Runtime/Heap.h"
2323
#include "swift/Runtime/HeapObject.h"
24-
#include "swift/Runtime/Mutex.h"
24+
#include "swift/Threading/Mutex.h"
2525
#include <assert.h>
2626

2727
namespace swift {

include/swift/Runtime/Concurrent.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "llvm/Support/Allocator.h"
2222
#include "Atomic.h"
2323
#include "Debug.h"
24-
#include "Mutex.h"
24+
#include "swift/Threading/Mutex.h"
2525

2626
#if defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__HAIKU__)
2727
#include <stdio.h>
@@ -624,7 +624,7 @@ using llvm::hash_value;
624624
/// process. It has no destructor, to avoid generating useless global destructor
625625
/// calls. The memory it allocates can be freed by calling clear() with no
626626
/// outstanding readers, but this won't destroy the static mutex it uses.
627-
template <class ElemTy, class MutexTy = StaticMutex>
627+
template <class ElemTy, class MutexTy = LazyMutex>
628628
struct ConcurrentReadableHashMap {
629629
// We don't call destructors. Make sure the elements will put up with this.
630630
static_assert(std::is_trivially_destructible<ElemTy>::value,
@@ -1171,7 +1171,7 @@ template <class ElemTy> struct HashMapElementWrapper {
11711171
/// by allocating them separately and storing pointers to them. The elements of
11721172
/// the hash table are instances of HashMapElementWrapper. A new getOrInsert
11731173
/// method is provided that directly returns the stable element pointer.
1174-
template <class ElemTy, class Allocator, class MutexTy = StaticMutex>
1174+
template <class ElemTy, class Allocator, class MutexTy = LazyMutex>
11751175
struct StableAddressConcurrentReadableHashMap
11761176
: public ConcurrentReadableHashMap<HashMapElementWrapper<ElemTy>, MutexTy> {
11771177
// Implicitly trivial destructor.

include/swift/Runtime/Config.h

-16
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,6 @@ extern uintptr_t __COMPATIBILITY_LIBRARIES_CANNOT_CHECK_THE_IS_SWIFT_BIT_DIRECTL
232232
// so changing this value is not sufficient.
233233
#define SWIFT_DEFAULT_LLVM_CC llvm::CallingConv::C
234234

235-
// SWIFT_FORMAT(fmt,first) marks a function as taking a format string argument
236-
// at argument `fmt`, with the first argument for the format string as `first`.
237-
#if __has_attribute(format)
238-
#define SWIFT_FORMAT(fmt,first) __attribute__((format(printf, fmt, first)))
239-
#else
240-
#define SWIFT_FORMAT(fmt,first)
241-
#endif
242-
243-
// SWIFT_VFORMAT(fmt) marks a function as taking a format string argument at
244-
// argument `fmt`, with the arguments in a `va_list`.
245-
#if __has_attribute(format)
246-
#define SWIFT_VFORMAT(fmt) __attribute__((format(printf, fmt, 0)))
247-
#else
248-
#define SWIFT_VFORMAT(fmt)
249-
#endif
250-
251235
/// Should we use absolute function pointers instead of relative ones?
252236
/// WebAssembly target uses it by default.
253237
#ifndef SWIFT_COMPACT_ABSOLUTE_FUNCTION_POINTER

include/swift/Runtime/EnvironmentVariables.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
#include "../Basic/Lazy.h"
17+
#include "swift/Threading/Once.h"
1818

1919
namespace swift {
2020
namespace runtime {
2121
namespace environment {
2222

2323
void initialize(void *);
2424

25-
extern OnceToken_t initializeToken;
25+
extern swift::once_t initializeToken;
2626

2727
// Declare backing variables.
2828
#define VARIABLE(name, type, defaultValue, help) extern type name ## _variable;
@@ -31,7 +31,7 @@ extern OnceToken_t initializeToken;
3131
// Define getter functions.
3232
#define VARIABLE(name, type, defaultValue, help) \
3333
inline type name() { \
34-
SWIFT_ONCE_F(initializeToken, initialize, nullptr); \
34+
swift::once(initializeToken, initialize, nullptr); \
3535
return name ## _variable; \
3636
}
3737
#include "../../../stdlib/public/runtime/EnvironmentVariables.def"

include/swift/Runtime/MutexC11.h

-89
This file was deleted.

0 commit comments

Comments
 (0)