forked from organicmaps/organicmaps
Refactored condition and prepared it for next step - WinXP support
This commit is contained in:
parent
64cb846aad
commit
3e8ab21776
6 changed files with 169 additions and 79 deletions
|
@ -19,6 +19,7 @@ SOURCES += \
|
|||
shared_buffer_manager.cpp \
|
||||
memory_mapped_file.cpp \
|
||||
path_utils.cpp \
|
||||
condition.cpp \
|
||||
|
||||
HEADERS += \
|
||||
SRC_FIRST.hpp \
|
||||
|
|
9
base/condition.cpp
Normal file
9
base/condition.cpp
Normal file
|
@ -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
|
|
@ -1,91 +1,21 @@
|
|||
#pragma once
|
||||
|
||||
#include "../std/target_os.hpp"
|
||||
|
||||
#if defined(OMIM_OS_BADA)
|
||||
#include <FBaseRtThreadMonitor.h>
|
||||
#else
|
||||
#include "mutex.hpp"
|
||||
#if !defined(OMIM_OS_WINDOWS_NATIVE)
|
||||
#include <pthread.h>
|
||||
#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
|
||||
|
|
48
base/condition_bada.cpp
Normal file
48
base/condition_bada.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
#include "../std/target_os.hpp"
|
||||
|
||||
#ifdef OMIM_OS_BADA
|
||||
|
||||
#include "condition.hpp"
|
||||
|
||||
#include <FBaseRtThreadMonitor.h>
|
||||
|
||||
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
|
51
base/condition_posix.cpp
Normal file
51
base/condition_posix.cpp
Normal file
|
@ -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 <pthread.h>
|
||||
|
||||
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
|
51
base/condition_windows_native.cpp
Normal file
51
base/condition_windows_native.cpp
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue