diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2024-11-23 22:33:43 +0100 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2024-11-23 22:33:43 +0100 |
| commit | b1e857cf1471d1871a9396696b22fa531da98249 (patch) | |
| tree | 3923008a8653057698cb339faf6dcfa92e18364b /project-base/src/windows/thread.c | |
| parent | 106bb7fcadf637cec883648916cc8d19529d6199 (diff) | |
add projbase to repo
Diffstat (limited to 'project-base/src/windows/thread.c')
| -rw-r--r-- | project-base/src/windows/thread.c | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/project-base/src/windows/thread.c b/project-base/src/windows/thread.c new file mode 100644 index 0000000..fbf5209 --- /dev/null +++ b/project-base/src/windows/thread.c @@ -0,0 +1,107 @@ +/* +* BSD 2-Clause “Simplified” License +* Copyright (c) 2019, Aldrik Ramaekers, aldrik.ramaekers@protonmail.com +* All rights reserved. +*/ + +#include <synchapi.h> + +thread thread_start(void *(*start_routine) (void *), void *arg) +{ + thread result; + result.valid = false; + + result.thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, + arg, 0, NULL); + result.valid = true; + + return result; +} + +void thread_join(thread *thread) +{ + if (thread->valid) + { + WaitForSingleObject(thread->thread, INFINITE); + CloseHandle(thread->thread); + } +} + +bool thread_tryjoin(thread *thread) +{ + if (thread->valid) + { + s32 result = WaitForSingleObject(thread->thread, 0); + return result == WAIT_OBJECT_0; + } + return false; +} + +void thread_detach(thread *thread) +{ + if (thread->valid) + { + CloseHandle(thread->thread); + } +} + +void thread_stop(thread *thread) +{ + if (thread->valid) + { + SuspendThread(thread->thread); + } +} + +u32 thread_get_id() +{ + return GetCurrentThreadId(); +} + +void thread_exit() +{ + ExitThread(0); +} + +void thread_sleep(u64 microseconds) +{ + Sleep(microseconds/1000); +} + +mutex mutex_create() +{ + mutex result; + result.mutex = CreateMutex( + NULL, // default security attributes + FALSE, // initially not owned + NULL); // unnamed mutex + + return result; +} + +mutex mutex_create_recursive() +{ + return mutex_create(); +} + +void mutex_lock(mutex *mutex) +{ + WaitForSingleObject( + mutex->mutex, // handle to mutex + INFINITE); // no time-out interval +} + +bool mutex_trylock(mutex *mutex) +{ + return WaitForSingleObject(mutex->mutex, 1) == WAIT_OBJECT_0; +} + +void mutex_unlock(mutex *mutex) +{ + ReleaseMutex(mutex->mutex); +} + +void mutex_destroy(mutex *mutex) +{ + CloseHandle(mutex->mutex); +} |
