Objectively
Object oriented framework for C.
Loading...
Searching...
No Matches
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 "OperationQueue.h"
27
28#define _Class _Operation
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 Operation *this = (Operation *) self;
46
47 release(this->locals.condition);
48 release(this->locals.dependencies);
49
50 super(Object, self, dealloc);
51}
52
53#pragma mark - Operation
54
59static void addDependency(Operation *self, Operation *dependency) {
60
61 assert(dependency);
62 assert(dependency != self);
63
64 assert($((Array *) self->locals.dependencies, indexOfObject, dependency) == -1);
65
66 $(self->locals.dependencies, addObject, dependency);
67}
68
73static void cancel(Operation *self) {
74
75 if (self->isCancelled == false) {
76 if (self->isFinished == false) {
77 if (self->isExecuting == false) {
78 self->isCancelled = true;
79
80 OperationQueue *queue = self->locals.queue;
81 if (queue) {
82 synchronized(queue->locals.condition, {
83 $(queue->locals.condition, signal);
84 });
85 }
86 }
87 }
88 }
89}
90
95static Array *dependencies(const Operation *self) {
96
97 ident dependencies = $((Object *) self->locals.dependencies, copy);
98
99 return (Array *) dependencies;
100}
101
106static Operation *init(Operation *self) {
107
108 self = (Operation *) super(Object, self, init);
109 if (self) {
110
111 self->locals.condition = $(alloc(Condition), init);
112 assert(self->locals.condition);
113
114 self->locals.dependencies = $(alloc(Array), init);
115 assert(self->locals.dependencies);
116 }
117
118 return self;
119}
120
126
127 self = $(self, init);
128 if (self) {
129 self->function = function;
130 self->data = data;
131 }
132
133 return self;
134}
135
140static bool isReady(const Operation *self) {
141
142 if (self->isDispatched || self->isExecuting || self->isFinished) {
143 return false;
144 }
145
146 if (self->isCancelled) {
147 return true;
148 }
149
150 const Array *dependencies = (Array *) self->locals.dependencies;
151 for (size_t i = 0; i < dependencies->count; i++) {
152
153 Operation *dependency = $(dependencies, objectAtIndex, i);
154 if (dependency->isFinished == false) {
155 return false;
156 }
157 }
158
159 return true;
160}
161
166static void removeDepdenency(Operation *self, Operation *dependency) {
167
168 assert(dependency);
169
170 $(self->locals.dependencies, removeObject, dependency);
171}
172
177static void start(Operation *self) {
178
179 if (self->isFinished || self->isExecuting) {
180 return;
181 }
182
183 if (self->isCancelled == false) {
184 self->isExecuting = true;
185 self->function(self);
186 self->isExecuting = false;
187 }
188
189 self->isFinished = true;
190
191 synchronized(self->locals.condition, {
192 $(self->locals.condition, broadcast);
193 });
194}
195
200static void waitUntilFinished(const Operation *self) {
201
202 synchronized(self->locals.condition, {
203 while (self->isFinished == false) {
204 $(self->locals.condition, wait);
205 }
206 });
207}
208
209#pragma mark - Class lifecycle
210
214static void initialize(Class *clazz) {
215
216 ((ObjectInterface *) clazz->interface)->copy = copy;
217 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
218
219 ((OperationInterface *) clazz->interface)->addDependency = addDependency;
220 ((OperationInterface *) clazz->interface)->cancel = cancel;
221 ((OperationInterface *) clazz->interface)->dependencies = dependencies;
222 ((OperationInterface *) clazz->interface)->init = init;
223 ((OperationInterface *) clazz->interface)->initWithFunction = initWithFunction;
224 ((OperationInterface *) clazz->interface)->isReady = isReady;
225 ((OperationInterface *) clazz->interface)->removeDependency = removeDepdenency;
226 ((OperationInterface *) clazz->interface)->start = start;
227 ((OperationInterface *) clazz->interface)->waitUntilFinished = waitUntilFinished;
228}
229
235 static Class *clazz;
236 static Once once;
237
238 do_once(&once, {
239 clazz = _initialize(&(const ClassDef) {
240 .name = "Operation",
241 .superclass = _Object(),
242 .instanceSize = sizeof(Operation),
243 .interfaceOffset = offsetof(Operation, interface),
244 .interfaceSize = sizeof(OperationInterface),
246 });
247 });
248
249 return clazz;
250}
251
252#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
static ssize_t indexOfObject(const Array *self, const ident obj)
Definition Array.c:403
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:335
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:129
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:222
#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 Array * dependencies(const Operation *self)
Definition Operation.c:95
static void removeDepdenency(Operation *self, Operation *dependency)
Definition Operation.c:166
static void waitUntilFinished(const Operation *self)
Definition Operation.c:200
static Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Definition Operation.c:125
static void start(Operation *self)
Definition Operation.c:177
static void dealloc(Object *self)
Definition Operation.c:43
static Operation * init(Operation *self)
Definition Operation.c:106
Class * _Operation(void)
Definition Operation.c:234
static Object * copy(const Object *self)
Definition Operation.c:35
static void initialize(Class *clazz)
Definition Operation.c:214
static void cancel(Operation *self)
Definition Operation.c:73
static void addDependency(Operation *self, Operation *dependency)
Definition Operation.c:59
static bool isReady(const Operation *self)
Definition Operation.c:140
void(* OperationFunction)(Operation *operation)
The function type for Operation execution.
Definition Operation.h:45
OperationQueues provide threads 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
Arrays.
Definition Array.h:56
Array * init(Array *self)
Initializes this Array.
Definition Array.c:420
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
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 isCancelled
true when this Operation has been cancelled, false otherwise.
Definition Operation.h:108
ident data
The user data.
Definition Operation.h:98
bool isFinished
true when this Operation is finished, false otherwise.
Definition Operation.h:124
Condition * condition
The Condition enabling waitUntilFinished.
Definition Operation.h:73
bool isDispatched
True once an OperationQueue has dispatched this Operation to one of its Threads, so that no other Thr...
Definition Operation.h:114
OperationFunction function
The Operation function.
Definition Operation.h:103
Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Initializes a synchronous Operation with the given function.
Definition Operation.c:125
Array * dependencies
Contains Operations which must finish before this one can start.
Definition Operation.h:78
bool isReady(const Operation *self)
Definition Operation.c:140
OperationQueue * queue
The OperationQueue this Operation was added to, if any.
Definition Operation.h:86
bool isExecuting
true when this Operation is executing, false otherwise.
Definition Operation.h:119
OperationQueues provide threads of execution for Operations.
Condition * condition
A condition signaled on addOperation and removeOperation.