forked from PerfectlySoft/Perfect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRWLockCache.swift
175 lines (146 loc) · 5.01 KB
/
RWLockCache.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// RWLockCache.swift
// PerfectLib
//
// Created by Kyle Jessup on 2015-12-01.
// Copyright © 2015 PerfectlySoft. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version, as supplemented by the
// Perfect Additional Terms.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License, as supplemented by the
// Perfect Additional Terms, for more details.
//
// You should have received a copy of the GNU Affero General Public License
// and the Perfect Additional Terms that immediately follow the terms and
// conditions of the GNU Affero General Public License along with this
// program. If not, see <http://www.perfect.org/AGPL_3_0_With_Perfect_Additional_Terms.txt>.
//
import Foundation
/*
NOTE: This class uses GCD and as such does not yet operate on Linux.
It is not included in the project for OS X but is left in the source directory for future consideration.
*/
/// This class implements a multi-reader single-writer thread-safe dictionary.
/// It provides a means for:
/// 1. Fetching a value given a key, with concurrent readers
/// 2. Setting the value for a key with one writer while all readers block
/// 3. Fetching a value for a key with a callback, which, if the key does NOT exist, will be called to generate the new value in a thread-safe manner.
/// 4. Fetching a value for a key with a callback, which, if the key DOES exist, will be called to validate the value. If the value does nto validate then it will be removed from the dictionary in a thread-safe manner.
/// 5. Iterating all key/value pairs in a thread-safe manner while the write lock is held.
/// 6. Retrieving all keys while the read lock is held.
/// 7. Executing arbitrary blocks while either the read or write lock is held.
///
/// Note that if the validator callback indicates that the value is not valid, it will be called a second time while the write lock is held to ensure that the value has not been re-validated by another thread.
public class RWLockCache<KeyT: Hashable, ValueT> {
public typealias KeyType = KeyT
public typealias ValueType = ValueT
public typealias ValueGenerator = () -> ValueType?
public typealias ValueValidator = (value: ValueType) -> Bool
private let queue = dispatch_queue_create("RWLockCache", DISPATCH_QUEUE_CONCURRENT)
private var cache = [KeyType : ValueType]()
public func valueForKey(key: KeyType) -> ValueType? {
var value: ValueType?
dispatch_sync(self.queue) {
value = self.cache[key]
}
return value
}
public func valueForKey(key: KeyType, missCallback: ValueGenerator, validatorCallback: ValueValidator) -> ValueType? {
var value: ValueType?
dispatch_sync(self.queue) {
value = self.cache[key]
}
if value == nil {
dispatch_barrier_sync(self.queue) {
value = self.cache[key]
if value == nil {
value = missCallback()
if value != nil {
self.cache[key] = value
}
}
}
} else if !validatorCallback(value: value!) {
dispatch_barrier_sync(self.queue) {
value = self.cache[key]
if value != nil && !validatorCallback(value: value!) {
self.cache.removeValueForKey(key)
value = nil
}
}
}
return value
}
public func valueForKey(key: KeyType, validatorCallback: ValueValidator) -> ValueType? {
var value: ValueType?
dispatch_sync(self.queue) {
value = self.cache[key]
}
if value != nil && !validatorCallback(value: value!) {
dispatch_barrier_sync(self.queue) {
value = self.cache[key]
if value != nil && !validatorCallback(value: value!) {
self.cache.removeValueForKey(key)
value = nil
}
}
}
return value
}
public func valueForKey(key: KeyType, missCallback: ValueGenerator) -> ValueType? {
var value: ValueType?
dispatch_sync(self.queue) {
value = self.cache[key]
}
if value == nil {
dispatch_barrier_sync(self.queue) {
value = self.cache[key]
if value == nil {
value = missCallback()
if value != nil {
self.cache[key] = value
}
}
}
}
return value
}
public func setValueForKey(key: KeyType, value: ValueType) {
dispatch_barrier_async(self.queue) {
self.cache[key] = value
}
}
public func keys() -> [KeyType] {
var keys = [KeyType]()
dispatch_sync(self.queue) {
for key in self.cache.keys {
keys.append(key)
}
}
return keys
}
public func keysAndValues(callback: (KeyType, ValueType) -> ()) {
dispatch_barrier_sync(self.queue) {
for (key, value) in self.cache {
callback(key, value)
}
}
}
public func withReadLock(callback: () -> ()) {
dispatch_sync(self.queue) {
callback()
}
}
public func withWriteLock(callback: () -> ()) {
dispatch_barrier_sync(self.queue) {
callback()
}
}
}