Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
OperationQueue.c File Reference
#include <assert.h>
#include "OperationQueue.h"

Go to the source code of this file.

Macros

#define _Class   _OperationQueue
 

Functions

Class_OperationQueue (void)
 
static void addOperation (OperationQueue *self, Operation *operation)
 
static OperationaddOperationWithFunction (OperationQueue *self, OperationFunction function, ident data)
 
static void cancelAllOperations (OperationQueue *self)
 
static Objectcopy (const Object *self)
 
static OperationQueuecurrentQueue (void)
 
static void dealloc (Object *self)
 
static OperationQueueinit (OperationQueue *self)
 
static void initialize (Class *clazz)
 
static size_t operationCount (const OperationQueue *self)
 
static Arrayoperations (const OperationQueue *self)
 
static void removeOperation (OperationQueue *self, Operation *operation)
 
static ident run (Thread *thread)
 ThreadFunction for the OperationQueue Thread.
 
static void waitUntilAllOperationsAreFinished (OperationQueue *self)
 

Variables

static __thread OperationQueue_currentQueue
 

Macro Definition Documentation

◆ _Class

#define _Class   _OperationQueue

Definition at line 28 of file OperationQueue.c.

Function Documentation

◆ _OperationQueue()

Class * _OperationQueue ( void  )

Definition at line 266 of file OperationQueue.c.

266 {
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}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
Class * _Object(void)
Definition Object.c:136
static void initialize(Class *clazz)
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
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
OperationQueues provide a thread of execution for Operations.

◆ addOperation()

static void addOperation ( OperationQueue self,
Operation operation 
)
static

Definition at line 63 of file OperationQueue.c.

63 {
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}
static void addObject(Array *self, const ident obj)
Definition Array.c:181
static void broadcast(Condition *self)
Definition Condition.c:57
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
Array * operations
The Operations.
Condition * condition
A condition signaled on addOperation and removeOperation.

◆ addOperationWithFunction()

static Operation * addOperationWithFunction ( OperationQueue self,
OperationFunction  function,
ident  data 
)
static

Definition at line 80 of file OperationQueue.c.

80 {
81
82 assert(function);
83
84 Operation *operation = $(alloc(Operation), initWithFunction, function, data);
85
86 $(self, addOperation, operation);
87
88 return operation;
89}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
static Data * data(void)
Definition Data.c:286
static Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Definition Operation.c:119
static void addOperation(OperationQueue *self, Operation *operation)
An abstraction for discrete units of work, or tasks.
Definition Operation.h:50

◆ cancelAllOperations()

static void cancelAllOperations ( OperationQueue self)
static

Definition at line 95 of file OperationQueue.c.

95 {
96
97 Array *operations = $(self, operations);
98
99 for (size_t i = 0; i < operations->count; i++) {
101 }
102
104}
static ident objectAtIndex(const Array *self, size_t index)
Definition Array.c:578
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
static void cancel(Operation *self)
Definition Operation.c:74
static Array * operations(const OperationQueue *self)
Arrays.
Definition Array.h:56
size_t count
The count of elements.
Definition Array.h:72

◆ copy()

static Object * copy ( const Object self)
static
See also
Object::copy(const Object *)

Definition at line 35 of file OperationQueue.c.

35 {
36
37 return NULL;
38}

◆ currentQueue()

static OperationQueue * currentQueue ( void  )
static

Definition at line 112 of file OperationQueue.c.

112 {
113
114 return _currentQueue;
115}
static __thread OperationQueue * _currentQueue

◆ dealloc()

static void dealloc ( Object self)
static
See also
Object::dealloc(Object *)

Definition at line 43 of file OperationQueue.c.

43 {
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}
#define super(type, obj, method,...)
static void dealloc(Object *self)
static void join(Thread *self, ident *status)
Definition Thread.c:130
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ init()

static OperationQueue * init ( OperationQueue self)
static

Definition at line 163 of file OperationQueue.c.

163 {
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}
static void start(Operation *self)
Definition Operation.c:171
static ident run(Thread *thread)
ThreadFunction for the OperationQueue Thread.
static OperationQueue * init(OperationQueue *self)
POSIX Threads conditional variables.
Definition Condition.h:44
Thread * thread
The backing Thread.
POSIX Threads.
Definition Thread.h:53

◆ initialize()

static void initialize ( Class clazz)
static
See also
Class::initialize(Class *)

Definition at line 246 of file OperationQueue.c.

246 {
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}
static void removeOperation(OperationQueue *self, Operation *operation)
static size_t operationCount(const OperationQueue *self)
static OperationQueue * currentQueue(void)
static void waitUntilAllOperationsAreFinished(OperationQueue *self)
static Operation * addOperationWithFunction(OperationQueue *self, OperationFunction function, ident data)
static void cancelAllOperations(OperationQueue *self)
static Object * copy(const Object *self)
ident interface
The interface of the Class.
Definition Class.h:105
void dealloc(Object *self)
Frees all resources held by this Object.
Definition Array.c:99
size_t operationCount(const OperationQueue *self)
OperationQueue * init(OperationQueue *self)
Initializes this OperationQueue.

◆ operationCount()

static size_t operationCount ( const OperationQueue self)
static

Definition at line 187 of file OperationQueue.c.

187 {
188
189 size_t count;
190
191 synchronized(self->locals.condition, {
192 count = ((Array *) self->locals.operations)->count;
193 });
194
195 return count;
196}

◆ operations()

static Array * operations ( const OperationQueue self)
static

Definition at line 202 of file OperationQueue.c.

202 {
203
205
206 synchronized(self->locals.condition, {
207 operations = $((Object * ) self->locals.operations, copy);
208 });
209
210 return (Array *) operations;
211}
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49

◆ removeOperation()

static void removeOperation ( OperationQueue self,
Operation operation 
)
static

Definition at line 217 of file OperationQueue.c.

217 {
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}
static void removeObject(Array *self, const ident obj)
Definition Array.c:642

◆ run()

static ident run ( Thread thread)
static

ThreadFunction for the OperationQueue Thread.

Definition at line 120 of file OperationQueue.c.

120 {
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}
static bool waitUntilDate(Condition *self, const Date *date)
Definition Condition.c:106
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
static bool isReady(const Operation *self)
Definition Operation.c:134
Microsecond-precision immutable dates.
Definition Date.h:74
bool isSuspended
When true, the queue will not start any new Operations.
ident data
The user data.
Definition Thread.h:69
bool isCancelled
true when this Thread has been cancelled, false otherwise.
Definition Thread.h:79

◆ waitUntilAllOperationsAreFinished()

static void waitUntilAllOperationsAreFinished ( OperationQueue self)
static

Definition at line 232 of file OperationQueue.c.

232 {
233
234 synchronized(self->locals.condition, {
235 while (((Array *) self->locals.operations)->count > 0) {
236 $(self->locals.condition, wait);
237 }
238 });
239}

Variable Documentation

◆ _currentQueue

__thread OperationQueue* _currentQueue
static

Definition at line 106 of file OperationQueue.c.