Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
OperationQueue Struct Reference

#include <OperationQueue.h>

Overview

OperationQueues provide a thread of execution for Operations.

Definition at line 44 of file OperationQueue.h.

Inheritance diagram for OperationQueue:
Object

Properties

_Bool isSuspended
 When true, the queue will not start any new Operations. More...
 
Object object
 The superclass. More...
 
Conditioncondition
 A condition signaled on addOperation and removeOperation. More...
 
MutableArrayoperations
 The Operations. More...
 
Threadthread
 The backing Thread. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_OperationQueue (void)
 The OperationQueue archetype. More...
 
void addOperation (OperationQueue *self, Operation *operation)
 Adds an Operation to this queue. More...
 
void cancelAllOperations (OperationQueue *self)
 Cancels all pending Operations residing within this Queue. More...
 
OperationQueuecurrentQueue (void)
 
OperationQueueinit (OperationQueue *self)
 Initializes this OperationQueue. More...
 
size_t operationCount (const OperationQueue *self)
 
Arrayoperations (const OperationQueue *self)
 
void removeOperation (OperationQueue *self, Operation *operation)
 Removes the Operation from this queue. More...
 
void waitUntilAllOperationsAreFinished (OperationQueue *self)
 Waits until all Operations submitted to this queue have finished. More...
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype. More...
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object. More...
 
void dealloc (Object *self)
 Frees all resources held by this Object. More...
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object. More...
 
_Bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object. More...
 
_Bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership. More...
 

Protected Attributes

OperationQueueInterface * interface
 The interface. More...
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface. More...
 

Property Details

◆ condition

Condition* OperationQueue::condition

A condition signaled on addOperation and removeOperation.

Definition at line 65 of file OperationQueue.h.

◆ interface

OperationQueueInterface* OperationQueue::interface
protected

The interface.

Definition at line 55 of file OperationQueue.h.

◆ isSuspended

_Bool OperationQueue::isSuspended

When true, the queue will not start any new Operations.

Definition at line 82 of file OperationQueue.h.

◆ object

Object OperationQueue::object

The superclass.

Definition at line 49 of file OperationQueue.h.

◆ operations

MutableArray* OperationQueue::operations

The Operations.

Definition at line 70 of file OperationQueue.h.

◆ thread

Thread* OperationQueue::thread

The backing Thread.

Definition at line 75 of file OperationQueue.h.

Method Details

◆ _OperationQueue()

Class * _OperationQueue ( void  )

The OperationQueue archetype.

Returns
The OperationQueue Class.

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 * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
OperationQueues provide a thread of execution for Operations.
OperationQueueInterface * interface
The interface.

◆ addOperation()

void addOperation ( OperationQueue self,
Operation operation 
)

Adds an Operation to this queue.

Parameters
selfThe OperationQueue.
operationThe Operation to add.

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 broadcast(Condition *self)
Definition: Condition.c:57
static void addObject(MutableArray *self, const ident obj)
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()

void cancelAllOperations ( OperationQueue self)

Cancels all pending Operations residing within this Queue.

Parameters
selfThe OperationQueue.

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}
static ident objectAtIndex(const Array *self, size_t index)
Definition: Array.c:394
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
static void cancel(Operation *self)
Definition: Operation.c:74
Immutable arrays.
Definition: Array.h:56
size_t count
The count of elements.
Definition: Array.h:72
An abstraction for discrete units of work, or tasks.
Definition: Operation.h:50

◆ currentQueue()

OperationQueue * currentQueue ( void  )
Returns
The current OperationQueue, or NULL if none can be determined.
Remarks
This method should only be called from a synchronous Operation that was dispatched via an OperationQueue. This method uses thread-local storage.

Definition at line 97 of file OperationQueue.c.

97 {
98
99 return _currentQueue;
100}
static __thread OperationQueue * _currentQueue

◆ init()

OperationQueue * init ( OperationQueue self)

Initializes this OperationQueue.

Parameters
selfThe OperationQueue.
Returns
The initialized OperationQueue, or NULL on error.

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
#define super(type, obj, method,...)
static Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Definition: Operation.c:119
static void start(Operation *self)
Definition: Operation.c:171
static ident run(Thread *thread)
ThreadFunction for the OperationQueue Thread.
POSIX Threads conditional variables.
Definition: Condition.h:44
Mutable arrays.
Definition: MutableArray.h:40
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Thread * thread
The backing Thread.
OperationQueue * init(OperationQueue *self)
Initializes this OperationQueue.
POSIX Threads.
Definition: Thread.h:53

◆ operationCount()

size_t operationCount ( const OperationQueue self)
Parameters
selfThe OperationQueue.
Returns
The instantaneous count of this OperationQueue's Operations.

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()

Array * operations ( const OperationQueue self)
Parameters
selfThe OperationQueue.
Returns
An instantaneous copy of this OperationQueue's Operations.

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
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40

◆ removeOperation()

void removeOperation ( OperationQueue self,
Operation operation 
)

Removes the Operation from this queue.

Parameters
selfThe OperationQueue.
operationThe Operation to remove.

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

◆ waitUntilAllOperationsAreFinished()

void waitUntilAllOperationsAreFinished ( OperationQueue self)

Waits until all Operations submitted to this queue have finished.

Parameters
selfThe OperationQueue.

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}

The documentation for this struct was generated from the following files: