@@ -163,7 +163,7 @@ extension ReadWriteLock {
163163 /// - Parameter body: The block to execute while holding the lock.
164164 /// - Returns: The value returned by the block.
165165 @inlinable
166- func withReaderLock< T> ( _ body: ( ) throws -> T ) rethrows -> T {
166+ public func withReaderLock< T> ( _ body: ( ) throws -> T ) rethrows -> T {
167167 lockRead ( )
168168 defer {
169169 self . unlock ( )
@@ -180,7 +180,7 @@ extension ReadWriteLock {
180180 /// - Parameter body: The block to execute while holding the lock.
181181 /// - Returns: The value returned by the block.
182182 @inlinable
183- func withWriterLock< T> ( _ body: ( ) throws -> T ) rethrows -> T {
183+ public func withWriterLock< T> ( _ body: ( ) throws -> T ) rethrows -> T {
184184 lockWrite ( )
185185 defer {
186186 self . unlock ( )
@@ -200,3 +200,61 @@ extension ReadWriteLock {
200200 try withWriterLock ( body)
201201 }
202202}
203+
204+ public final class Locked < Value> : @unchecked Sendable {
205+
206+ private var internalValue : Value
207+
208+ private let lock = Lock ( )
209+
210+ public var protectedValue : Value {
211+ get {
212+ lock. withLock { internalValue }
213+ }
214+ _modify {
215+ lock. lock ( )
216+ defer { lock. unlock ( ) }
217+ yield & internalValue
218+ }
219+ }
220+
221+ public init ( initialValue: Value ) {
222+ self . internalValue = initialValue
223+ }
224+
225+ public func locking< T> ( _ block: ( inout Value ) throws -> T ) rethrows -> T {
226+ try lock. withLock { try block ( & internalValue) }
227+ }
228+ }
229+
230+ public final class ReadWriteLocked < Value> : @unchecked Sendable {
231+
232+ private var internalValue : Value
233+
234+ private let rwlock = ReadWriteLock ( )
235+
236+ public var protectedValue : Value {
237+ get {
238+ rwlock. withReaderLock { internalValue }
239+ }
240+ _modify {
241+ rwlock. lockWrite ( )
242+ defer { rwlock. unlock ( ) }
243+ yield & internalValue
244+ }
245+ }
246+
247+ public init ( initialValue: Value ) {
248+ self . internalValue = initialValue
249+ }
250+
251+ public func readLocking< T> ( _ block: ( Value ) throws -> T ) rethrows -> T {
252+ try rwlock. withReaderLock { try block ( internalValue) }
253+ }
254+
255+ public func writeLocking< T> ( _ block: ( inout Value ) throws -> T ) rethrows -> T {
256+ try rwlock. withWriterLock { try block ( & internalValue) }
257+ }
258+
259+
260+ }
0 commit comments