Skip to content

Commit 89748d4

Browse files
committed
Actually implement the API now
1 parent 92e6a98 commit 89748d4

File tree

5 files changed

+68
-33
lines changed

5 files changed

+68
-33
lines changed

stdlib/public/Synchronization/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ if(SWIFT_BUILD_SDK_OVERLAY)
4343
endif()
4444

4545
set(SWIFT_SYNCHRONIZATION_DARWIN_SOURCES
46-
Mutex/DarwinImpl.swift
46+
Mutex/DarwinImpl.swift
4747
)
4848

4949
# Linux dependencies and sources
5050

5151
set(SWIFT_SYNCHRONIZATION_LINUX_SOURCES
52-
Mutex/LinuxImpl.swift
52+
Mutex/LinuxImpl.swift
5353
)
5454

5555
# Wasm dependencies and sources
5656

5757
set(SWIFT_SYNCHRONIZATION_WASM_SOURCES
58-
Mutex/WasmImpl.swift
58+
Mutex/WasmImpl.swift
5959
)
6060

6161
# Windows dependencies and sources
@@ -67,7 +67,7 @@ if(SWIFT_BUILD_SDK_OVERLAY)
6767
endif()
6868

6969
set(SWIFT_SYNCHRONIZATION_WINDOWS_SOURCES
70-
Mutex/WindowsImpl.swift
70+
Mutex/WindowsImpl.swift
7171
)
7272

