Skip to content

Commit 643c277

Browse files
authored
Merge pull request #71383 from Azoy/mutex
[stdlib] Implement Mutex in Synchronization
2 parents c70c521 + cb3fc4b commit 643c277

Some content is hidden

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

54 files changed

+1409
-51
lines changed

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,45 @@ endfunction()
18231823
# Presence of a build flavor requires SWIFT_MODULE_DEPENDS_MACCATALYST to be
18241824
# defined and have values.
18251825
#
1826+
# SWIFT_SOURCES_DEPENDS_MACOS
1827+
# Sources that are built when this library is being built for macOS
1828+
#
1829+
# SWIFT_SOURCES_DEPENDS_IOS
1830+
# Sources that are built when this library is being built for iOS
1831+
#
1832+
# SWIFT_SOURCES_DEPENDS_TVOS
1833+
# Sources that are built when this library is being built for tvOS
1834+
#
1835+
# SWIFT_SOURCES_DEPENDS_WATCHOS
1836+
# Sources that are built when this library is being built for watchOS
1837+
#
1838+
# SWIFT_SOURCES_DEPENDS_VISIONOS
1839+
# Sources that are built when this library is being built for visionOS
1840+
#
1841+
# SWIFT_SOURCES_DEPENDS_FREESTANDING
1842+
# Sources that are built when this library is being built for freestanding
1843+
#
1844+
# SWIFT_SOURCES_DEPENDS_FREEBSD
1845+
# Sources that are built when this library is being built for FreeBSD
1846+
#
1847+
# SWIFT_SOURCES_DEPENDS_OPENBSD
1848+
# Sources that are built when this library is being built for OpenBSD
1849+
#
1850+
# SWIFT_SOURCES_DEPENDS_LINUX
1851+
# Sources that are built when this library is being built for Linux
1852+
#
1853+
# SWIFT_SOURCES_DEPENDS_CYGWIN
1854+
# Sources that are built when this library is being built for Cygwin
1855+
#
1856+
# SWIFT_SOURCES_DEPENDS_HAIKU
1857+
# Sources that are built when this library is being built for Haiku
1858+
#
1859+
# SWIFT_SOURCES_DEPENDS_WASI
1860+
# Sources that are built when this library is being built for WASI
1861+
#
1862+
# SWIFT_SOURCES_DEPENDS_WINDOWS
1863+
# Sources that are built when this library is being built for Windows
1864+
#
18261865
# source1 ...
18271866
# Sources to add into this library.
18281867
function(add_swift_target_library name)
@@ -1898,7 +1937,20 @@ function(add_swift_target_library name)
18981937
TARGET_SDKS
18991938
SWIFT_COMPILE_FLAGS_MACCATALYST
19001939
SWIFT_MODULE_DEPENDS_MACCATALYST
1901-
SWIFT_MODULE_DEPENDS_MACCATALYST_UNZIPPERED)
1940+
SWIFT_MODULE_DEPENDS_MACCATALYST_UNZIPPERED
1941+
SWIFT_SOURCES_DEPENDS_MACOS
1942+
SWIFT_SOURCES_DEPENDS_IOS
1943+
SWIFT_SOURCES_DEPENDS_TVOS
1944+
SWIFT_SOURCES_DEPENDS_WATCHOS
1945+
SWIFT_SOURCES_DEPENDS_VISIONOS
1946+
SWIFT_SOURCES_DEPENDS_FREESTANDING
1947+
SWIFT_SOURCES_DEPENDS_FREEBSD
1948+
SWIFT_SOURCES_DEPENDS_OPENBSD
1949+
SWIFT_SOURCES_DEPENDS_LINUX
1950+
SWIFT_SOURCES_DEPENDS_CYGWIN
1951+
SWIFT_SOURCES_DEPENDS_HAIKU
1952+
SWIFT_SOURCES_DEPENDS_WASI
1953+
SWIFT_SOURCES_DEPENDS_WINDOWS)
19021954

