From 4df6b08043dcb9374c3c7694d65938529f989ee8 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sat, 31 Mar 2012 09:09:40 +0200 Subject: [PATCH] Implemented thread synchronisation primitives. --- base/condvar.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ base/condvar.h | 33 +++++++++++++++++++++++++++ base/i2-base.h | 2 ++ base/mutex.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++ base/mutex.h | 33 +++++++++++++++++++++++++++ 5 files changed, 183 insertions(+) create mode 100644 base/condvar.cpp create mode 100644 base/condvar.h create mode 100644 base/mutex.cpp create mode 100644 base/mutex.h diff --git a/base/condvar.cpp b/base/condvar.cpp new file mode 100644 index 000000000..7f33ed1b7 --- /dev/null +++ b/base/condvar.cpp @@ -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; +} diff --git a/base/condvar.h b/base/condvar.h new file mode 100644 index 000000000..6ce196e4d --- /dev/null +++ b/base/condvar.h @@ -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 */ \ No newline at end of file diff --git a/base/i2-base.h b/base/i2-base.h index a2c978548..8ba5d3f8a 100644 --- a/base/i2-base.h +++ b/base/i2-base.h @@ -33,6 +33,8 @@ # include "unix.h" #endif +#include "mutex.h" +#include "condvar.h" #include "object.h" #include "memory.h" #include "delegate.h" diff --git a/base/mutex.cpp b/base/mutex.cpp new file mode 100644 index 000000000..ce706cdaa --- /dev/null +++ b/base/mutex.cpp @@ -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; +} diff --git a/base/mutex.h b/base/mutex.h new file mode 100644 index 000000000..6d0edd10f --- /dev/null +++ b/base/mutex.h @@ -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 */