Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OOAsyncQueue Class Reference

#include <OOAsyncQueue.h>

+ Inheritance diagram for OOAsyncQueue:
+ Collaboration diagram for OOAsyncQueue:

Instance Methods

(BOOL) - enqueue:
 
(id) - dequeue
 
(id) - tryDequeue
 
(BOOL) - empty
 
(unsigned) - count
 
(void) - emptyQueue
 
(id) - init [implementation]
 
(void) - dealloc [implementation]
 
(NSString *) - description [implementation]
 
(void) - doEmptyQueueWithAcquiredLock [implementation]
 
(id) - doDequeAndUnlockWithAcquiredLock [implementation]
 
(void) - recycleElementWithAcquiredLock: [implementation]
 

Private Attributes

NSConditionLock * _lock
 
struct OOAsyncQueueElement_head
 
struct OOAsyncQueueElement_tail
 
struct OOAsyncQueueElement_pool
 
unsigned _elemCount
 
unsigned _poolCount
 

Detailed Description

Definition at line 36 of file OOAsyncQueue.h.

Method Documentation

◆ count

- (unsigned) count

Definition at line 64 of file OOAsyncQueue.m.

232{
233 return _elemCount;
234}
unsigned _elemCount

◆ dealloc

- (void) dealloc
implementation

Definition at line 64 of file OOAsyncQueue.m.

110{
111 OOAsyncQueueElement *element = NULL;
112
113 [_lock lock];
114
115 if (_elemCount != 0)
116 {
117 OOLogWARN(@"asyncQueue.nonEmpty", @"%@ deallocated while non-empty, flushing.", self);
118 [self doEmptyQueueWithAcquiredLock];
119 }
120
121 // Free element pool.
122 while (_pool != NULL)
123 {
124 element = _pool;
125 _pool = element->next;
126 free(element);
127 }
128
129 [_lock unlockWithCondition:kConditionDead];
130 [_lock release];
131
132 [super dealloc];
133}
#define OOLogWARN(class, format,...)
Definition OOLogging.h:113
struct OOAsyncQueueElement * _pool
OOAsyncQueueElement * next

◆ dequeue

- (id) dequeue

Definition at line 64 of file OOAsyncQueue.m.

197{
198 [_lock lockWhenCondition:kConditionQueuedData];
199 return [self doDequeAndUnlockWithAcquiredLock];
200}

◆ description

- (NSString *) description
implementation

Definition at line 64 of file OOAsyncQueue.m.

137{
138 // Don't bother locking, the value would be out of date immediately anyway.
139 return [NSString stringWithFormat:@"<%@ %p>{%u elements}", [self class], self, _elemCount];
140}

◆ doDequeAndUnlockWithAcquiredLock

- (id) doDequeAndUnlockWithAcquiredLock
implementation

Provided by category OOAsyncQueue(OOPrivate).

Definition at line 64 of file OOAsyncQueue.m.

275{
276 OOAsyncQueueElement *element = NULL;
277 id result;
278
279 if (_head == NULL)
280 {
281 // Can happen if you enter debugger.
282 return nil;
283 }
284
285// assert(_head != NULL);
286
287 // Dequeue element.
288 element = _head;
289 _head = _head->next;
290 if (_head == NULL) _tail = NULL;
291 --_elemCount;
292
293 // Unpack payload.
294 result = [element->object autorelease];
295
296 // Recycle element.
297 [self recycleElementWithAcquiredLock:element];
298
299 // Ensure sane status.
300 assert((_head == NULL && _tail == NULL && _elemCount == 0) || (_head != NULL && _tail != NULL && _elemCount != 0));
301
302 // Unlock with appropriate state.
303 [_lock unlockWithCondition:(_head == NULL) ? kConditionNoData : kConditionQueuedData];
304
305 return result;
306}
return nil

◆ doEmptyQueueWithAcquiredLock

- (void) doEmptyQueueWithAcquiredLock
implementation

Provided by category OOAsyncQueue(OOPrivate).

Definition at line 64 of file OOAsyncQueue.m.

252{
253 OOAsyncQueueElement *element = NULL;
254
255 // Loop over queue.
256 while (_head != NULL)
257 {
258 // Dequeue element.
259 element = _head;
260 _head = _head->next;
261 --_elemCount;
262
263 // We don't need the payload any longer.
264 [element->object release];
265
266 // Or the element.
267 [self recycleElementWithAcquiredLock:element];
268 }
269
270 _tail = NULL;
271}

◆ empty

- (BOOL) empty

Definition at line 64 of file OOAsyncQueue.m.

226{
227 return _head != NULL;
228}
struct OOAsyncQueueElement * _head