19031955
cmake_parse_arguments(SWIFTLIB
19041956
"${SWIFTLIB_options}"
@@ -2169,6 +2221,36 @@ function(add_swift_target_library name)
21692221
list(APPEND swiftlib_link_flags_all "-Xlinker" "-ignore_auto_link")
21702222
endif()
21712223

2224+
# Append SDK specific sources to the full list of sources
2225+
set(sources ${SWIFTLIB_SOURCES})
2226+
if(sdk STREQUAL "OSX")
2227+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_MACOS})
2228+
elseif(sdk STREQUAL "IOS" OR sdk STREQUAL "IOS_SIMULATOR")
2229+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_IOS})
2230+
elseif(sdk STREQUAL "TVOS" OR sdk STREQUAL "TVOS_SIMULATOR")
2231+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_TVOS})
2232+
elseif(sdk STREQUAL "WATCHOS" OR sdk STREQUAL "WATCHOS_SIMULATOR")
2233+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_WATCHOS})
2234+
elseif(sdk STREQUAL "XROS" OR sdk STREQUAL "XROS_SIMULATOR")
2235+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_VISIONOS})
2236+
elseif(sdk STREQUAL "FREESTANDING")
2237+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_FREESTANDING})
2238+
elseif(sdk STREQUAL "FREEBSD")
2239+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_FREEBSD})
2240+
elseif(sdk STREQUAL "OPENBSD")
2241+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_OPENBSD})
2242+
elseif(sdk STREQUAL "LINUX" OR sdk STREQUAL "ANDROID")
2243+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_LINUX})
2244+
elseif(sdk STREQUAL "CYGWIN")
2245+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_CYGWIN})
2246+
elseif(sdk STREQUAL "HAIKU")
2247+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_HAIKU})
2248+
elseif(sdk STREQUAL "WASI")
2249+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_WASI})
2250+
elseif(sdk STREQUAL "WINDOWS")
2251+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_WINDOWS})
2252+
endif()
2253+
21722254
# We unconditionally removed "-z,defs" from CMAKE_SHARED_LINKER_FLAGS in
21732255
# swift_common_standalone_build_config_llvm within
21742256
# SwiftSharedCMakeConfig.cmake, where it was added by a call to
@@ -2373,7 +2455,7 @@ function(add_swift_target_library name)
23732455
${SWIFTLIB_NO_LINK_NAME_keyword}
23742456
${SWIFTLIB_OBJECT_LIBRARY_keyword}
23752457
${SWIFTLIB_INSTALL_WITH_SHARED_keyword}
2376-
${SWIFTLIB_SOURCES}
2458+
${sources}
23772459
MODULE_TARGETS ${module_variant_names}
23782460
SDK ${sdk}
23792461
ARCHITECTURE ${arch}

stdlib/public/SwiftShims/swift/shims/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ set(sources
2424
Visibility.h
2525
_SwiftConcurrency.h
2626
_SwiftDistributed.h
27+
_SynchronizationShims.h
2728

2829
module.modulemap
2930
)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 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+
#ifndef SWIFT_STDLIB_SYNCHRONIZATION_SHIMS_H
14+
#define SWIFT_STDLIB_SYNCHRONIZATION_SHIMS_H
15+
16+
#include "SwiftStdbool.h"
17+
#include "SwiftStdint.h"
18+
19+
#if defined(__linux__)
20+
#include <errno.h>
21+
#include <linux/futex.h>
22+
#include <sys/syscall.h>
23+
#include <unistd.h>
24+
25+
static inline __swift_uint32_t _swift_stdlib_gettid() {
26+
static __thread tid = 0;
27+
28+
if (tid == 0) {
29+
tid = syscall(SYS_gettid);
30+
}
31+
32+
return tid;
33+
}
34+
35+
static inline __swift_uint32_t _swift_stdlib_futex_lock(__swift_uint32_t *lock) {
36+
int ret = syscall(SYS_futex, lock, FUTEX_LOCK_PI_PRIVATE,
37+
/* val */ 0, // this value is ignored by this futex op
38+
/* timeout */ NULL); // block indefinitely
39+
40+
if (ret == 0) {
41+
return ret;
42+
}
43+
44+
return errno;
45+
}
46+
47+
static inline __swift_uint32_t _swift_stdlib_futex_trylock(__swift_uint32_t *lock) {
48+
int ret = syscall(SYS_futex, lock, FUTEX_TRYLOCK_PI);
49+
50+
if (ret == 0) {
51+
return ret;
52+
}
53+
54+
return errno;
55+
}
56+
57+
static inline __swift_uint32_t _swift_stdlib_futex_unlock(__swift_uint32_t *lock) {
58+
int ret = syscall(SYS_futex, lock, FUTEX_UNLOCK_PI_PRIVATE);
59+
60+
if (ret == 0) {
61+
return ret;
62+
}
63+
64+
return errno;
65+
}
66+
67+
#endif // defined(__linux__)
68+
69+
#endif // SWIFT_STDLIB_SYNCHRONIZATION_SHIMS_H

