unix_semaphore.h (770B)
1 #pragma once 2 3 #include <semaphore.h> 4 5 #include "log.h" 6 7 struct PlatformSemaphore { 8 sem_t sem; 9 }; 10 11 inline void platform_semaphore_init( PlatformSemaphore * sem ) { 12 int ok = sem_init( &sem->sem, 0, 0 ); 13 if( ok != 0 ) { 14 FATAL( "sem_init" ); 15 } 16 } 17 18 inline void platform_semaphore_signal( PlatformSemaphore * sem ) { 19 int ok = sem_post( &sem->sem ); 20 if( ok != 0 && errno != EOVERFLOW ) { 21 FATAL( "sem_post" ); 22 } 23 } 24 25 inline void platform_semaphore_wait( PlatformSemaphore * sem ) { 26 while( true ) { 27 int ok = sem_wait( &sem->sem ); 28 if( ok == 0 ) break; 29 if( errno == EINTR ) continue; 30 FATAL( "sem_wait" ); 31 } 32 } 33 34 inline void platform_semaphore_destroy( PlatformSemaphore * sem ) { 35 int ok = sem_destroy( &sem->sem ); 36 if( ok != 0 ) { 37 FATAL( "sem_destroy" ); 38 } 39 }