Skip to content

Commit c781516

Browse files
authored
Merge pull request #74183 from Azoy/60-mutex
[6.0] [stdlib] Implement Mutex in Synchronization
2 parents 7bd3046 + dcd103f commit c781516

Some content is hidden

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

55 files changed

+1479
-51
lines changed

stdlib/cmake/modules/AddSwiftStdlib.cmake

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

19021954
cmake_parse_arguments(SWIFTLIB
19031955
"${SWIFTLIB_options}"
@@ -2168,6 +2220,36 @@ function(add_swift_target_library name)
21682220
list(APPEND swiftlib_link_flags_all "-Xlinker" "-ignore_auto_link")
21692221
endif()
21702222

2223+
# Append SDK specific sources to the full list of sources
2224+
set(sources ${SWIFTLIB_SOURCES})
2225+
if(sdk STREQUAL "OSX")
2226+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_MACOS})
2227+
elseif(sdk STREQUAL "IOS" OR sdk STREQUAL "IOS_SIMULATOR")
2228+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_IOS})
2229+
elseif(sdk STREQUAL "TVOS" OR sdk STREQUAL "TVOS_SIMULATOR")
2230+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_TVOS})
2231+
elseif(sdk STREQUAL "WATCHOS" OR sdk STREQUAL "WATCHOS_SIMULATOR")
2232+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_WATCHOS})
2233+
elseif(sdk STREQUAL "XROS" OR sdk STREQUAL "XROS_SIMULATOR")
2234+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_VISIONOS})
2235+
elseif(sdk STREQUAL "FREESTANDING")
2236+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_FREESTANDING})
2237+
elseif(sdk STREQUAL "FREEBSD")
2238+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_FREEBSD})
2239+
elseif(sdk STREQUAL "OPENBSD")
2240+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_OPENBSD})
2241+
elseif(sdk STREQUAL "LINUX" OR sdk STREQUAL "ANDROID")
2242+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_LINUX})
2243+
elseif(sdk STREQUAL "CYGWIN")
2244+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_CYGWIN})
2245+
elseif(sdk STREQUAL "HAIKU")
2246+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_HAIKU})
2247+
elseif(sdk STREQUAL "WASI")
2248+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_WASI})
2249+
elseif(sdk STREQUAL "WINDOWS")
2250+
list(APPEND sources ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_WINDOWS})
2251+
endif()
2252+
21712253
# We unconditionally removed "-z,defs" from CMAKE_SHARED_LINKER_FLAGS in
21722254
# swift_common_standalone_build_config_llvm within
21732255
# SwiftSharedCMakeConfig.cmake, where it was added by a call to
@@ -2372,7 +2454,7 @@ function(add_swift_target_library name)
23722454
${SWIFTLIB_NO_LINK_NAME_keyword}
23732455
${SWIFTLIB_OBJECT_LIBRARY_keyword}
23742456
${SWIFTLIB_INSTALL_WITH_SHARED_keyword}
2375-
${SWIFTLIB_SOURCES}
2457+
${sources}
23762458
MODULE_TARGETS ${module_variant_names}
23772459
SDK ${sdk}
23782460
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
@@ -22,14 +22,14 @@ public struct Atomic<Value: AtomicRepresentable> {
2222
@available(SwiftStdlib 6.0, *)
2323
@_alwaysEmitIntoClient
2424
@_transparent
25-
var address: UnsafeMutablePointer<Value.AtomicRepresentation> {
26-
UnsafeMutablePointer<Value.AtomicRepresentation>(rawAddress)
25+
var _address: UnsafeMutablePointer<Value.AtomicRepresentation> {
26+
UnsafeMutablePointer<Value.AtomicRepresentation>(_rawAddress)
2727
}
2828

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

@@ -40,7 +40,7 @@ public struct Atomic<Value: AtomicRepresentable> {
4040
@_alwaysEmitIntoClient
4141
@_transparent
4242
public init(_ initialValue: consuming Value) {
43-
address.initialize(to: Value.encodeAtomicRepresentation(initialValue))
43+
_address.initialize(to: Value.encodeAtomicRepresentation(initialValue))
4444
}
4545

4646
// Deinit's can't be marked @_transparent. Do these things need all of these
@@ -49,10 +49,10 @@ public struct Atomic<Value: AtomicRepresentable> {
4949
@_alwaysEmitIntoClient
5050
@inlinable
5151
deinit {
52-
let oldValue = Value.decodeAtomicRepresentation(address.pointee)
52+
let oldValue = Value.decodeAtomicRepresentation(_address.pointee)
5353
_ = consume oldValue
5454

55-
address.deinitialize(count: 1)
55+
_address.deinitialize(count: 1)
5656
}
5757
}
5858

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)