CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <cx/cx.h>
10#include <cx/platform/base.h>
11
12#include <cx/thread/threadobj.h>
13
14CX_C_BEGIN
15
26
32_Ret_opt_valid_ Thread* thrCurrent(void);
33
37intptr thrOSThreadID(_In_ Thread* thread);
38
44
45_Ret_opt_valid_ _Check_return_ Thread*
46_thrCreate(_In_ threadFunc func, _In_ strref name, int n, _In_ stvar args[], bool ui);
47// Thread* thrCreate(threadFunc func, strref name, ...)
48//
49// Creates and starts a thread. The return value MUST be released with thrRelease to avoid leaking
50// memory.
51#define thrCreate(func, name, ...) \
52 _thrCreate(func, name, count_macro_args(__VA_ARGS__), (stvar[]) { __VA_ARGS__ }, false)
53#define thrCreateUI(func, name, ...) \
54 _thrCreate(func, name, count_macro_args(__VA_ARGS__), (stvar[]) { __VA_ARGS__ }, true)
55
56void _thrRun(_In_ threadFunc func, _In_ strref name, int n, _In_ stvar args[]);
57
68#define thrRun(func, name, ...) \
69 _thrRun(func, name, count_macro_args(__VA_ARGS__), (stvar[]) { __VA_ARGS__ })
70
74_meta_inline bool thrRunning(_In_ Thread* thread)
75{
76 return atomicLoad(bool, &thread->running, Acquire);
77}
78
98_meta_inline bool thrLoop(_In_ Thread* thread)
99{
100 return !atomicLoad(bool, &thread->requestExit, Acquire);
101}
102
113bool thrRequestExit(_In_ Thread* thread);
114
121bool thrWait(_In_ Thread* thread, int64 timeout);
122
129bool thrShutdown(_In_ Thread* thread);
130
138int thrShutdownMany(_In_ sa_Thread threads);
139
140bool _thrPlatformSetPriority(_Inout_ Thread* thread, int prio);
141
148#define thrSetPriority(thread, prio) _thrPlatformSetPriority(thread, THREAD_##prio)
149
156#define thrSetPriorityV(thread, prio) _thrPlatformSetPriority(thread, prio)
157
166#define thrRelease(pthread) objRelease(pthread)
167
168typedef struct Event Event;
169
179void thrRegisterSysThread(_Inout_ Thread* thread);
180
182// end of thread_core group
183
184CX_C_END
Compiler and platform detection macros.
#define stvar(typen, val)
Definition stvar.h:162
bool thrLoop(Thread *thread)
Definition thread.h:98
int thrShutdownMany(sa_Thread threads)
intptr thrOSThreadID(Thread *thread)
Thread * thrCurrent(void)
bool thrShutdown(Thread *thread)
bool thrRunning(Thread *thread)
Definition thread.h:74
intptr thrCurrentOSThreadID(void)
ThreadPriority
Thread scheduling priority levels.
Definition thread.h:17
void thrRegisterSysThread(Thread *thread)
bool thrRequestExit(Thread *thread)
bool thrWait(Thread *thread, int64 timeout)
@ THREAD_Normal
Normal priority (default)
Definition thread.h:18
@ THREAD_Idle
Idle priority (only runs when system is idle)
Definition thread.h:21
@ THREAD_High
High priority.
Definition thread.h:22
@ THREAD_Higher
Higher priority.
Definition thread.h:23
@ THREAD_Batch
Batch processing priority (below normal)
Definition thread.h:19
@ THREAD_Low
Low priority.
Definition thread.h:20
@ THREAD_Realtime
Real-time priority (use with caution)
Definition thread.h:24
Definition event.h:53