◆ emptyQueue

- (void) emptyQueue

Definition at line 64 of file OOAsyncQueue.m.

238{
239 [_lock lock];
240 [self doEmptyQueueWithAcquiredLock];
241
242 assert(_head == NULL && _tail == NULL && _elemCount == 0);
243 [_lock unlockWithCondition:kConditionNoData];
244}
struct OOAsyncQueueElement * _tail

◆ enqueue:

- (BOOL) enqueue: (id) object

Definition at line 64 of file OOAsyncQueue.m.

143 :(id)object
144{
145 OOAsyncQueueElement *element = NULL;
146 BOOL success = NO;
147
148 if (EXPECT_NOT(object == nil)) return NO;
149
150 [_lock lock];
151
152 // Get an element.
153 if (_pool != NULL)
154 {
155 element = _pool;
156 _pool = element->next;
157 --_poolCount;
158 }
159 else
160 {
161 element = AllocElement();
162 if (element == NULL) goto FAIL;
163 }
164
165 // Set element fields.
166 element->object = [object retain];
167 element->next = NULL;
168
169 // Insert in queue.
170 if (_head == NULL)
171 {
172 // Queue was empty, element is entire queue.
173 _head = _tail = element;
174 element->next = NULL;
175 assert(_elemCount == 0);
176 _elemCount = 1;
177 }
178 else
179 {
180 assert(_tail != NULL);
181 assert(_tail->next == NULL);
182 assert(_elemCount != 0);
183
184 _tail->next = element;
185 _tail = element;
186 ++_elemCount;
187 }
188 success = YES;
189
190FAIL:
191 [_lock unlockWithCondition:kConditionQueuedData];
192 return success;
193}
OOINLINE OOAsyncQueueElement * AllocElement(void)
#define EXPECT_NOT(x)
#define FAIL(s)
unsigned _poolCount

◆ init

- (id) init
implementation

Definition at line 64 of file OOAsyncQueue.m.

92{
93 self = [super init];
94 if (self != nil)
95 {
96 _lock = [[NSConditionLock alloc] initWithCondition:kConditionNoData];
97 [_lock setName:@"OOAsyncQueue lock"];
98 if (_lock == nil)
99 {
100 [self release];
101 self = nil;
102 }
103 }
104
105 return self;
106}
NSConditionLock * _lock

◆ recycleElementWithAcquiredLock:

- (void) recycleElementWithAcquiredLock: (OOAsyncQueueElement *) element
implementation

Provided by category OOAsyncQueue(OOPrivate).

Definition at line 64 of file OOAsyncQueue.m.

309 :(OOAsyncQueueElement *)element
310{
311 if (_poolCount < kMaxPoolElements)
312 {
313 // Add to pool for reuse.
314 element->next = _pool;
315 _pool = element;
316 ++_poolCount;
317 }
318 else
319 {
320 // Delete.
321 FreeElement(element);
322 }
323}
@ kMaxPoolElements
OOINLINE void FreeElement(OOAsyncQueueElement *element)

◆ tryDequeue

- (id) tryDequeue

Definition at line 64 of file OOAsyncQueue.m.

204{
205#if OO_BUGGY_PTHREADS
206/* pthread_mutex_trylock is buggy on 64-bit windows with the pthread
207 * library in use, so avoid doing things which use it This is a little
208 * more blocking, but no thread should be hanging on to _lock for very
209 * long, so hopefully it won't be noticeable.
210 */
211 [_lock lock];
212 if ([_lock condition] != kConditionQueuedData)
213 {
214 [_lock unlock];
215 return NO;
216 }
217#else
218 // Mac and Linux can do it properly
219 if (![_lock tryLockWhenCondition:kConditionQueuedData]) return nil;
220#endif
221 return [self doDequeAndUnlockWithAcquiredLock];
222}
@ kConditionQueuedData

Member Data Documentation

◆ _elemCount

- (unsigned) _elemCount
private

Definition at line 43 of file OOAsyncQueue.h.

◆ _head

- (struct OOAsyncQueueElement*) _head
private

Definition at line 40 of file OOAsyncQueue.h.

◆ _lock

- (NSConditionLock*) _lock
private

Definition at line 39 of file OOAsyncQueue.h.

◆ _pool

- (struct OOAsyncQueueElement *) _pool
private

Definition at line 42 of file OOAsyncQueue.h.

◆ _poolCount

- (unsigned) _poolCount
private

Definition at line 44 of file OOAsyncQueue.h.

◆ _tail

- (struct OOAsyncQueueElement *) _tail
private

Definition at line 41 of file OOAsyncQueue.h.


The documentation for this class was generated from the following files: