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

#include <Operation.h>

Overview

An abstraction for discrete units of work, or tasks.

Operations are typically executed via an OperationQueue.

Definition at line 53 of file Operation.h.

Inheritance diagram for Operation:
Object

Properties

bool asynchronous
 If true, this Operation will be expected to coordinate its own concurrency and internal state management by overriding start.
 
ident data
 The user data.
 
OperationFunction function
 The Operation function.
 
bool isCancelled
 true when this Operation has been cancelled, false otherwise.
 
bool isDispatched
 True once an OperationQueue has dispatched this Operation to one of its Threads, so that no other Thread also starts it.
 
bool isExecuting
 true when this Operation is executing, false otherwise.
 
bool isFinished
 true when this Operation is finished, false otherwise.
 
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_Operation (void)
 The Operation archetype.
 
void addDependency (Operation *self, Operation *dependency)
 Makes this Operation dependent on the completion of dependency.
 
void cancel (Operation *self)
 Cancels this Operation, allowing it to complete immediately.
 
Arraydependencies (const Operation *self)
 
Operationinit (Operation *self)
 Initializes this Operation.
 
OperationinitWithFunction (Operation *self, OperationFunction function, ident data)
 Initializes a synchronous Operation with the given function.
 
bool isReady (const Operation *self)
 
void removeDependency (Operation *self, Operation *dependency)
 Removes the dependency on dependency.
 
void start (Operation *self)
 Starts this Operation.
 
void waitUntilFinished (const Operation *self)
 Blocks the current thread until this Operation isFinished.
 
- 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

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

Property Details

◆ asynchronous

bool Operation::asynchronous

If true, this Operation will be expected to coordinate its own concurrency and internal state management by overriding start.

Definition at line 93 of file Operation.h.

◆ condition

Condition* Operation::condition

The Condition enabling waitUntilFinished.

Definition at line 73 of file Operation.h.

◆ data

ident Operation::data

The user data.

Definition at line 98 of file Operation.h.

◆ dependencies

Array* Operation::dependencies

Contains Operations which must finish before this one can start.

Definition at line 78 of file Operation.h.

◆ function

OperationFunction Operation::function

The Operation function.

Definition at line 103 of file Operation.h.

◆ interface

OperationInterface* Operation::interface
protected

The interface.

Definition at line 64 of file Operation.h.

◆ isCancelled

bool Operation::isCancelled

true when this Operation has been cancelled, false otherwise.

Definition at line 108 of file Operation.h.

◆ isDispatched

bool Operation::isDispatched

True once an OperationQueue has dispatched this Operation to one of its Threads, so that no other Thread also starts it.

Definition at line 114 of file Operation.h.

◆ isExecuting

bool Operation::isExecuting

true when this Operation is executing, false otherwise.

Definition at line 119 of file Operation.h.

◆ isFinished

bool Operation::isFinished

true when this Operation is finished, false otherwise.

Definition at line 124 of file Operation.h.

◆ object

Object Operation::object

The superclass.

Definition at line 58 of file Operation.h.

◆ queue

OperationQueue* Operation::queue

The OperationQueue this Operation was added to, if any.

Remarks
Not retained; an OperationQueue retains its Operations. This exists so that cancel can notify the queue, which must re-evaluate because cancelling an Operation makes it ready.

Definition at line 86 of file Operation.h.

Method Details

◆ _Operation()

Class * _Operation ( void  )

The Operation archetype.

Returns
The Operation Class.

Definition at line 234 of file Operation.c.

234 {
235 static Class *clazz;
236 static Once once;
237
238 do_once(&once, {
239 clazz = _initialize(&(const ClassDef) {
240 .name = "Operation",
241 .superclass = _Object(),
242 .instanceSize = sizeof(Operation),
243 .interfaceOffset = offsetof(Operation, interface),
244 .interfaceSize = sizeof(OperationInterface),
246 });
247 });
248
249 return clazz;
250}
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
An abstraction for discrete units of work, or tasks.
Definition Operation.h:53
OperationInterface * interface
The interface.
Definition Operation.h:64

◆ addDependency()

void addDependency ( Operation self,
Operation dependency 
)

Makes this Operation dependent on the completion of dependency.

Parameters
selfThe Operation.
dependencyThe Operation to await.

Definition at line 59 of file Operation.c.

59 {
60
61 assert(dependency);
62 assert(dependency != self);
63
64 assert($((Array *) self->locals.dependencies, indexOfObject, dependency) == -1);
65
66 $(self->locals.dependencies, addObject, dependency);
67}
static void addObject(Array *self, const ident obj)
Definition Array.c:181
static ssize_t indexOfObject(const Array *self, const ident obj)
Definition Array.c:403
Arrays.
Definition Array.h:56
Array * dependencies
Contains Operations which must finish before this one can start.
Definition Operation.h:78

◆ cancel()

