Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions | Variables
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 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. More...
 
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 252 of file OperationQueue.c.

252 {
253 static Class *clazz;
254 static Once once;
255
256 do_once(&once, {
257 clazz = _initialize(&(const ClassDef) {
258 .name = "OperationQueue",
259 .superclass = _Object(),
260 .instanceSize = sizeof(OperationQueue),
261 .interfaceOffset = offsetof(OperationQueue, interface),
262 .interfaceSize = sizeof(OperationQueueInterface),
264 });
265 });
266
267 return clazz;
268}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
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
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
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}
void broadcast(Condition *self)
Signals all Threads waiting on this Condition.
Definition: Condition.c:57
void addObject(MutableArray *self, const ident obj)
Adds the specified Object to this MutableArray.
Definition: MutableArray.c:99
_Bool isCancelled
true when this Operation has been cancelled, false otherwise.
Definition: Operation.h:99
_Bool isExecuting
true when this Operation is executing, false otherwise.
Definition: Operation.h:104
_Bool isFinished
true when this Operation is finished, false otherwise.
Definition: Operation.h:109
Condition * condition
A condition signaled on addOperation and removeOperation.
MutableArray * operations
The Operations.

◆ cancelAllOperations()

static void cancelAllOperations ( OperationQueue self)
static

Definition at line 80 of file OperationQueue.c.

80 {
81
82 Array *operations = $(self, operations);
83
84 for (size_t i = 0; i < operations->count; i++) {
86 }
87
89}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
Immutable arrays.
Definition: Array.h:56
ident objectAtIndex(const Array *self, int index)
size_t count
The count of elements.
Definition: Array.h:72
An abstraction for discrete units of work, or tasks.
Definition: Operation.h:50
void cancel(Operation *self)
Cancels this Operation, allowing it to complete immediately.
Definition: Operation.c:74
Array * operations(const OperationQueue *self)

◆ 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 97 of file OperationQueue.c.

97 {
98
99 return _currentQueue;
100}
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,...)
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:50
void join(Thread *self, ident *status)
Wait for the specified Thread to terminate.
Definition: Thread.c:130

◆ init()

static OperationQueue * init ( OperationQueue self)
static

Definition at line 148 of file OperationQueue.c.

148 {
149
150 self = (OperationQueue *) super(Object, self, init);
151 if (self) {
152
153 self->locals.condition = $(alloc(Condition), init);
154 assert(self->locals.condition);
155
156 self->locals.operations = $(alloc(MutableArray), init);
157 assert(self->locals.operations);
158
159 self->locals.thread = $(alloc(Thread), initWithFunction, run, self);
160 assert(self->locals.thread);
161
162 $(self->locals.thread, start);
163 }
164
165 return self;
166}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
static ident run(Thread *thread)
ThreadFunction for the OperationQueue Thread.
POSIX Threads conditional variables.
Definition: Condition.h:44
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Mutable arrays.
Definition: MutableArray.h:40
void start(Operation *self)
Starts this Operation.
Definition: Operation.c:171
Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Initializes a synchronous Operation with the given function.
Definition: Operation.c:119
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 233 of file OperationQueue.c.

233 {
234
235 ((ObjectInterface *) clazz->interface)->copy = copy;
236 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
237
238 ((OperationQueueInterface *) clazz->interface)->addOperation = addOperation;
239 ((OperationQueueInterface *) clazz->interface)->cancelAllOperations = cancelAllOperations;
240 ((OperationQueueInterface *) clazz->interface)->currentQueue = currentQueue;
241 ((OperationQueueInterface *) clazz->interface)->init = init;
242 ((OperationQueueInterface *) clazz->interface)->operationCount = operationCount;
243 ((OperationQueueInterface *) clazz->interface)->operations = operations;
244 ((OperationQueueInterface *) clazz->interface)->removeOperation = removeOperation;
245 ((OperationQueueInterface *) clazz->interface)->waitUntilAllOperationsAreFinished = waitUntilAllOperationsAreFinished;
246}
ident interface
The interface of the Class.
Definition: Class.h:105
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
OperationQueue * currentQueue(void)
void removeOperation(OperationQueue *self, Operation *operation)
Removes the Operation from this queue.
void cancelAllOperations(OperationQueue *self)
Cancels all pending Operations residing within this Queue.
size_t operationCount(const OperationQueue *self)
OperationQueue * init(OperationQueue *self)
Initializes this OperationQueue.
void waitUntilAllOperationsAreFinished(OperationQueue *self)
Waits until all Operations submitted to this queue have finished.
void addOperation(OperationQueue *self, Operation *operation)
Adds an Operation to this queue.

◆ operationCount()

static size_t operationCount ( const OperationQueue self)
static

Definition at line 172 of file OperationQueue.c.

172 {
173
174 size_t count;
175
176 synchronized(self->locals.condition, {
177 count = ((Array *) self->locals.operations)->count;
178 });
179
180 return count;
181}

◆ operations()

static Array * operations ( const OperationQueue self)
static

Definition at line 187 of file OperationQueue.c.

187 {
188
190
191 synchronized(self->locals.condition, {
192 operations = $((Object * ) self->locals.operations, copy);
193 });
194
195 return (Array *) operations;
196}
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 202 of file OperationQueue.c.

202 {
203
204 assert(operation);
205 assert(operation->isExecuting == false);
206
207 synchronized(self->locals.condition, {
208 $(self->locals.operations, removeObject, operation);
209 $(self->locals.condition, broadcast);
210 });
211}
void removeObject(MutableArray *self, const ident obj)
Removes the specified Object from this MutableArray.
Definition: MutableArray.c:270

◆ run()

static ident run ( Thread thread)
static

ThreadFunction for the OperationQueue Thread.

Definition at line 105 of file OperationQueue.c.

105 {
106
107 OperationQueue *self = _currentQueue = thread->data;
108
109 while (thread->isCancelled == false) {
110
111 if (self->isSuspended == false) {
112 Array *operations = NULL;
113
114 retry:
115
117 operations = $(self, operations);
118
119 for (size_t i = 0; i < operations->count; i++) {
120
121 Operation *operation = $(operations, objectAtIndex, i);
122 if ($(operation, isReady)) {
123 $(operation, start);
124 goto retry;
125 }
126 }
127
129 }
130
131 const Time interval = { .tv_usec = 10 };
132 Date *date = $$(Date, dateWithTimeSinceNow, &interval);
133
134 synchronized(self->locals.condition, {
135 $(self->locals.condition, waitUntilDate, date);
136 });
137
138 release(date);
139 }
140
141 return NULL;
142}
struct timeval Time
Time (seconds and microseconds).
Definition: Date.h:60
_Bool waitUntilDate(Condition *self, const Date *date)
Waits until the specified Date for this Condition to be signaled.
Definition: Condition.c:106
Microsecond-precision immutable dates.
Definition: Date.h:70
Date * dateWithTimeSinceNow(const Time interval)
Returns a new Date with the given Time since now.
Date * date(void)
Returns a new Date with the current Time.
Definition: Date.c:98
_Bool isReady(const Operation *self)
Definition: Operation.c:134
_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 217 of file OperationQueue.c.

217 {
218
219 Array *operations = (Array *) self->locals.operations;
220 while (operations->count > 0) {
221
222 synchronized(self->locals.condition, {
223 $(self->locals.condition, wait);
224 });
225 }
226}
void wait(Condition *self)
Waits indefinitely for this Condition to be signaled.

Variable Documentation

◆ _currentQueue

__thread OperationQueue* _currentQueue
static

Definition at line 91 of file OperationQueue.c.