stdlib/public/SwiftShims/swift/shims/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ module SwiftOverlayShims {
3131
header "LibcOverlayShims.h"
3232
export *
3333
}
34+
35+
module _SynchronizationShims {
36+
header "_SynchronizationShims.h"
37+
export *
38+
}

stdlib/public/Synchronization/Atomic.swift renamed to stdlib/public/Synchronization/Atomics/Atomic.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public struct Atomic<Value: AtomicRepresentable>: ~Copyable {
2121
@available(SwiftStdlib 6.0, *)
2222
@_alwaysEmitIntoClient
2323
@_transparent
24-
var address: UnsafeMutablePointer<Value.AtomicRepresentation> {
25-
UnsafeMutablePointer<Value.AtomicRepresentation>(rawAddress)
24+
var _address: UnsafeMutablePointer<Value.AtomicRepresentation> {
25+
UnsafeMutablePointer<Value.AtomicRepresentation>(_rawAddress)
2626
}
2727

2828
@available(SwiftStdlib 6.0, *)
2929
@_alwaysEmitIntoClient
3030
@_transparent
31-
var rawAddress: Builtin.RawPointer {
31+
var _rawAddress: Builtin.RawPointer {
3232
Builtin.unprotectedAddressOfBorrow(self)
3333
}
3434

@@ -39,7 +39,7 @@ public struct Atomic<Value: AtomicRepresentable>: ~Copyable {
3939
@_alwaysEmitIntoClient
4040
@_transparent
4141
public init(_ initialValue: consuming Value) {
42-
address.initialize(to: Value.encodeAtomicRepresentation(initialValue))
42+
_address.initialize(to: Value.encodeAtomicRepresentation(initialValue))
4343
}
4444

4545
// Deinit's can't be marked @_transparent. Do these things need all of these
@@ -48,10 +48,10 @@ public struct Atomic<Value: AtomicRepresentable>: ~Copyable {
4848
@_alwaysEmitIntoClient
4949
@inlinable
5050
deinit {
51-
let oldValue = Value.decodeAtomicRepresentation(address.pointee)
51+
let oldValue = Value.decodeAtomicRepresentation(_address.pointee)
5252
_ = consume oldValue
5353

54-
address.deinitialize(count: 1)
54+
_address.deinitialize(count: 1)
5555
}
5656
}
5757

stdlib/public/Synchronization/AtomicBool.swift renamed to stdlib/public/Synchronization/Atomics/AtomicBool.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,31 @@ extension Atomic where Value == Bool {
9393
let original = switch ordering {
9494
case .relaxed:
9595
Builtin.atomicrmw_and_monotonic_Int8(
96-
rawAddress,
96+
_rawAddress,
9797
builtinOperand
9898
)
9999

100100
case .acquiring:
101101
Builtin.atomicrmw_and_acquire_Int8(
102-
rawAddress,
102+
_rawAddress,
103103
builtinOperand
104104
)
105105

106106
case .releasing:
107107
Builtin.atomicrmw_and_release_Int8(
108-
rawAddress,
108+
_rawAddress,
109109
builtinOperand
110110
)
111111

112112
case .acquiringAndReleasing:
113113
Builtin.atomicrmw_and_acqrel_Int8(
114-
rawAddress,
114+
_rawAddress,
115115
builtinOperand
116116
)
117117

118118
case .sequentiallyConsistent:
119119
Builtin.atomicrmw_and_seqcst_Int8(
120-
rawAddress,
120+
_rawAddress,
121121
builtinOperand
122122
)
123123

@@ -151,31 +151,31 @@ extension Atomic where Value == Bool {
151151
let original = switch ordering {
152152
case .relaxed:
153153
Builtin.atomicrmw_or_monotonic_Int8(
154-
rawAddress,
154+
_rawAddress,
155155
builtinOperand
156156
)
157157

158158
case .acquiring:
159159
Builtin.atomicrmw_or_acquire_Int8(
160-
rawAddress,
160+
_rawAddress,
161161
builtinOperand
162162
)
163163

164164
case .releasing:
165165
Builtin.atomicrmw_or_release_Int8(
166-
rawAddress,
166+
_rawAddress,
167167
builtinOperand
168168
)
169169

170170
case .acquiringAndReleasing:
171171
Builtin.atomicrmw_or_acqrel_Int8(
172-
rawAddress,
172+
_rawAddress,
173173
builtinOperand
174174
)
175175

176176
case .sequentiallyConsistent:
177177
Builtin.atomicrmw_or_seqcst_Int8(
178-
rawAddress,
178+
_rawAddress,
179179
builtinOperand
180180
)
181181

@@ -209,31 +209,31 @@ extension Atomic where Value == Bool {
209209
let original = switch ordering {
210210
case .relaxed:
211211
Builtin.atomicrmw_xor_monotonic_Int8(
212-
rawAddress,
212+
_rawAddress,
213213
builtinOperand
214214
)
215215

216216
case .acquiring:
217217
Builtin.atomicrmw_xor_acquire_Int8(
218-
rawAddress,
218+
_rawAddress,
219219
builtinOperand
220220
)
221221

222222
case .releasing:
223223
Builtin.atomicrmw_xor_release_Int8(
224-
rawAddress,
224+
_rawAddress,
225225
builtinOperand
226226
)
227227

228228
case .acquiringAndReleasing:
229229
Builtin.atomicrmw_xor_acqrel_Int8(
230-
rawAddress,
230+
_rawAddress,
231231
builtinOperand
232232
)
233233

234234
case .sequentiallyConsistent:
235235
Builtin.atomicrmw_xor_seqcst_Int8(
236-
rawAddress,
236+
_rawAddress,
237237
builtinOperand
238238
)
239239

stdlib/public/Synchronization/AtomicIntegers.swift.gyb renamed to stdlib/public/Synchronization/Atomics/AtomicIntegers.swift.gyb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,20 @@ extension Atomic where Value == ${intType} {
120120
% if intType == "Int" or intType == "UInt":
121121
#if _pointerBitWidth(_64)
122122
Builtin.atomicrmw_${atomicOperationName(intType, builtinName)}_${llvmOrder}_Int64(
123-
rawAddress,
123+
_rawAddress,
124124
operand._value
125125
)
126126
#elseif _pointerBitWidth(_32)
127127
Builtin.atomicrmw_${atomicOperationName(intType, builtinName)}_${llvmOrder}_Int32(
128-
rawAddress,
128+
_rawAddress,
129129
operand._value
130130
)
131131
#else
132132
#error("Unsupported platform")
133133
#endif
134134
% else:
135135
Builtin.atomicrmw_${atomicOperationName(intType, builtinName)}_${llvmOrder}_Int${bits}(
136-
rawAddress,
136+
_rawAddress,
137137
operand._value
138138
)
139139
% end

0 commit comments

Comments
 (0)