diff options
author | David Robillard <d@drobilla.net> | 2022-10-23 12:55:40 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-10-23 13:26:00 -0400 |
commit | e883ea50dd1154294e21e946e391dd38e04d6527 (patch) | |
tree | 2019aecf50e1eb2f73a2aaf567d0f4573d9efc31 /src/win32/sem_win32.c | |
parent | 07b1289cd4907aa3defe3fc600f3db1dcefcb719 (diff) | |
download | zix-e883ea50dd1154294e21e946e391dd38e04d6527.tar.gz zix-e883ea50dd1154294e21e946e391dd38e04d6527.tar.bz2 zix-e883ea50dd1154294e21e946e391dd38e04d6527.zip |
Split up platform sources
This puts more onus on the build system to do things properly, but it's still
easy enough to build, even manually: all the files in the appropriate system
subdirectory just need to be included in the build.
Otherwise, the several nested levels of preprocessor conditionals get
confusing, and clang-format doesn't format code properly.
Diffstat (limited to 'src/win32/sem_win32.c')
-rw-r--r-- | src/win32/sem_win32.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/win32/sem_win32.c b/src/win32/sem_win32.c new file mode 100644 index 0000000..979390e --- /dev/null +++ b/src/win32/sem_win32.c @@ -0,0 +1,63 @@ +// Copyright 2012-2022 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#include "zix/sem.h" + +#include "zix/status.h" + +#include <windows.h> + +#include <limits.h> +#include <stdint.h> +#include <time.h> + +ZixStatus +zix_sem_init(ZixSem* sem, unsigned initial) +{ + sem->sem = CreateSemaphore(NULL, (LONG)initial, LONG_MAX, NULL); + return sem->sem ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR; +} + +ZixStatus +zix_sem_destroy(ZixSem* sem) +{ + return CloseHandle(sem->sem) ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR; +} + +ZixStatus +zix_sem_post(ZixSem* sem) +{ + return ReleaseSemaphore(sem->sem, 1, NULL) ? ZIX_STATUS_SUCCESS + : ZIX_STATUS_ERROR; +} + +ZixStatus +zix_sem_wait(ZixSem* sem) +{ + return WaitForSingleObject(sem->sem, INFINITE) == WAIT_OBJECT_0 + ? ZIX_STATUS_SUCCESS + : ZIX_STATUS_ERROR; +} + +ZixStatus +zix_sem_try_wait(ZixSem* sem) +{ + const DWORD r = WaitForSingleObject(sem->sem, 0); + + return (r == WAIT_OBJECT_0) ? ZIX_STATUS_SUCCESS + : (r == WAIT_TIMEOUT) ? ZIX_STATUS_UNAVAILABLE + : ZIX_STATUS_ERROR; +} + +ZixStatus +zix_sem_timed_wait(ZixSem* sem, + const uint32_t seconds, + const uint32_t nanoseconds) +{ + const uint32_t milliseconds = seconds * 1000U + nanoseconds / 1000000U; + const DWORD r = WaitForSingleObject(sem->sem, milliseconds); + + return (r == WAIT_OBJECT_0) ? ZIX_STATUS_SUCCESS + : (r == WAIT_TIMEOUT) ? ZIX_STATUS_TIMEOUT + : ZIX_STATUS_ERROR; +} |