Oolite
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 35 of file OOAsyncQueue.h.

Method Documentation

◆ count

- (unsigned) count

Definition at line 207 of file OOAsyncQueue.m.

208{
209 return _elemCount;
210}
unsigned _elemCount

References _elemCount, and count.

Referenced by count.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dealloc

- (void) dealloc
implementation

Definition at line 97 of file OOAsyncQueue.m.

98{
99 OOAsyncQueueElement* element = NULL;
100
101 [_lock lock];
102
103 if (_elemCount != 0) {
104 OOLogWARN(@"asyncQueue.nonEmpty", @"%@ deallocated while non-empty, flushing.", self);
105 [self doEmptyQueueWithAcquiredLock];
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}
struct OOAsyncQueueElement OOAsyncQueueElement
#define OOLogWARN(class, format,...)
Definition OOLogging.h:126
struct OOAsyncQueueElement * _pool
OOAsyncQueueElement * next

References _elemCount, _pool, dealloc, doEmptyQueueWithAcquiredLock, OOAsyncQueueElement::next, and OOLogWARN.

Referenced by dealloc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dequeue

- (id) dequeue

Definition at line 175 of file OOAsyncQueue.m.

176{
177 [_lock lockWhenCondition:kConditionQueuedData];
178 return [self doDequeAndUnlockWithAcquiredLock];
179}

References dequeue, and doDequeAndUnlockWithAcquiredLock.

Referenced by dequeue, OOAsyncLogger::loggerThread, and OOAsyncWorkManagerInternal::waitForTaskToComplete:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ description

- (NSString *) description
implementation

Definition at line 121 of file OOAsyncQueue.m.

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}

References description.

Referenced by description.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ doDequeAndUnlockWithAcquiredLock

- (id) doDequeAndUnlockWithAcquiredLock
implementation

Provided by category OOAsyncQueue(OOPrivate).

Definition at line 246 of file OOAsyncQueue.m.

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}
return nil
struct OOAsyncQueueElement * _head
struct OOAsyncQueueElement * _tail

Referenced by dequeue, and tryDequeue.

Here is the caller graph for this function:

◆ doEmptyQueueWithAcquiredLock

- (void) doEmptyQueueWithAcquiredLock
implementation

Provided by category OOAsyncQueue(OOPrivate).

Definition at line 225 of file OOAsyncQueue.m.

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}

Referenced by dealloc, and emptyQueue.

Here is the caller graph for this function:

◆ empty

- (BOOL) empty

Definition at line 202 of file OOAsyncQueue.m.

203{
204 return _head != NULL;
205}

References _head, and empty.

Referenced by empty.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ emptyQueue

- (void) emptyQueue

Definition at line 212 of file OOAsyncQueue.m.

213{
214 [_lock lock];
215 [self doEmptyQueueWithAcquiredLock];
216
217 assert(_head == NULL && _tail == NULL && _elemCount == 0);
218 [_lock unlockWithCondition:kConditionNoData];
219}

References _elemCount, _head, _tail, doEmptyQueueWithAcquiredLock, and emptyQueue.

Referenced by emptyQueue.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ enqueue:

- (BOOL) enqueue: (id) object

Definition at line 127 of file OOAsyncQueue.m.

127 :(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}
OOINLINE OOAsyncQueueElement * AllocElement(void)
#define EXPECT_NOT(x)
#define FAIL(s)
unsigned _poolCount

References _elemCount, _head, _pool, _poolCount, _tail, AllocElement(), EXPECT_NOT, FAIL, OOAsyncQueueElement::next, nil, and OOAsyncQueueElement::object.

Referenced by OOAsyncLogger::asyncLogMessage:, OOAsyncLogger::endLogging, OOAsyncLogger::flushLog, OOAsyncWorkManagerInternal::queueResult:, and OOAsyncLogger::startLogging.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init

- (id) init
implementation

Definition at line 82 of file OOAsyncQueue.m.

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}
NSConditionLock * _lock

References _lock, init, and nil.

Referenced by init.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ recycleElementWithAcquiredLock:

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

Provided by category OOAsyncQueue(OOPrivate).

Definition at line 280 of file OOAsyncQueue.m.

280 :(OOAsyncQueueElement*)element
281{
283 // Add to pool for reuse.
284 element->next = _pool;
285 _pool = element;
286 ++_poolCount;
287 } else {
288 // Delete.
289 FreeElement(element);
290 }
291}
OOINLINE void FreeElement(OOAsyncQueueElement *element)
@ kMaxPoolElements

Referenced by OOAsyncQueue(OOPrivate)::doDequeAndUnlockWithAcquiredLock, and OOAsyncQueue(OOPrivate)::doEmptyQueueWithAcquiredLock.

Here is the caller graph for this function:

◆ tryDequeue

- (id) tryDequeue

Definition at line 181 of file OOAsyncQueue.m.

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
199 return [self doDequeAndUnlockWithAcquiredLock];
200}
@ kConditionQueuedData

References _lock, doDequeAndUnlockWithAcquiredLock, kConditionQueuedData, nil, and tryDequeue.

Referenced by OOAsyncWorkManagerInternal::completePendingTasks, and tryDequeue.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _elemCount

- (unsigned) _elemCount
private

Definition at line 41 of file OOAsyncQueue.h.

Referenced by count, dealloc, emptyQueue, and enqueue:.

◆ _head

- (struct OOAsyncQueueElement*) _head
private

Definition at line 38 of file OOAsyncQueue.h.

Referenced by empty, emptyQueue, and enqueue:.

◆ _lock

- (NSConditionLock*) _lock
private

Definition at line 37 of file OOAsyncQueue.h.

Referenced by init, and tryDequeue.

◆ _pool

- (struct OOAsyncQueueElement *) _pool
private

Definition at line 40 of file OOAsyncQueue.h.

Referenced by dealloc, and enqueue:.

◆ _poolCount

- (unsigned) _poolCount
private

Definition at line 42 of file OOAsyncQueue.h.

Referenced by enqueue:.

◆ _tail

- (struct OOAsyncQueueElement *) _tail
private

Definition at line 39 of file OOAsyncQueue.h.

Referenced by emptyQueue, and enqueue:.


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