7373
set(SWIFT_SYNCHRNOIZATION_SWIFT_FLAGS

stdlib/public/Synchronization/Cell.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ import Builtin
1717
@usableFromInline
1818
@_rawLayout(like: Value)
1919
internal struct _Cell<Value: ~Copyable>: ~Copyable {
20+
@available(SwiftStdlib 6.0, *)
21+
@_alwaysEmitIntoClient
22+
@_transparent
23+
var address: UnsafeMutablePointer<Value> {
24+
UnsafeMutablePointer<Value>(rawAddress)
25+
}
26+
2027
@available(SwiftStdlib 6.0, *)
2128
@_alwaysEmitIntoClient
2229
@_transparent
@@ -28,13 +35,13 @@ internal struct _Cell<Value: ~Copyable>: ~Copyable {
2835
@_alwaysEmitIntoClient
2936
@_transparent
3037
init(_ initialValue: consuming Value) {
31-
Builtin.initialize(initialValue, rawAddress)
38+
address.initialize(to: initialValue)
3239
}
3340

3441
@available(SwiftStdlib 6.0, *)
3542
@_alwaysEmitIntoClient
3643
@inlinable
3744
deinit {
38-
Builtin.destroy(Value.self, rawAddress)
45+
address.deinitialize(count: 1)
3946
}
4047
}

stdlib/public/Synchronization/Mutex/DarwinImpl.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ internal struct _MutexHandle: ~Copyable {
3232
@_alwaysEmitIntoClient
3333
@_transparent
3434
borrowing func lock() {
35-
os_unfair_lock_lock(os_unfair_lock_t(value.rawAddress))
35+
os_unfair_lock_lock(value.address)
3636
}
3737

3838
@available(SwiftStdlib 6.0, *)
3939
@_alwaysEmitIntoClient
4040
@_transparent
4141
borrowing func tryLock() -> Bool {
42-
os_unfair_lock_trylock(os_unfair_lock_t(value.rawAddress))
42+
os_unfair_lock_trylock(value.address)
4343
}
4444

4545
@available(SwiftStdlib 6.0, *)
4646
@_alwaysEmitIntoClient
4747
@_transparent
4848
borrowing func unlock() {
49-
os_unfair_lock_unlock(os_unfair_lock_t(value.rawAddress))
49+
os_unfair_lock_unlock(value.address)
5050
}
5151
}

stdlib/public/Synchronization/Mutex/Mutex.swift

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public struct Mutex<Value: ~Copyable>: ~Copyable {
5353
}
5454

5555
@available(SwiftStdlib 6.0, *)
56-
extension Mutex {
56+
extension Mutex where Value: ~Copyable {
5757
/// Attempts to acquire the lock and then calls the given closure if
5858
/// successful.
5959
///
@@ -85,14 +85,22 @@ extension Mutex {
8585
///
8686
/// - Returns: The return value, if any, of the `body` closure parameter
8787
/// or nil if the lock couldn't be acquired.
88-
// @available(SwiftStdlib 6.0, *)
89-
// @_alwaysEmitIntoClient
90-
// @_transparent
91-
// public borrowing func tryWithLock<Result: ~Copyable & Sendable, E>(
92-
// _ body: @Sendable (inout Value) throws(E) -> Result
93-
// ) throws(E) -> Result? {
94-
// fatalError()
95-
// }
88+
@available(SwiftStdlib 6.0, *)
89+
@_alwaysEmitIntoClient
90+
@_transparent
91+
public borrowing func tryWithLock<Result: ~Copyable & Sendable, E: Error>(
92+
_ body: @Sendable (inout Value) throws(E) -> Result
93+
) throws(E) -> Result? {
94+
guard handle.tryLock() else {
95+
return nil
96+
}
97+
98+
defer {
99+
handle.unlock()
100+
}
101+
102+
return try body(&value.address.pointee)
103+
}
96104

97105
/// Attempts to acquire the lock and then calls the given closure if
98106
/// successful.
@@ -128,14 +136,22 @@ extension Mutex {
128136
///
129137
/// - Returns: The return value, if any, of the `body` closure parameter
130138
/// or nil if the lock couldn't be acquired.
131-
// @available(SwiftStdlib 6.0, *)
132-
// @_alwaysEmitIntoClient
133-
// @_transparent
134-
// public borrowing func tryWithLock<Result: ~Copyable, E>(
135-
// _ body: (inout Value) throws(E) -> Result
136-
// ) throws(E) -> Result? {
137-
// fatalError()
138-
// }
139+
@available(SwiftStdlib 6.0, *)
140+
@_alwaysEmitIntoClient
141+
@_transparent
142+
public borrowing func tryWithLockUnchecked<Result: ~Copyable, E: Error>(
143+
_ body: (inout Value) throws(E) -> Result
144+
) throws(E) -> Result? {
145+
guard handle.tryLock() else {
146+
return nil
147+
}
148+
149+
defer {
150+
handle.unlock()
151+
}
152+
153+
return try body(&value.address.pointee)
154+
}
139155

140156
/// Calls the given closure after acquring the lock and then releases
141157
/// ownership.
@@ -163,10 +179,16 @@ extension Mutex {
163179
@available(SwiftStdlib 6.0, *)
164180
@_alwaysEmitIntoClient
165181
@_transparent
166-
public borrowing func withLock<Result: ~Copyable & Sendable, E>(
182+
public borrowing func withLock<Result: ~Copyable & Sendable, E: Error>(
167183
_ body: @Sendable (inout Value) throws(E) -> Result
168184
) throws(E) -> Result {
169-
fatalError()
185+
handle.lock()
186+
187+
defer {
188+
handle.unlock()
189+
}
190+
191+
return try body(&value.address.pointee)
170192
}
171193

172194
/// Calls the given closure after acquring the lock and then releases
@@ -198,10 +220,16 @@ extension Mutex {
198220
@available(SwiftStdlib 6.0, *)
199221
@_alwaysEmitIntoClient
200222
@_transparent
201-
public borrowing func withLock<Result: ~Copyable, E>(
223+
public borrowing func withLockUnchecked<Result: ~Copyable, E: Error>(
202224
_ body: (inout Value) throws(E) -> Result
203225
) throws(E) -> Result {
204-
fatalError()
226+
handle.lock()
227+
228+
defer {
229+
handle.unlock()
230+
}
231+
232+
return try body(&value.address.pointee)
205233
}
206234
}
207235

stdlib/public/Synchronization/Mutex/WindowsImpl.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ internal struct _MutexHandle: ~Copyable {
3131
@_alwaysEmitIntoClient
3232
@_transparent
3333
borrowing func lock() {
34-
AcquireSRWLockExclusive(PSRWLOCK(value.rawAddress))
34+
AcquireSRWLockExclusive(value.address)
3535
}
3636

3737
@available(SwiftStdlib 6.0, *)
3838
@_alwaysEmitIntoClient
3939
@_transparent
4040
borrowing func tryLock() -> Bool {
4141
// Windows BOOLEAN gets imported as 'UInt8'...
42-
TryAcquireSRWLockExclusive(PSRWLOCK(value.rawAddress)) != 0
42+
TryAcquireSRWLockExclusive(value.address) != 0
4343
}
4444

4545
@available(SwiftStdlib 6.0, *)
4646
@_alwaysEmitIntoClient
4747
@_transparent
4848
borrowing func unlock() {
49-
ReleaseSRWLockExclusive(PSRWLOCK(value.rawAddress))
49+
ReleaseSRWLockExclusive(value.address)
5050
}
5151
}

0 commit comments

Comments
 (0)