fix some threading crashes

This commit is contained in:
Isaac0-dev 2024-11-26 16:28:27 +10:00
parent 507f578dbb
commit 10ead40b5d
3 changed files with 31 additions and 27 deletions

View file

@ -18,7 +18,7 @@ struct LoadingSegment {
extern struct LoadingSegment gCurrLoadingSegment;
#define LOADING_SCREEN_MUTEX(...) \
if (!gCLIOpts.hideLoadingScreen && gLoadingThread.state != INVALID) { \
if (!gCLIOpts.hideLoadingScreen && gLoadingThread.state == RUNNING) { \
pthread_mutex_lock(&gLoadingThread.mutex); \
__VA_ARGS__; \
pthread_mutex_unlock(&gLoadingThread.mutex); \

View file

@ -61,6 +61,8 @@ int init_thread(struct ThreadHandle *handle, void *(*entry)(void *), void *arg,
int join_thread(struct ThreadHandle *handle) {
assert(handle != NULL);
handle->state = STOPPED;
// Join the thread and wait for it to finish.
return pthread_join(handle->thread, NULL);
}
@ -68,6 +70,8 @@ int join_thread(struct ThreadHandle *handle) {
int detach_thread(struct ThreadHandle *handle) {
assert(handle != NULL);
handle->state = STOPPED;
// Detach the thread, it will no longer be joinable afterwards.
return pthread_detach(handle->thread);
}

View file

@ -7,8 +7,8 @@
#include "types.h"
// Macros
#define MUTEX_LOCK(handle) if (handle.state != INVALID) { lock_mutex(&handle); }
#define MUTEX_UNLOCK(handle) if (handle.state != INVALID) { unlock_mutex(&handle); }
#define MUTEX_LOCK(handle) if (handle.state == RUNNING) { lock_mutex(&handle); }
#define MUTEX_UNLOCK(handle) if (handle.state == RUNNING) { unlock_mutex(&handle); }
// Types
enum ThreadState {