solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1
34#ifndef SOLIDC_THREADS_H
35#define SOLIDC_THREADS_H
36
37#if !defined(_GNU_SOURCE)
38#define _GNU_SOURCE // for nanosleep and extended POSIX features
39#endif
40
41#include <stdint.h> // for uintptr_t
42
43#ifdef _WIN32
44// Windows comes first before tlhelp32.h
45#include <windows.h>
46
47// This comment prevents clang-format messing order of imports
48#include <io.h>
49#include <lmcons.h> // for UNLEN constant (get_username)
50#include <tlhelp32.h> // for CreateToolhelp32Snapshot (get_ppid)
51#else
52#include <errno.h> // for error constants
53#include <grp.h> // for getgrgid
54#include <pthread.h> // for pthread functions
55#include <pwd.h> // for getpwuid
56#include <sys/stat.h> // for file operations
57#include <sys/wait.h> // for process waiting
58#include <unistd.h> // for POSIX system calls
59#endif
60
61#ifdef __cplusplus
62extern "C" {
63#endif
64
65/* Type Definitions */
66
67#ifdef _WIN32
69typedef HANDLE Thread;
70
72typedef struct ThreadAttr {
73 SECURITY_ATTRIBUTES sa;
74 DWORD stackSize;
76#else
78typedef pthread_t Thread;
79
81typedef pthread_attr_t ThreadAttr;
82#endif
83
92typedef void* (*ThreadStartRoutine)(void* arg);
93
94/* Thread Management Functions */
95
108int thread_create(Thread* thread, ThreadStartRoutine start_routine, void* data);
109
121int thread_create_attr(Thread* thread, ThreadAttr* attr, ThreadStartRoutine start_routine, void* data);
122
134int thread_join(Thread tid, void** retval);
135
148int thread_detach(Thread tid);
149
157void thread_exit(void* retval);
158
159#ifdef _WIN32
167DWORD thread_self();
168#else
176pthread_t thread_self();
177#endif
178
179/* Thread Attribute Management */
180
190
200
201/* Utility Functions */
202
213void sleep_ms(int ms);
214
215/* System Information Functions */
216
223int get_pid();
224
234unsigned long get_tid();
235
244long get_ncpus();
245
256int get_ppid();
257
268unsigned int get_uid();
269
279unsigned int get_gid();
280
292char* get_username();
293
306char* get_groupname();
307
308#ifdef __cplusplus
309}
310#endif
311
312#endif /* SOLIDC_THREADS_H */
unsigned int get_uid()
Definition thread.c:626
unsigned int get_gid()
Definition thread.c:628
int get_ppid()
Definition thread.c:617
void thread_exit(void *retval)
Definition thread.c:231
char * get_groupname()
Definition thread.c:639
int thread_create(Thread *thread, ThreadStartRoutine start_routine, void *data)
Definition thread.c:95
pthread_attr_t ThreadAttr
Definition thread.h:81
int thread_attr_destroy(ThreadAttr *attr)
Definition thread.c:292
void sleep_ms(int ms)
Definition thread.c:310
int thread_create_attr(Thread *thread, ThreadAttr *attr, ThreadStartRoutine start_routine, void *data)
Definition thread.c:140
pthread_t thread_self()
Definition thread.c:267
int get_pid()
Definition thread.c:338
unsigned long get_tid()
Definition thread.c:351
int thread_detach(Thread tid)
Definition thread.c:240
int thread_join(Thread tid, void **retval)
Definition thread.c:184
pthread_t Thread
Definition thread.h:78
void *(* ThreadStartRoutine)(void *arg)
Definition thread.h:92
long get_ncpus()
Definition thread.c:363
int thread_attr_init(ThreadAttr *attr)
Definition thread.c:272
char * get_username()
Definition thread.c:630