Oolite
Loading...
Searching...
No Matches
OOAsyncQueue.m
Go to the documentation of this file.
1/*
2
3OOAsyncQueue.m
4By Jens Ayton
5
6
7Copyright (C) 2007-2013 Jens Ayton
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in all
17copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25SOFTWARE.
26
27*/
28
29#include <assert.h>
30
32#import "OOAsyncQueue.h"
34#import "OOLogging.h"
35#include <stdlib.h>
36
37#ifndef OO_BUGGY_PTHREADS
38#if OOLITE_WINDOWS
39// Maybe add #if OOLITE_64_BIT too?
40#define OO_BUGGY_PTHREADS 1
41#else
42#define OO_BUGGY_PTHREADS 0
43#endif
44#endif
45
46enum {
50};
51
52enum {
54};
55
61
63{
64 return malloc(sizeof(OOAsyncQueueElement));
65}
66
68{
69 free(element);
70}
71
72@interface OOAsyncQueue (OOPrivate)
73
76- (void)recycleElementWithAcquiredLock:(OOAsyncQueueElement*)element;
77
78@end
79
80@implementation OOAsyncQueue
81
82- (id)init
83{
84 self = [super init];
85 if (self != nil) {
86 _lock = [[NSConditionLock alloc] initWithCondition:kConditionNoData];
87 [_lock setName:@"OOAsyncQueue lock"];
88 if (_lock == nil) {
89 [self release];
90 self = nil;
91 }
92 }
93
94 return self;
95}
96
97- (void)dealloc
98{
99 OOAsyncQueueElement* element = NULL;
100
101 [_lock lock];
102
103 if (_elemCount != 0) {
104 OOLogWARN(@"asyncQueue.nonEmpty", @"%@ deallocated while non-empty, flushing.", self);
106 }
107
108 // Free element pool.
109 while (_pool != NULL) {
110 element = _pool;
111 _pool = element->next;
112 free(element);
113 }
114
115 [_lock unlockWithCondition:kConditionDead];
116 [_lock release];
117
118 [super dealloc];
119}
120
121- (NSString*)description
122{
123 // Don't bother locking, the value would be out of date immediately anyway.
124 return [NSString stringWithFormat:@"<%@ %p>{%u elements}", [self class], self, _elemCount];
125}
126
127- (BOOL)enqueue:(id)object
128{
129 OOAsyncQueueElement* element = NULL;
130 BOOL success = NO;
131
132 if (EXPECT_NOT(object == nil))
133 return NO;
134
135 [_lock lock];
136
137 // Get an element.
138 if (_pool != NULL) {
139 element = _pool;
140 _pool = element->next;
141 --_poolCount;
142 } else {
143 element = AllocElement();
144 if (element == NULL)
145 goto FAIL;
146 }
147
148 // Set element fields.
149 element->object = [object retain];
150 element->next = NULL;
151
152 // Insert in queue.
153 if (_head == NULL) {
154 // Queue was empty, element is entire queue.
155 _head = _tail = element;
156 element->next = NULL;
157 assert(_elemCount == 0);
158 _elemCount = 1;
159 } else {
160 assert(_tail != NULL);
161 assert(_tail->next == NULL);
162 assert(_elemCount != 0);
163
164 _tail->next = element;
165 _tail = element;
166 ++_elemCount;
167 }
168 success = YES;
169
170FAIL:
171 [_lock unlockWithCondition:kConditionQueuedData];
172 return success;
173}
174
176{
177 [_lock lockWhenCondition:kConditionQueuedData];
179}
180
182{
183#if OO_BUGGY_PTHREADS
184 /* pthread_mutex_trylock is buggy on 64-bit windows with the pthread
185 * library in use, so avoid doing things which use it This is a little
186 * more blocking, but no thread should be hanging on to _lock for very
187 * long, so hopefully it won't be noticeable.
188 */
189 [_lock lock];
190 if ([_lock condition] != kConditionQueuedData) {
191 [_lock unlock];
192 return nil;
193 }
194#else
195 // Mac and Linux can do it properly
196 if (![_lock tryLockWhenCondition:kConditionQueuedData])
197 return nil;
198#endif
200}
201
202- (BOOL)empty
203{
204 return _head != NULL;
205}
206
207- (unsigned)count
208{
209 return _elemCount;
210}
211
213{
214 [_lock lock];
216
217 assert(_head == NULL && _tail == NULL && _elemCount == 0);
218 [_lock unlockWithCondition:kConditionNoData];
219}
220
221@end
222
223@implementation OOAsyncQueue (OOPrivate)
224
226{
227 OOAsyncQueueElement* element = NULL;
228
229 // Loop over queue.
230 while (_head != NULL) {
231 // Dequeue element.
232 element = _head;
233 _head = _head->next;
234 --_elemCount;
235
236 // We don't need the payload any longer.
237 [element->object release];
238
239 // Or the element.
240 [self recycleElementWithAcquiredLock:element];
241 }
242
243 _tail = NULL;
244}
245
247{
248 OOAsyncQueueElement* element = NULL;
249 id result;
250
251 if (_head == NULL) {
252 // Can happen if you enter debugger.
253 return nil;
254 }
255
256 // assert(_head != NULL);
257
258 // Dequeue element.
259 element = _head;
260 _head = _head->next;
261 if (_head == NULL)
262 _tail = NULL;
263 --_elemCount;
264
265 // Unpack payload.
266 result = [element->object autorelease];
267
268 // Recycle element.
269 [self recycleElementWithAcquiredLock:element];
270
271 // Ensure sane status.
272 assert((_head == NULL && _tail == NULL && _elemCount == 0) || (_head != NULL && _tail != NULL && _elemCount != 0));
273
274 // Unlock with appropriate state.
275 [_lock unlockWithCondition:(_head == NULL) ? kConditionNoData : kConditionQueuedData];
276
277 return result;
278}
279
280- (void)recycleElementWithAcquiredLock:(OOAsyncQueueElement*)element
281{
282 if (_poolCount < kMaxPoolElements) {
283 // Add to pool for reuse.
284 element->next = _pool;
285 _pool = element;
286 ++_poolCount;
287 } else {
288 // Delete.
289 FreeElement(element);
290 }
291}
292
293@end
OOINLINE void FreeElement(OOAsyncQueueElement *element)
@ kConditionQueuedData
@ kConditionNoData
@ kConditionDead
@ kMaxPoolElements
OOINLINE OOAsyncQueueElement * AllocElement(void)
#define EXPECT_NOT(x)
#define OOINLINE
#define FAIL(s)
#define OOLogWARN(class, format,...)
Definition OOLogging.h:126
return nil
id doDequeAndUnlockWithAcquiredLock()
unsigned _poolCount
unsigned _elemCount
struct OOAsyncQueueElement * _pool
struct OOAsyncQueueElement * _head
void recycleElementWithAcquiredLock:(OOAsyncQueueElement *element)
void doEmptyQueueWithAcquiredLock()
unsigned count()
NSString * description()
struct OOAsyncQueueElement * _tail
NSConditionLock * _lock
OOAsyncQueueElement * next