Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
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 $(this->locals.thread, cancel);
48 $(this->locals.thread, join, NULL);
49
50 release(this->locals.thread);
51 release(this->locals.condition);
52 release(this->locals.operations);
53
54 super(Object, self, dealloc);
55}
56
57#pragma mark - OperationQueue
58
63static void addOperation(OperationQueue *self, Operation *operation) {
64
65 assert(operation);
66 assert(operation->isCancelled == false);
67 assert(operation->isExecuting == false);
68 assert(operation->isFinished == false);
69
70 synchronized(self->locals.condition, {
71 $(self->locals.operations, addObject, operation);
72 $(self->locals.condition, broadcast);
73 });
74}
75
81
82 Array *operations = $(self, operations);
83
84 for (size_t i = 0; i < operations->count; i++) {
86 }
87
89}
90
92
98
99 return _currentQueue;
100}
101
105static ident run(Thread *thread) {
106
107 OperationQueue *self = _currentQueue = thread->data;
108
109 while (thread->isCancelled == false) {
110
111 if (self->isSuspended == false) {
112 Array *operations = NULL;
113
114 retry:
115
117 operations = $(self, operations);
118
119 for (size_t i = 0; i < operations->count; i++) {
120
121 Operation *operation = $(operations, objectAtIndex, i);
122 if ($(operation, isReady)) {
123 $(operation, start);
124 goto retry;
125 }
126 }
127
129 }
130
131 const Time interval = { .tv_usec = 10 };
132 Date *date = $$(Date, dateWithTimeSinceNow, &interval);
133
134 synchronized(self->locals.condition, {
135 $(self->locals.condition, waitUntilDate, date);
136 });
137
138 release(date);
139 }
140
141 return NULL;
142}
143
149
150 self = (OperationQueue *) super(Object, self, init);
151 if (self) {
152
153 self->locals.condition = $(alloc(Condition), init);
154 assert(self->locals.condition);
155
156 self->locals.operations = $(alloc(MutableArray), init);
157 assert(self->locals.operations);
158
159 self->locals.thread = $(alloc(Thread), initWithFunction, run, self);
160 assert(self->locals.thread);
161
162 $(self->locals.thread, start);
163 }
164
165 return self;
166}
167
172static size_t operationCount(const OperationQueue *self) {
173
174 size_t count;
175
176 synchronized(self->locals.condition, {
177 count = ((Array *) self->locals.operations)->count;
178 });
179
180 return count;
181}
182
187static Array *operations(const OperationQueue *self) {
188
190
191 synchronized(self->locals.condition, {
192 operations = $((Object * ) self->locals.operations, copy);
193 });
194
195 return (Array *) operations;
196}
197
202static void removeOperation(OperationQueue *self, Operation *operation) {
203
204 assert(operation);
205 assert(operation->isExecuting == false);
206
207 synchronized(self->locals.condition, {
208 $(self->locals.operations, removeObject, operation);
209 $(self->locals.condition, broadcast);
210 });
211}
212
218
219 Array *operations = (Array *) self->locals.operations;
220 while (operations->count > 0) {
221
222 synchronized(self->locals.condition, {
223 $(self->locals.condition, wait);
224 });
225 }
226}
227
228#pragma mark - Class lifecycle
229
233static void initialize(Class *clazz) {
234
235 ((ObjectInterface *) clazz->interface)->copy = copy;
236 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
237
238 ((OperationQueueInterface *) clazz->interface)->addOperation = addOperation;
239 ((OperationQueueInterface *) clazz->interface)->cancelAllOperations = cancelAllOperations;
240 ((OperationQueueInterface *) clazz->interface)->currentQueue = currentQueue;
241 ((OperationQueueInterface *) clazz->interface)->init = init;
242 ((OperationQueueInterface *) clazz->interface)->operationCount = operationCount;
243 ((OperationQueueInterface *) clazz->interface)->operations = operations;
244 ((OperationQueueInterface *) clazz->interface)->removeOperation = removeOperation;
245 ((OperationQueueInterface *) clazz->interface)->waitUntilAllOperationsAreFinished = waitUntilAllOperationsAreFinished;
246}
247
253 static Class *clazz;
254 static Once once;
255
256 do_once(&once, {
257 clazz = _initialize(&(const ClassDef) {
258 .name = "OperationQueue",
259 .superclass = _Object(),
260 .instanceSize = sizeof(OperationQueue),
261 .interfaceOffset = offsetof(OperationQueue, interface),
262 .interfaceSize = sizeof(OperationQueueInterface),
264 });
265 });
266
267 return clazz;
268}
269
270#undef _Class
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
#define super(type, obj, method,...)
struct timeval Time
Time (seconds and microseconds).
Definition: Date.h:60
static __thread OperationQueue * _currentQueue
static ident run(Thread *thread)
ThreadFunction for the OperationQueue Thread.
static void initialize(Class *clazz)
OperationQueues provide a thread of execution for Operations.
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
Immutable arrays.
Definition: Array.h:56
ident objectAtIndex(const Array *self, int index)
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:95
ident interface
The interface of the Class.
Definition: Class.h:105
POSIX Threads conditional variables.
Definition: Condition.h:44
void wait(Condition *self)
Waits indefinitely for this Condition to be signaled.
_Bool waitUntilDate(Condition *self, const Date *date)
Waits until the specified Date for this Condition to be signaled.
Definition: Condition.c:106
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
void broadcast(Condition *self)
Signals all Threads waiting on this Condition.
Definition: Condition.c:57
Microsecond-precision immutable dates.
Definition: Date.h:70
Date * dateWithTimeSinceNow(const Time interval)
Returns a new Date with the given Time since now.
Date * date(void)
Returns a new Date with the current Time.
Definition: Date.c:98
Mutable arrays.
Definition: MutableArray.h:40
void addObject(MutableArray *self, const ident obj)
Adds the specified Object to this MutableArray.
Definition: MutableArray.c:99
void removeObject(MutableArray *self, const ident obj)
Removes the specified Object from this MutableArray.
Definition: MutableArray.c:270
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50
An abstraction for discrete units of work, or tasks.
Definition: Operation.h:50
_Bool isReady(const Operation *self)
Definition: Operation.c:134
void start(Operation *self)
Starts this Operation.
Definition: Operation.c:171
_Bool isCancelled
true when this Operation has been cancelled, false otherwise.
Definition: Operation.h:99
void cancel(Operation *self)
Cancels this Operation, allowing it to complete immediately.
Definition: Operation.c:74
Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Initializes a synchronous Operation with the given function.
Definition: Operation.c:119
_Bool isExecuting
true when this Operation is executing, false otherwise.
Definition: Operation.h:104
_Bool isFinished
true when this Operation is finished, false otherwise.
Definition: Operation.h:109
OperationQueues provide a thread of execution for Operations.
Class * _OperationQueue(void)
The OperationQueue archetype.
OperationQueue * currentQueue(void)
_Bool isSuspended
When true, the queue will not start any new Operations.
void removeOperation(OperationQueue *self, Operation *operation)
Removes the Operation from this queue.
void cancelAllOperations(OperationQueue *self)
Cancels all pending Operations residing within this Queue.
size_t operationCount(const OperationQueue *self)
Thread * thread
The backing Thread.
OperationQueue * init(OperationQueue *self)
Initializes this OperationQueue.
void waitUntilAllOperationsAreFinished(OperationQueue *self)
Waits until all Operations submitted to this queue have finished.
Condition * condition
A condition signaled on addOperation and removeOperation.
void addOperation(OperationQueue *self, Operation *operation)
Adds an Operation to this queue.
Array * operations(const OperationQueue *self)
MutableArray * operations
The Operations.
POSIX Threads.
Definition: Thread.h:53
ident data
The user data.
Definition: Thread.h:69
void join(Thread *self, ident *status)
Wait for the specified Thread to terminate.
Definition: Thread.c:130
_Bool isCancelled
true when this Thread has been cancelled, false otherwise.
Definition: Thread.h:79