ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
os/thread.h
Go to the documentation of this file.
1#pragma once
2
3#include <libplatform/thread.h>
4
5#if 0
6
29
30/* Storage capacities in elements; text buffers include the terminating NUL. */
31#define PLATFORM_THREAD_NAME_BUFFER_SIZE 128
32
33#include <stdbool.h>
34#include <stdint.h>
35
36// Type definitions MUST come before including common.h to avoid circular dependencies.
37// common.h → abstraction.h → debug/lock.h → thread.h, and lock.h needs platform_thread_t.
38#ifdef _WIN32
39#include "windows_compat.h"
41typedef HANDLE platform_thread_t;
43typedef DWORD thread_id_t;
45typedef DWORD tls_key_t;
46#else
47#include <pthread.h>
49typedef pthread_t platform_thread_t;
51typedef pthread_t thread_id_t;
53typedef pthread_key_t tls_key_t;
54#endif
55
56#include "../common.h" // For asciichat_error_t (must come AFTER type definitions)
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62// ============================================================================
63// Thread-Local Storage Macros
64// ============================================================================
65
88#ifdef _WIN32
89#define PLATFORM_THREAD_LOCAL __declspec(thread)
90#else
91#define PLATFORM_THREAD_LOCAL __thread
92#endif
93
94// ============================================================================
95// Thread Lifecycle Management
96// ============================================================================
97
107typedef struct {
108 void *(*user_func)(void *); // User's thread function
109 void *user_arg; // User's argument
110 char name[PLATFORM_THREAD_NAME_BUFFER_SIZE]; // Owned name available before the first worker log
111} asciichat_thread_wrapper_t;
112
125void *thread_wrapper_impl(void *arg);
126
127// ============================================================================
128// Thread Functions
129// ============================================================================
130
148int platform_thread_create(platform_thread_t *thread, const char *name, void *(*func)(void *), void *arg);
149
160int thread_join(platform_thread_t *thread, void **retval);
161
167int thread_detach(platform_thread_t *thread);
168
181int thread_join_timeout(platform_thread_t *thread, void **retval, uint64_t timeout_ns);
182
192void thread_exit(void *retval);
193
202thread_id_t thread_self(void);
203
212int thread_equal(thread_id_t t1, thread_id_t t2);
213
223uint64_t thread_current_id(void);
224
232bool thread_is_initialized(platform_thread_t *thread);
233
243void thread_init(platform_thread_t *thread);
244
263asciichat_error_t thread_set_realtime_priority(void);
264
285asciichat_error_t thread_create_or_fail(platform_thread_t *thread, void *(*func)(void *), void *arg,
286 const char *thread_name, const char *client_id);
287
299uintptr_t thread_to_key(platform_thread_t thread);
300
301// ============================================================================
302// Thread-Local Storage (TLS) Functions
303// ============================================================================
304
317int platform_tls_key_create(tls_key_t *key, void (*destructor)(void *));
318
329int platform_tls_key_delete(tls_key_t key);
330
341void *platform_tls_get(tls_key_t key);
342
353int platform_tls_set(tls_key_t key, void *value);
354
355#ifdef __cplusplus
356}
357#endif
358
360#endif
⚙️ Common definitions, error codes, macros, and types shared throughout the application
unsigned long long uint64_t
Definition common.h:63
uintptr_t thread_to_key(platform_thread_t thread)
Convert a thread handle to a registry key.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44