Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions | Variables
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 Objectcopy (const Object *self)
 
static ThreadcurrentThread (void)
 
static void dealloc (Object *self)
 
static void detach (Thread *self)
 
static Threadinit (Thread *self)
 
static void initialize (Class *clazz)
 
static ThreadinitWithFunction (Thread *self, ThreadFunction function, ident data)
 
static void join (Thread *self, ident *status)
 
static ident run (ident obj)
 Wraps the user-specified ThreadFunction, providing cleanup. More...
 
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 140 of file Thread.c.

140 {
141
142 int err = pthread_kill(*((pthread_t *) self->thread), signal);
143 assert(err == 0);
144}
void signal(Condition *self)
Signals a single Thread waiting on this Condition.

◆ _Thread()

Class * _Thread ( void  )

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 * _Object(void)
The Object archetype.
Definition: Object.c:136
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 // 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

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

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

◆ 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,...)
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50

◆ detach()

static void detach ( Thread self)
static

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

static Thread * init ( Thread self)
static

Definition at line 103 of file Thread.c.

103 {
104
105 return $(self, initWithFunction, NULL, NULL);
106}
Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Initializes a synchronous Operation with the given function.
Definition: Operation.c:119

◆ initialize()

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

Definition at line 186 of file Thread.c.

186 {
187
188 ((ObjectInterface *) clazz->interface)->copy = copy;
189 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
190
191 ((ThreadInterface *) clazz->interface)->cancel = cancel;
192 ((ThreadInterface *) clazz->interface)->currentThread = currentThread;
193 ((ThreadInterface *) clazz->interface)->detach = detach;
194 ((ThreadInterface *) clazz->interface)->init = init;
195 ((ThreadInterface *) clazz->interface)->initWithFunction = initWithFunction;
196 ((ThreadInterface *) clazz->interface)->join = join;
197 ((ThreadInterface *) clazz->interface)->kill = _kill;
198 ((ThreadInterface *) clazz->interface)->start = start;
199}
static void _kill(Thread *self, int signal)
Definition: Thread.c:140
ident interface
The interface of the Class.
Definition: Class.h:105
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
void start(Operation *self)
Starts this Operation.
Definition: Operation.c:171
void cancel(Operation *self)
Cancels this Operation, allowing it to complete immediately.
Definition: Operation.c:74
void join(Thread *self, ident *status)
Wait for the specified Thread to terminate.
Definition: Thread.c:130
Thread * currentThread(void)
Returns the currently executing Thread.
Definition: Thread.c:80
void detach(Thread *self)
Daemonize this Thread.
Definition: Thread.c:89

◆ initWithFunction()

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

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}
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75
ident data
The user data.
Definition: Thread.h:69
ThreadFunction function
The Thread function.
Definition: Thread.h:74

◆ join()

static void join ( Thread self,
ident status 
)
static

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}

◆ run()

static ident run ( ident  obj)
static

Wraps the user-specified ThreadFunction, providing cleanup.

Definition at line 149 of file Thread.c.

149 {
150
151 Thread *self = _currentThread = (Thread *) obj;
152
153 self->isExecuting = true;
154
155 ident ret = self->function(self);
156
157 self->isFinished = true;
158
159 self->isExecuting = false;
160
161 return ret;
162}
#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:89
_Bool isFinished
true when this Thread is finished, false otherwise.
Definition: Thread.h:94

◆ start()

static void start ( Thread self)
static

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

Variable Documentation

◆ _currentThread

__thread Thread* _currentThread
static

Definition at line 74 of file Thread.c.