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
Class * clazz
 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.
 
Operation * addOperationWithFunction (OperationQueue *self, OperationFunction *function, ident data)
 Adds an Operation to this queue.
 
Operation * addOperationWithFunction (OperationQueue *self, OperationFunction function, ident data)
 
void cancelAllOperations (OperationQueue *self)
 Cancels all pending Operations residing within this Queue.
 
OperationQueue * currentQueue (void)
 
OperationQueue * init (OperationQueue *self)
 Initializes this OperationQueue as a serial queue.
 
OperationQueue * initWithMaxConcurrentOperations (OperationQueue *self, size_t maxConcurrentOperations)
 Initializes this OperationQueue with the given concurrency.
 
size_t operationCount (const OperationQueue *self)
 
Array * operations (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.
 
Object * copy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
String * description (const Object *self)
 
int hash (const Object *self)
 
Object * init (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 [0]
 The interface type.
 
- Protected Attributes inherited from Object
ObjectInterface * interface [0]
 The interface type.
 

Property Details

◆ interface

OperationQueueInterface* OperationQueue::interface[0]
protected

The interface type.

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 74 of file OperationQueue.h.

◆ object

Object OperationQueue::object

The superclass.

Definition at line 49 of file OperationQueue.h.

Method Details

◆ _OperationQueue()

Class * _OperationQueue ( void  )

The OperationQueue archetype.

Returns
The OperationQueue Class.

Definition at line 329 of file OperationQueue.c.

329 {
330 static Class *clazz;
331 static Once once;
332
333 do_once(&once, {
334 clazz = _initialize(&(const ClassDef) {
335 .name = "OperationQueue",
336 .superclass = _Object(),
337 .instanceSize = sizeof(OperationQueue),
338 .interfaceSize = sizeof(OperationQueueInterface),
339 .initialize = initialize,
340 });
341 });
342
343 return clazz;
344}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:151
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:90
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.

◆ addOperation()

void addOperation ( OperationQueue *  self,
Operation *  operation 
)

Adds an Operation to this queue.

Parameters
selfThe OperationQueue.
operationThe Operation to add.

Definition at line 65 of file OperationQueue.c.

65 {
66
67 assert(operation);
68 assert($(operation, isCancelled) == false);
69 assert(operation->isExecuting == false);
70 assert(operation->isFinished == false);
71
72 synchronized(self->condition, {
73 operation->queue = self;
74 $(self->operations, addObject, operation);
75 $(self->condition, signal);
76 });
77}
static void addObject(Array *self, const ident obj)
Definition Array.c:181
bool isFinished
true when this Operation is finished, false otherwise.
Definition Operation.h:116
bool isExecuting
true when this Operation is executing, false otherwise.
Definition Operation.h:111

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

83 {
84
85 assert(function);
86
87 Operation *operation = $(alloc(Operation), initWithFunction, function, data);
88
89 $(self, addOperation, operation);
90
91 return operation;
92}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:226
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 98 of file OperationQueue.c.

98 {
99
100 Array *operations = $(self, operations);
101
102 for (size_t i = 0; i < operations->count; i++) {
103 $((Operation *) $(operations, objectAtIndex, i), cancel);
104 }
105
106 release(operations);
107}
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:356
static void cancel(Operation *self)
Definition Operation.c:73
static Array * operations(const OperationQueue *self)
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 115 of file OperationQueue.c.

115 {
116 return _currentQueue;
117}
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 173 of file OperationQueue.c.

173 {
174 return $(self, initWithMaxConcurrentOperations, 1);
175}
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 181 of file OperationQueue.c.

181 {
182
183 assert(maxConcurrentOperations);
184
185 self = (OperationQueue *) super(Object, self, init);
186 if (self) {
187
188 self->condition = $(alloc(Condition), init);
189 assert(self->condition);
190
191 self->operations = $(alloc(Array), init);
192 assert(self->operations);
193
194 self->threadPool = $(alloc(ThreadPool), initWithSize, maxConcurrentOperations);
195 assert(self->threadPool);
196
197 for (size_t i = 0; i < maxConcurrentOperations; i++) {
198 $(self->threadPool, detach, run, self);
199 }
200 }
201
202 return self;
203}
#define super(type, obj, method,...)
static ident run(Thread *thread)
ThreadFunction for the OperationQueue Threads.
static void detach(Thread *self)
Definition Thread.c:90
static ThreadPool * initWithSize(ThreadPool *self, size_t size)
Definition ThreadPool.c:290
POSIX Threads conditional variables.
Definition Condition.h:44
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
OperationQueue * init(OperationQueue *self)
Initializes this OperationQueue as a serial queue.
A fixed set of Threads that run ThreadFunctions on behalf of the caller.
Definition ThreadPool.h:64

◆ operationCount()

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

Definition at line 209 of file OperationQueue.c.

209 {
210
211 size_t count;
212
213 synchronized(self->condition, {
214 count = ((Array *) self->operations)->count;
215 });
216
217 return count;
218}

◆ operations()

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

Definition at line 224 of file OperationQueue.c.

224 {
225
226 ident operations;
227
228 synchronized(self->condition, {
229 operations = $((Object * ) self->operations, copy);
230 });
231
232 return (Array *) operations;
233}
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 239 of file OperationQueue.c.

239 {
240
241 assert(operation);
242 assert(operation->isExecuting == false);
243
244 synchronized(self->condition, {
245 operation->queue = NULL;
246 $(self->operations, removeObject, operation);
247 $(self->condition, signal);
248 });
249}
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 255 of file OperationQueue.c.

255 {
256
257 synchronized(self->condition, {
258 self->isSuspended = false;
259 $(self->condition, broadcast);
260 });
261}
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 267 of file OperationQueue.c.

267 {
268
269 synchronized(self->condition, {
270 self->isSuspended = true;
271 });
272}

◆ waitUntilAllOperationsAreFinished()

void waitUntilAllOperationsAreFinished ( OperationQueue *  self)

Waits until all Operations submitted to this queue have finished.

Parameters
selfThe OperationQueue.

Definition at line 278 of file OperationQueue.c.

278 {
279
280 while (true) {
281
282 Operation *operation = NULL;
283
284 synchronized(self->condition, {
285 const Array *operations = (Array *) self->operations;
286 if (operations->count) {
287 operation = retain($(operations, firstObject));
288 }
289 });
290
291 if (operation == NULL) {
292 break;
293 }
294
295 $(operation, waitUntilFinished);
296
297 release(operation);
298 }
299}
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:372
static void waitUntilFinished(const Operation *self)
Definition Operation.c:208

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