ReadWriteBox(value:) initializer

This commit is contained in:
Gwendal Roué committed 2019-10-10 13:50:42 +02:00
1 parent 7dd90c4af3
commit b59521e247
4 files changed
+16 -8

No files matched your search

+2 -2
View File
@@ -921,7 +921,7 @@ private struct InsertQuery: Hashable {
}
extension InsertQuery {
static let sqlCache = ReadWriteBox([InsertQuery: String]())
static let sqlCache = ReadWriteBox(value: [InsertQuery: String]())
var sql: String {
if let sql = InsertQuery.sqlCache.read({ $0[self] }) {
return sql
@@ -958,7 +958,7 @@ private struct UpdateQuery: Hashable {
}
extension UpdateQuery {
static let sqlCache = ReadWriteBox([UpdateQuery: String]())
static let sqlCache = ReadWriteBox(value: [UpdateQuery: String]())
var sql: String {
if let sql = UpdateQuery.sqlCache.read({ $0[self] }) {
return sql
+1 -1
View File
@@ -47,7 +47,7 @@ final class Pool<T> {
}
private let makeElement: () throws -> T
private var items: ReadWriteBox<[Item]> = ReadWriteBox([])
private var items: ReadWriteBox<[Item]> = ReadWriteBox(value: [])
private let itemsSemaphore: DispatchSemaphore // limits the number of elements
private let itemsGroup: DispatchGroup // knows when no element is used
private let barrierQueue: DispatchQueue
+12 -4
View File
@@ -2,12 +2,15 @@ import Dispatch
/// A ReadWriteBox grants multiple readers and single-writer guarantees on a value.
final class ReadWriteBox<T> {
private var _value: T
private var queue: DispatchQueue
var value: T {
get { return read { $0 } }
set { write { $0 = newValue } }
}
init(_ value: T) {
init(value: T) {
_value = value
queue = DispatchQueue(label: "GRDB.ReadWriteBox", attributes: [.concurrent])
}
@@ -23,9 +26,6 @@ final class ReadWriteBox<T> {
try block(&_value)
}
}
private var _value: T
private var queue: DispatchQueue
}
extension ReadWriteBox where T: Numeric {
@@ -36,4 +36,12 @@ extension ReadWriteBox where T: Numeric {
return n
}
}
@discardableResult
func decrement() -> T {
return write { n in
n -= 1
return n
}
}
}
+1 -1
View File
@@ -8,7 +8,7 @@ import XCTest
class PoolTests: XCTestCase {
/// Returns a Pool whose elements are incremented integers: 1, 2, 3...
private func makeCounterPool(maximumCount: Int) -> Pool<Int> {
let count = ReadWriteBox(0)
let count = ReadWriteBox(value: 0)
return Pool(maximumCount: maximumCount, makeElement: count.increment)
}