diff --git a/base/base.pro b/base/base.pro index 3eeee55df1..6df29fb82e 100644 --- a/base/base.pro +++ b/base/base.pro @@ -19,6 +19,7 @@ SOURCES += \ shared_buffer_manager.cpp \ memory_mapped_file.cpp \ path_utils.cpp \ + condition.cpp \ HEADERS += \ SRC_FIRST.hpp \ diff --git a/base/condition.cpp b/base/condition.cpp new file mode 100644 index 0000000000..c345feab00 --- /dev/null +++ b/base/condition.cpp @@ -0,0 +1,9 @@ +#include "../std/target_os.hpp" + +#if defined(OMIM_OS_BADA) + #include "condition_bada.cpp" +#elif defined(OMIM_OS_WINDOWS_NATIVE) + #include "condition_windows_native.cpp" +#else + #include "condition_posix.cpp" +#endif diff --git a/base/condition.hpp b/base/condition.hpp index 6dc2c7f431..7290ce9093 100644 --- a/base/condition.hpp +++ b/base/condition.hpp @@ -1,91 +1,21 @@ #pragma once -#include "../std/target_os.hpp" - -#if defined(OMIM_OS_BADA) - #include -#else - #include "mutex.hpp" - #if !defined(OMIM_OS_WINDOWS_NATIVE) - #include - #endif -#endif - namespace threads { /// Implements mutexed condition semantics class Condition { -#if defined(OMIM_OS_BADA) - Osp::Base::Runtime::Monitor m_Monitor; -#else - Mutex m_Mutex; - #if defined(OMIM_OS_WINDOWS_NATIVE) - CONDITION_VARIABLE m_Condition; - #else - pthread_cond_t m_Condition; - #endif -#endif + class Impl; + Impl * m_pImpl; - public: - Condition() - { -#if defined(OMIM_OS_BADA) - m_Monitor.Construct(); -#elif defined(OMIM_OS_WINDOWS_NATIVE) - ::InitializeConditionVariable(&m_Condition); -#else - ::pthread_cond_init(&m_Condition, 0); -#endif - } + public: + Condition(); + ~Condition(); - ~Condition() - { -#if !defined(OMIM_OS_WINDOWS_NATIVE) && !defined(OMIM_OS_BADA) - // only for pthreads - ::pthread_cond_destroy(&m_Condition); -#endif - } - - void Signal() - { -#if defined(OMIM_OS_BADA) - m_Monitor.Notify(); -#elif defined(OMIM_OS_WINDOWS_NATIVE) - ::WakeConditionVariable(&m_Condition); -#else - ::pthread_cond_signal(&m_Condition); -#endif - } - - void Wait() - { -#if defined(OMIM_OS_BADA) - m_Monitor.Wait(); -#elif defined(OMIM_OS_WINDOWS_NATIVE) - ::SleepConditionVariableCS(&m_Condition, &m_Mutex.m_Mutex, INFINITE); -#else - ::pthread_cond_wait(&m_Condition, &m_Mutex.m_Mutex); -#endif - } - - void Lock() - { -#if defined(OMIM_OS_BADA) - m_Monitor.Enter(); -#else - m_Mutex.Lock(); -#endif - } - - void Unlock() - { -#if defined(OMIM_OS_BADA) - m_Monitor.Exit(); -#else - m_Mutex.Unlock(); -#endif - } + void Signal(); + void Wait(); + void Lock(); + void Unlock(); }; /// ScopeGuard wrapper around mutex diff --git a/base/condition_bada.cpp b/base/condition_bada.cpp new file mode 100644 index 0000000000..68a683d55a --- /dev/null +++ b/base/condition_bada.cpp @@ -0,0 +1,48 @@ +#include "../std/target_os.hpp" + +#ifdef OMIM_OS_BADA + +#include "condition.hpp" + +#include + +namespace threads +{ + class Condition::Impl + { + public: + Osp::Base::Runtime::Monitor m_Monitor; + }; + + Condition::Condition() : m_pImpl(new Condition::Impl()) + { + m_pImpl->m_Monitor.Construct(); + } + + Condition::~Condition() + { + delete m_pImpl; + } + + void Condition::Signal() + { + m_pImpl->m_Monitor.Notify(); + } + + void Condition::Wait() + { + m_pImpl->m_Monitor.Wait(); + } + + void Condition::Lock() + { + m_pImpl->m_Monitor.Enter(); + } + + void Condition::Unlock() + { + m_pImpl->m_Monitor.Exit(); + } +} + +#endif // OMIM_OS_BADA diff --git a/base/condition_posix.cpp b/base/condition_posix.cpp new file mode 100644 index 0000000000..abbc1f5560 --- /dev/null +++ b/base/condition_posix.cpp @@ -0,0 +1,51 @@ +#include "../std/target_os.hpp" + +#if !defined(OMIM_OS_BADA) && !defined(OMIM_OS_WINDOWS_NATIVE) + +#include "condition.hpp" +#include "mutex.hpp" + +#include + +namespace threads +{ + class Condition::Impl + { + public: + Mutex m_Mutex; + pthread_cond_t m_Condition; + }; + + Condition::Condition() : m_pImpl(new Condition::Impl) + { + ::pthread_cond_init(&m_pImpl->m_Condition, 0); + } + + Condition::~Condition() + { + ::pthread_cond_destroy(&m_pImpl->m_Condition); + delete m_pImpl; + } + + void Condition::Signal() + { + ::pthread_cond_signal(&m_pImpl->m_Condition); + } + + void Condition::Wait() + { + ::pthread_cond_wait(&m_pImpl->m_Condition, &m_pImpl->m_Mutex.m_Mutex); + } + + void Condition::Lock() + { + m_pImpl->m_Mutex.Lock(); + } + + void Condition::Unlock() + { + m_pImpl->m_Mutex.Unlock(); + } +} + +#endif diff --git a/base/condition_windows_native.cpp b/base/condition_windows_native.cpp new file mode 100644 index 0000000000..8abced5c9f --- /dev/null +++ b/base/condition_windows_native.cpp @@ -0,0 +1,51 @@ +#include "../std/target_os.hpp" + +#ifdef OMIM_OS_WINDOWS_NATIVE + +#include "condition.hpp" +#include "mutex.hpp" + +#include "../std/windows.hpp" + +namespace threads +{ + /// Implements mutexed condition semantics + class Condition::Impl + { + public: + CONDITION_VARIABLE m_Condition; + Mutex m_mutex; + }; + + Condition::Condition() : m_pImpl(new Condition::Impl()) + { + ::InitializeConditionVariable(&m_pImpl->m_Condition); + } + + Condition::~Condition() + { + delete m_pImpl; + } + + void Condition::Signal() + { + ::WakeConditionVariable(&m_pImpl->m_Condition); + } + + void Condition::Wait() + { + ::SleepConditionVariableCS(&m_pImpl->m_Condition, &m_pImpl->m_mutex.m_Mutex, INFINITE); + } + + void Condition::Lock() + { + m_pImpl->m_mutex.Lock(); + } + + void Condition::Unlock() + { + m_pImpl->m_mutex.Unlock(); + } +} + +#endif // OMIM_OS_WINDOWS_NATIVE