Objectively
Object oriented framework for C.
Loading...
Searching...
No Matches
OperationQueue Struct Reference

#include <OperationQueue.h>

Overview

OperationQueues provide threads 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 as a serial queue.
 
OperationQueueinitWithMaxConcurrentOperations (OperationQueue *self, size_t maxConcurrentOperations)
 Initializes this OperationQueue with the given concurrency.
 
size_t operationCount (const OperationQueue *self)
 
Arrayoperations (const OperationQueue *self)
 
void removeOperation (OperationQueue *self, Operation *operation)
 Removes the Operation from this queue.
 
void resume (OperationQueue *self)
 Resumes this queue, allowing it to start Operations again.
 
void suspend (OperationQueue *self)
 Suspends this queue, preventing it from starting any new Operations.
 
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.

Remarks
Read-only; use suspend and resume, which notify the queue's Threads of the transition.

Definition at line 84 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.

◆ threads

Array* OperationQueue::threads

The backing Threads, one per concurrently executing Operation.

Definition at line 75 of file OperationQueue.h.

Method Details

◆ _OperationQueue()

Class * _OperationQueue ( void  )

The OperationQueue archetype.

Returns
The OperationQueue Class.

Definition at line 349 of file OperationQueue.c.

349 {
350 static Class *clazz;
351 static Once once;
352
353 do_once(&once, {
354 clazz = _initialize(&(const ClassDef) {
355 .name = "OperationQueue",
356 .superclass = _Object(),
357 .instanceSize = sizeof(OperationQueue),
358 .interfaceOffset = offsetof(OperationQueue, interface),
359 .interfaceSize = sizeof(OperationQueueInterface),
361 });
362 });
363
364 return clazz;
365}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:129
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 threads 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 74 of file OperationQueue.c.

74 {
75
76 assert(operation);
77 assert(operation->isCancelled == false);
78 assert(operation->isExecuting == false);
79 assert(operation->isFinished == false);
80
81 synchronized(self->locals.condition, {
82 operation->locals.queue = self;
83 $(self->locals.operations, addObject, operation);
84 $(self->locals.condition, signal);
85 });
86}
static void addObject(Array *self, const ident obj)
Definition Array.c:181
bool isCancelled
true when this Operation has been cancelled, false otherwise.
Definition Operation.h:108
bool isFinished
true when this Operation is finished, false otherwise.
Definition Operation.h:124
OperationQueue * queue
The OperationQueue this Operation was added to, if any.
Definition Operation.h:86
bool isExecuting
true when this Operation is executing, false otherwise.
Definition Operation.h:119
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 92 of file OperationQueue.c.

92 {
93
94 assert(function);
95
96 Operation *operation = $(alloc(Operation), initWithFunction, function, data);
97
98 $(self, addOperation, operation);
99
100 return operation;
101}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:222
static Data * data(void)
Definition Data.c:286
static Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Definition Operation.c:125
An abstraction for discrete units of work, or tasks.
Definition Operation.h:53
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 107 of file OperationQueue.c.

107 {
108
109 Array *operations = $(self, operations);
110
111 for (size_t i = 0; i < operations->count; i++) {
113 }
114
116}
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:335
static void cancel(Operation *self)
Definition Operation.c:73
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 124 of file OperationQueue.c.

124 {
125 return _currentQueue;
126}
static __thread OperationQueue * _currentQueue

◆ init()

OperationQueue * init ( OperationQueue self)

Initializes this OperationQueue as a serial queue.

Parameters
selfThe OperationQueue.
Returns
The initialized OperationQueue, or NULL on error.
Remarks
Serial queues run one Operation at a time. Use initWithMaxConcurrentOperations for concurrency.

Definition at line 182 of file OperationQueue.c.

182 {
183 return $(self, initWithMaxConcurrentOperations, 1);
184}
OperationQueue * initWithMaxConcurrentOperations(OperationQueue *self, size_t maxConcurrentOperations)
Initializes this OperationQueue with the given concurrency.

◆ initWithMaxConcurrentOperations()

OperationQueue * initWithMaxConcurrentOperations ( OperationQueue self,
size_t  maxConcurrentOperations 
)

