Objectively
Object oriented framework for C.
Loading...
Searching...
No Matches
Thread.c File Reference
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <pthread.h>
#include "Thread.h"

Go to the source code of this file.

Macros

#define _Class   _Thread
 

Functions

static void _kill (Thread *self, int signal)
 
Class * _Thread (void)
 
static void cancel (Thread *self)
 
static Object * copy (const Object *self)
 
static Thread * currentThread (void)
 
static void dealloc (Object *self)
 
static void detach (Thread *self)
 
static Thread * init (Thread *self)
 
static void initialize (Class *clazz)
 
static Thread * initWithFunction (Thread *self, ThreadFunction function, ident data)
 
static bool isCancelled (const Thread *self)
 
static void join (Thread *self, ident *status)
 
static ident run (ident obj)
 Wraps the user-specified ThreadFunction, providing cleanup.
 
static void start (Thread *self)
 

Variables

static __thread Thread * _currentThread
 

Macro Definition Documentation

◆ _Class

#define _Class   _Thread

Definition at line 33 of file Thread.c.

Function Documentation

◆ _kill()

static void _kill ( Thread *  self,
int  signal 
)
static

Definition at line 149 of file Thread.c.

149 {
150
151 int err = pthread_kill(*((pthread_t *) self->thread), signal);
152 assert(err == 0);
153}

◆ _Thread()

Class * _Thread ( void  )

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}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:151
Class * _Object(void)
Definition Object.c:136
static void initialize(Class *clazz)
Definition Thread.c:195
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
POSIX Threads.
Definition Thread.h:53

◆ cancel()

static void cancel ( Thread *  self)
static

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}

◆ copy()

static Object * copy ( const Object *  self)
static
See also
Object::copy(const Object *)

Definition at line 40 of file Thread.c.

40 {
41 return NULL;
42}

◆ currentThread()

static Thread * currentThread ( void  )
static

Definition at line 81 of file Thread.c.

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

◆ dealloc()

static void dealloc ( Object *  self)
static
See also
Object::dealloc(Object *)

Definition at line 47 of file Thread.c.

47 {
48
49 Thread *this = (Thread *) self;
50
51 assert(this->isExecuting == false);
52
53 free(this->thread);
54
55 super(Object, self, dealloc);
56}
#define super(type, obj, method,...)
static void dealloc(Object *self)
Definition Thread.c:47
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ detach()

static void detach ( Thread *  self)
static

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()

static Thread * init ( Thread *  self)
static

Definition at line 104 of file Thread.c.

104 {
105
106 return $(self, initWithFunction, NULL, NULL);
107}
static Thread * initWithFunction(Thread *self, ThreadFunction function, ident data)
Definition Thread.c:113

◆ initialize()

static void initialize ( Class *  clazz)
static
See also
Class::initialize(Class *)

Definition at line 195 of file Thread.c.

195 {
196
197 ((ObjectInterface *) clazz->interface)->copy = copy;
198 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
199
200 ((ThreadInterface *) clazz->interface)->cancel = cancel;
201 ((ThreadInterface *) clazz->interface)->currentThread = currentThread;
202 ((ThreadInterface *) clazz->interface)->detach = detach;
203 ((ThreadInterface *) clazz->interface)->init = init;
204 ((ThreadInterface *) clazz->interface)->initWithFunction = initWithFunction;
205 ((ThreadInterface *) clazz->interface)->isCancelled = isCancelled;
206 ((ThreadInterface *) clazz->interface)->join = join;
207 ((ThreadInterface *) clazz->interface)->kill = _kill;
208 ((ThreadInterface *) clazz->interface)->start = start;
209}
static void detach(Thread *self)
Definition Thread.c:90
static void start(Thread *self)
Definition Thread.c:177
static void join(Thread *self, ident *status)
Definition Thread.c:139
static void cancel(Thread *self)
Definition Thread.c:64
static bool isCancelled(const Thread *self)
Definition Thread.c:131
static void _kill(Thread *self, int signal)
Definition Thread.c:149
static Object * copy(const Object *self)
Definition Thread.c:40
static Thread * init(Thread *self)
Definition Thread.c:104
static Thread * currentThread(void)
Definition Thread.c:81
ident interface
The interface of the Class.
Definition Class.h:100
void dealloc(Object *self)
Frees all resources held by this Object.
Definition Array.c:99
Thread * initWithFunction(Thread *self, ThreadFunction function, ident data)
Initializes this Thread with the specified ThreadFunction and data.
Definition Thread.c:113
void detach(Thread *self)
Daemonize this Thread.
Definition Thread.c:90

◆ initWithFunction()

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

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}
static Data * data(void)
Definition Data.c:286
ident data
The user data.
Definition Thread.h:69
ThreadFunction function
The Thread function.
Definition Thread.h:74

◆ isCancelled()

static bool isCancelled ( const Thread *  self)
static

Definition at line 131 of file Thread.c.

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

◆ join()

static void join ( Thread *  self,
ident *  status 
)
static

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}

◆ run()

static ident run ( ident  obj)
static

Wraps the user-specified ThreadFunction, providing cleanup.

Definition at line 158 of file Thread.c.

158 {
159
160 Thread *self = _currentThread = (Thread *) obj;
161
162 self->isExecuting = true;
163
164 ident ret = self->function(self);
165
166 self->isFinished = true;
167
168 self->isExecuting = false;
169
170 return ret;
171}
#define obj
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
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

◆ start()

static void start ( Thread *  self)
static

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(ident obj)
Wraps the user-specified ThreadFunction, providing cleanup.
Definition Thread.c:158

Variable Documentation

◆ _currentThread

__thread Thread* _currentThread
static

Definition at line 75 of file Thread.c.