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 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
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 * _Thread (void)
 The Thread archetype.
 
void cancel (Thread *self)
 Cancel this Thread from another Thread.
 
Thread * currentThread (void)
 Returns the currently executing Thread.
 
void detach (Thread *self)
 Daemonize this Thread.
 
Thread * init (Thread *self)
 Initializes this Thread.
 
Thread * initWithFunction (Thread *self, ThreadFunction function, ident data)
 Initializes this Thread with the specified ThreadFunction and data.
 
bool isCancelled (const Thread *self)
 
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.
 
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

ThreadInterface * interface [0]
 The interface type.
 
- Protected Attributes inherited from Object
ObjectInterface * interface [0]
 The interface type.
 

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[0]
protected

The interface type.

Definition at line 64 of file Thread.h.

◆ isDetached

bool Thread::isDetached

true when this Thread has been detached, false otherwise.

Definition at line 86 of file Thread.h.

◆ isExecuting

bool Thread::isExecuting

true when this Thread is executing, false otherwise.

Definition at line 91 of file Thread.h.

◆ isFinished

bool Thread::isFinished

true when this Thread is finished, false otherwise.

Definition at line 96 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 215 of file Thread.c.

215 {
216 static Class *clazz;
217 static Once once;
218
219 do_once(&once, {
220 clazz = _initialize(&(const ClassDef) {
221 .name = "Thread",
222 .superclass = _Object(),
223 .instanceSize = sizeof(Thread),
224 .interfaceSize = sizeof(ThreadInterface),
225 .initialize = initialize,
226 });
227 });
228
229 return clazz;
230}
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
POSIX Threads.
Definition Thread.h:53

◆ 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 __atomic_store_n(&self->isCancelled, true, __ATOMIC_RELEASE);
73}

◆ 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:86

◆ 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

◆ isCancelled()

bool isCancelled ( const Thread *  self)
Parameters
selfThe Thread.
Returns
true if this Thread has been cancelled, false otherwise.
Remarks
This MAY be called from any thread, including while another thread cancels this one.

Definition at line 131 of file Thread.c.

131 {
132 return __atomic_load_n(&self->isCancelled, __ATOMIC_ACQUIRE);
133}

◆ 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 139 of file Thread.c.

139 {
140
141 int err = pthread_join(*((pthread_t *) self->thread), status);
142 assert(err == 0);
143}

◆ 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 177 of file Thread.c.

177 {
178
179 assert(self->function);
180
181 assert(self->isCancelled == false);
182 assert(self->isDetached == false);
183 assert(self->isExecuting == false);
184 assert(self->isFinished == false);
185
186 int err = pthread_create(self->thread, NULL, run, self);
187 assert(err == 0);
188}
static ident run(Thread *thread)
ThreadFunction for the OperationQueue Threads.
bool isExecuting
true when this Thread is executing, false otherwise.
Definition Thread.h:91
bool isFinished
true when this Thread is finished, false otherwise.
Definition Thread.h:96

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