@@ -52,48 +52,41 @@ public struct Mutex<Value: ~Copyable>: ~Copyable {
52
52
}
53
53
}
54
54
55
+ @available ( SwiftStdlib 6 . 0 , * )
56
+ extension Mutex : @unchecked Sendable where Value: ~ Copyable { }
57
+
55
58
@available ( SwiftStdlib 6 . 0 , * )
56
59
extension Mutex where Value: ~ Copyable {
57
- /// Attempts to acquire the lock and then calls the given closure if
58
- /// successful.
59
- ///
60
- /// If the calling thread was successful in acquiring the lock, the
61
- /// closure will be executed and then immediately after it will
62
- /// release ownership of the lock. If we were unable to acquire the
63
- /// lock, this will return `nil`.
60
+ /// Calls the given closure after acquring the lock and then releases
61
+ /// ownership.
64
62
///
65
63
/// This method is equivalent to the following sequence of code:
66
64
///
67
- /// guard mutex.tryLock() else {
68
- /// return nil
69
- /// }
65
+ /// mutex.lock()
70
66
/// defer {
71
67
/// mutex.unlock()
72
68
/// }
73
69
/// return try body(&value)
74
70
///
75
- /// - Warning: Recursive calls to `tryWithLock ` within the
71
+ /// - Warning: Recursive calls to `withLock ` within the
76
72
/// closure parameter has behavior that is platform dependent.
77
73
/// Some platforms may choose to panic the process, deadlock,
78
74
/// or leave this behavior unspecified.
79
75
///
80
76
/// - Parameter body: A closure with a parameter of `Value`
81
77
/// that has exclusive access to the value being stored within
82
78
/// this mutex. This closure is considered the critical section
83
- /// as it will only be executed if the calling thread acquires
84
- /// the lock.
79
+ /// as it will only be executed once the calling thread has
80
+ /// acquired the lock.
85
81
///
86
- /// - Returns: The return value, if any, of the `body` closure parameter
87
- /// or nil if the lock couldn't be acquired.
82
+ /// - Returns: The return value, if any, of the `body` closure parameter.
88
83
@available ( SwiftStdlib 6 . 0 , * )
89
84
@_alwaysEmitIntoClient
90
85
@_transparent
91
- public borrowing func tryWithLock < Result: ~ Copyable & Sendable, E: Error > (
86
+ public borrowing func withLock < Result: ~ Copyable & Sendable, E: Error > (
92
87
_ body: @Sendable ( inout Value ) throws ( E ) -> Result
93
- ) throws ( E) -> Result ? {
94
- guard handle. tryLock ( ) else {
95
- return nil
96
- }
88
+ ) throws ( E) -> Result {
89
+ handle. lock ( )
97
90
98
91
defer {
99
92
handle. unlock ( )
@@ -120,10 +113,7 @@ extension Mutex where Value: ~Copyable {
120
113
/// }
121
114
/// return try body(&value)
122
115
///
123
- /// - Note: This version of `tryWithLock` is unchecked because it does
124
- /// not enforce any sendability guarantees.
125
- ///
126
- /// - Warning: Recursive calls to `tryWithLockUnchecked` within the
116
+ /// - Warning: Recursive calls to `withLockIfAvailable` within the
127
117
/// closure parameter has behavior that is platform dependent.
128
118
/// Some platforms may choose to panic the process, deadlock,
129
119
/// or leave this behavior unspecified.
@@ -139,8 +129,8 @@ extension Mutex where Value: ~Copyable {
139
129
@available ( SwiftStdlib 6 . 0 , * )
140
130
@_alwaysEmitIntoClient
141
131
@_transparent
142
- public borrowing func tryWithLockUnchecked < Result: ~ Copyable, E: Error > (
143
- _ body: ( inout Value ) throws ( E ) -> Result
132
+ public borrowing func withLockIfAvailable < Result: ~ Copyable & Sendable , E: Error > (
133
+ _ body: @ Sendable ( inout Value ) throws ( E ) -> Result
144
134
) throws ( E) -> Result ? {
145
135
guard handle. tryLock ( ) else {
146
136
return nil
@@ -152,85 +142,6 @@ extension Mutex where Value: ~Copyable {
152
142
153
143
return try body ( & value. address. pointee)
154
144
}
155
-
156
- /// Calls the given closure after acquring the lock and then releases
157
- /// ownership.
158
- ///
159
- /// This method is equivalent to the following sequence of code:
160
- ///
161
- /// mutex.lock()
162
- /// defer {
163
- /// mutex.unlock()
164
- /// }
165
- /// return try body(&value)
166
- ///
167
- /// - Warning: Recursive calls to `withLock` within the
168
- /// closure parameter has behavior that is platform dependent.
169
- /// Some platforms may choose to panic the process, deadlock,
170
- /// or leave this behavior unspecified.
171
- ///
172
- /// - Parameter body: A closure with a parameter of `Value`
173
- /// that has exclusive access to the value being stored within
174
- /// this mutex. This closure is considered the critical section
175
- /// as it will only be executed once the calling thread has
176
- /// acquired the lock.
177
- ///
178
- /// - Returns: The return value, if any, of the `body` closure parameter.
179
- @available ( SwiftStdlib 6 . 0 , * )
180
- @_alwaysEmitIntoClient
181
- @_transparent
182
- public borrowing func withLock< Result: ~ Copyable & Sendable, E: Error > (
183
- _ body: @Sendable ( inout Value ) throws ( E ) -> Result
184
- ) throws ( E) -> Result {
185
- handle. lock ( )
186
-
187
- defer {
188
- handle. unlock ( )
189
- }
190
-
191
- return try body ( & value. address. pointee)
192
- }
193
-
194
- /// Calls the given closure after acquring the lock and then releases
195
- /// ownership.
196
- ///
197
- /// This method is equivalent to the following sequence of code:
198
- ///
199
- /// mutex.lock()
200
- /// defer {
201
- /// mutex.unlock()
202
- /// }
203
- /// return try body(&value)
204
- ///
205
- /// - Warning: Recursive calls to `withLockUnchecked` within the
206
- /// closure parameter has behavior that is platform dependent.
207
- /// Some platforms may choose to panic the process, deadlock,
208
- /// or leave this behavior unspecified.
209
- ///
210
- /// - Note: This version of `withLock` is unchecked because it does
211
- /// not enforce any sendability guarantees.
212
- ///
213
- /// - Parameter body: A closure with a parameter of `Value`
214
- /// that has exclusive access to the value being stored within
215
- /// this mutex. This closure is considered the critical section
216
- /// as it will only be executed once the calling thread has
217
- /// acquired the lock.
218
- ///
219
- /// - Returns: The return value, if any, of the `body` closure parameter.
220
- @available ( SwiftStdlib 6 . 0 , * )
221
- @_alwaysEmitIntoClient
222
- @_transparent
223
- public borrowing func withLockUnchecked< Result: ~ Copyable, E: Error > (
224
- _ body: ( inout Value ) throws ( E ) -> Result
225
- ) throws ( E) -> Result {
226
- handle. lock ( )
227
-
228
- defer {
229
- handle. unlock ( )
230
- }
231
-
232
- return try body ( & value. address. pointee)
233
- }
234
145
}
235
146
236
147
@available ( SwiftStdlib 6 . 0 , * )
@@ -256,6 +167,3 @@ extension Mutex where Value == Void {
256
167
handle. unlock ( )
257
168
}
258
169
}
259
-
260
- @available ( SwiftStdlib 6 . 0 , * )
261
- extension Mutex : @unchecked Sendable where Value: Sendable { }
0 commit comments