forked from groue/GRDB.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename ReadWriteBox to ReadWriteLock
- Loading branch information
Showing
8 changed files
with
96 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Dispatch | ||
|
||
/// A ReadWriteLock grants multiple readers and single-writer guarantees on | ||
/// a value. It is backed by a concurrent DispatchQueue. | ||
final class ReadWriteLock<T> { | ||
private var _value: T | ||
private var queue: DispatchQueue | ||
|
||
init(_ value: T) { | ||
_value = value | ||
queue = DispatchQueue(label: "GRDB.ReadWriteLock", attributes: [.concurrent]) | ||
} | ||
|
||
/// Reads the value. | ||
func read<U>(_ body: (T) throws -> U) rethrows -> U { | ||
try queue.sync { | ||
try body(_value) | ||
} | ||
} | ||
|
||
/// Runs the provided closure while holding a lock on the value. | ||
/// | ||
/// - parameter body: A closure that can modify the value. | ||
func withLock<U>(_ body: (inout T) throws -> U) rethrows -> U { | ||
try queue.sync(flags: [.barrier]) { | ||
try body(&_value) | ||
} | ||
} | ||
} | ||
|
||
// @unchecked because `_value` is protected by `queue` | ||
extension ReadWriteLock: @unchecked Sendable where T: Sendable { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters