Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Operation.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 "Operation.h"
27#include "OperationQueue.h"
28
29#define _Class _Operation
30
31#pragma mark - Object
32
36static Object *copy(const Object *self) {
37
38 return NULL;
39}
40
44static void dealloc(Object *self) {
45
46 Operation *this = (Operation *) self;
47
48 release(this->locals.condition);
49 release(this->locals.dependencies);
50
51 super(Object, self, dealloc);
52}
53
54#pragma mark - Operation
55
60static void addDependency(Operation *self, Operation *dependency) {
61
62 assert(dependency);
63 assert(dependency != self);
64
65 assert($((Array *) self->locals.dependencies, indexOfObject, dependency) == -1);
66
67 $(self->locals.dependencies, addObject, dependency);
68}
69
74static void cancel(Operation *self) {
75
76 if (self->isCancelled == false) {
77 if (self->isFinished == false) {
78 if (self->isExecuting == false) {
79 self->isCancelled = true;
80 }
81 }
82 }
83}
84
89static Array *dependencies(const Operation *self) {
90
91 ident dependencies = $((Object *) self->locals.dependencies, copy);
92
93 return (Array *) dependencies;
94}
95
100static Operation *init(Operation *self) {
101
102 self = (Operation *) super(Object, self, init);
103 if (self) {
104
105 self->locals.condition = $(alloc(Condition), init);
106 assert(self->locals.condition);
107
108 self->locals.dependencies = $(alloc(MutableArray), init);
109 assert(self->locals.dependencies);
110 }
111
112 return self;
113}
114
120
121 self = $(self, init);
122 if (self) {
123 self->function = function;
124 self->data = data;
125 }
126
127 return self;
128}
129
134static _Bool isReady(const Operation *self) {
135
136 if (self->isExecuting || self->isFinished) {
137 return false;
138 }
139
140 if (self->isCancelled) {
141 return true;
142 }
143
144 const Array *dependencies = (Array *) self->locals.dependencies;
145 for (size_t i = 0; i < dependencies->count; i++) {
146
147 Operation *dependency = $(dependencies, objectAtIndex, i);
148 if (dependency->isFinished == false) {
149 return false;
150 }
151 }
152
153 return true;
154}
155
160static void removeDepdenency(Operation *self, Operation *dependency) {
161
162 assert(dependency);
163
164 $(self->locals.dependencies, removeObject, dependency);
165}
166
171static void start(Operation *self) {
172
173 if (self->isFinished || self->isExecuting) {
174 return;
175 }
176
177 if (self->isCancelled == false) {
178 self->isExecuting = true;
179 self->function(self);
180 self->isExecuting = false;
181 }
182
183 self->isFinished = true;
184
185 synchronized(self->locals.condition, {
186 $(self->locals.condition, broadcast);
187 });
188
190 if (currentQueue) {
192 }
193}
194
199static void waitUntilFinished(const Operation *self) {
200
201 synchronized(self->locals.condition, {
202 while (self->isFinished == false) {
203 $(self->locals.condition, wait);
204 }
205 });
206}
207
208#pragma mark - Class lifecycle
209
213static void initialize(Class *clazz) {
214
215 ((ObjectInterface *) clazz->interface)->copy = copy;
216 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
217
218 ((OperationInterface *) clazz->interface)->addDependency = addDependency;
219 ((OperationInterface *) clazz->interface)->cancel = cancel;
220 ((OperationInterface *) clazz->interface)->dependencies = dependencies;
221 ((OperationInterface *) clazz->interface)->init = init;
222 ((OperationInterface *) clazz->interface)->initWithFunction = initWithFunction;
223 ((OperationInterface *) clazz->interface)->isReady = isReady;
224 ((OperationInterface *) clazz->interface)->removeDependency = removeDepdenency;
225 ((OperationInterface *) clazz->interface)->start = start;
226 ((OperationInterface *) clazz->interface)->waitUntilFinished = waitUntilFinished;
227}
228
234 static Class *clazz;
235 static Once once;
236
237 do_once(&once, {
238 clazz = _initialize(&(const ClassDef) {
239 .name = "Operation",
240 .superclass = _Object(),
241 .instanceSize = sizeof(Operation),
242 .interfaceOffset = offsetof(Operation, interface),
243 .interfaceSize = sizeof(OperationInterface),
245 });
246 });
247
248 return clazz;
249}
250
251#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,...)
static void removeDepdenency(Operation *self, Operation *dependency)
Definition: Operation.c:160
static void initialize(Class *clazz)
Definition: Operation.c:213
An abstraction for discrete units of work, or tasks.
void(* OperationFunction)(Operation *operation)
The function type for Operation execution.
Definition: Operation.h:42
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
ssize_t indexOfObject(const Array *self, const ident obj)
Definition: Array.c:271
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.
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
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
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75
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 * init(Object *self)
Initializes this Object.
Definition: Object.c:83
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
ident data
The user data.
Definition: Operation.h:89
Condition * condition
The Condition enabling waitUntilFinished.
Definition: Operation.h:71
_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
OperationFunction function
The Operation function.
Definition: Operation.h:94
void cancel(Operation *self)
Cancels this Operation, allowing it to complete immediately.
Definition: Operation.c:74
MutableArray * dependencies
Contains Operations which must finish before this one can start.
Definition: Operation.h:76
Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Initializes a synchronous Operation with the given function.
Definition: Operation.c:119
Class * _Operation(void)
The Operation archetype.
Definition: Operation.c:233
Array * dependencies(const Operation *self)
Definition: Operation.c:89
_Bool isExecuting
true when this Operation is executing, false otherwise.
Definition: Operation.h:104
void addDependency(Operation *self, Operation *dependency)
Makes this Operation dependent on the completion of dependency.
Definition: Operation.c:60
_Bool isFinished
true when this Operation is finished, false otherwise.
Definition: Operation.h:109
void waitUntilFinished(const Operation *self)
Blocks the current thread until this Operation isFinished.
Definition: Operation.c:199
OperationQueues provide a thread of execution for Operations.
OperationQueue * currentQueue(void)
void removeOperation(OperationQueue *self, Operation *operation)
Removes the Operation from this queue.