2012-03-31 03:09:40 -04:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2012-04-16 02:36:50 -04:00
|
|
|
Mutex::Mutex(void)
|
2012-03-31 03:09:40 -04:00
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
InitializeCriticalSection(&m_Mutex);
|
|
|
|
|
#else /* _WIN32 */
|
2012-04-01 03:48:52 -04:00
|
|
|
pthread_mutex_init(&m_Mutex, NULL);
|
2012-03-31 03:09:40 -04:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-16 02:36:50 -04:00
|
|
|
Mutex::~Mutex(void)
|
2012-03-31 03:09:40 -04:00
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
DeleteCriticalSection(&m_Mutex);
|
|
|
|
|
#else /* _WIN32 */
|
2012-04-01 03:48:52 -04:00
|
|
|
pthread_mutex_destroy(&m_Mutex);
|
2012-03-31 03:09:40 -04:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-16 02:36:50 -04:00
|
|
|
bool Mutex::TryEnter(void)
|
2012-03-31 03:09:40 -04:00
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
return (TryEnterCriticalSection(&m_Mutex) == TRUE);
|
|
|
|
|
#else /* _WIN32 */
|
2012-04-01 03:48:52 -04:00
|
|
|
return pthread_mutex_trylock(&m_Mutex);
|
2012-03-31 03:09:40 -04:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-16 02:36:50 -04:00
|
|
|
void Mutex::Enter(void)
|
2012-03-31 03:09:40 -04:00
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
EnterCriticalSection(&m_Mutex);
|
|
|
|
|
#else /* _WIN32 */
|
2012-04-01 03:48:52 -04:00
|
|
|
pthread_mutex_lock(&m_Mutex);
|
2012-03-31 03:09:40 -04:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-16 02:36:50 -04:00
|
|
|
void Mutex::Exit(void)
|
2012-03-31 03:09:40 -04:00
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
LeaveCriticalSection(&m_Mutex);
|
|
|
|
|
#else /* _WIN32 */
|
2012-04-01 03:48:52 -04:00
|
|
|
pthread_mutex_unlock(&m_Mutex);
|
2012-03-31 03:09:40 -04:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2012-04-16 02:36:50 -04:00
|
|
|
CRITICAL_SECTION *Mutex::Get(void)
|
2012-03-31 03:09:40 -04:00
|
|
|
#else /* _WIN32 */
|
2012-04-16 02:36:50 -04:00
|
|
|
pthread_mutex_t *Mutex::Get(void)
|
2012-03-31 03:09:40 -04:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
{
|
|
|
|
|
return &m_Mutex;
|
|
|
|
|
}
|