vessel/core/thread.c
Arija A. 0a8709bf31
refact: Rename src->core
Signed-off-by: Arija A. <ari@ari.lt>
2025-06-14 02:27:39 +03:00

101 lines
1.9 KiB
C

#include "include/conf.h"
#include "include/def.h"
#include "include/thread.h"
#include <pthread.h>
bool vs_Thread_init(vs_Thread *thread, void *(*target)(void *), void *arg) {
return thread && 0 == pthread_create(&thread->_thread, NULL, target, arg);
}
bool vs_Thread_detach(vs_Thread *thread) { return thread && 0 == pthread_detach(thread->_thread); }
bool vs_Thread_attach(vs_Thread *thread, void **retval) {
return thread && 0 == pthread_join(thread->_thread, retval);
}
bool vs_Thread_destroy(vs_Thread *thread) { return NULL != thread; }
bool vs_Lock_disable(vs_Lock *lock) {
if (!lock || !lock->_enabled) {
return false;
}
if (0 == pthread_mutex_destroy(&lock->_lock)) {
lock->_enabled = false;
return true;
}
lock->_enabled = true;
return false;
}
bool vs_Lock_enable(vs_Lock *lock) {
if (!lock || lock->_enabled) {
return false;
}
if (0 == pthread_mutex_init(&lock->_lock, NULL)) {
lock->_enabled = true;
return true;
}
lock->_enabled = false;
return false;
}
bool vs_Lock_init(vs_Lock *lock) {
if (!lock) {
return false;
}
if (0 == pthread_mutex_init(&lock->_lock, NULL)) {
lock->_enabled = true;
return true;
}
lock->_enabled = false;
return false;
}
bool vs_Lock_lock(vs_Lock *lock) {
if (!lock) {
return false;
}
if (lock->_enabled) {
return 0 == pthread_mutex_lock(&lock->_lock);
}
return true;
}
bool vs_Lock_unlock(vs_Lock *lock) {
if (!lock) {
return false;
}
if (lock->_enabled) {
return 0 == pthread_mutex_unlock(&lock->_lock);
}
return true;
}
bool vs_Lock_destroy(vs_Lock *lock) {
if (!lock) {
return false;
}
if (!lock->_enabled) {
return true;
}
if (0 == pthread_mutex_destroy(&lock->_lock)) {
lock->_enabled = false;
return true;
}
return false;
}