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->condition);
48 release(this->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->dependencies, indexOfObject, dependency) == -1);
65
66 $(self->dependencies, addObject, dependency);
67}
68
73static void cancel(Operation *self) {
74
75 if ($(self, isCancelled) || self->isFinished) {
76 return;
77 }
78
79 __atomic_store_n(&self->isCancelled, true, __ATOMIC_RELEASE);
80
81 if (self->isExecuting == false) {
82 OperationQueue *queue = self->queue;
83 if (queue) {
84 synchronized(queue->condition, {
85 $(queue->condition, signal);
86 });
87 }
88 }
89}
90
95static Array *dependencies(const Operation *self) {
96
97 ident dependencies = $((Object *) self->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->condition = $(alloc(Condition), init);
112 assert(self->condition);
113
114 self->dependencies = $(alloc(Array), init);
115 assert(self->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 isCancelled(const Operation *self) {
141 return __atomic_load_n(&self->isCancelled, __ATOMIC_ACQUIRE);
142}
143
148static bool isReady(const Operation *self) {
149
150 if (self->isDispatched || self->isExecuting || self->isFinished) {
151 return false;
152 }
153
154 if ($(self, isCancelled)) {
155 return true;
156 }
157
158 const Array *dependencies = (Array *) self->dependencies;
159 for (size_t i = 0; i < dependencies->count; i++) {
160
161 Operation *dependency = $(dependencies, objectAtIndex, i);
162 if (dependency->isFinished == false) {
163 return false;
164 }
165 }
166
167 return true;
168}
169
174static void removeDepdenency(Operation *self, Operation *dependency) {
175
176 assert(dependency);
177
178 $(self->dependencies, removeObject, dependency);
179}
180
185static void start(Operation *self) {
186
187 if (self->isFinished || self->isExecuting) {
188 return;
189 }
190
191 if ($(self, isCancelled) == false) {
192 self->isExecuting = true;
193 self->function(self);
194 self->isExecuting = false;
195 }
196
197 self->isFinished = true;
198
199 synchronized(self->condition, {
200 $(self->condition, broadcast);
201 });
202}
203
208static void waitUntilFinished(const Operation *self) {
209
210 synchronized(self->condition, {
211 while (self->isFinished == false) {
212 $(self->condition, wait);
213 }
214 });
215}
216
217#pragma mark - Class lifecycle
218
222static void initialize(Class *clazz) {
223
224 ((ObjectInterface *) clazz->interface)->copy = copy;
225 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
226
227 ((OperationInterface *) clazz->interface)->addDependency = addDependency;
228 ((OperationInterface *) clazz->interface)->cancel = cancel;
229 ((OperationInterface *) clazz->interface)->dependencies = dependencies;
230 ((OperationInterface *) clazz->interface)->init = init;
231 ((OperationInterface *) clazz->interface)->initWithFunction = initWithFunction;
232 ((OperationInterface *) clazz->interface)->isCancelled = isCancelled;
233 ((OperationInterface *) clazz->interface)->isReady = isReady;
234 ((OperationInterface *) clazz->interface)->removeDependency = removeDepdenency;
235 ((OperationInterface *) clazz->interface)->start = start;
236 ((OperationInterface *) clazz->interface)->waitUntilFinished = waitUntilFinished;
237}
238
244 static Class *clazz;
245 static Once once;
246
247 do_once(&once, {
248 clazz = _initialize(&(const ClassDef) {
249 .name = "Operation",
250 .superclass = _Object(),
251 .instanceSize = sizeof(Operation),
252 .interfaceSize = sizeof(OperationInterface),
253 .initialize = initialize,
254 });
255 });
256
257 return clazz;
258}
259
260#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:356
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:151
#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 Array * dependencies(const Operation *self)
Definition Operation.c:95
static bool isCancelled(const Operation *self)
Definition Operation.c:140
static void removeDepdenency(Operation *self, Operation *dependency)
Definition Operation.c:174
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 dealloc(Object *self)
Definition Operation.c:43
static Operation * init(Operation *self)
Definition Operation.c:106
Class * _Operation(void)
Definition Operation.c:243
static Object * copy(const Object *self)
Definition Operation.c:35
static void initialize(Class *clazz)
Definition Operation.c:222
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:148
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: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
ident data
The user data.
Definition Operation.h:81
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
OperationFunction function
The Operation function.
Definition Operation.h:92
Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Initializes a synchronous Operation with the given function.
Definition Operation.c:125
bool isExecuting
true when this Operation is executing, false otherwise.
Definition Operation.h:111
OperationQueues provide threads of execution for Operations.