void cancel ( Operation self)

Cancels this Operation, allowing it to complete immediately.

Parameters
selfThe Operation.

Definition at line 73 of file Operation.c.

73 {
74
75 if (self->isCancelled == false) {
76 if (self->isFinished == false) {
77 if (self->isExecuting == false) {
78 self->isCancelled = true;
79
80 OperationQueue *queue = self->locals.queue;
81 if (queue) {
82 synchronized(queue->locals.condition, {
83 $(queue->locals.condition, signal);
84 });
85 }
86 }
87 }
88 }
89}
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
OperationQueues provide threads of execution for Operations.
Condition * condition
A condition signaled on addOperation and removeOperation.

◆ dependencies()

Array * dependencies ( const Operation self)
Parameters
selfThe Operation.
Returns
An instantaneous copy of this Operations' dependencies.

Definition at line 95 of file Operation.c.

95 {
96
97 ident dependencies = $((Object *) self->locals.dependencies, copy);
98
99 return (Array *) dependencies;
100}
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition Array.c:84

◆ init()

Operation * init ( Operation self)

Initializes this Operation.

Parameters
selfThe Operation.
Returns
The initialized Operation, or NULL on error.
Remarks
Asynchronous subclasses should invoke this initializer.

Definition at line 106 of file Operation.c.

106 {
107
108 self = (Operation *) super(Object, self, init);
109 if (self) {
110
111 self->locals.condition = $(alloc(Condition), init);
112 assert(self->locals.condition);
113
114 self->locals.dependencies = $(alloc(Array), init);
115 assert(self->locals.dependencies);
116 }
117
118 return self;
119}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:222
#define super(type, obj, method,...)
POSIX Threads conditional variables.
Definition Condition.h:44
Operation * init(Operation *self)
Initializes this Operation.
Definition Operation.c:106
Condition * condition
The Condition enabling waitUntilFinished.
Definition Operation.h:73

◆ initWithFunction()

Operation * initWithFunction ( Operation self,
OperationFunction  function,
ident  data 
)

Initializes a synchronous Operation with the given function.

Parameters
selfThe Operation.
functionThe OperationFunction.
dataThe user data.
Returns
The initialized Operation, or NULL on error.

Definition at line 125 of file Operation.c.

125 {
126
127 self = $(self, init);
128 if (self) {
129 self->function = function;
130 self->data = data;
131 }
132
133 return self;
134}
ident data
The user data.
Definition Operation.h:98
OperationFunction function
The Operation function.
Definition Operation.h:103

◆ isReady()

bool isReady ( const Operation self)
Parameters
selfThe Operation.
Returns
true when all criteria for this Operation to start are met.

Definition at line 140 of file Operation.c.

140 {
141
142 if (self->isDispatched || self->isExecuting || self->isFinished) {
143 return false;
144 }
145
146 if (self->isCancelled) {
147 return true;
148 }
149
150 const Array *dependencies = (Array *) self->locals.dependencies;
151 for (size_t i = 0; i < dependencies->count; i++) {
152
153 Operation *dependency = $(dependencies, objectAtIndex, i);
154 if (dependency->isFinished == false) {
155 return false;
156 }
157 }
158
159 return true;
160}
static ident objectAtIndex(const Array *self, size_t index)
Definition Array.c:578
size_t count
The count of elements.
Definition Array.h:72
bool isDispatched
True once an OperationQueue has dispatched this Operation to one of its Threads, so that no other Thr...
Definition Operation.h:114

◆ removeDependency()

void removeDependency ( Operation self,
Operation dependency 
)

Removes the dependency on dependency.

Parameters
selfThe Operation.
dependencyThe dependency.

◆ start()

void start ( Operation self)

Starts this Operation.

Parameters
selfThe Operation.
Remarks
The default implementation of this method checks the state of the Operation and, if all criteria are met, dispatches the configured function synchronously. When this method returns, the Operation isFinished.
Asynchronous Operations should override this method and coordinate their own state transitions. This method should not be invoked by super.

Definition at line 177 of file Operation.c.

177 {
178
179 if (self->isFinished || self->isExecuting) {
180 return;
181 }
182
183 if (self->isCancelled == false) {
184 self->isExecuting = true;
185 self->function(self);
186 self->isExecuting = false;
187 }
188
189 self->isFinished = true;
190
191 synchronized(self->locals.condition, {
192 $(self->locals.condition, broadcast);
193 });
194}
static void broadcast(Condition *self)
Definition Condition.c:57

◆ waitUntilFinished()

void waitUntilFinished ( const Operation self)

Blocks the current thread until this Operation isFinished.

Parameters
selfThe Operation.

Definition at line 200 of file Operation.c.

200 {
201
202 synchronized(self->locals.condition, {
203 while (self->isFinished == false) {
204 $(self->locals.condition, wait);
205 }
206 });
207}

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