Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
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. More...
 
ThreadFunction function
 The Thread function. More...
 
_Bool isCancelled
 true when this Thread has been cancelled, false otherwise. More...
 
_Bool isDetached
 true when this Thread has been detached, false otherwise. More...
 
_Bool isExecuting
 true when this Thread is executing, false otherwise. More...
 
_Bool isFinished
 true when this Thread is finished, false otherwise. More...
 
Object object
 The superclass. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_Thread (void)
 The Thread archetype. More...
 
void cancel (Thread *self)
 Cancel this Thread from another Thread. More...
 
ThreadcurrentThread (void)
 Returns the currently executing Thread. More...
 
void detach (Thread *self)
 Daemonize this Thread. More...
 
Threadinit (Thread *self)
 Initializes this Thread. More...
 
ThreadinitWithFunction (Thread *self, ThreadFunction function, ident data)
 Initializes this Thread with the specified ThreadFunction and data. More...
 
void join (Thread *self, ident *status)
 Wait for the specified Thread to terminate. More...
 
void kill (Thread *self, int signal)
 Sends the given signal to this Thread. More...
 
void start (Thread *self)
 Start this Thread. 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

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

Property Details

◆ data

ident Thread::data ( void  )

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

205 {
206 static Class *clazz;
207 static Once once;
208
209 do_once(&once, {
210 clazz = _initialize(&(const ClassDef) {
211 .name = "Thread",
212 .superclass = _Object(),
213 .instanceSize = sizeof(Thread),
214 .interfaceOffset = offsetof(Thread, interface),
215 .interfaceSize = sizeof(ThreadInterface),
217 });
218 });
219
220 return clazz;
221}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: Thread.c:186
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
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 // int err = pthread_cancel(*((pthread_t *) self->thread));
69 // assert(err == 0);
70
71 self->isCancelled = true;
72}
_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 80 of file Thread.c.

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

◆ detach()

void detach ( Thread self)

Daemonize this Thread.

Parameters
selfThe Thread.

Definition at line 89 of file Thread.c.

89 {
90
91 assert(self->isDetached == false);
92
93 int err = pthread_detach(*((pthread_t *) self->thread));
94 assert(err == 0);
95
96 self->isDetached = true;
97}
_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 103 of file Thread.c.

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

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

112 {
113
114 self = (Thread *) super(Object, self, init);
115 if (self) {
116 self->function = function;
117 self->data = data;
118
119 self->thread = calloc(1, sizeof(pthread_t));
120 assert(self->thread);
121 }
122
123 return self;
124}
#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:103

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

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

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

168 {
169
170 assert(self->function);
171
172 assert(self->isCancelled == false);
173 assert(self->isDetached == false);
174 assert(self->isExecuting == false);
175 assert(self->isFinished == false);
176
177 int err = pthread_create(self->thread, NULL, run, self);
178 assert(err == 0);
179}
static ident run(ident obj)
Wraps the user-specified ThreadFunction, providing cleanup.
Definition: Thread.c:149
_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: