You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
#ifndef __BASICMUTEX_H_INCL__
#define __BASICMUTEX_H_INCL__
#ifndef __linux__
#ifndef WIN_PTHREAD
#define WIN_PTHREAD
#endif
#endif
#ifdef WIN_PTHREAD
#pragma comment(lib, "pthreadVC2.lib")
#endif
#ifndef MUTEX_HANDLE_TYPE
#if defined( __linux__ ) || defined(WIN_PTHREAD)
// Linux impl.
#include <pthread.h>
#define MUTEX_HANDLE_TYPE pthread_mutex_t
#define MUTEX_INIT( X ) pthread_mutex_init( &X, NULL )
#define MUTEX_LOCK( X ) pthread_mutex_lock( &X )
#define MUTEX_TRY_LOCK( X ) pthread_mutex_trylock( &X )
#define MUTEX_UNLOCK( X ) pthread_mutex_unlock( &X )
#define MUTEX_DESTROY( X ) pthread_mutex_destroy( &X )
#else
// Windows impl.
#include "Windows.h"
#define MUTEX_HANDLE_TYPE HANDLE
#define MUTEX_INIT( X ) X = CreateMutex( NULL, FALSE, NULL )
#define MUTEX_LOCK( X ) WaitForSingleObject( X, INFINITE )
#define MUTEX_TRY_LOCK( X ) (WaitForSingleObject( X, 0 )==WAIT_OBJECT_0)
#define MUTEX_UNLOCK( X ) ReleaseMutex( X )
#define MUTEX_DESTROY( X ) CloseHandle( X )
#endif
#endif
/**
* Mutex wraper * * @author Gagua */class basicMutex {private: MUTEX_HANDLE_TYPE m_sMutexHandle;public: // Constructor
basicMutex();
// Destructor
virtual ~basicMutex();
void lock( void ); bool try_lock( void ); void unlock( void ); // Copy constructor
// TODO: Uncomment the copy constructor when you need it.
//basicMutex(const basicMutex& src);
// Assignment operator
// TODO: Uncomment the assignment operator when you need it.
//basicMutex& operator=(const basicMutex& src);
};
// Constructor implementation
inline basicMutex::basicMutex(){ MUTEX_INIT( m_sMutexHandle );};
// Destructor implementation
inline basicMutex::~basicMutex(){ MUTEX_UNLOCK( m_sMutexHandle ); MUTEX_DESTROY( m_sMutexHandle );};
inline void basicMutex::lock( void ){ MUTEX_LOCK( m_sMutexHandle );};
inline bool basicMutex::try_lock( void ){ return ( MUTEX_TRY_LOCK( m_sMutexHandle ) != 0 );};
inline void basicMutex::unlock( void ){ MUTEX_UNLOCK( m_sMutexHandle );};
// TODO: Uncomment the copy constructor when you need it.
//inline basicMutex::basicMutex(const basicMutex& src)
//{
// // TODO: copy
//}
// TODO: Uncomment the assignment operator when you need it.
//inline basicMutex& basicMutex::operator=(const basicMutex& rhs)
//{
// if (this == &rhs) {
// return *this;
// }
//
// // TODO: assignment
//
// return *this;
//}
#endif // __BASICMUTEX_H_INCL__
|