|
solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
|
Cross-platform thread management and system information utilities. More...
#include <stdint.h>#include <errno.h>#include <grp.h>#include <pthread.h>#include <pwd.h>#include <sys/stat.h>#include <sys/wait.h>#include <unistd.h>Go to the source code of this file.
Typedefs | |
| typedef pthread_t | Thread |
| typedef pthread_attr_t | ThreadAttr |
| typedef void *(* | ThreadStartRoutine) (void *arg) |
Functions | |
| int | thread_create (Thread *thread, ThreadStartRoutine start_routine, void *data) |
| int | thread_create_attr (Thread *thread, ThreadAttr *attr, ThreadStartRoutine start_routine, void *data) |
| int | thread_join (Thread tid, void **retval) |
| int | thread_detach (Thread tid) |
| void | thread_exit (void *retval) |
| pthread_t | thread_self () |
| int | thread_attr_init (ThreadAttr *attr) |
| int | thread_attr_destroy (ThreadAttr *attr) |
| void | sleep_ms (int ms) |
| int | get_pid () |
| unsigned long | get_tid () |
| long | get_ncpus () |
| int | get_ppid () |
| unsigned int | get_uid () |
| unsigned int | get_gid () |
| char * | get_username () |
| char * | get_groupname () |
Cross-platform thread management and system information utilities.
Provides a unified interface for thread creation, synchronization, and system information queries across Windows and POSIX systems. All thread functions return 0 on success and non-zero on error for consistency with POSIX conventions.
Thread safety is documented per function. Generally, thread creation and attribute management functions are thread-safe, but individual thread attribute objects should not be modified concurrently.
Windows Return Value Limitation: On Windows, thread return values (void*) are stored as 32-bit DWORD values. If your thread returns a 64-bit pointer on 64-bit Windows, the upper 32 bits will be lost. For portable code, return values should fit in 32 bits (e.g., cast integer status codes to void*).
Thread ID Portability: The get_tid() function returns unsigned long, but pthread_t is an opaque type that may not be convertible to an integer on all platforms. Use get_tid() for logging/debugging only, not as a unique identifier for synchronization.
Thread Handle Lifetime: After calling thread_detach() on Windows, the thread handle becomes invalid and must not be used for any subsequent operations. This matches POSIX semantics where detached threads cannot be joined.
Definition in file thread.h.
| typedef pthread_t Thread |
| typedef pthread_attr_t ThreadAttr |
| typedef void *(* ThreadStartRoutine) (void *arg) |
Thread start routine function pointer type.
| arg | User-provided argument passed to the thread. |
| unsigned int get_gid | ( | ) |
Returns the current group ID or equivalent identifier.
| char * get_groupname | ( | ) |
Returns the current group name.
| long get_ncpus | ( | ) |
Returns the number of available CPU cores.
| int get_pid | ( | ) |
| int get_ppid | ( | ) |
Returns the parent process ID.
| unsigned long get_tid | ( | ) |
Returns the current thread ID as an unsigned long.
| unsigned int get_uid | ( | ) |
Returns the current user ID or equivalent identifier.
| char * get_username | ( | ) |
Returns the current user name.
| void sleep_ms | ( | int | ms | ) |
Suspends execution of the calling thread for specified milliseconds.
| ms | Number of milliseconds to sleep. Must be non-negative and <= INT_MAX. |
Definition at line 310 of file thread.c.
References sleep_ms().
Referenced by sleep_ms().
| int thread_attr_destroy | ( | ThreadAttr * | attr | ) |
Destroys thread attributes and releases associated resources.
| attr | Pointer to initialized thread attributes. Must not be NULL. |
Definition at line 292 of file thread.c.
References thread_attr_destroy().
Referenced by thread_attr_destroy().
| int thread_attr_init | ( | ThreadAttr * | attr | ) |
Initializes thread attributes to default values.
| attr | Pointer to thread attributes structure. Must not be NULL. |
Definition at line 272 of file thread.c.
References thread_attr_init().
Referenced by thread_attr_init().
| int thread_create | ( | Thread * | thread, |
| ThreadStartRoutine | start_routine, | ||
| void * | data | ||
| ) |
Creates a new thread with default attributes.
| thread | Pointer to store the created thread handle. Must not be NULL. |
| start_routine | Function to execute in the new thread. Must not be NULL. |
| data | Argument to pass to the start routine. Can be NULL. |
Definition at line 95 of file thread.c.
References thread_create().
Referenced by thread_create().
| int thread_create_attr | ( | Thread * | thread, |
| ThreadAttr * | attr, | ||
| ThreadStartRoutine | start_routine, | ||
| void * | data | ||
| ) |
Creates a new thread with custom attributes.
| thread | Pointer to store the created thread handle. Must not be NULL. |
| attr | Pointer to initialized thread attributes. Must not be NULL. |
| start_routine | Function to execute in the new thread. Must not be NULL. |
| data | Argument to pass to the start routine. Can be NULL. |
Definition at line 140 of file thread.c.
References thread_create_attr().
Referenced by thread_create_attr().
| int thread_detach | ( | Thread | tid | ) |
Detaches a thread, allowing system to reclaim resources when it terminates.
| tid | Thread handle to detach. Must be a valid handle from thread_create(). |
Definition at line 240 of file thread.c.
References thread_detach().
Referenced by thread_detach().
| void thread_exit | ( | void * | retval | ) |
Terminates the calling thread and returns a value to the caller.
| retval | The return value for the thread. On POSIX, this is a void pointer. On Windows, this should be cast from a DWORD exit code. |
Definition at line 231 of file thread.c.
References thread_exit().
Referenced by thread_exit().
| int thread_join | ( | Thread | tid, |
| void ** | retval | ||
| ) |
Waits for a thread to terminate and retrieves its exit value.
| tid | Thread handle to wait for. Must be a valid handle from thread_create(). |
| retval | Pointer to store thread's return value. Can be NULL if not needed. |
Definition at line 184 of file thread.c.
References thread_join().
Referenced by thread_join(), and threadpool_destroy().
| pthread_t thread_self | ( | ) |
Returns the current thread's handle (POSIX-specific).
Definition at line 267 of file thread.c.
References thread_self().
Referenced by thread_self().