Files

62 lines
1.0 KiB
C
Raw Permalink Normal View History

#include <assert.h>
2026-03-16 10:35:43 +00:00
#include <fx/core/error.h>
#include <fx/core/thread.h>
#include <stdlib.h>
#include <string.h>
2026-03-16 10:35:43 +00:00
struct fx_thread {
pthread_t thr_p;
2026-03-16 10:35:43 +00:00
fx_result thr_result;
};
2026-03-16 10:35:43 +00:00
static fx_once thread_tls_init_once = FX_ONCE_INIT;
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;
}
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)
{
2026-03-16 10:35:43 +00:00
if (fx_init_once(&thread_tls_init_once)) {
thread_tls_init();
}
2026-03-16 10:35:43 +00:00
struct fx_thread *thread = pthread_getspecific(thread_tls_key);
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)
{
return pthread_mutex_lock(mut) == 0;
}
2026-03-16 10:35:43 +00:00
bool fx_mutex_trylock(fx_mutex *mut)
{
return pthread_mutex_trylock(mut) == 0;
}
2026-03-16 10:35:43 +00:00
bool fx_mutex_unlock(fx_mutex *mut)
{
return pthread_mutex_unlock(mut) == 0;
}