Objectively
Ultra-lightweight object oriented framework for GNU 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 $(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 assert(function);
83
84 Operation *operation = $(alloc(Operation), initWithFunction, function, data);
85
86 $(self, addOperation, operation);
87
88 return operation;
89}
90
96
97 Array *operations = $(self, operations);
98
99 for (size_t i = 0; i < operations->count; i++) {
101 }
102
104}
105
107
113
114 return _currentQueue;
115}
116
120static ident run(Thread *thread) {
121
122 OperationQueue *self = _currentQueue = thread->data;
123
124 while (thread->isCancelled == false) {
125
126 if (self->isSuspended == false) {
127 Array *operations = NULL;
128
129 retry:
130
132 operations = $(self, operations);
133
134 for (size_t i = 0; i < operations->count; i++) {
135
136 Operation *operation = $(operations, objectAtIndex, i);
137 if ($(operation, isReady)) {
138 $(operation, start);
139 goto retry;
140 }
141 }
142
144 }
145
146 const Time interval = { .tv_usec = 10 };
147 Date *date = $$(Date, dateWithTimeSinceNow, &interval);
148
149 synchronized(self->locals.condition, {
150 $(self->locals.condition, waitUntilDate, date);
151 });
152
153 release(date);
154 }
155
156 return NULL;
157}
158
164
165 self = (OperationQueue *) super(Object, self, init);
166 if (self) {
167
168 self->locals.condition = $(alloc(Condition), init);
169 assert(self->locals.condition);
170
171 self->locals.operations = $(alloc(Array), init);
172 assert(self->locals.operations);
173
174 self->locals.thread = $(alloc(Thread), initWithFunction, run, self);
175 assert(self->locals.thread);
176
177 $(self->locals.thread, start);
178 }
179
180 return self;
181}
182
187static size_t operationCount(const OperationQueue *self) {
188
189 size_t count;
190
191 synchronized(self->locals.condition, {
192 count = ((Array *) self->locals.operations)->count;
193 });
194
195 return count;
196}
197
202static Array *operations(const OperationQueue *self) {
203
205
206 synchronized(self->locals.condition, {
207 operations = $((Object * ) self->locals.operations, copy);
208 });
209
210 return (Array *) operations;
211}
212
217static void removeOperation(OperationQueue *self, Operation *operation) {
218
219 assert(operation);
220 assert(operation->isExecuting == false);
221
222 synchronized(self->locals.condition, {
223 $(self->locals.operations, removeObject, operation);
224 $(self->locals.condition, broadcast);
225 });
226}
227
233
234 synchronized(self->locals.condition, {
235 while (((Array *) self->locals.operations)->count > 0) {
236 $(self->locals.condition, wait);
237 }
238 });
239}
240
241#pragma mark - Class lifecycle
242
246static void initialize(Class *clazz) {
247
248 ((ObjectInterface *) clazz->interface)->copy = copy;
249 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
250
251 ((OperationQueueInterface *) clazz->interface)->addOperation = addOperation;
252 ((OperationQueueInterface *) clazz->interface)->addOperationWithFunction = addOperationWithFunction;
253 ((OperationQueueInterface *) clazz->interface)->cancelAllOperations = cancelAllOperations;
254 ((OperationQueueInterface *) clazz->interface)->currentQueue = currentQueue;
255 ((OperationQueueInterface *) clazz->interface)->init = init;
256 ((OperationQueueInterface *) clazz->interface)->operationCount = operationCount;
257 ((OperationQueueInterface *) clazz->interface)->operations = operations;
258 ((OperationQueueInterface *) clazz->interface)->removeOperation = removeOperation;
259 ((OperationQueueInterface *) clazz->interface)->waitUntilAllOperationsAreFinished = waitUntilAllOperationsAreFinished;
260}
261
267 static Class *clazz;
268 static Once once;
269
270 do_once(&once, {
271 clazz = _initialize(&(const ClassDef) {
272 .name = "OperationQueue",
273 .superclass = _Object(),
274 .instanceSize = sizeof(OperationQueue),
275 .interfaceOffset = offsetof(OperationQueue, interface),
276 .interfaceSize = sizeof(OperationQueueInterface),
278 });
279 });
280
281 return clazz;
282}
283
284#undef _Class
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
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
#define super(type, obj, method,...)
static void broadcast(Condition *self)
Definition Condition.c:57
static bool waitUntilDate(Condition *self, const Date *date)
Definition Condition.c:106
static Data * data(void)
Definition Data.c:286
static Date * dateWithTimeSinceNow(const Time *interval)
Definition Date.c:107
static Date * date(void)
Definition Date.c:98
struct timeval Time
Time (seconds and microseconds).
Definition Date.h:64
Class * _Object(void)
Definition Object.c:136
static Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Definition Operation.c:119
static void start(Operation *self)
Definition Operation.c:171
static void cancel(Operation *self)
Definition Operation.c:74
static bool isReady(const Operation *self)
Definition Operation.c:134
void(* OperationFunction)(Operation *operation)
The function type for Operation execution.
Definition Operation.h:42
Class * _OperationQueue(void)
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 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 Thread.
static OperationQueue * init(OperationQueue *self)
static Array * operations(const OperationQueue *self)
static void dealloc(Object *self)
static Object * copy(const Object *self)
static void initialize(Class *clazz)
OperationQueues provide a thread of execution for Operations.
static void join(Thread *self, ident *status)
Definition Thread.c:130
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:95
ident interface
The interface of the Class.
Definition Class.h:105
POSIX Threads conditional variables.
Definition Condition.h:44
Microsecond-precision immutable dates.
Definition Date.h:74
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:50
bool isCancelled
true when this Operation has been cancelled, false otherwise.
Definition Operation.h:99
bool isFinished
true when this Operation is finished, false otherwise.
Definition Operation.h:109
bool isExecuting
true when this Operation is executing, false otherwise.
Definition Operation.h:104
OperationQueues provide a thread of execution for Operations.
size_t operationCount(const OperationQueue *self)
Thread * thread
The backing Thread.
OperationQueue * init(OperationQueue *self)
Initializes this OperationQueue.
bool isSuspended
When true, the queue will not start any new Operations.
Array * operations
The Operations.
Condition * condition
A condition signaled on addOperation and removeOperation.
POSIX Threads.
Definition Thread.h:53
ident data
The user data.
Definition Thread.h:69
bool isCancelled
true when this Thread has been cancelled, false otherwise.
Definition Thread.h:79