|
4 | 4 | /// A synchronization primitive that protects shared mutable state via mutual exclusion. |
5 | 5 | /// |
6 | 6 | /// A back-port of Swift's `Mutex` type for wider platform availability. |
7 | | - @frozen |
8 | 7 | @_staticExclusiveOnly |
9 | 8 | @available(iOS, obsoleted: 18, message: "Use 'Synchronization.Mutex', instead.") |
10 | 9 | @available(macOS, obsoleted: 15, message: "Use 'Synchronization.Mutex', instead.") |
11 | 10 | @available(tvOS, obsoleted: 18, message: "Use 'Synchronization.Mutex', instead.") |
12 | 11 | @available(visionOS, obsoleted: 2, message: "Use 'Synchronization.Mutex', instead.") |
13 | 12 | @available(watchOS, obsoleted: 11, message: "Use 'Synchronization.Mutex', instead.") |
14 | 13 | public struct Mutex<Value: ~Copyable>: ~Copyable { |
15 | | - @usableFromInline |
16 | | - let _lock = NSLock() |
17 | | - |
18 | | - @usableFromInline |
19 | | - let _box: Box |
| 14 | + private let _lock = NSLock() |
| 15 | + private let _box: Box |
20 | 16 |
|
21 | 17 | /// Initializes a value of this mutex with the given initial state. |
22 | 18 | /// |
23 | 19 | /// - Parameter initialValue: The initial value to give to the mutex. |
24 | | - @_transparent |
25 | 20 | public init(_ initialValue: consuming sending Value) { |
26 | 21 | _box = Box(initialValue) |
27 | 22 | } |
28 | 23 |
|
29 | | - @usableFromInline |
30 | | - final class Box { |
31 | | - @usableFromInline |
| 24 | + private final class Box { |
32 | 25 | var value: Value |
33 | | - @usableFromInline |
34 | 26 | init(_ initialValue: consuming sending Value) { |
35 | 27 | value = initialValue |
36 | 28 | } |
|
41 | 33 |
|
42 | 34 | extension Mutex where Value: ~Copyable { |
43 | 35 | /// Calls the given closure after acquiring the lock and then releases ownership. |
44 | | - @_transparent |
45 | 36 | public borrowing func withLock<Result: ~Copyable, E: Error>( |
46 | 37 | _ body: (inout sending Value) throws(E) -> sending Result |
47 | 38 | ) throws(E) -> sending Result { |
|
51 | 42 | } |
52 | 43 |
|
53 | 44 | /// Attempts to acquire the lock and then calls the given closure if successful. |
54 | | - @_transparent |
55 | 45 | public borrowing func withLockIfAvailable<Result: ~Copyable, E: Error>( |
56 | 46 | _ body: (inout sending Value) throws(E) -> sending Result |
57 | 47 | ) throws(E) -> sending Result? { |
|
62 | 52 | } |
63 | 53 |
|
64 | 54 | extension Mutex where Value == Void { |
65 | | - @_transparent |
66 | 55 | public borrowing func _unsafeLock() { |
67 | 56 | _lock.lock() |
68 | 57 | } |
69 | 58 |
|
70 | | - @_transparent |
71 | 59 | public borrowing func _unsafeTryLock() -> Bool { |
72 | 60 | _lock.try() |
73 | 61 | } |
74 | 62 |
|
75 | | - @_transparent |
76 | 63 | public borrowing func _unsafeUnlock() { |
77 | 64 | _lock.unlock() |
78 | 65 | } |
|
0 commit comments