-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTJConcurrentMutableCollection.m
More file actions
148 lines (126 loc) · 3.61 KB
/
Copy pathTJConcurrentMutableCollection.m
File metadata and controls
148 lines (126 loc) · 3.61 KB
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
//
// TJConcurrentMutableCollection.m
// Close-up
//
// Created by Tim Johnsen on 6/30/26.
//
#import "TJConcurrentMutableCollection.h"
@implementation TJConcurrentMutableCollection {
id _collection;
void (^_writeBlock)(id, id, id);
id (^_readBlock)(id, id);
}
- (instancetype)init
{
return [self initWithDictionary:nil queue:nil];
}
- (instancetype)initWithCollection:(id)collection writeBlock:(void (^)(id _Nonnull, id _Nonnull, id _Nonnull))writeBlock readBlock:(id _Nonnull (^)(id _Nonnull, id _Nonnull))readBlock queue:(nullable dispatch_queue_t)queue
{
if (self = [super init]) {
_collection = collection;
_writeBlock = writeBlock;
_readBlock = readBlock;
_queue = queue ?: dispatch_queue_create("com.tijo.TJConcurrentMutableCollection", DISPATCH_QUEUE_CONCURRENT);
}
return self;
}
#pragma mark - Convenience Initializers
- (instancetype)initWithDictionary:(NSMutableDictionary *)dictionary queue:(dispatch_queue_t)queue
{
if (dictionary == nil) {
dictionary = [NSMutableDictionary new];
}
return [self initWithCollection:dictionary
writeBlock:^(id key, id obj, NSMutableDictionary *dict) {
dict[key] = obj;
}
readBlock:^id(id key, NSMutableDictionary *dict) {
return dict[key];
}
queue:queue];
}
- (instancetype)initWithMapTable:(NSMapTable *)mapTable queue:(dispatch_queue_t)queue
{
if (mapTable == nil) {
mapTable = [NSMapTable strongToWeakObjectsMapTable];
}
return [self initWithCollection:mapTable
writeBlock:^(id key, id obj, NSMapTable *table) {
if (obj) {
[table setObject:obj forKey:key];
} else {
[table removeObjectForKey:key];
}
}
readBlock:^id(id key, NSMapTable *table) {
return [table objectForKey:key];
}
queue:queue];
}
#pragma mark - Accessors
- (id)objectForKey:(id)key
{
__block id obj;
dispatch_sync(_queue, ^{
obj = _readBlock(key, _collection);
});
return obj;
}
- (void)objectForKey:(id)key completion:(void (^)(_Nullable id))completion
{
dispatch_async(_queue, ^{
completion(self->_readBlock(key, self->_collection));
});
}
- (void)readCollection:(void (^)(id))completion waitUntilDone:(BOOL)waitUntilDone
{
if (waitUntilDone) {
dispatch_sync(_queue, ^{
completion(_collection);
});
} else {
dispatch_async(_queue, ^{
completion(self->_collection);
});
}
}
- (void)setObject:(id)obj forKey:(id)key
{
[self setObject:obj forKey:key waitUntilDone:NO];
}
- (void)setObject:(id)obj forKey:(id)key waitUntilDone:(BOOL)waitUntilDone
{
if (waitUntilDone) {
dispatch_barrier_sync(_queue, ^{
_writeBlock(key, obj, _collection);
});
} else {
dispatch_barrier_async(_queue, ^{
self->_writeBlock(key, obj, self->_collection);
});
}
}
- (void)removeObjectForKey:(id)key
{
[self setObject:nil forKey:key waitUntilDone:NO];
}
- (void)removeObjectForKey:(id)key waitUntilDone:(BOOL)waitUntilDone
{
[self setObject:nil forKey:key waitUntilDone:waitUntilDone];
}
- (void)setObject:(id)obj forKeyedSubscript:(id)key
{
[self setObject:obj forKey:key];
}
- (id)objectForKeyedSubscript:(id)key {
return [self objectForKey:key];
}
+ (instancetype)dictionary
{
return [[self alloc] initWithDictionary:nil queue:nil];
}
+ (instancetype)mapTable
{
return [[self alloc] initWithMapTable:nil queue:nil];
}
@end