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

#include <Thread.h>

Overview

POSIX Threads.

Asynchronous computing via multiple threads of execution.

Definition at line 53 of file Thread.h.

Inheritance diagram for Thread:
Object

Properties

ident data
 The user data.
 
ThreadFunction function
 The Thread function.
 
bool isCancelled
 true when this Thread has been cancelled, false otherwise.
 
bool isDetached
 true when this Thread has been detached, false otherwise.
 
bool isExecuting
 true when this Thread is executing, false otherwise.
 
bool isFinished
 true when this Thread 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_Thread (void)
 The Thread archetype.
 
void cancel (Thread *self)
 Cancel this Thread from another Thread.
 
ThreadcurrentThread (void)
 Returns the currently executing Thread.
 
void detach (Thread *self)
 Daemonize this Thread.
 
Threadinit (Thread *self)
 Initializes this Thread.
 
ThreadinitWithFunction (Thread *self, ThreadFunction function, ident data)
 Initializes this Thread with the specified ThreadFunction and data.
 
void join (Thread *self, ident *status)
 Wait for the specified Thread to terminate.
 
void kill (Thread *self, int signal)
 Sends the given signal to this Thread.
 
void start (Thread *self)
 Start this Thread.
 
- 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

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

Property Details

◆ data

ident Thread::data

The user data.

Definition at line 69 of file Thread.h.

◆ function

ThreadFunction Thread::function

The Thread function.

Definition at line 74 of file Thread.h.

◆ interface

ThreadInterface* Thread::interface
protected

The interface.

Definition at line 64 of file Thread.h.

◆ isCancelled

bool Thread::isCancelled

true when this Thread has been cancelled, false otherwise.

Definition at line 79 of file Thread.h.

◆ isDetached

bool Thread::isDetached

true when this Thread has been detached, false otherwise.

Definition at line 84 of file Thread.h.

◆ isExecuting

bool Thread::isExecuting

true when this Thread is executing, false otherwise.

Definition at line 89 of file Thread.h.

◆ isFinished

bool Thread::isFinished

true when this Thread is finished, false otherwise.

Definition at line 94 of file Thread.h.

◆ object

Object Thread::object

The superclass.

Definition at line 58 of file Thread.h.

Method Details

◆ _Thread()

Class * _Thread ( void  )

The Thread archetype.

Returns
The Thread Class.

Definition at line 206 of file Thread.c.

206 {
207 static Class *clazz;
208 static Once once;
209
210 do_once(&once, {
211 clazz = _initialize(&(const ClassDef) {
212 .name = "Thread",
213 .superclass = _Object(),
214 .instanceSize = sizeof(Thread),
215 .interfaceOffset = offsetof(Thread, interface),
216 .interfaceSize = sizeof(ThreadInterface),
218 });
219 });
220
221 return clazz;
222}
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
POSIX Threads.
Definition Thread.h:53
ThreadInterface * interface
The interface.
Definition Thread.h:64

◆ cancel()

void cancel ( Thread self)

Cancel this Thread from another Thread.

Parameters
selfThe Thread.

Definition at line 64 of file Thread.c.

64 {
65
66 assert(self->isCancelled == false);
67
68 // https://stackoverflow.com/a/4760758
69 // int err = pthread_cancel(*((pthread_t *) self->thread));
70 // assert(err == 0);
71
72 self->isCancelled = true;
73}
bool isCancelled
true when this Thread has been cancelled, false otherwise.
Definition Thread.h:79

◆ currentThread()

Thread * currentThread ( void  )

Returns the currently executing Thread.

Returns
The currently executing Thread.

Definition at line 81 of file Thread.c.

81 {
82
83 return _currentThread;
84}
static __thread Thread * _currentThread
Definition Thread.c:75

◆ detach()

void detach ( Thread self)

Daemonize this Thread.

Parameters
selfThe Thread.

Definition at line 90 of file Thread.c.

90 {
91
92 assert(self->isDetached == false);
93
94 int err = pthread_detach(*((pthread_t *) self->thread));
95 assert(err == 0);
96
97 self->isDetached = true;
98}
bool isDetached
true when this Thread has been detached, false otherwise.
Definition Thread.h:84

◆ init()

Thread * init ( Thread self)

Initializes this Thread.

Parameters
selfThe Thread.
Returns
The initialized Thread, or NULL on error.

Definition at line 104 of file Thread.c.

104 {
105
106 return $(self, initWithFunction, NULL, NULL);
107}
Thread * initWithFunction(Thread *self, ThreadFunction function, ident data)
Initializes this Thread with the specified ThreadFunction and data.
Definition Thread.c:113

◆ initWithFunction()

Thread * initWithFunction ( Thread self,
ThreadFunction  function,
ident  data 
)

Initializes this Thread with the specified ThreadFunction and data.

Parameters
selfThe Thread.
functionThe ThreadFunction to run.
dataThe user data.
Returns
The initialized Thread, or NULL on error.

Definition at line 113 of file Thread.c.

113 {
114
115 self = (Thread *) super(Object, self, init);
116 if (self) {
117 self->function = function;
118 self->data = data;
119
120 self->thread = calloc(1, sizeof(pthread_t));
121 assert(self->thread);
122 }
123
124 return self;
125}
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
ident data
The user data.
Definition Thread.h:69
ThreadFunction function
The Thread function.
Definition Thread.h:74
Thread * init(Thread *self)
Initializes this Thread.
Definition Thread.c:104

◆ join()

void join ( Thread self,
ident status 
)

Wait for the specified Thread to terminate.

Parameters
selfThe Thread.
statusIf not NULL, the return value of this Thread's ThreadFunction is returned here.

Definition at line 131 of file Thread.c.

131 {
132
133 int err = pthread_join(*((pthread_t *) self->thread), status);
134 assert(err == 0);
135}

◆ kill()

void kill ( Thread self,
int  signal 
)

Sends the given signal to this Thread.

Parameters
selfThe Thread.
signalThe signal to send.

◆ start()

void start ( Thread self)

Start this Thread.

Parameters
selfThe Thread.

Definition at line 169 of file Thread.c.

169 {
170
171 assert(self->function);
172
173 assert(self->isCancelled == false);
174 assert(self->isDetached == false);
175 assert(self->isExecuting == false);
176 assert(self->isFinished == false);
177
178 int err = pthread_create(self->thread, NULL, run, self);
179 assert(err == 0);
180}
static ident run(Thread *thread)
ThreadFunction for the OperationQueue Threads.
bool isExecuting
true when this Thread is executing, false otherwise.
Definition Thread.h:89
bool isFinished
true when this Thread is finished, false otherwise.
Definition Thread.h:94

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