Objectively
Object oriented framework for C.
Loading...
Searching...
No Matches
Thread.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25#include <errno.h>
26#include <signal.h>
27#include <stdlib.h>
28
29#include <pthread.h>
30
31#include "Thread.h"
32
33#define _Class _Thread
34
35#pragma mark - Object
36
40static Object *copy(const Object *self) {
41 return NULL;
42}
43
47static void dealloc(Object *self) {
48
49 Thread *this = (Thread *) self;
50
51 assert(this->isExecuting == false);
52
53 free(this->thread);
54
55 super(Object, self, dealloc);
56}
57
58#pragma mark - Thread
59
64static void cancel(Thread *self) {
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}
74
75static __thread Thread *_currentThread;
76
81static Thread *currentThread(void) {
82
83 return _currentThread;
84}
85
90static void detach(Thread *self) {
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}
99
104static Thread *init(Thread *self) {
105
106 return $(self, initWithFunction, NULL, NULL);
107}
108
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}
126
131static void join(Thread *self, ident *status) {
132
133 int err = pthread_join(*((pthread_t *) self->thread), status);
134 assert(err == 0);
135}
136
141static void _kill(Thread *self, int signal) {
142
143 int err = pthread_kill(*((pthread_t *) self->thread), signal);
144 assert(err == 0);
145}
146
151
152 Thread *self = _currentThread = (Thread *) obj;
153
154 self->isExecuting = true;
155
156 ident ret = self->function(self);
157
158 self->isFinished = true;
159
160 self->isExecuting = false;
161
162 return ret;
163}
164
169static void start(Thread *self) {
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}
181
182#pragma mark - Class lifecycle
183
187static void initialize(Class *clazz) {
188
189 ((ObjectInterface *) clazz->interface)->copy = copy;
190 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
191
192 ((ThreadInterface *) clazz->interface)->cancel = cancel;
193 ((ThreadInterface *) clazz->interface)->currentThread = currentThread;
194 ((ThreadInterface *) clazz->interface)->detach = detach;
195 ((ThreadInterface *) clazz->interface)->init = init;
196 ((ThreadInterface *) clazz->interface)->initWithFunction = initWithFunction;
197 ((ThreadInterface *) clazz->interface)->join = join;
198 ((ThreadInterface *) clazz->interface)->kill = _kill;
199 ((ThreadInterface *) clazz->interface)->start = start;
200}
201
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}
223
224#undef _Class
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:129
#define obj
#define super(type, obj, method,...)
static Data * data(void)
Definition Data.c:286
Class * _Object(void)
Definition Object.c:136
static void detach(Thread *self)
Definition Thread.c:90
static void start(Thread *self)
Definition Thread.c:169
static Thread * initWithFunction(Thread *self, ThreadFunction function, ident data)
Definition Thread.c:113
static void join(Thread *self, ident *status)
Definition Thread.c:131
static void cancel(Thread *self)
Definition Thread.c:64
static void _kill(Thread *self, int signal)
Definition Thread.c:141
static ident run(ident obj)
Wraps the user-specified ThreadFunction, providing cleanup.
Definition Thread.c:150
Class * _Thread(void)
Definition Thread.c:206
static __thread Thread * _currentThread
Definition Thread.c:75
static void dealloc(Object *self)
Definition Thread.c:47
static Object * copy(const Object *self)
Definition Thread.c:40
static void initialize(Class *clazz)
Definition Thread.c:187
static Thread * init(Thread *self)
Definition Thread.c:104
static Thread * currentThread(void)
Definition Thread.c:81
POSIX Threads.
ident(* ThreadFunction)(Thread *thread)
The function type for Thread execution.
Definition Thread.h:45
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
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
ident interface
The interface of the Class.
Definition Class.h:105
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:99
POSIX Threads.
Definition Thread.h:53
bool isExecuting
true when this Thread is executing, false otherwise.
Definition Thread.h:89
ident data
The user data.
Definition Thread.h:69
Thread * initWithFunction(Thread *self, ThreadFunction function, ident data)
Initializes this Thread with the specified ThreadFunction and data.
Definition Thread.c:113
void join(Thread *self, ident *status)
Wait for the specified Thread to terminate.
Definition Thread.c:131
bool isDetached
true when this Thread has been detached, false otherwise.
Definition Thread.h:84
ThreadFunction function
The Thread function.
Definition Thread.h:74
bool isFinished
true when this Thread is finished, false otherwise.
Definition Thread.h:94
bool isCancelled
true when this Thread has been cancelled, false otherwise.
Definition Thread.h:79
void detach(Thread *self)
Daemonize this Thread.
Definition Thread.c:90