2013-09-05 02:17:46 +02:00
|
|
|
|
|
|
|
#ifndef CONDITION_VARIABLE_H_
|
|
|
|
#define CONDITION_VARIABLE_H_
|
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
#define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z))
|
2013-09-05 02:17:46 +02:00
|
|
|
#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
|
|
|
|
|
|
|
|
#ifndef __has_include
|
|
|
|
#define __has_include(s) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__
|
|
|
|
|
|
|
|
// GCC 4.4 provides <condition_variable>
|
|
|
|
#include <condition_variable>
|
|
|
|
|
|
|
|
#elif __has_include(<condition_variable>) && !ANDROID
|
|
|
|
|
|
|
|
// clang and libc++ provide <condition_variable> on OSX. However, the version
|
|
|
|
// of libc++ bundled with OSX 10.7 and 10.8 is buggy: it uses _ as a variable.
|
|
|
|
//
|
|
|
|
// We work around this issue by undefining and redefining _.
|
|
|
|
|
|
|
|
#undef _
|
|
|
|
#include <condition_variable>
|
|
|
|
#define _(s) wxGetTranslation((s))
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// partial std::condition_variable implementation for win32/pthread
|
|
|
|
|
2014-04-09 02:15:08 +02:00
|
|
|
#include "common/std_mutex.h"
|
2013-09-05 02:17:46 +02:00
|
|
|
|
|
|
|
#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__)
|
|
|
|
#define USE_RVALUE_REFERENCES
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_WIN32) && defined(_M_X64)
|
|
|
|
#define USE_CONDITION_VARIABLES
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
#define USE_EVENTS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
|
|
|
|
class condition_variable
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && defined(USE_CONDITION_VARIABLES)
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef CONDITION_VARIABLE native_type;
|
2013-09-05 02:17:46 +02:00
|
|
|
#elif defined(_WIN32)
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef HANDLE native_type;
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef pthread_cond_t native_type;
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
#ifdef USE_EVENTS
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef native_type native_handle_type;
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
typedef native_type* native_handle_type;
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
condition_variable()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#if defined(_WIN32) && defined(USE_CONDITION_VARIABLES)
|
2014-04-02 00:20:08 +02:00
|
|
|
InitializeConditionVariable(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#elif defined(_WIN32)
|
2014-04-02 00:20:08 +02:00
|
|
|
m_handle = CreateEvent(NULL, false, false, NULL);
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
pthread_cond_init(&m_handle, NULL);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
~condition_variable()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#if defined(_WIN32) && !defined(USE_CONDITION_VARIABLES)
|
2014-04-02 00:20:08 +02:00
|
|
|
CloseHandle(m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#elif !defined(_WIN32)
|
2014-04-02 00:20:08 +02:00
|
|
|
pthread_cond_destroy(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
condition_variable(const condition_variable&) /*= delete*/;
|
|
|
|
condition_variable& operator=(const condition_variable&) /*= delete*/;
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
void notify_one()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#if defined(_WIN32) && defined(USE_CONDITION_VARIABLES)
|
2014-04-02 00:20:08 +02:00
|
|
|
WakeConditionVariable(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#elif defined(_WIN32)
|
2014-04-02 00:20:08 +02:00
|
|
|
SetEvent(m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
pthread_cond_signal(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
void notify_all()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#if defined(_WIN32) && defined(USE_CONDITION_VARIABLES)
|
2014-04-02 00:20:08 +02:00
|
|
|
WakeAllConditionVariable(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#elif defined(_WIN32)
|
2014-04-02 00:20:08 +02:00
|
|
|
// TODO: broken
|
|
|
|
SetEvent(m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
pthread_cond_broadcast(&m_handle);
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
2014-04-02 00:20:08 +02:00
|
|
|
void wait(unique_lock<mutex>& lock)
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#ifdef _WIN32
|
2014-04-02 00:20:08 +02:00
|
|
|
#ifdef USE_SRWLOCKS
|
|
|
|
SleepConditionVariableSRW(&m_handle, lock.mutex()->native_handle(), INFINITE, 0);
|
|
|
|
#elif defined(USE_CONDITION_VARIABLES)
|
|
|
|
SleepConditionVariableCS(&m_handle, lock.mutex()->native_handle(), INFINITE);
|
|
|
|
#else
|
|
|
|
// TODO: broken, the unlock and wait need to be atomic
|
|
|
|
lock.unlock();
|
|
|
|
WaitForSingleObject(m_handle, INFINITE);
|
|
|
|
lock.lock();
|
|
|
|
#endif
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
pthread_cond_wait(&m_handle, lock.mutex()->native_handle());
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Predicate>
|
|
|
|
void wait(unique_lock<mutex>& lock, Predicate pred)
|
|
|
|
{
|
|
|
|
while (!pred())
|
|
|
|
wait(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
//template <class Clock, class Duration>
|
|
|
|
//cv_status wait_until(unique_lock<mutex>& lock,
|
|
|
|
// const chrono::time_point<Clock, Duration>& abs_time);
|
|
|
|
|
|
|
|
//template <class Clock, class Duration, class Predicate>
|
|
|
|
// bool wait_until(unique_lock<mutex>& lock,
|
|
|
|
// const chrono::time_point<Clock, Duration>& abs_time,
|
|
|
|
// Predicate pred);
|
|
|
|
|
|
|
|
//template <class Rep, class Period>
|
|
|
|
//cv_status wait_for(unique_lock<mutex>& lock,
|
|
|
|
// const chrono::duration<Rep, Period>& rel_time);
|
|
|
|
|
|
|
|
//template <class Rep, class Period, class Predicate>
|
|
|
|
// bool wait_for(unique_lock<mutex>& lock,
|
|
|
|
// const chrono::duration<Rep, Period>& rel_time,
|
|
|
|
// Predicate pred);
|
|
|
|
|
|
|
|
native_handle_type native_handle()
|
|
|
|
{
|
2013-09-05 02:17:46 +02:00
|
|
|
#ifdef USE_EVENTS
|
2014-04-02 00:20:08 +02:00
|
|
|
return m_handle;
|
2013-09-05 02:17:46 +02:00
|
|
|
#else
|
2014-04-02 00:20:08 +02:00
|
|
|
return &m_handle;
|
2013-09-05 02:17:46 +02:00
|
|
|
#endif
|
2014-04-02 00:20:08 +02:00
|
|
|
}
|
2013-09-05 02:17:46 +02:00
|
|
|
|
|
|
|
private:
|
2014-04-02 00:20:08 +02:00
|
|
|
native_type m_handle;
|
2013-09-05 02:17:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|