2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2012-03-31 09:09:40 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Mutex
|
|
|
|
*
|
|
|
|
* Constructor for the Mutex class.
|
|
|
|
*/
|
2012-04-16 08:36:50 +02:00
|
|
|
Mutex::Mutex(void)
|
2012-03-31 09:09:40 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
InitializeCriticalSection(&m_Mutex);
|
|
|
|
#else /* _WIN32 */
|
2012-04-01 09:48:52 +02:00
|
|
|
pthread_mutex_init(&m_Mutex, NULL);
|
2012-03-31 09:09:40 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* ~Mutex
|
|
|
|
*
|
|
|
|
* Destructor for the Mutex class.
|
|
|
|
*/
|
2012-04-16 08:36:50 +02:00
|
|
|
Mutex::~Mutex(void)
|
2012-03-31 09:09:40 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
DeleteCriticalSection(&m_Mutex);
|
|
|
|
#else /* _WIN32 */
|
2012-04-01 09:48:52 +02:00
|
|
|
pthread_mutex_destroy(&m_Mutex);
|
2012-03-31 09:09:40 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* TryEnter
|
|
|
|
*
|
|
|
|
* Tries to lock the mutex. If the mutex cannot be immediatelly
|
|
|
|
* locked the operation fails.
|
|
|
|
*
|
|
|
|
* @returns true if the operation succeeded, false otherwise.
|
|
|
|
*/
|
2012-04-16 08:36:50 +02:00
|
|
|
bool Mutex::TryEnter(void)
|
2012-03-31 09:09:40 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return (TryEnterCriticalSection(&m_Mutex) == TRUE);
|
|
|
|
#else /* _WIN32 */
|
2012-04-01 09:48:52 +02:00
|
|
|
return pthread_mutex_trylock(&m_Mutex);
|
2012-03-31 09:09:40 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Enter
|
|
|
|
*
|
|
|
|
* Locks the mutex.
|
|
|
|
*/
|
2012-04-16 08:36:50 +02:00
|
|
|
void Mutex::Enter(void)
|
2012-03-31 09:09:40 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
EnterCriticalSection(&m_Mutex);
|
|
|
|
#else /* _WIN32 */
|
2012-04-01 09:48:52 +02:00
|
|
|
pthread_mutex_lock(&m_Mutex);
|
2012-03-31 09:09:40 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Exit
|
|
|
|
*
|
|
|
|
* Unlocks the mutex.
|
|
|
|
*/
|
2012-04-16 08:36:50 +02:00
|
|
|
void Mutex::Exit(void)
|
2012-03-31 09:09:40 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
LeaveCriticalSection(&m_Mutex);
|
|
|
|
#else /* _WIN32 */
|
2012-04-01 09:48:52 +02:00
|
|
|
pthread_mutex_unlock(&m_Mutex);
|
2012-03-31 09:09:40 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Get
|
|
|
|
*
|
|
|
|
* Retrieves the platform-specific mutex handle.
|
|
|
|
*
|
|
|
|
* @returns The platform-specific mutex handle.
|
|
|
|
*/
|
2012-03-31 09:09:40 +02:00
|
|
|
#ifdef _WIN32
|
2012-04-16 08:36:50 +02:00
|
|
|
CRITICAL_SECTION *Mutex::Get(void)
|
2012-03-31 09:09:40 +02:00
|
|
|
#else /* _WIN32 */
|
2012-04-16 08:36:50 +02:00
|
|
|
pthread_mutex_t *Mutex::Get(void)
|
2012-03-31 09:09:40 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
{
|
|
|
|
return &m_Mutex;
|
|
|
|
}
|