#include #include #include #include #include struct fx_thread { pthread_t thr_p; fx_result thr_result; }; static fx_once thread_tls_init_once = FX_ONCE_INIT; static pthread_key_t thread_tls_key = {0}; static void thread_dtor(void *p) { struct fx_thread *thread = p; } static void thread_tls_init() { pthread_key_create(&thread_tls_key, thread_dtor); } struct fx_thread *fx_thread_self(void) { if (fx_init_once(&thread_tls_init_once)) { thread_tls_init(); } 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; } bool fx_mutex_lock(fx_mutex *mut) { return pthread_mutex_lock(mut) == 0; } bool fx_mutex_trylock(fx_mutex *mut) { return pthread_mutex_trylock(mut) == 0; } bool fx_mutex_unlock(fx_mutex *mut) { return pthread_mutex_unlock(mut) == 0; }