2021-02-01 01:24:35 +08:00
|
|
|
#ifndef SC_THREAD_H
|
|
|
|
#define SC_THREAD_H
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2021-06-26 21:29:08 +08:00
|
|
|
#include <stdatomic.h>
|
2021-02-01 01:24:35 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
/* Forward declarations */
|
|
|
|
typedef struct SDL_Thread SDL_Thread;
|
|
|
|
typedef struct SDL_mutex SDL_mutex;
|
|
|
|
typedef struct SDL_cond SDL_cond;
|
|
|
|
|
|
|
|
typedef int sc_thread_fn(void *);
|
2021-06-26 21:29:08 +08:00
|
|
|
typedef unsigned sc_thread_id;
|
|
|
|
typedef atomic_uint sc_atomic_thread_id;
|
2021-02-01 01:24:35 +08:00
|
|
|
|
|
|
|
typedef struct sc_thread {
|
|
|
|
SDL_Thread *thread;
|
|
|
|
} sc_thread;
|
|
|
|
|
|
|
|
typedef struct sc_mutex {
|
|
|
|
SDL_mutex *mutex;
|
2021-02-01 01:55:03 +08:00
|
|
|
#ifndef NDEBUG
|
2021-06-26 21:29:08 +08:00
|
|
|
sc_atomic_thread_id locker;
|
2021-02-01 01:55:03 +08:00
|
|
|
#endif
|
2021-02-01 01:24:35 +08:00
|
|
|
} sc_mutex;
|
|
|
|
|
|
|
|
typedef struct sc_cond {
|
|
|
|
SDL_cond *cond;
|
|
|
|
} sc_cond;
|
|
|
|
|
|
|
|
bool
|
|
|
|
sc_thread_create(sc_thread *thread, sc_thread_fn fn, const char *name,
|
|
|
|
void *userdata);
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_thread_join(sc_thread *thread, int *status);
|
|
|
|
|
|
|
|
bool
|
|
|
|
sc_mutex_init(sc_mutex *mutex);
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_mutex_destroy(sc_mutex *mutex);
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_mutex_lock(sc_mutex *mutex);
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_mutex_unlock(sc_mutex *mutex);
|
|
|
|
|
2021-02-01 01:54:52 +08:00
|
|
|
sc_thread_id
|
|
|
|
sc_thread_get_id(void);
|
|
|
|
|
2021-02-01 01:55:03 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
bool
|
|
|
|
sc_mutex_held(struct sc_mutex *mutex);
|
|
|
|
# define sc_mutex_assert(mutex) assert(sc_mutex_held(mutex))
|
|
|
|
#else
|
|
|
|
# define sc_mutex_assert(mutex)
|
|
|
|
#endif
|
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
bool
|
|
|
|
sc_cond_init(sc_cond *cond);
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_cond_destroy(sc_cond *cond);
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_cond_wait(sc_cond *cond, sc_mutex *mutex);
|
|
|
|
|
|
|
|
// return true on signaled, false on timeout
|
|
|
|
bool
|
|
|
|
sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, uint32_t ms);
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_cond_signal(sc_cond *cond);
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_cond_broadcast(sc_cond *cond);
|
|
|
|
|
|
|
|
#endif
|