ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
include/ascii-chat/platform/thread.h
Go to the documentation of this file.
1#pragma once
2
26#include <stdbool.h>
27#include <stdint.h>
28
29// Type definitions MUST come before including common.h to avoid circular dependencies.
30// common.h → abstraction.h → debug/lock.h → thread.h, and lock.h needs asciichat_thread_t.
31#ifdef _WIN32
32#include "windows_compat.h"
34typedef HANDLE asciichat_thread_t;
36typedef DWORD thread_id_t;
38typedef DWORD tls_key_t;
39#else
40#include <pthread.h>
42typedef pthread_t asciichat_thread_t;
44typedef pthread_t thread_id_t;
46typedef pthread_key_t tls_key_t;
47#endif
48
49#include "../common.h" // For asciichat_error_t (must come AFTER type definitions)
50
51#ifdef __cplusplus
52extern "C" {
53#endif
54
55// ============================================================================
56// Thread-Local Storage Macros
57// ============================================================================
58
80#ifdef _WIN32
81#define PLATFORM_THREAD_LOCAL __declspec(thread)
82#else
83#define PLATFORM_THREAD_LOCAL __thread
84#endif
85
86// ============================================================================
87// Thread Lifecycle Management
88// ============================================================================
89
99typedef struct {
100 void *(*user_func)(void *); // User's thread function
101 void *user_arg; // User's argument
103
117
118// ============================================================================
119// Thread Functions
120// ============================================================================
121
139int asciichat_thread_create(asciichat_thread_t *thread, const char *name, void *(*func)(void *), void *arg);
140
151int asciichat_thread_join(asciichat_thread_t *thread, void **retval);
152
159
172int asciichat_thread_join_timeout(asciichat_thread_t *thread, void **retval, uint64_t timeout_ns);
173
183void asciichat_thread_exit(void *retval);
184
194
204
215
224
235
255
276asciichat_error_t thread_create_or_fail(asciichat_thread_t *thread, void *(*func)(void *), void *arg,
277 const char *thread_name, const char *client_id);
278
291
292// ============================================================================
293// Thread-Local Storage (TLS) Functions
294// ============================================================================
295
308int ascii_tls_key_create(tls_key_t *key, void (*destructor)(void *));
309
321
332void *ascii_tls_get(tls_key_t key);
333
344int ascii_tls_set(tls_key_t key, void *value);
345
346#ifdef __cplusplus
347}
348#endif
349
unsigned long long uint64_t
Definition common.h:59
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
int ascii_tls_key_create(tls_key_t *key, void(*destructor)(void *))
Create a thread-local storage key.
Definition threading.c:97
int ascii_tls_set(tls_key_t key, void *value)
Set thread-local value for a key.
Definition threading.c:109
thread_id_t asciichat_thread_self(void)
Get the current thread's ID.
Definition threading.c:56
asciichat_error_t asciichat_thread_set_realtime_priority(void)
Set the current thread to real-time priority.
int asciichat_thread_join_timeout(asciichat_thread_t *thread, void **retval, uint64_t timeout_ns)
Wait for a thread to complete with timeout.
pthread_key_t tls_key_t
Thread-local storage key type (POSIX: pthread_key_t)
void * asciichat_thread_wrapper_impl(void *arg)
Internal thread wrapper function that executes user code with cleanup.
void asciichat_thread_init(asciichat_thread_t *thread)
Initialize a thread handle to an uninitialized state.
bool asciichat_thread_is_initialized(asciichat_thread_t *thread)
Check if a thread handle has been initialized.
void * ascii_tls_get(tls_key_t key)
Get thread-local value for a key.
Definition threading.c:105
int ascii_tls_key_delete(tls_key_t key)
Delete a thread-local storage key.
Definition threading.c:101
pthread_t thread_id_t
Thread ID type (POSIX: pthread_t)
pthread_t asciichat_thread_t
Thread handle type (POSIX: pthread_t)
uint64_t asciichat_thread_current_id(void)
Get the current thread's unique numeric ID.
Definition threading.c:92
uintptr_t asciichat_thread_to_key(asciichat_thread_t thread)
Convert a thread handle to a uintptr_t registry key.
Definition threading.c:87
asciichat_error_t thread_create_or_fail(asciichat_thread_t *thread, void *(*func)(void *), void *arg, const char *thread_name, const char *client_id)
Create a thread with standardized error handling and logging.
Definition thread.c:14
#define asciichat_thread_create(thread_ptr, attr, start_routine, arg)
#define asciichat_thread_equal(t1, t2)
void * asciichat_thread_t
#define asciichat_thread_detach(thread)
#define asciichat_thread_exit(status)
#define asciichat_thread_join(thread, timeout_ms)
ClangTool/LibTooling compatibility shim for stdbool.h.
Internal thread wrapper for automatic cleanup.
Wrapper for windows.h with C23 alignment compatibility.