ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
threading.c
Go to the documentation of this file.
1
9#include <pthread.h>
10#include <stdint.h>
11
12// Emscripten provides pthread.h but mutexes don't work correctly with pthreads in WASM
13// The JS side is single-threaded, so we can safely make mutexes no-ops
14// This avoids deadlocks from pthread_mutex_t not being properly initialized in WASM
15
16int mutex_init(mutex_t *mutex, const char *name) {
17 (void)mutex;
18 (void)name;
19 return 0; // Success
20}
21
23 (void)mutex;
24 return 0; // Success
25}
26
28 (void)mutex;
29 return 0; // Success - no-op
30}
31
33 (void)mutex;
34 return 0; // Success - no-op
35}
36
38 (void)mutex;
39 return 0; // Success - no-op
40}
41
42// Thread functions (not used in mirror mode, but provided for completeness)
43int asciichat_thread_create(asciichat_thread_t *thread, const char *name, void *(*start_routine)(void *), void *arg) {
44 (void)name; // Unused in WASM - no thread registry
45 return pthread_create(thread, NULL, start_routine, arg);
46}
47
48int asciichat_thread_join(asciichat_thread_t *thread, void **retval) {
49 return pthread_join(*thread, retval);
50}
51
53 return pthread_detach(*thread);
54}
55
57 return pthread_self();
58}
59
61 return pthread_equal(t1, t2);
62}
63
64// Read-write lock functions
65int rwlock_init(rwlock_t *rwlock, const char *name) {
66 (void)name;
67 return pthread_rwlock_init((pthread_rwlock_t *)rwlock, NULL);
68}
69
71 return pthread_rwlock_rdlock((pthread_rwlock_t *)rwlock);
72}
73
75 return pthread_rwlock_wrlock((pthread_rwlock_t *)rwlock);
76}
77
79 return pthread_rwlock_unlock((pthread_rwlock_t *)rwlock);
80}
81
83 return pthread_rwlock_unlock((pthread_rwlock_t *)rwlock);
84}
85
86// Convert thread handle to unique key for named registry lookups
88 return (uintptr_t)thread;
89}
90
91// Thread ID function
93 return (uint64_t)pthread_self();
94}
95
96// Thread-local storage functions
97int ascii_tls_key_create(tls_key_t *key, void (*destructor)(void *)) {
98 return pthread_key_create(key, destructor);
99}
100
102 return pthread_key_delete(key);
103}
104
106 return pthread_getspecific(key);
107}
108
109int ascii_tls_set(tls_key_t key, void *value) {
110 return pthread_setspecific(key, value);
111}
Platform abstraction layer umbrella header providing unified cross-platform API.
mutex_t mutex
unsigned long long uint64_t
Definition common.h:59
uintptr_t asciichat_thread_to_key(asciichat_thread_t thread)
Convert a thread handle to a registry key.
Definition threading.c:87
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
asciichat_thread_t asciichat_thread_self(void)
Get the current thread's ID.
Definition threading.c:56
int mutex_lock_impl(mutex_t *mutex)
Lock a mutex (implementation function)
Definition threading.c:27
int rwlock_rdunlock_impl(rwlock_t *rwlock)
Release a read lock (implementation function)
Definition threading.c:78
int rwlock_init(rwlock_t *rwlock, const char *name)
Initialize a read-write lock with a name.
Definition threading.c:65
int rwlock_rdlock_impl(rwlock_t *rwlock)
Acquire a read lock (implementation function)
Definition threading.c:70
int mutex_init(mutex_t *mutex, const char *name)
Initialize a mutex with a name.
Definition threading.c:16
int mutex_trylock_impl(mutex_t *mutex)
Try to lock a mutex without blocking (implementation function)
Definition threading.c:32
int rwlock_wrunlock_impl(rwlock_t *rwlock)
Release a write lock (implementation function)
Definition threading.c:82
int rwlock_wrlock_impl(rwlock_t *rwlock)
Acquire a write lock (implementation function)
Definition threading.c:74
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
uint64_t asciichat_thread_current_id(void)
Get the current thread's unique numeric ID.
Definition threading.c:92
int mutex_unlock_impl(mutex_t *mutex)
Unlock a mutex (implementation function)
Definition threading.c:37
int mutex_destroy(mutex_t *mutex)
Destroy a mutex.
Definition threading.c:22
🧵 Cross-platform thread interface for ascii-chat
#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_join(thread, timeout_ms)
Mutex type (POSIX: pthread_mutex_t with debug tracking)
Read-write lock type (POSIX: pthread_rwlock_t with debug tracking)
Definition rwlock.h:61
rwlock_t rwlock
Read-write lock for thread-safe access (uthash requires external locking)
Definition util/time.c:34