diff options
| author | Mikhail Romanko <me@blankhex.com> | 2025-03-02 23:18:23 +0300 |
|---|---|---|
| committer | Mikhail Romanko <me@blankhex.com> | 2025-03-02 23:18:23 +0300 |
| commit | d403d41f2c54ca382d3a1be17491fdf94097c693 (patch) | |
| tree | 9aa2dc87bca34ee2360f82e87bbc0ce09eff0a70 /src/Platform/Win32/Mutex.c | |
| parent | 2ca6a3e316f356c0467f52f80d8588ee8ed76314 (diff) | |
| download | bhlib-d403d41f2c54ca382d3a1be17491fdf94097c693.tar.gz | |
Add initial implementation of threads/mutexes/semaphores/cvs/spinlocks
Added initial implementation (or wrapper) of the threading library.
It's rather basic, but should work for most of the tasks.
Unfortunately, spinlock implementation relies on GCC/Clang compiler
built-ins (or in-worst-case-scenario on Win32 - InterlockExchange).
In the future, I should revisit this code and fix/reimplement some stuff
(or add support for Windows XP).
Diffstat (limited to 'src/Platform/Win32/Mutex.c')
| -rw-r--r-- | src/Platform/Win32/Mutex.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/Platform/Win32/Mutex.c b/src/Platform/Win32/Mutex.c new file mode 100644 index 0000000..f34aac1 --- /dev/null +++ b/src/Platform/Win32/Mutex.c @@ -0,0 +1,51 @@ +#include "Thread.h" + +#include <BH/Thread.h> + + +BH_Mutex *BH_MutexNew(void) +{ + BH_Mutex *mutex; + + /* Allocate space for mutex and initialize it */ + mutex = malloc(sizeof(BH_Mutex)); + if (mutex && !InitializeCriticalSectionAndSpinCount(&mutex->handle, 0x400)) + { + free(mutex); + return NULL; + } + + return mutex; +} + + +void BH_MutexFree(BH_Mutex *mutex) +{ + DeleteCriticalSection(&mutex->handle); + free(mutex); +} + + +int BH_MutexLock(BH_Mutex *mutex) +{ + EnterCriticalSection(&mutex->handle); + + return BH_OK; +} + + +int BH_MutexUnlock(BH_Mutex *mutex) +{ + LeaveCriticalSection(&mutex->handle); + + return BH_OK; +} + + +int BH_MutexLockTry(BH_Mutex *mutex) +{ + if (!TryEnterCriticalSection(&mutex->handle)) + return BH_ERROR; + + return BH_OK; +} |
