win32_semaphore.h (858B)
1 #pragma once 2 3 #include <windows.h> 4 5 #include "log.h" 6 7 struct PlatformSemaphore { 8 HANDLE sem; 9 }; 10 11 inline void platform_semaphore_init( PlatformSemaphore * sem ) { 12 LONG max = 1024; // TODO 13 sem->sem = CreateSemaphoreA( NULL, 0, max, NULL ); 14 if( sem->sem == NULL ) { 15 FATAL( "CreateSemaphore" ); 16 } 17 } 18 19 inline void platform_semaphore_signal( PlatformSemaphore * sem ) { 20 if( ReleaseSemaphore( sem->sem, 1, NULL ) == 0 ) { 21 DWORD error = GetLastError(); 22 if( error == ERROR_TOO_MANY_POSTS ) { 23 return; 24 } 25 26 FATAL( "ReleaseSemaphore" ); 27 } 28 } 29 30 inline void platform_semaphore_wait( PlatformSemaphore * sem ) { 31 if( WaitForSingleObject( sem->sem, INFINITE ) == WAIT_FAILED ) { 32 FATAL( "WaitForSingleObject" ); 33 } 34 } 35 36 inline void platform_semaphore_destroy( PlatformSemaphore * sem ) { 37 if( CloseHandle( sem->sem ) == NULL ) { 38 FATAL( "CloseHandle" ); 39 } 40 }