Objectively
Ultra-lightweight object oriented framework for GNU 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 isCancelled
 true when this Thread has been cancelled, false otherwise.
 
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
Classclazz
 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.
 
ThreadcurrentThread (void)
 Returns the currently executing Thread.
 
void detach (Thread *self)
 Daemonize this Thread.
 
Threadinit (Thread *self)
 Initializes this Thread.
 
ThreadinitWithFunction (Thread *self, ThreadFunction function, ident data)
 Initializes this Thread with the specified ThreadFunction and data.
 
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.
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (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
 The interface.
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface.
 

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
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}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
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:55
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(Thread *thread)
ThreadFunction for the OperationQueue Thread.
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: