commit dc44416f2df4d85e2ef37b769d2a6f297cafa6b4 parent fbd656cc6811b2093dec674f6241860da0a038c1 Author: Michael Savage <mikejsavage@gmail.com> Date: Sun Dec 18 23:12:53 +0200 Add win32_mutex.h Diffstat:
platform_mutex.h | | | 4 | +++- |
win32_mutex.h | | | 42 | ++++++++++++++++++++++++++++++++++++++++++ |
diff --git a/platform_mutex.h b/platform_mutex.h @@ -3,7 +3,9 @@ #include "platform.h" -#if PLATFORM_UNIX +#if PLATFORM_WINDOWS +#include "win32_mutex.h" +#elif PLATFORM_UNIX #include "unix_mutex.h" #else #error new platform diff --git a/win32_mutex.h b/win32_mutex.h @@ -0,0 +1,42 @@ +#pragma once + +#include <windows.h> + +#include "log.h" + +struct Mutex { + HANDLE mutex; +}; + +inline void mutex_init( Mutex * mutex ) { + mutex->mutex = CreateMutex( NULL, FALSE, NULL ); + int ok = pthread_mutex_init( &mutex->mutex, NULL ); + if( ok != 0 ) { + err( 1, "pthread_mutex_init: %d", ok ); + } +} + +inline void mutex_destroy( Mutex * mutex ) { + if( CloseHandle( mutex->mutex ) == 0 ) { + FATAL( "CloseHandle failed" ); + } +} + +inline void mutex_lock( Mutex * mutex ) { + if( WaitForSingleObject( sem->sem, INFINITE ) == WAIT_FAILED ) { + FATAL( "WaitForSingleObject failed" ); + } +} + +inline bool mutex_trylock( Mutex * mutex ) { + DWORD ok = WaitForSingleObject( sem->sem, 0 ); + if( ok == WAIT_FAILED ) FATAL( "WaitForSingleObject failed" ); + if( ok == WAIT_TIMEOUT ) return false; + return true; +} + +inline void mutex_unlock( Mutex * mutex ) { + if( ReleaseMutex( mutex->mutex ) == 0 ) { + FATAL( "ReleaseMutex failed" ); + } +}