diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2024-03-03 14:29:17 +0100 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2024-03-03 14:29:17 +0100 |
| commit | a3685d46c883c96e122b12bfebc6975705962e07 (patch) | |
| tree | cddf8e88aee97ffe791ebaf5a5243d2346d8450d /src/mutex.cpp | |
v2 initial commit
Diffstat (limited to 'src/mutex.cpp')
| -rw-r--r-- | src/mutex.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/mutex.cpp b/src/mutex.cpp new file mode 100644 index 0000000..089e5bd --- /dev/null +++ b/src/mutex.cpp @@ -0,0 +1,102 @@ +#include "mutex.h" + +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 +} + +int 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); +} + + +thread thread_start(void *(*start_routine) (void *), void *arg) +{ + thread result; + result.valid = 0; + + result.thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, + arg, 0, NULL); + result.valid = 1; + + return result; +} + +void thread_join(thread *thread) +{ + if (thread->valid) + { + WaitForSingleObject(thread->thread, INFINITE); + CloseHandle(thread->thread); + } +} + +int thread_tryjoin(thread *thread) +{ + if (thread->valid) + { + int result = WaitForSingleObject(thread->thread, 0); + return result == WAIT_OBJECT_0; + } + return 0; +} + +void thread_detach(thread *thread) +{ + if (thread->valid) + { + CloseHandle(thread->thread); + } +} + +void thread_stop(thread *thread) +{ + if (thread->valid) + { + SuspendThread(thread->thread); + } +} + +int thread_get_id() +{ + return GetCurrentThreadId(); +} + +void thread_exit() +{ + ExitThread(0); +} + +void thread_sleep(int microseconds) +{ + Sleep(microseconds/1000); +}
\ No newline at end of file |
