Musa-Cpp-Lib-V2/lib/Base/Threads.h

61 lines
1.1 KiB
C

// #thread_primitives #move?
#if OS_WINDOWS
struct Condition_Variable {
CONDITION_VARIABLE condition_variable;
};
struct Semaphore {
HANDLE event;
};
struct Mutex {
CRITICAL_SECTION csection;
};
struct OS_Thread {
HANDLE windows_thread;
s32 windows_thread_id;
};
struct File {
HANDLE handle;
};
#endif
#define POSIX_THREADS OS_LINUX || OS_MACOS || OS_IOS || OS_ANDROID
#if OS_MACOS
struct Semaphore {
task_t owner;
semaphore_t event = 0;
};
#endif
#if OS_LINUX || OS_ANDROID
struct Semaphore {
sem_t semaphore;
};
#endif
#if OS_IS_UNIX // #posix threads
struct OS_Thread {
pthread_t thread_handle;
Semaphore is_alive;
Semaphore suspended;
b32 is_done;
};
#endif
internal void mutex_init (Mutex* mutex);
internal void mutex_destroy (Mutex* mutex);
internal void lock (Mutex* mutex);
internal void unlock (Mutex* mutex);
#define lock_guard(x) \
Mutex_Lock_Guard Concat(_auto_lock_guard_, __LINE__)(x)
struct Mutex_Lock_Guard {
Mutex* mutex;
Mutex_Lock_Guard (Mutex* mutex) {
this->mutex = mutex;
lock(mutex);
}
~Mutex_Lock_Guard () {
unlock(mutex);
}
};