2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#ifndef GIMBAL_THREAD_H
22#define GIMBAL_THREAD_H
24#include "../meta/instances/gimbal_object.h"
25#include "../meta/signals/gimbal_signal.h"
26#include "../meta/signals/gimbal_closure.h"
29#include "../allocators/gimbal_scope_allocator.h"
33
34
35
37#define GBL_THREAD(self) (GBL_CAST
(GblThread, self))
38#define GBL_THREAD_CLASS(klass) (GBL_CLASS_CAST
(GblThread, klass))
39#define GBL_THREAD_GET_CLASS(self) (GBL_CLASSOF
(GblThread, self))
42#define GBL_SELF_TYPE GblThread
73
74
75
76
77
78
79
80
81
82
91
92
93
94
95
96
97
98
99
120 (started, (GBL_INSTANCE_TYPE, pReceiver)),
121 (finished, (GBL_INSTANCE_TYPE, pReceiver), (GBL_ENUM_TYPE, result)),
122 (signaled, (GBL_INSTANCE_TYPE, pReceiver), (GBL_ENUM_TYPE, signal))
127
128
129
142
143
144
145
147GBL_EXPORT GblThread* GblThread_create (GblThreadFn pCallback,
184
185
186
187
193GBL_EXPORT GBL_RESULT GblThread_nanoSleep (uint64_t nsec,
196GBL_EXPORT GBL_RESULT GblThread_exit (GBL_RESULT result,
197 const char* pMessage,
204
205
206
220#define GblThread_create__3(cb, ud, start) ((GblThread_create
)(cb, ud, start))
221#define GblThread_create__2(cb, ud) (GblThread_create__3
(cb, ud, GBL_TRUE))
222#define GblThread_create__1(cb) (GblThread_create__2
(cb, GBL_NULL))
224#define GblThread_exit__2(result, msg)
225 (GblThread_exit
(result, msg, __FILE__, __func__, __LINE__))
226#define GblThread_exit__1(result)
227 (GblThread_exit
(result, GBL_NULL, __FILE__, __func__, __LINE__))
229#define GblThread_nanoSleep__2(nsec, pRem)
230 ((GblThread_nanoSleep
)(nsec, pRem))
231#define GblThread_nanoSleep__1(nsec)
232 (GblThread_nanoSleep__2
(nsec, GBL_NULL))
234#define GblThread_foreach__2(it, closure)
236#define GblThread_foreach__1(it)
237 (GblThread_foreach__2
(it, GBL_NULL))
#define GBL_CLOSURE_TYPE
Type UUID for GblClosure.
#define GBL_FORWARD_DECLARE_STRUCT(S)
#define GBL_TYPEID(instanceStruct)
#define GBL_INSTANCE_DERIVE(derivedInstance, baseInstance)
#define GBL_DECLARE_ENUM(E)
#define GBL_CLASS_DERIVE(...)
#define GBL_ENUM_TYPE
Type UUID of GblEnumClass.
#define GBL_FLAGS_TYPE
GblType UUID for flags.
#define GBL_VA_OVERLOAD_CALL_ARGC(BASE,...)
#define GBL_FUNCTION_TYPE
#define GBL_BOOL_TYPE
Builtin ID for boolean GblVariant type.
#define GBL_PROPERTIES(object,...)
Declares a list of properties for the given object/instance structure.
#define GBL_SIGNALS(instanceStruct,...)
Declares a list of signals to be associated with the given instanceStruct.
@ GBL_THREAD_PRIORITY_MEDIUM
Medium.
@ GBL_THREAD_PRIORITY_LOW
Lowest.
@ GBL_THREAD_PRIORITY_HIGH
High.
@ GBL_THREAD_PRIORITY_REAL_TIME
Highest.
@ GBL_THREAD_STATE_RUNNING
Running/Executing.
@ GBL_THREAD_STATE_FINISHED
Finished/Completed.
@ GBL_THREAD_STATE_INITIALIZING
Initializing/Constructing.
@ GBL_THREAD_STATE_UNKNOWN
Unknown.
@ GBL_THREAD_STATE_READY
Ready/Waiting.
GblThread * GblThread_find(const char *pName)
Searches (linearly) for a thread with the string name given by pName.
#define GblThread_foreach(...)
Provides default argument handling for GblThread_foreach()
GblType GblThread_type(void)
Returns the GblType UUID associated with GblThread.
size_t GblThread_count(void)
Returns the current number of live threads (not necessarily all active)
uintptr_t GblThreadAffinity
Represents a CPU affinity bitmask, with each bit being affinity to a single core.
GblBool GblThread_foreach(GblThreadIterFn pIt, void *pCl)
Iterates over all live threads, passing each thread and pCl back to the pIt callback.
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
uint16_t GblRefCount
Type able to hold a reference counter across the codebase.
GblThreadFn pFnRun
Main execution entry point for a given thread, calls callback + closure then signals.
Object representing a thread, its local storage, and logic.
GBL_RESULT GblThread_setPriority(GblThread *pSelf, GBL_THREAD_PRIORITY priority)
Sets the priority of the given thread to priority.
GBL_RESULT GblThread_setAffinity(GblThread *pSelf, GblThreadAffinity affinity)
Sets the CPU affinity of the given thread to affinity.
GBL_RESULT GblThread_join(GblThread *pSelf)
Blocks execution of the current thread, awaiting the completion of the given thread.
GblThread * GblThread_ref(const GblThread *pSelf)
Returns a new reference to an existing thread, incrementing its reference count by 1.
GBL_RESULT GblThread_spinWait(GblThread *pSelf)
Performs a non-blocking wait on the given thread, spinning on a timeout.
const char * GblThread_name(const GblThread *pSelf)
Returns the string name assigned to the given thread.
GBL_RESULT GblThread_yield(void)
Suspends execution of the current thread, allowing other threads to execute temporarily.
GblThreadFn GblThread_callback(const GblThread *pSelf)
Returns the C callback function assigned to the given thread, or NULL if there isn't one.
void GblThread_setCallback(GblThread *pSelf, GblThreadFn pCb)
Assigns the C callback function of the given thread to pCb, which will be executed with the thread.
GBL_RESULT GblThread_setName(GblThread *pSelf, const char *pName)
Sets the string name pName to be the name of the given thread.
GBL_RESULT GblThread_start(GblThread *pSelf)
Immediately starts a thread which has not yet joined or finished executing.
volatile sig_atomic_t signalStatus
Pending signal state for a given thread.
GBL_THREAD_STATE state
Current state for a given thread MAKE ME ATOMIC.
GblBool GblThread_isJoined(const GblThread *pSelf)
Returns GBL_TRUE if the given thread has been joined with its parent and is done executing,...
void GblThread_setClosure(GblThread *pSelf, GblClosure *pClosure)
Assigns the closure of the given thread to pClosure, freeing the previous closure if there was one.
GblRefCount GblThread_unref(GblThread *pSelf)
Decrements the reference count of a GblThread instance, deleting it if it's the last one.
GblCallRecord returnStatus
Return information from a completed thread.
GblThread * GblThread_current(void)
Returns a pointer to the GblThread instance associatedw with the current thread.
GblClosure * GblThread_closure(const GblThread *pSelf)
Returns the closure assigned to the given thread, or NULL if there isn't one.