Skip to content

Commit d086367

Browse files
committed
Fix macos toolchain build
1 parent 732871f commit d086367

File tree

1 file changed

+25
-74
lines changed

1 file changed

+25
-74
lines changed

Sources/Sharing/Internal/MutexBackport.swift

Lines changed: 25 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,40 @@
11
import Foundation
22

3-
#if compiler(>=6)
4-
/// A synchronization primitive that protects shared mutable state via mutual exclusion.
5-
///
6-
/// A back-port of Swift's `Mutex` type for wider platform availability.
7-
#if hasFeature(StaticExclusiveOnly)
8-
@_staticExclusiveOnly
9-
#endif
10-
package struct Mutex<Value: ~Copyable>: ~Copyable {
11-
private let _lock = NSLock()
12-
private let _box: Box
3+
package struct Mutex<Value> {
4+
private let _lock = NSLock()
5+
private let _box: Box
136

14-
/// Initializes a value of this mutex with the given initial state.
15-
///
16-
/// - Parameter initialValue: The initial value to give to the mutex.
17-
package init(_ initialValue: consuming sending Value) {
18-
_box = Box(initialValue)
19-
}
20-
21-
private final class Box {
22-
var value: Value
23-
init(_ initialValue: consuming sending Value) {
24-
value = initialValue
25-
}
26-
}
7+
package init(_ initialValue: consuming Value) {
8+
_box = Box(initialValue)
279
}
2810

29-
extension Mutex: @unchecked Sendable where Value: ~Copyable {}
30-
31-
extension Mutex where Value: ~Copyable {
32-
/// Calls the given closure after acquiring the lock and then releases ownership.
33-
borrowing package func withLock<Result: ~Copyable, E: Error>(
34-
_ body: (inout sending Value) throws(E) -> sending Result
35-
) throws(E) -> sending Result {
36-
_lock.lock()
37-
defer { _lock.unlock() }
38-
return try body(&_box.value)
39-
}
40-
41-
/// Attempts to acquire the lock and then calls the given closure if successful.
42-
borrowing package func withLockIfAvailable<Result: ~Copyable, E: Error>(
43-
_ body: (inout sending Value) throws(E) -> sending Result
44-
) throws(E) -> sending Result? {
45-
guard _lock.try() else { return nil }
46-
defer { _lock.unlock() }
47-
return try body(&_box.value)
11+
private final class Box {
12+
var value: Value
13+
init(_ initialValue: consuming Value) {
14+
value = initialValue
4815
}
4916
}
50-
#else
51-
package struct Mutex<Value> {
52-
private let _lock = NSLock()
53-
private let _box: Box
17+
}
5418

55-
package init(_ initialValue: consuming Value) {
56-
_box = Box(initialValue)
57-
}
19+
extension Mutex: @unchecked Sendable {}
5820

59-
private final class Box {
60-
var value: Value
61-
init(_ initialValue: consuming Value) {
62-
value = initialValue
63-
}
64-
}
21+
extension Mutex {
22+
borrowing package func withLock<Result>(
23+
_ body: (inout Value) throws -> Result
24+
) rethrows -> Result {
25+
_lock.lock()
26+
defer { _lock.unlock() }
27+
return try body(&_box.value)
6528
}
6629

67-
extension Mutex: @unchecked Sendable {}
68-
69-
extension Mutex {
70-
borrowing package func withLock<Result>(
71-
_ body: (inout Value) throws -> Result
72-
) rethrows -> Result {
73-
_lock.lock()
74-
defer { _lock.unlock() }
75-
return try body(&_box.value)
76-
}
77-
78-
borrowing package func withLockIfAvailable<Result>(
79-
_ body: (inout Value) throws -> Result
80-
) rethrows -> Result? {
81-
guard _lock.try() else { return nil }
82-
defer { _lock.unlock() }
83-
return try body(&_box.value)
84-
}
30+
borrowing package func withLockIfAvailable<Result>(
31+
_ body: (inout Value) throws -> Result
32+
) rethrows -> Result? {
33+
guard _lock.try() else { return nil }
34+
defer { _lock.unlock() }
35+
return try body(&_box.value)
8536
}
86-
#endif
37+
}
8738

8839
extension Mutex where Value == Void {
8940
borrowing package func _unsafeLock() {

0 commit comments

Comments
 (0)