@@ -47,56 +47,56 @@ typealias LockPrimitive = pthread_mutex_t
47
47
enum LockOperations { }
48
48
49
49
extension LockOperations {
50
- @inlinable
51
- static func create( _ mutex: UnsafeMutablePointer < LockPrimitive > ) {
52
- mutex. assertValidAlignment ( )
53
-
54
- #if os(Windows)
55
- InitializeSRWLock ( mutex)
56
- #else
57
- var attr = pthread_mutexattr_t ( )
58
- pthread_mutexattr_init ( & attr)
59
-
60
- let err = pthread_mutex_init ( mutex, & attr)
61
- precondition ( err == 0 , " \( #function) failed in pthread_mutex with error \( err) " )
62
- #endif
63
- }
64
-
65
- @inlinable
66
- static func destroy( _ mutex: UnsafeMutablePointer < LockPrimitive > ) {
67
- mutex. assertValidAlignment ( )
68
-
69
- #if os(Windows)
70
- // SRWLOCK does not need to be freed
71
- #else
72
- let err = pthread_mutex_destroy ( mutex)
73
- precondition ( err == 0 , " \( #function) failed in pthread_mutex with error \( err) " )
74
- #endif
75
- }
76
-
77
- @inlinable
78
- static func lock( _ mutex: UnsafeMutablePointer < LockPrimitive > ) {
79
- mutex. assertValidAlignment ( )
80
-
81
- #if os(Windows)
82
- AcquireSRWLockExclusive ( mutex)
83
- #else
84
- let err = pthread_mutex_lock ( mutex)
85
- precondition ( err == 0 , " \( #function) failed in pthread_mutex with error \( err) " )
86
- #endif
87
- }
88
-
89
- @inlinable
90
- static func unlock( _ mutex: UnsafeMutablePointer < LockPrimitive > ) {
91
- mutex. assertValidAlignment ( )
92
-
93
- #if os(Windows)
94
- ReleaseSRWLockExclusive ( mutex)
95
- #else
96
- let err = pthread_mutex_unlock ( mutex)
97
- precondition ( err == 0 , " \( #function) failed in pthread_mutex with error \( err) " )
98
- #endif
99
- }
50
+ @inlinable
51
+ static func create( _ mutex: UnsafeMutablePointer < LockPrimitive > ) {
52
+ mutex. assertValidAlignment ( )
53
+
54
+ #if os(Windows)
55
+ InitializeSRWLock ( mutex)
56
+ #else
57
+ var attr = pthread_mutexattr_t ( )
58
+ pthread_mutexattr_init ( & attr)
59
+
60
+ let err = pthread_mutex_init ( mutex, & attr)
61
+ precondition ( err == 0 , " \( #function) failed in pthread_mutex with error \( err) " )
62
+ #endif
63
+ }
64
+
65
+ @inlinable
66
+ static func destroy( _ mutex: UnsafeMutablePointer < LockPrimitive > ) {
67
+ mutex. assertValidAlignment ( )
68
+
69
+ #if os(Windows)
70
+ // SRWLOCK does not need to be freed
71
+ #else
72
+ let err = pthread_mutex_destroy ( mutex)
73
+ precondition ( err == 0 , " \( #function) failed in pthread_mutex with error \( err) " )
74
+ #endif
75
+ }
76
+
77
+ @inlinable
78
+ static func lock( _ mutex: UnsafeMutablePointer < LockPrimitive > ) {
79
+ mutex. assertValidAlignment ( )
80
+
81
+ #if os(Windows)
82
+ AcquireSRWLockExclusive ( mutex)
83
+ #else
84
+ let err = pthread_mutex_lock ( mutex)
85
+ precondition ( err == 0 , " \( #function) failed in pthread_mutex with error \( err) " )
86
+ #endif
87
+ }
88
+
89
+ @inlinable
90
+ static func unlock( _ mutex: UnsafeMutablePointer < LockPrimitive > ) {
91
+ mutex. assertValidAlignment ( )
92
+
93
+ #if os(Windows)
94
+ ReleaseSRWLockExclusive ( mutex)
95
+ #else
96
+ let err = pthread_mutex_unlock ( mutex)
97
+ precondition ( err == 0 , " \( #function) failed in pthread_mutex with error \( err) " )
98
+ #endif
99
+ }
100
100
}
101
101
102
102
// Tail allocate both the mutex and a generic value using ManagedBuffer.
@@ -130,59 +130,59 @@ extension LockOperations {
130
130
@usableFromInline
131
131
final class LockStorage < Value> : ManagedBuffer < Value , LockPrimitive > {
132
132
133
- @inlinable
134
- static func create( value: Value ) -> Self {
135
- let buffer = Self . create ( minimumCapacity: 1 ) { _ in
136
- return value
137
- }
138
- // Avoid 'unsafeDowncast' as there is a miscompilation on 5.10.
139
- let storage = buffer as! Self
133
+ @inlinable
134
+ static func create( value: Value ) -> Self {
135
+ let buffer = Self . create ( minimumCapacity: 1 ) { _ in
136
+ value
137
+ }
138
+ // Avoid 'unsafeDowncast' as there is a miscompilation on 5.10.
139
+ let storage = buffer as! Self
140
140
141
- storage. withUnsafeMutablePointers { _, lockPtr in
142
- LockOperations . create ( lockPtr)
143
- }
141
+ storage. withUnsafeMutablePointers { _, lockPtr in
142
+ LockOperations . create ( lockPtr)
143
+ }
144
144
145
- return storage
146
- }
145
+ return storage
146
+ }
147
147
148
- @inlinable
149
- func lock( ) {
150
- self . withUnsafeMutablePointerToElements { lockPtr in
151
- LockOperations . lock ( lockPtr)
148
+ @inlinable
149
+ func lock( ) {
150
+ self . withUnsafeMutablePointerToElements { lockPtr in
151
+ LockOperations . lock ( lockPtr)
152
+ }
152
153
}
153
- }
154
154
155
- @inlinable
156
- func unlock( ) {
157
- self . withUnsafeMutablePointerToElements { lockPtr in
158
- LockOperations . unlock ( lockPtr)
155
+ @inlinable
156
+ func unlock( ) {
157
+ self . withUnsafeMutablePointerToElements { lockPtr in
158
+ LockOperations . unlock ( lockPtr)
159
+ }
159
160
}
160
- }
161
161
162
- @usableFromInline
163
- deinit {
164
- self . withUnsafeMutablePointerToElements { lockPtr in
165
- LockOperations . destroy ( lockPtr)
162
+ @usableFromInline
163
+ deinit {
164
+ self . withUnsafeMutablePointerToElements { lockPtr in
165
+ LockOperations . destroy ( lockPtr)
166
+ }
166
167
}
167
- }
168
-
169
- @ inlinable
170
- func withLockPrimitive < T > (
171
- _ body : ( UnsafeMutablePointer < LockPrimitive > ) throws -> T
172
- ) rethrows -> T {
173
- try self . withUnsafeMutablePointerToElements { lockPtr in
174
- return try body ( lockPtr )
168
+
169
+ @ inlinable
170
+ func withLockPrimitive < T > (
171
+ _ body : ( UnsafeMutablePointer < LockPrimitive > ) throws -> T
172
+ ) rethrows -> T {
173
+ try self . withUnsafeMutablePointerToElements { lockPtr in
174
+ try body ( lockPtr )
175
+ }
175
176
}
176
- }
177
-
178
- @ inlinable
179
- func withLockedValue < T > ( _ mutate : ( inout Value ) throws -> T ) rethrows -> T {
180
- try self . withUnsafeMutablePointers { valuePtr , lockPtr in
181
- LockOperations . lock ( lockPtr)
182
- defer { LockOperations . unlock ( lockPtr ) }
183
- return try mutate ( & valuePtr . pointee )
177
+
178
+ @ inlinable
179
+ func withLockedValue < T > ( _ mutate : ( inout Value ) throws -> T ) rethrows -> T {
180
+ try self . withUnsafeMutablePointers { valuePtr , lockPtr in
181
+ LockOperations . lock ( lockPtr )
182
+ defer { LockOperations . unlock ( lockPtr) }
183
+ return try mutate ( & valuePtr . pointee )
184
+ }
184
185
}
185
- }
186
186
}
187
187
188
188
extension LockStorage : @unchecked Sendable { }
@@ -197,83 +197,83 @@ extension LockStorage: @unchecked Sendable {}
197
197
/// `SRWLOCK` type.
198
198
@usableFromInline
199
199
struct Lock {
200
- @usableFromInline
201
- internal let _storage : LockStorage < Void >
202
-
203
- /// Create a new lock.
204
- @usableFromInline
205
- init ( ) {
206
- self . _storage = . create( value: ( ) )
207
- }
208
-
209
- /// Acquire the lock.
210
- ///
211
- /// Whenever possible, consider using `withLock` instead of this method and
212
- /// `unlock`, to simplify lock handling.
213
- @inlinable
214
- func lock( ) {
215
- self . _storage. lock ( )
216
- }
217
-
218
- /// Release the lock.
219
- ///
220
- /// Whenever possible, consider using `withLock` instead of this method and
221
- /// `lock`, to simplify lock handling.
222
- @inlinable
223
- func unlock( ) {
224
- self . _storage. unlock ( )
225
- }
226
-
227
- @inlinable
228
- internal func withLockPrimitive< T> (
229
- _ body: ( UnsafeMutablePointer < LockPrimitive > ) throws -> T
230
- ) rethrows -> T {
231
- return try self . _storage. withLockPrimitive ( body)
232
- }
200
+ @usableFromInline
201
+ internal let _storage : LockStorage < Void >
202
+
203
+ /// Create a new lock.
204
+ @usableFromInline
205
+ init ( ) {
206
+ self . _storage = . create( value: ( ) )
207
+ }
208
+
209
+ /// Acquire the lock.
210
+ ///
211
+ /// Whenever possible, consider using `withLock` instead of this method and
212
+ /// `unlock`, to simplify lock handling.
213
+ @inlinable
214
+ func lock( ) {
215
+ self . _storage. lock ( )
216
+ }
217
+
218
+ /// Release the lock.
219
+ ///
220
+ /// Whenever possible, consider using `withLock` instead of this method and
221
+ /// `lock`, to simplify lock handling.
222
+ @inlinable
223
+ func unlock( ) {
224
+ self . _storage. unlock ( )
225
+ }
226
+
227
+ @inlinable
228
+ internal func withLockPrimitive< T> (
229
+ _ body: ( UnsafeMutablePointer < LockPrimitive > ) throws -> T
230
+ ) rethrows -> T {
231
+ try self . _storage. withLockPrimitive ( body)
232
+ }
233
233
}
234
234
235
235
extension Lock {
236
- /// Acquire the lock for the duration of the given block.
237
- ///
238
- /// This convenience method should be preferred to `lock` and `unlock` in
239
- /// most situations, as it ensures that the lock will be released regardless
240
- /// of how `body` exits.
241
- ///
242
- /// - Parameter body: The block to execute while holding the lock.
243
- /// - Returns: The value returned by the block.
244
- @inlinable
245
- func withLock< T> ( _ body: ( ) throws -> T ) rethrows -> T {
246
- self . lock ( )
247
- defer {
248
- self . unlock ( )
236
+ /// Acquire the lock for the duration of the given block.
237
+ ///
238
+ /// This convenience method should be preferred to `lock` and `unlock` in
239
+ /// most situations, as it ensures that the lock will be released regardless
240
+ /// of how `body` exits.
241
+ ///
242
+ /// - Parameter body: The block to execute while holding the lock.
243
+ /// - Returns: The value returned by the block.
244
+ @inlinable
245
+ func withLock< T> ( _ body: ( ) throws -> T ) rethrows -> T {
246
+ self . lock ( )
247
+ defer {
248
+ self . unlock ( )
249
+ }
250
+ return try body ( )
249
251
}
250
- return try body ( )
251
- }
252
252
}
253
253
254
254
extension Lock : Sendable { }
255
255
256
256
extension UnsafeMutablePointer {
257
- @inlinable
258
- func assertValidAlignment( ) {
259
- assert ( UInt ( bitPattern: self ) % UInt( MemoryLayout< Pointee> . alignment) == 0 )
260
- }
257
+ @inlinable
258
+ func assertValidAlignment( ) {
259
+ assert ( UInt ( bitPattern: self ) % UInt( MemoryLayout< Pointee> . alignment) == 0 )
260
+ }
261
261
}
262
262
263
263
@usableFromInline
264
264
struct LockedValueBox < Value> {
265
- @usableFromInline
266
- let storage : LockStorage < Value >
267
-
268
- @usableFromInline
269
- init ( _ value: Value ) {
270
- self . storage = . create( value: value)
271
- }
272
-
273
- @inlinable
274
- func withLockedValue< T> ( _ mutate: ( inout Value ) throws -> T ) rethrows -> T {
275
- return try self . storage. withLockedValue ( mutate)
276
- }
265
+ @usableFromInline
266
+ let storage : LockStorage < Value >
267
+
268
+ @usableFromInline
269
+ init ( _ value: Value ) {
270
+ self . storage = . create( value: value)
271
+ }
272
+
273
+ @inlinable
274
+ func withLockedValue< T> ( _ mutate: ( inout Value ) throws -> T ) rethrows -> T {
275
+ try self . storage. withLockedValue ( mutate)
276
+ }
277
277
}
278
278
279
279
extension LockedValueBox : Sendable where Value: Sendable { }
0 commit comments