62 lines
1.0 KiB
C
62 lines
1.0 KiB
C
|
|
#include <assert.h>
|
||
|
|
#include <blue/core/error.h>
|
||
|
|
#include <blue/core/thread.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
struct b_thread {
|
||
|
|
pthread_t thr_p;
|
||
|
|
b_result thr_result;
|
||
|
|
};
|
||
|
|
|
||
|
|
static b_once thread_tls_init_once = B_ONCE_INIT;
|
||
|
|
static pthread_key_t thread_tls_key = {0};
|
||
|
|
|
||
|
|
static void thread_dtor(void *p)
|
||
|
|
{
|
||
|
|
struct b_thread *thread = p;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void thread_tls_init()
|
||
|
|
{
|
||
|
|
pthread_key_create(&thread_tls_key, thread_dtor);
|
||
|
|
}
|
||
|
|
|
||
|
|
struct b_thread *b_thread_self(void)
|
||
|
|
{
|
||
|
|
if (b_init_once(&thread_tls_init_once)) {
|
||
|
|
thread_tls_init();
|
||
|
|
}
|
||
|
|
|
||
|
|
struct b_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 b_mutex_lock(b_mutex *mut)
|
||
|
|
{
|
||
|
|
return pthread_mutex_lock(mut) == 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool b_mutex_trylock(b_mutex *mut)
|
||
|
|
{
|
||
|
|
return pthread_mutex_trylock(mut) == 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool b_mutex_unlock(b_mutex *mut)
|
||
|
|
{
|
||
|
|
return pthread_mutex_unlock(mut) == 0;
|
||
|
|
}
|