Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
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 50 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. More...
 
ident data
 The user data. More...
 
OperationFunction function
 The Operation function. More...
 
_Bool isCancelled
 true when this Operation has been cancelled, false otherwise. More...
 
_Bool isExecuting
 true when this Operation is executing, false otherwise. More...
 
_Bool isFinished
 true when this Operation is finished, false otherwise. More...
 
Object object
 The superclass. More...
 
Conditioncondition
 The Condition enabling waitUntilFinished. More...
 
MutableArraydependencies
 Contains Operations which must finish before this one can start. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_Operation (void)
 The Operation archetype. More...
 
void addDependency (Operation *self, Operation *dependency)
 Makes this Operation dependent on the completion of dependency. More...
 
void cancel (Operation *self)
 Cancels this Operation, allowing it to complete immediately. More...
 
Arraydependencies (const Operation *self)
 
Operationinit (Operation *self)
 Initializes this Operation. More...
 
OperationinitWithFunction (Operation *self, OperationFunction function, ident data)
 Initializes a synchronous Operation with the given function. More...
 
_Bool isReady (const Operation *self)
 
void removeDependency (Operation *self, Operation *dependency)
 Removes the dependency on dependency. More...
 
void start (Operation *self)
 Starts this Operation. More...
 
void waitUntilFinished (const Operation *self)
 Blocks the current thread until this Operation isFinished. 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

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

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 84 of file Operation.h.

◆ condition

Condition* Operation::condition

The Condition enabling waitUntilFinished.

Definition at line 71 of file Operation.h.

◆ data

ident Operation::data ( void  )

The user data.

Definition at line 89 of file Operation.h.

◆ dependencies

MutableArray* Operation::dependencies

Contains Operations which must finish before this one can start.

Definition at line 76 of file Operation.h.

◆ function

OperationFunction Operation::function

The Operation function.

Definition at line 94 of file Operation.h.

◆ interface

OperationInterface* Operation::interface
protected

The interface.

Definition at line 61 of file Operation.h.

◆ isCancelled

_Bool Operation::isCancelled

true when this Operation has been cancelled, false otherwise.

Definition at line 99 of file Operation.h.

◆ isExecuting

_Bool Operation::isExecuting

true when this Operation is executing, false otherwise.

Definition at line 104 of file Operation.h.

◆ isFinished

_Bool Operation::isFinished

true when this Operation is finished, false otherwise.

Definition at line 109 of file Operation.h.

◆ object

Object Operation::object

The superclass.

Definition at line 55 of file Operation.h.

Method Details

◆ _Operation()

Class * _Operation ( void  )

The Operation archetype.

Returns
The Operation Class.

Definition at line 233 of file Operation.c.

233 {
234 static Class *clazz;
235 static Once once;
236
237 do_once(&once, {
238 clazz = _initialize(&(const ClassDef) {
239 .name = "Operation",
240 .superclass = _Object(),
241 .instanceSize = sizeof(Operation),
242 .interfaceOffset = offsetof(Operation, interface),
243 .interfaceSize = sizeof(OperationInterface),
245 });
246 });
247
248 return clazz;
249}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: Operation.c:213
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
An abstraction for discrete units of work, or tasks.
Definition: Operation.h:50
OperationInterface * interface
The interface.
Definition: Operation.h:61

◆ 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 60 of file Operation.c.

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

◆ cancel()

void cancel ( Operation self)

Cancels this Operation, allowing it to complete immediately.

Parameters
selfThe Operation.

Definition at line 74 of file Operation.c.

74 {
75
76 if (self->isCancelled == false) {
77 if (self->isFinished == false) {
78 if (self->isExecuting == false) {
79 self->isCancelled = true;
80 }
81 }
82 }
83}
_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

◆ dependencies()

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

Definition at line 89 of file Operation.c.

89 {
90
91 ident dependencies = $((Object *) self->locals.dependencies, copy);
92
93 return (Array *) dependencies;
94}
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:40

◆ 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 100 of file Operation.c.

100 {
101
102 self = (Operation *) super(Object, self, init);
103 if (self) {
104
105 self->locals.condition = $(alloc(Condition), init);
106 assert(self->locals.condition);
107
108 self->locals.dependencies = $(alloc(MutableArray), init);
109 assert(self->locals.dependencies);
110 }
111
112 return self;
113}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
#define super(type, obj, method,...)
POSIX Threads conditional variables.
Definition: Condition.h:44
Mutable arrays.
Definition: MutableArray.h:40
Operation * init(Operation *self)
Initializes this Operation.
Definition: Operation.c:100
Condition * condition
The Condition enabling waitUntilFinished.
Definition: Operation.h:71

◆ initWithFunction()

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

Initializes a synchronous Operation with the given function.

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

Definition at line 119 of file Operation.c.

119 {
120
121 self = $(self, init);
122 if (self) {
123 self->function = function;
124 self->data = data;
125 }
126
127 return self;
128}
ident data
The user data.
Definition: Operation.h:89
OperationFunction function
The Operation function.
Definition: Operation.h:94

◆ isReady()

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

Definition at line 134 of file Operation.c.

134 {
135
136 if (self->isExecuting || self->isFinished) {
137 return false;
138 }
139
140 if (self->isCancelled) {
141 return true;
142 }
143
144 const Array *dependencies = (Array *) self->locals.dependencies;
145 for (size_t i = 0; i < dependencies->count; i++) {
146
147 Operation *dependency = $(dependencies, objectAtIndex, i);
148 if (dependency->isFinished == false) {
149 return false;
150 }
151 }
152
153 return true;
154}
static ident objectAtIndex(const Array *self, size_t index)
Definition: Array.c:394
size_t count
The count of elements.
Definition: Array.h:72

◆ 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 and has removed itself from any queues it belonged to.
Asynchronous Operations should override this method and coordinate their own state transitions and queue removal. This method should not be invoked by super.

Definition at line 171 of file Operation.c.

171 {
172
173 if (self->isFinished || self->isExecuting) {
174 return;
175 }
176
177 if (self->isCancelled == false) {
178 self->isExecuting = true;
179 self->function(self);
180 self->isExecuting = false;
181 }
182
183 self->isFinished = true;
184
185 synchronized(self->locals.condition, {
186 $(self->locals.condition, broadcast);
187 });
188
190 if (currentQueue) {
192 }
193}
static void broadcast(Condition *self)
Definition: Condition.c:57
static void removeOperation(OperationQueue *self, Operation *operation)
static OperationQueue * currentQueue(void)
OperationQueues provide a thread of execution for Operations.

◆ waitUntilFinished()

void waitUntilFinished ( const Operation self)

Blocks the current thread until this Operation isFinished.

Parameters
selfThe Operation.

Definition at line 199 of file Operation.c.

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

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