Skip to content

Commit 121c763

Browse files
committed
Add a single-threaded stdlib mode, use it for the 'minimal' stdlib
1 parent 303026b commit 121c763

File tree

17 files changed

+97
-30
lines changed

17 files changed

+97
-30
lines changed

CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,6 @@ option(SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
390390
"Should the runtime be built with support for non-thread-safe leak detecting entrypoints"
391391
FALSE)
392392

393-
option(SWIFT_STDLIB_USE_NONATOMIC_RC
394-
"Build the standard libraries and overlays with nonatomic reference count operations enabled"
395-
FALSE)
396-
397393
option(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS
398394
"Enable runtime function counters and expose the API."
399395
FALSE)

include/swift/Basic/Lazy.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#define SWIFT_BASIC_LAZY_H
1515

1616
#include <memory>
17-
#ifdef __APPLE__
17+
#ifdef SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
18+
// No dependencies on single-threaded environments.
19+
#elif defined(__APPLE__)
1820
#include <dispatch/dispatch.h>
1921
#elif defined(__wasi__)
2022
// No pthread on wasi, see https://bugs.swift.org/browse/SR-12097 for more details.
@@ -44,7 +46,11 @@ inline void wasi_call_once(int *flag, void *context, void (*func)(void *)) {
4446

4547
namespace swift {
4648

47-
#ifdef __APPLE__
49+
#ifdef SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
50+
using OnceToken_t = bool;
51+
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
52+
if (!TOKEN) { TOKEN = true; (FUNC)(CONTEXT); }
53+
#elif defined(__APPLE__)
4854
using OnceToken_t = dispatch_once_t;
4955
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
5056
::dispatch_once_f(&TOKEN, CONTEXT, FUNC)

include/swift/Runtime/Mutex.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020

2121
#include <type_traits>
2222

23-
#if (defined(__APPLE__) || defined(__linux__) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__))
23+
#ifdef SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
24+
#include "swift/Runtime/MutexSingleThreaded.h"
25+
#elif (defined(__APPLE__) || defined(__linux__) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__))
2426
#include "swift/Runtime/MutexPThread.h"
2527
#elif defined(_WIN32)
2628
#include "swift/Runtime/MutexWin32.h"
2729
#elif defined(__wasi__)
28-
#include "swift/Runtime/MutexWASI.h"
30+
#include "swift/Runtime/MutexSingleThreaded.h"
2931
#else
3032
#error "Implement equivalent of MutexPThread.h/cpp for your platform."
3133
#endif

include/swift/Runtime/MutexWASI.h renamed to include/swift/Runtime/MutexSingleThreaded.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- MutexWASI.h - -----------------------------------------*- C++ -*-===//
1+
//===--- MutexSingleThreaded.h - --------------------------------*- C++ -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -10,16 +10,12 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
//
13-
// No-op implementation of locks for the WebAssembly System Interface. The
14-
// implementation does not need to perform locking, because as of January 2020
15-
// WebAssembly does not support threads.
16-
// See the current status at https://github.com/WebAssembly/proposals and
17-
// https://github.com/webassembly/threads
13+
// No-op implementation of locks for single-threaded environments.
1814
//
1915
//===----------------------------------------------------------------------===//
2016

21-
#ifndef SWIFT_RUNTIME_MUTEX_WASI_H
22-
#define SWIFT_RUNTIME_MUTEX_WASI_H
17+
#ifndef SWIFT_RUNTIME_MUTEX_SINGLE_THREADED_H
18+
#define SWIFT_RUNTIME_MUTEX_SINGLE_THREADED_H
2319

2420
namespace swift {
2521

include/swift/Runtime/Once.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222

2323
namespace swift {
2424

25-
#ifdef __APPLE__
25+
#ifdef SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
26+
27+
typedef bool swift_once_t;
28+
29+
#elif defined(__APPLE__)
2630

2731
// On OS X and iOS, swift_once_t matches dispatch_once_t.
2832
typedef long swift_once_t;

stdlib/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ option(SWIFT_RUNTIME_MACHO_NO_DYLD
2121
"Build stdlib assuming the runtime environment uses Mach-O but does not support dynamic modules."
2222
FALSE)
2323

24+
option(SWIFT_STDLIB_USE_NONATOMIC_RC
25+
"Build the standard libraries and overlays with nonatomic reference count operations enabled"
26+
FALSE)
27+
28+
option(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
29+
"Build the standard libraries assuming that they will be used in an environment with only a single thread. If set, also forces SWIFT_STDLIB_USE_NONATOMIC_RC to On."
30+
FALSE)
31+
32+
if(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
33+
set(SWIFT_STDLIB_USE_NONATOMIC_RC TRUE)
34+
endif()
35+
2436
#
2537
# End of user-configurable options.
2638
#

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,14 @@ function(_add_target_variant_c_compile_flags)
306306
list(APPEND result "-DSWIFT_RUNTIME_MACHO_NO_DYLD")
307307
endif()
308308

309+
if(SWIFT_STDLIB_USE_NONATOMIC_RC)
310+
list(APPEND result "-DSWIFT_STDLIB_USE_NONATOMIC_RC")
311+
endif()
312+
313+
if(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
314+
list(APPEND result "-DSWIFT_STDLIB_SINGLE_THREADED_RUNTIME")
315+
endif()
316+
309317
set("${CFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE)
310318
endfunction()
311319

stdlib/public/runtime/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,12 @@ if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
152152
INSTALL_IN_COMPONENT never_install)
153153
endif()
154154

155-
if(SWIFT_STDLIB_USE_NONATOMIC_RC)
156-
set(_RUNTIME_NONATOMIC_FLAGS -DSWIFT_STDLIB_USE_NONATOMIC_RC)
157-
endif()
158155
add_swift_target_library(swiftRuntime OBJECT_LIBRARY
159156
${swift_runtime_sources}
160157
${swift_runtime_objc_sources}
161158
${swift_runtime_leaks_sources}
162159
C_COMPILE_FLAGS
163160
${swift_runtime_library_compile_flags}
164-
${_RUNTIME_NONATOMIC_FLAGS}
165161
LINK_FLAGS ${swift_runtime_linker_flags}
166162
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
167163
INSTALL_IN_COMPONENT never_install)

stdlib/public/runtime/Exclusivity.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,14 @@ class SwiftTLSContext {
250250
// Each of these cases should define a function with this prototype:
251251
// AccessSets &getAllSets();
252252

253-
#if SWIFT_TLS_HAS_RESERVED_PTHREAD_SPECIFIC
253+
#if SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
254+
255+
static SwiftTLSContext &getTLSContext() {
256+
static SwiftTLSContext TLSContext;
257+
return TLSContext;
258+
}
259+
260+
#elif SWIFT_TLS_HAS_RESERVED_PTHREAD_SPECIFIC
254261
// Use the reserved TSD key if possible.
255262

256263
static SwiftTLSContext &getTLSContext() {

stdlib/public/runtime/MetadataCache.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,22 @@ class MetadataCacheEntryBase
775775
using super::asImpl;
776776

777777
private:
778+
#ifdef SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
779+
using ThreadID = int;
780+
static ThreadID CurrentThreadID() {
781+
return 0;
782+
}
783+
#else
784+
using ThreadID = std::thread::id;
785+
static ThreadID CurrentThreadID() {
786+
return std::this_thread::get_id();
787+
}
788+
#endif
789+
778790
/// Additional storage that is only ever accessed under the lock.
779791
union LockedStorage_t {
780792
/// The thread that is allocating the entry.
781-
std::thread::id AllocatingThread;
793+
ThreadID AllocatingThread;
782794

783795
/// The completion queue.
784796
MetadataCompletionQueueEntry *CompletionQueue;
@@ -837,7 +849,7 @@ class MetadataCacheEntryBase
837849
MetadataCacheEntryBase()
838850
: LockedStorageKind(LSK::AllocatingThread),
839851
TrackingInfo(PrivateMetadataTrackingInfo::initial().getRawValue()) {
840-
LockedStorage.AllocatingThread = std::this_thread::get_id();
852+
LockedStorage.AllocatingThread = CurrentThreadID();
841853
}
842854

843855
// Note that having an explicit destructor here is important to make this
@@ -850,7 +862,7 @@ class MetadataCacheEntryBase
850862

851863
bool isBeingAllocatedByCurrentThread() const {
852864
return LockedStorageKind == LSK::AllocatingThread &&
853-
LockedStorage.AllocatingThread == std::this_thread::get_id();
865+
LockedStorage.AllocatingThread == CurrentThreadID();
854866
}
855867

856868
/// Given that this thread doesn't own the right to initialize the

0 commit comments

Comments
 (0)