2025-08-09 19:49:06 +01:00
|
|
|
#include <assert.h>
|
2026-03-16 10:35:43 +00:00
|
|
|
#include <fx/core/error.h>
|
|
|
|
|
#include <fx/core/thread.h>
|
2025-08-09 19:49:06 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_thread {
|
2025-08-09 19:49:06 +01:00
|
|
|
pthread_t thr_p;
|
2026-03-16 10:35:43 +00:00
|
|
|
fx_result thr_result;
|
2025-08-09 19:49:06 +01:00
|
|
|
};
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
static fx_once thread_tls_init_once = FX_ONCE_INIT;
|
2025-08-09 19:49:06 +01:00
|
|
|
static pthread_key_t thread_tls_key = {0};
|
|
|
|
|
|
|
|
|
|
static void thread_dtor(void *p)
|
|
|
|
|
{
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_thread *thread = p;
|
2025-08-09 19:49:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void thread_tls_init()
|
|
|
|
|
{
|
|
|
|
|
pthread_key_create(&thread_tls_key, thread_dtor);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_thread *fx_thread_self(void)
|
2025-08-09 19:49:06 +01:00
|
|
|
{
|
2026-03-16 10:35:43 +00:00
|
|
|
if (fx_init_once(&thread_tls_init_once)) {
|
2025-08-09 19:49:06 +01:00
|
|
|
thread_tls_init();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_thread *thread = pthread_getspecific(thread_tls_key);
|
2025-08-09 19:49:06 +01:00
|
|
|
if (thread) {
|
|
|
|
|
return thread;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
thread = malloc(sizeof *thread);
|
|
|
|
|
assert(thread);
|
|
|
|
|
|
|
|
|
|
memset(thread, 0x0, sizeof *thread);
|
|
|
|
|
|
|
|
|
|
thread->thr_p = pthread_self();
|
|
|
|
|
|
|
|
|
|
pthread_setspecific(thread_tls_key, thread);
|
|
|
|
|
|
|
|
|
|
return thread;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
bool fx_mutex_lock(fx_mutex *mut)
|
2025-08-09 19:49:06 +01:00
|
|
|
{
|
|
|
|
|
return pthread_mutex_lock(mut) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
bool fx_mutex_trylock(fx_mutex *mut)
|
2025-08-09 19:49:06 +01:00
|
|
|
{
|
|
|
|
|
return pthread_mutex_trylock(mut) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
bool fx_mutex_unlock(fx_mutex *mut)
|
2025-08-09 19:49:06 +01:00
|
|
|
{
|
|
|
|
|
return pthread_mutex_unlock(mut) == 0;
|
|
|
|
|
}
|