Initializes this OperationQueue with the given concurrency.

Parameters
selfThe OperationQueue.
maxConcurrentOperationsThe number of Operations to run concurrently, which MUST be at least one.
Returns
The initialized OperationQueue, or NULL on error.

Definition at line 190 of file OperationQueue.c.

190 {
191
192 assert(maxConcurrentOperations);
193
194 self = (OperationQueue *) super(Object, self, init);
195 if (self) {
196
197 self->locals.condition = $(alloc(Condition), init);
198 assert(self->locals.condition);
199
200 self->locals.operations = $(alloc(Array), init);
201 assert(self->locals.operations);
202
203 self->locals.threads = $(alloc(Array), init);
204 assert(self->locals.threads);
205
206 for (size_t i = 0; i < maxConcurrentOperations; i++) {
207
208 Thread *thread = $(alloc(Thread), initWithFunction, run, self);
209 assert(thread);
210
211 $(self->locals.threads, addObject, thread);
212
213 release(thread);
214 }
215
216 const Array *threads = (Array *) self->locals.threads;
217 for (size_t i = 0; i < threads->count; i++) {
218 $((Thread *) $(threads, objectAtIndex, i), start);
219 }
220 }
221
222 return self;
223}
#define super(type, obj, method,...)
static void start(Operation *self)
Definition Operation.c:177
static ident run(Thread *thread)
ThreadFunction for the OperationQueue Threads.
POSIX Threads conditional variables.
Definition Condition.h:44
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
Array * threads
The backing Threads, one per concurrently executing Operation.
OperationQueue * init(OperationQueue *self)
Initializes this OperationQueue as a serial queue.
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 229 of file OperationQueue.c.

229 {
230
231 size_t count;
232
233 synchronized(self->locals.condition, {
234 count = ((Array *) self->locals.operations)->count;
235 });
236
237 return count;
238}

◆ operations()

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

Definition at line 244 of file OperationQueue.c.

244 {
245
247
248 synchronized(self->locals.condition, {
249 operations = $((Object * ) self->locals.operations, copy);
250 });
251
252 return (Array *) operations;
253}
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 259 of file OperationQueue.c.

259 {
260
261 assert(operation);
262 assert(operation->isExecuting == false);
263
264 synchronized(self->locals.condition, {
265 operation->locals.queue = NULL;
266 $(self->locals.operations, removeObject, operation);
267 $(self->locals.condition, signal);
268 });
269}
static void removeObject(Array *self, const ident obj)
Definition Array.c:642

◆ resume()

void resume ( OperationQueue self)

Resumes this queue, allowing it to start Operations again.

Parameters
selfThe OperationQueue.

Definition at line 275 of file OperationQueue.c.

275 {
276
277 synchronized(self->locals.condition, {
278 self->isSuspended = false;
279 $(self->locals.condition, broadcast);
280 });
281}
static void broadcast(Condition *self)
Definition Condition.c:57
bool isSuspended
When true, the queue will not start any new Operations.

◆ suspend()

void suspend ( OperationQueue self)

Suspends this queue, preventing it from starting any new Operations.

Parameters
selfThe OperationQueue.
Remarks
Operations already executing run to completion.

Definition at line 287 of file OperationQueue.c.

287 {
288
289 synchronized(self->locals.condition, {
290 self->isSuspended = true;
291 });
292}

◆ waitUntilAllOperationsAreFinished()

void waitUntilAllOperationsAreFinished ( OperationQueue self)

Waits until all Operations submitted to this queue have finished.

Parameters
selfThe OperationQueue.

Definition at line 298 of file OperationQueue.c.

298 {
299
300 while (true) {
301
302 Operation *operation = NULL;
303
304 synchronized(self->locals.condition, {
305 const Array *operations = (Array *) self->locals.operations;
306 if (operations->count) {
307 operation = retain($(operations, firstObject));
308 }
309 });
310
311 if (operation == NULL) {
312 break;
313 }
314
315 $(operation, waitUntilFinished);
316
317 release(operation);
318 }
319}
static ident firstObject(const Array *self)
Definition Array.c:394
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:350
static void waitUntilFinished(const Operation *self)
Definition Operation.c:200

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