Objectively
Object oriented framework for C.
Loading...
Searching...
No Matches
OperationQueue.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25
26#include "OperationQueue.h"
27
28#define _Class _OperationQueue
29
30#pragma mark - Object
31
35static Object *copy(const Object *self) {
36
37 return NULL;
38}
39
43static void dealloc(Object *self) {
44
45 OperationQueue *this = (OperationQueue *) self;
46
47 synchronized(this->condition, {
48 this->isCancelled = true;
49 $(this->condition, broadcast);
50 });
51
52 release(this->threadPool);
53 release(this->condition);
54 release(this->operations);
55
56 super(Object, self, dealloc);
57}
58
59#pragma mark - OperationQueue
60
65static void addOperation(OperationQueue *self, Operation *operation) {
66
67 assert(operation);
68 assert($(operation, isCancelled) == false);
69 assert(operation->isExecuting == false);
70 assert(operation->isFinished == false);
71
72 synchronized(self->condition, {
73 operation->queue = self;
74 $(self->operations, addObject, operation);
75 $(self->condition, signal);
76 });
77}
78
84
85 assert(function);
86
87 Operation *operation = $(alloc(Operation), initWithFunction, function, data);
88
89 $(self, addOperation, operation);
90
91 return operation;
92}
93
99
100 Array *operations = $(self, operations);
101
102 for (size_t i = 0; i < operations->count; i++) {
104 }
105
107}
108
110
116 return _currentQueue;
117}
118
122static bool isOperationReady(const ident obj, ident data) {
123 return $((Operation *) obj, isReady);
124}
125
129static ident run(Thread *thread) {
130
131 OperationQueue *self = _currentQueue = thread->data;
132
133 bool isCancelled = false;
134
135 while (!isCancelled) {
136
137 Operation *operation = NULL;
138
139 synchronized(self->condition, {
140
141 isCancelled = self->isCancelled;
142
143 if (!isCancelled && self->isSuspended == false) {
144
145 operation = $(self->operations, find, isOperationReady, NULL);
146 if (operation) {
147 operation->isDispatched = true;
148 $(self->condition, signal);
149 }
150 }
151
152 if (operation == NULL && !isCancelled) {
153 $(self->condition, wait);
154 }
155 });
156
157 if (operation == NULL) {
158 continue;
159 }
160
161 $(operation, start);
162
163 $(self, removeOperation, operation);
164 }
165
166 return NULL;
167}
168
174 return $(self, initWithMaxConcurrentOperations, 1);
175}
176
181static OperationQueue *initWithMaxConcurrentOperations(OperationQueue *self, size_t maxConcurrentOperations) {
182
183 assert(maxConcurrentOperations);
184
185 self = (OperationQueue *) super(Object, self, init);
186 if (self) {
187
188 self->condition = $(alloc(Condition), init);
189 assert(self->condition);
190
191 self->operations = $(alloc(Array), init);
192 assert(self->operations);
193
194 self->threadPool = $(alloc(ThreadPool), initWithSize, maxConcurrentOperations);
195 assert(self->threadPool);
196
197 for (size_t i = 0; i < maxConcurrentOperations; i++) {
198 $(self->threadPool, detach, run, self);
199 }
200 }
201
202 return self;
203}
204
209static size_t operationCount(const OperationQueue *self) {
210
211 size_t count;
212
213 synchronized(self->condition, {
214 count = ((Array *) self->operations)->count;
215 });
216
217 return count;
218}
219
224static Array *operations(const OperationQueue *self) {
225
227
228 synchronized(self->condition, {
229 operations = $((Object * ) self->operations, copy);
230 });
231
232 return (Array *) operations;
233}
234
239static void removeOperation(OperationQueue *self, Operation *operation) {
240
241 assert(operation);
242 assert(operation->isExecuting == false);
243
244 synchronized(self->condition, {
245 operation->queue = NULL;
246 $(self->operations, removeObject, operation);
247 $(self->condition, signal);
248 });
249}
250
255static void resume(OperationQueue *self) {
256
257 synchronized(self->condition, {
258 self->isSuspended = false;
259 $(self->condition, broadcast);
260 });
261}
262
267static void suspend(OperationQueue *self) {
268
269 synchronized(self->condition, {
270 self->isSuspended = true;
271 });
272}
273
279
280 while (true) {
281
282 Operation *operation = NULL;
283
284 synchronized(self->condition, {
285 const Array *operations = (Array *) self->operations;
286 if (operations->count) {
287 operation = retain($(operations, firstObject));
288 }
289 });
290
291 if (operation == NULL) {
292 break;
293 }
294
295 $(operation, waitUntilFinished);
296
297 release(operation);
298 }
299}
300
301#pragma mark - Class lifecycle
302
306static void initialize(Class *clazz) {
307
308 ((ObjectInterface *) clazz->interface)->copy = copy;
309 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
310
311 ((OperationQueueInterface *) clazz->interface)->addOperation = addOperation;
312 ((OperationQueueInterface *) clazz->interface)->addOperationWithFunction = addOperationWithFunction;
313 ((OperationQueueInterface *) clazz->interface)->cancelAllOperations = cancelAllOperations;
314 ((OperationQueueInterface *) clazz->interface)->currentQueue = currentQueue;
315 ((OperationQueueInterface *) clazz->interface)->init = init;
316 ((OperationQueueInterface *) clazz->interface)->initWithMaxConcurrentOperations = initWithMaxConcurrentOperations;
317 ((OperationQueueInterface *) clazz->interface)->operationCount = operationCount;
318 ((OperationQueueInterface *) clazz->interface)->operations = operations;
319 ((OperationQueueInterface *) clazz->interface)->removeOperation = removeOperation;
320 ((OperationQueueInterface *) clazz->interface)->resume = resume;
321 ((OperationQueueInterface *) clazz->interface)->suspend = suspend;
322 ((OperationQueueInterface *) clazz->interface)->waitUntilAllOperationsAreFinished = waitUntilAllOperationsAreFinished;
323}
324
330 static Class *clazz;
331 static Once once;
332
333 do_once(&once, {
334 clazz = _initialize(&(const ClassDef) {
335 .name = "OperationQueue",
336 .superclass = _Object(),
337 .instanceSize = sizeof(OperationQueue),
338 .interfaceSize = sizeof(OperationQueueInterface),
339 .initialize = initialize,
340 });
341 });
342
343 return clazz;
344}
345
346#undef _Class
static ident find(const Array *self, Predicate predicate, ident data)
Definition Array.c:377
static ident objectAtIndex(const Array *self, size_t index)
Definition Array.c:578
static void removeObject(Array *self, const ident obj)
Definition Array.c:642
static void addObject(Array *self, const ident obj)
Definition Array.c:181
static ident firstObject(const Array *self)
Definition Array.c:394
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:356
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:151
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:372
#define obj
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:226
#define super(type, obj, method,...)
static void broadcast(Condition *self)
Definition Condition.c:57
static Data * data(void)
Definition Data.c:286
Class * _Object(void)
Definition Object.c:136
static bool isCancelled(const Operation *self)
Definition Operation.c:140
static void waitUntilFinished(const Operation *self)
Definition Operation.c:208
static Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Definition Operation.c:125
static void start(Operation *self)
Definition Operation.c:185
static void cancel(Operation *self)
Definition Operation.c:73
static bool isReady(const Operation *self)
Definition Operation.c:148
void(* OperationFunction)(Operation *operation)
The function type for Operation execution.
Definition Operation.h:45
Class * _OperationQueue(void)
static void resume(OperationQueue *self)
static void removeOperation(OperationQueue *self, Operation *operation)
static size_t operationCount(const OperationQueue *self)
static OperationQueue * currentQueue(void)
static void addOperation(OperationQueue *self, Operation *operation)
static void waitUntilAllOperationsAreFinished(OperationQueue *self)
static void suspend(OperationQueue *self)
static OperationQueue * initWithMaxConcurrentOperations(OperationQueue *self, size_t maxConcurrentOperations)
static Operation * addOperationWithFunction(OperationQueue *self, OperationFunction function, ident data)
static __thread OperationQueue * _currentQueue
static void cancelAllOperations(OperationQueue *self)
static ident run(Thread *thread)
ThreadFunction for the OperationQueue Threads.
static OperationQueue * init(OperationQueue *self)
static bool isOperationReady(const ident obj, ident data)
Predicate matching the next Operation eligible to start.
static Array * operations(const OperationQueue *self)
static void dealloc(Object *self)
static Object * copy(const Object *self)
static void initialize(Class *clazz)
OperationQueues provide threads of execution for Operations.
static void detach(Thread *self)
Definition Thread.c:90
static ThreadPool * initWithSize(ThreadPool *self, size_t size)
Definition ThreadPool.c:290
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
long Once
The Once type.
Definition Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition Once.h:43
Arrays.
Definition Array.h:56
size_t count
The count of elements.
Definition Array.h:72
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition Class.h:41
The runtime representation of a Class.
Definition Class.h:90
ident interface
The interface of the Class.
Definition Class.h:100
POSIX Threads conditional variables.
Definition Condition.h:44
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
void dealloc(Object *self)
Frees all resources held by this Object.
Definition Array.c:99
An abstraction for discrete units of work, or tasks.
Definition Operation.h:53
bool isFinished
true when this Operation is finished, false otherwise.
Definition Operation.h:116
bool isDispatched
True once an OperationQueue has dispatched this Operation to one of its Threads, so that no other Thr...
Definition Operation.h:106
bool isExecuting
true when this Operation is executing, false otherwise.
Definition Operation.h:111
OperationQueues provide threads of execution for Operations.
size_t operationCount(const OperationQueue *self)
OperationQueue * initWithMaxConcurrentOperations(OperationQueue *self, size_t maxConcurrentOperations)
Initializes this OperationQueue with the given concurrency.
OperationQueue * init(OperationQueue *self)
Initializes this OperationQueue as a serial queue.
bool isSuspended
When true, the queue will not start any new Operations.
POSIX Threads.
Definition Thread.h:53
ident data
The user data.
Definition Thread.h:69
A fixed set of Threads that run ThreadFunctions on behalf of the caller.
Definition ThreadPool.h:64