]> granicus.if.org Git - icinga2/commitdiff
Implemented thread synchronisation primitives.
authorGunnar Beutner <gunnar.beutner@netways.de>
Sat, 31 Mar 2012 07:09:40 +0000 (09:09 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Sat, 31 Mar 2012 07:09:40 +0000 (09:09 +0200)
base/condvar.cpp [new file with mode: 0644]
base/condvar.h [new file with mode: 0644]
base/i2-base.h
base/mutex.cpp [new file with mode: 0644]
base/mutex.h [new file with mode: 0644]

diff --git a/base/condvar.cpp b/base/condvar.cpp
new file mode 100644 (file)
index 0000000..7f33ed1
--- /dev/null
@@ -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 (file)
index 0000000..6ce196e
--- /dev/null
@@ -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
index a2c978548ae06141e089a8d43d1fdd92a3b6f515..8ba5d3f8ac849510376829f545dc724b8383e099 100644 (file)
@@ -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 (file)
index 0000000..ce706cd
--- /dev/null
@@ -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 (file)
index 0000000..6d0edd1
--- /dev/null
@@ -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 */