2014-02-10 18:54:46 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2013-03-29 14:42:41 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2013-03-29 14:42:41 +00:00
|
|
|
|
2014-02-17 04:51:41 +00:00
|
|
|
#define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z))
|
2013-03-29 14:42:41 +00:00
|
|
|
#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
|
|
|
|
|
2013-03-29 14:53:45 +00:00
|
|
|
#ifndef __has_include
|
|
|
|
#define __has_include(s) 0
|
|
|
|
#endif
|
|
|
|
|
2013-10-29 05:23:17 +00:00
|
|
|
#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__
|
2013-03-29 14:42:41 +00:00
|
|
|
|
|
|
|
// GCC 4.4 provides <condition_variable>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <condition_variable> // IWYU pragma: export
|
2013-03-29 14:42:41 +00:00
|
|
|
|
2013-04-19 03:52:05 +00:00
|
|
|
#elif __has_include(<condition_variable>) && !ANDROID
|
2013-03-29 14:42:41 +00:00
|
|
|
|
|
|
|
// 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 _.
|
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <condition_variable> // IWYU pragma: export
|
2013-03-29 14:42:41 +00:00
|
|
|
|
2013-10-19 09:27:57 +00:00
|
|
|
#elif _MSC_VER >= 1700
|
|
|
|
|
|
|
|
// The standard implementation is included since VS2012
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <condition_variable> // IWYU pragma: export
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2013-03-29 14:42:41 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
// partial std::condition_variable implementation for win32/pthread
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/StdMutex.h"
|
2013-03-29 14:42:41 +00:00
|
|
|
|
|
|
|
#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__)
|
|
|
|
#define USE_RVALUE_REFERENCES
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
|
|
|
|
class condition_variable
|
|
|
|
{
|
2014-08-03 18:42:06 +00:00
|
|
|
#if defined(_WIN32)
|
2013-03-29 14:42:41 +00:00
|
|
|
typedef CONDITION_VARIABLE native_type;
|
|
|
|
#else
|
|
|
|
typedef pthread_cond_t native_type;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef native_type* native_handle_type;
|
|
|
|
|
|
|
|
condition_variable()
|
|
|
|
{
|
2014-08-03 18:42:06 +00:00
|
|
|
#if defined(_WIN32)
|
2013-03-29 14:42:41 +00:00
|
|
|
InitializeConditionVariable(&m_handle);
|
|
|
|
#else
|
2014-03-09 20:14:26 +00:00
|
|
|
pthread_cond_init(&m_handle, nullptr);
|
2013-03-29 14:42:41 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
~condition_variable()
|
|
|
|
{
|
2014-08-03 18:42:06 +00:00
|
|
|
#ifndef _WIN32
|
2013-03-29 14:42:41 +00:00
|
|
|
pthread_cond_destroy(&m_handle);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
condition_variable(const condition_variable&) /*= delete*/;
|
|
|
|
condition_variable& operator=(const condition_variable&) /*= delete*/;
|
|
|
|
|
|
|
|
void notify_one()
|
|
|
|
{
|
2014-08-03 18:42:06 +00:00
|
|
|
#if defined(_WIN32)
|
2013-03-29 14:42:41 +00:00
|
|
|
WakeConditionVariable(&m_handle);
|
|
|
|
#else
|
|
|
|
pthread_cond_signal(&m_handle);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void notify_all()
|
|
|
|
{
|
2014-08-03 18:42:06 +00:00
|
|
|
#if defined(_WIN32)
|
2013-03-29 14:42:41 +00:00
|
|
|
WakeAllConditionVariable(&m_handle);
|
|
|
|
#else
|
|
|
|
pthread_cond_broadcast(&m_handle);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void wait(unique_lock<mutex>& lock)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
SleepConditionVariableSRW(&m_handle, lock.mutex()->native_handle(), INFINITE, 0);
|
|
|
|
#else
|
|
|
|
pthread_cond_wait(&m_handle, lock.mutex()->native_handle());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
return &m_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
native_type m_handle;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|