Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
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.
 
Object object
 The superclass.
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

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

Protected Attributes

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

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

Array* 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 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}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
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:55
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 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() [1/2]

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

Adds an Operation to this queue.

Parameters
selfThe OperationQueue.
functionThe OperationFunction to add.
dataThe user data.
Returns
The Operation, which should be released when no longer required.

◆ addOperationWithFunction() [2/2]

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

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
An abstraction for discrete units of work, or tasks.
Definition Operation.h:50
void addOperation(OperationQueue *self, Operation *operation)
Adds an Operation to this queue.

◆ cancelAllOperations()

void cancelAllOperations ( OperationQueue self)

Cancels all pending Operations residing within this Queue.

Parameters
selfThe OperationQueue.

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
Arrays.
Definition Array.h:56
size_t count
The count of elements.
Definition Array.h:72

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

112 {
113
114 return _currentQueue;
115}
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 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}
#define super(type, obj, method,...)
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
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 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()

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

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

◆ removeOperation()

void removeOperation ( OperationQueue self,
Operation operation 
)

Removes the Operation from this queue.

Parameters
selfThe OperationQueue.
operationThe Operation to remove.

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

◆ waitUntilAllOperationsAreFinished()

void waitUntilAllOperationsAreFinished ( OperationQueue self)

Waits until all Operations submitted to this queue have finished.

Parameters
selfThe OperationQueue.

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}

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