@@ -52,48 +52,41 @@ public struct Mutex<Value: ~Copyable>: ~Copyable {
5252 }
5353}
5454
55+ @available ( SwiftStdlib 6 . 0 , * )
56+ extension Mutex : @unchecked Sendable where Value: ~ Copyable { }
57+
5558@available ( SwiftStdlib 6 . 0 , * )
5659extension 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.
6462 ///
6563 /// This method is equivalent to the following sequence of code:
6664 ///
67- /// guard mutex.tryLock() else {
68- /// return nil
69- /// }
65+ /// mutex.lock()
7066 /// defer {
7167 /// mutex.unlock()
7268 /// }
7369 /// return try body(&value)
7470 ///
75- /// - Warning: Recursive calls to `tryWithLock ` within the
71+ /// - Warning: Recursive calls to `withLock ` within the
7672 /// closure parameter has behavior that is platform dependent.
7773 /// Some platforms may choose to panic the process, deadlock,
7874 /// or leave this behavior unspecified.
7975 ///
8076 /// - Parameter body: A closure with a parameter of `Value`
8177 /// that has exclusive access to the value being stored within
8278 /// 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.
8581 ///
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.
8883 @available ( SwiftStdlib 6 . 0 , * )
8984 @_alwaysEmitIntoClient
9085 @_transparent
91- public borrowing func tryWithLock < Result: ~ Copyable & Sendable, E: Error > (
86+ public borrowing func withLock < Result: ~ Copyable & Sendable, E: Error > (
9287 _ 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 ( )
9790
9891 defer {
9992 handle. unlock ( )
@@ -120,10 +113,7 @@ extension Mutex where Value: ~Copyable {
120113 /// }
121114 /// return try body(&value)
122115 ///
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
127117 /// closure parameter has behavior that is platform dependent.
128118 /// Some platforms may choose to panic the process, deadlock,
129119 /// or leave this behavior unspecified.
@@ -139,8 +129,8 @@ extension Mutex where Value: ~Copyable {
139129 @available ( SwiftStdlib 6 . 0 , * )
140130 @_alwaysEmitIntoClient
141131 @_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
144134 ) throws ( E) -> Result ? {
145135 guard handle. tryLock ( ) else {
146136 return nil
@@ -152,85 +142,6 @@ extension Mutex where Value: ~Copyable {
152142
153143 return try body ( & value. address. pointee)
154144 }
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- }
234145}
235146
236147@available ( SwiftStdlib 6 . 0 , * )
@@ -256,6 +167,3 @@ extension Mutex where Value == Void {
256167 handle. unlock ( )
257168 }
258169}
259-
260- @available ( SwiftStdlib 6 . 0 , * )
261- extension Mutex : @unchecked Sendable where Value: Sendable { }
0 commit comments