2021-02-01 01:24:35 +08:00
|
|
|
#include "thread.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <SDL2/SDL_thread.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
bool
|
|
|
|
sc_thread_create(sc_thread *thread, sc_thread_fn fn, const char *name,
|
|
|
|
void *userdata) {
|
2021-12-10 04:32:11 +08:00
|
|
|
// The thread name length is limited on some systems. Never use a name
|
|
|
|
// longer than 16 bytes (including the final '\0')
|
|
|
|
assert(strlen(name) <= 15);
|
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
SDL_Thread *sdl_thread = SDL_CreateThread(fn, name, userdata);
|
|
|
|
if (!sdl_thread) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2021-02-01 01:24:35 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->thread = sdl_thread;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_thread_join(sc_thread *thread, int *status) {
|
|
|
|
SDL_WaitThread(thread->thread, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
sc_mutex_init(sc_mutex *mutex) {
|
|
|
|
SDL_mutex *sdl_mutex = SDL_CreateMutex();
|
|
|
|
if (!sdl_mutex) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2021-02-01 01:24:35 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex->mutex = sdl_mutex;
|
2021-02-01 01:55:03 +08:00
|
|
|
#ifndef NDEBUG
|
2021-06-26 21:29:08 +08:00
|
|
|
atomic_init(&mutex->locker, 0);
|
2021-02-01 01:55:03 +08:00
|
|
|
#endif
|
2021-02-01 01:24:35 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_mutex_destroy(sc_mutex *mutex) {
|
|
|
|
SDL_DestroyMutex(mutex->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_mutex_lock(sc_mutex *mutex) {
|
2021-02-02 05:20:02 +08:00
|
|
|
// SDL mutexes are recursive, but we don't want to use recursive mutexes
|
|
|
|
assert(!sc_mutex_held(mutex));
|
2021-02-01 01:24:35 +08:00
|
|
|
int r = SDL_LockMutex(mutex->mutex);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (r) {
|
2022-02-05 21:06:03 +08:00
|
|
|
LOGE("Could not lock mutex: %s", SDL_GetError());
|
2021-02-01 01:24:35 +08:00
|
|
|
abort();
|
|
|
|
}
|
2021-02-01 01:55:03 +08:00
|
|
|
|
2021-06-26 21:29:08 +08:00
|
|
|
atomic_store_explicit(&mutex->locker, sc_thread_get_id(),
|
|
|
|
memory_order_relaxed);
|
2021-02-01 01:24:35 +08:00
|
|
|
#else
|
|
|
|
(void) r;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_mutex_unlock(sc_mutex *mutex) {
|
2021-02-01 01:55:03 +08:00
|
|
|
#ifndef NDEBUG
|
2021-02-02 05:20:02 +08:00
|
|
|
assert(sc_mutex_held(mutex));
|
2021-06-26 21:29:08 +08:00
|
|
|
atomic_store_explicit(&mutex->locker, 0, memory_order_relaxed);
|
2021-02-01 01:55:03 +08:00
|
|
|
#endif
|
2021-02-01 01:24:35 +08:00
|
|
|
int r = SDL_UnlockMutex(mutex->mutex);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (r) {
|
2022-02-05 21:06:03 +08:00
|
|
|
LOGE("Could not lock mutex: %s", SDL_GetError());
|
2021-02-01 01:24:35 +08:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void) r;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-01 01:54:52 +08:00
|
|
|
sc_thread_id
|
|
|
|
sc_thread_get_id(void) {
|
|
|
|
return SDL_ThreadID();
|
|
|
|
}
|
|
|
|
|
2021-02-01 01:55:03 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
bool
|
|
|
|
sc_mutex_held(struct sc_mutex *mutex) {
|
2021-06-26 21:29:08 +08:00
|
|
|
sc_thread_id locker_id =
|
|
|
|
atomic_load_explicit(&mutex->locker, memory_order_relaxed);
|
|
|
|
return locker_id == sc_thread_get_id();
|
2021-02-01 01:55:03 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
bool
|
|
|
|
sc_cond_init(sc_cond *cond) {
|
|
|
|
SDL_cond *sdl_cond = SDL_CreateCond();
|
|
|
|
if (!sdl_cond) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2021-02-01 01:24:35 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cond->cond = sdl_cond;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_cond_destroy(sc_cond *cond) {
|
|
|
|
SDL_DestroyCond(cond->cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_cond_wait(sc_cond *cond, sc_mutex *mutex) {
|
|
|
|
int r = SDL_CondWait(cond->cond, mutex->mutex);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (r) {
|
2022-02-05 21:06:03 +08:00
|
|
|
LOGE("Could not wait on condition: %s", SDL_GetError());
|
2021-02-01 01:24:35 +08:00
|
|
|
abort();
|
|
|
|
}
|
2021-02-01 01:55:03 +08:00
|
|
|
|
2021-06-26 21:29:08 +08:00
|
|
|
atomic_store_explicit(&mutex->locker, sc_thread_get_id(),
|
|
|
|
memory_order_relaxed);
|
2021-02-01 01:24:35 +08:00
|
|
|
#else
|
|
|
|
(void) r;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2021-07-04 23:04:20 +08:00
|
|
|
sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick deadline) {
|
|
|
|
sc_tick now = sc_tick_now();
|
|
|
|
if (deadline <= now) {
|
2021-07-04 22:50:19 +08:00
|
|
|
return false; // timeout
|
|
|
|
}
|
|
|
|
|
2021-07-04 23:04:20 +08:00
|
|
|
uint32_t ms = SC_TICK_TO_MS(deadline - now);
|
2021-02-01 01:24:35 +08:00
|
|
|
int r = SDL_CondWaitTimeout(cond->cond, mutex->mutex, ms);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (r < 0) {
|
2022-02-05 21:06:03 +08:00
|
|
|
LOGE("Could not wait on condition with timeout: %s", SDL_GetError());
|
2021-02-01 01:24:35 +08:00
|
|
|
abort();
|
|
|
|
}
|
2021-02-01 01:55:03 +08:00
|
|
|
|
2021-06-26 21:29:08 +08:00
|
|
|
atomic_store_explicit(&mutex->locker, sc_thread_get_id(),
|
|
|
|
memory_order_relaxed);
|
2021-02-01 01:24:35 +08:00
|
|
|
#endif
|
|
|
|
assert(r == 0 || r == SDL_MUTEX_TIMEDOUT);
|
|
|
|
return r == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_cond_signal(sc_cond *cond) {
|
|
|
|
int r = SDL_CondSignal(cond->cond);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (r) {
|
2022-02-05 21:06:03 +08:00
|
|
|
LOGE("Could not signal a condition: %s", SDL_GetError());
|
2021-02-01 01:24:35 +08:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void) r;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_cond_broadcast(sc_cond *cond) {
|
|
|
|
int r = SDL_CondBroadcast(cond->cond);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (r) {
|
2022-02-05 21:06:03 +08:00
|
|
|
LOGE("Could not broadcast a condition: %s", SDL_GetError());
|
2021-02-01 01:24:35 +08:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void) r;
|
|
|
|
#endif
|
|
|
|
}
|