Implemented thread synchronisation primitives.

This commit is contained in:
Gunnar Beutner 2012-03-31 09:09:40 +02:00
parent 70df015ed0
commit 4df6b08043
5 changed files with 183 additions and 0 deletions

58
base/condvar.cpp Normal file
View File

@ -0,0 +1,58 @@
#include "i2-base.h"
using namespace icinga;
condvar::condvar(void)
{
#ifdef _WIN32
InitializeConditionVariable(&m_CondVar);
#else /* _WIN32 */
#endif /* _WIN32 */
}
condvar::~condvar(void)
{
#ifdef _WIN32
/* nothing to do here */
#else /* _WIN32 */
#endif /* _WIN32 */
}
void condvar::wait(mutex *mtx)
{
#ifdef _WIN32
SleepConditionVariableCS(&m_CondVar, mtx->get(), INFINITE);
#else /* _WIN32 */
pthread_cond_wait(&m_CondVar, mtx->get());
#endif /* _WIN32 */
}
void condvar::signal(void)
{
#ifdef _WIN32
WakeConditionVariable(&m_CondVar);
#else /* _WIN32 */
pthread_cond_signal(&m_CondVar);
#endif /* _WIN32 */
}
void condvar::broadcast(void)
{
#ifdef _WIN32
WakeAllConditionVariable(&m_CondVar);
#else /* _WIN32 */
pthread_cond_broadcast(&m_CondVar);
#endif /* _WIN32 */
}
#ifdef _WIN32
CONDITION_VARIABLE *condvar::get(void)
#else /* _WIN32 */
pthread_cond_t *condvar::get(void)
#endif /* _WIN32 */
{
return &m_CondVar;
}

33
base/condvar.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef I2_CONDVAR_H
#define I2_CONDVAR_H
namespace icinga
{
class condvar
{
private:
#ifdef _WIN32
CONDITION_VARIABLE m_CondVar;
#else /* _WIN32 */
pthread_cond_t m_CondVar;
#endif /* _WIN32 */
public:
condvar(void);
~condvar(void);
void wait(mutex *mtx);
void signal(void);
void broadcast(void);
#ifdef _WIN32
CONDITION_VARIABLE *get(void);
#else /* _WIN32 */
pthread_cond_t *get(void);
#endif /* _WIN32 */
};
}
#endif /* I2_CONDVAR_H */

View File

@ -33,6 +33,8 @@
# include "unix.h"
#endif
#include "mutex.h"
#include "condvar.h"
#include "object.h"
#include "memory.h"
#include "delegate.h"

57
base/mutex.cpp Normal file
View File

@ -0,0 +1,57 @@
#include "i2-base.h"
using namespace icinga;
mutex::mutex(void)
{
#ifdef _WIN32
InitializeCriticalSection(&m_Mutex);
#else /* _WIN32 */
pthread_mutex_init(&m_Mutex);
#endif /* _WIN32 */
}
mutex::~mutex(void)
{
#ifdef _WIN32
DeleteCriticalSection(&m_Mutex);
#else /* _WIN32 */
pthread_mutex_init(&m_Mutex);
#endif /* _WIN32 */
}
bool mutex::tryenter(void)
{
#ifdef _WIN32
return (TryEnterCriticalSection(&m_Mutex) == TRUE);
#else /* _WIN32 */
return pthread_mutex_tryenter(&m_Mutex);
#endif /* _WIN32 */
}
void mutex::enter(void)
{
#ifdef _WIN32
EnterCriticalSection(&m_Mutex);
#else /* _WIN32 */
pthread_mutex_enter(&m_Mutex);
#endif /* _WIN32 */
}
void mutex::exit(void)
{
#ifdef _WIN32
LeaveCriticalSection(&m_Mutex);
#else /* _WIN32 */
pthread_mutex_exit(&m_Mutex);
#endif /* _WIN32 */
}
#ifdef _WIN32
CRITICAL_SECTION *mutex::get(void)
#else /* _WIN32 */
pthread_mutex_t *mutex::get(void)
#endif /* _WIN32 */
{
return &m_Mutex;
}

33
base/mutex.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef I2_MUTEX_H
#define I2_MUTEX_H
namespace icinga
{
class mutex
{
private:
#ifdef _WIN32
CRITICAL_SECTION m_Mutex;
#else /* _WIN32 */
pthread_mutex_t m_Mutex;
#endif /* _WIN32 */
public:
mutex(void);
~mutex(void);
bool tryenter(void);
void enter(void);
void exit(void);
#ifdef _WIN32
CRITICAL_SECTION *get(void);
#else /* _WIN32 */
pthread_mutex_t *get(void);
#endif /* _WIN32 */
};
}
#endif /* I2_MUTEX_H */