commit 01c3a5746d8422addfb3a67775a0cccd5db2d714 parent 947864ac502ee07207f5188d508339947affa7f3 Author: Michael Savage <mikejsavage@gmail.com> Date: Wed Sep 14 20:53:53 +0100 platform_mutex.h/unix_mutex.h Diffstat:
platform_mutex.h | | | 10 | ++++++++++ |
unix_mutex.h | | | 40 | ++++++++++++++++++++++++++++++++++++++++ |
diff --git a/platform_mutex.h b/platform_mutex.h @@ -0,0 +1,10 @@ +#ifndef _PLATFORM_MUTEX_H_ +#define _PLATFORM_MUTEX_H_ + +#if defined( __linux__ ) +#include "unix_mutex.h" +#else +#error new platform +#endif + +#endif // _PLATFORM_MUTEX_H_ diff --git a/unix_mutex.h b/unix_mutex.h @@ -0,0 +1,40 @@ +#ifndef _UNIX_MUTEX_H_ +#define _UNIX_MUTEX_H_ + +#include <err.h> +#include <pthread.h> + +struct Mutex { + pthread_mutex_t mutex; +}; + +inline void mutex_init( Mutex * mutex ) { + int ok = pthread_mutex_init( &mutex->mutex, NULL ); + if( ok != 0 ) { + err( 1, "pthread_mutex_init: %d", ok ); + } +} + +inline void mutex_lock( Mutex * mutex ) { + int ok = pthread_mutex_lock( &mutex->mutex ); + if( ok != 0 ) { + err( 1, "pthread_mutex_lock: %d", ok ); + } +} + +inline bool mutex_trylock( Mutex * mutex ) { + int ok = pthread_mutex_trylock( &mutex->mutex ); + if( ok == 0 ) return true; + if( ok == EBUSY ) return false; + + err( 1, "pthread_mutex_lock: %d", ok ); +} + +inline void mutex_unlock( Mutex * mutex ) { + int ok = pthread_mutex_unlock( &mutex->mutex ); + if( ok != 0 ) { + err( 1, "pthread_mutex_unlock: %d", ok ); + } +} + +#endif // _UNIX_MUTEX_H_