1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
/**
* @file condition_variable.h
* @brief std::condition_variable implementation for MinGW
*
* (c) 2013-2016 by Mega Limited, Auckland, New Zealand
* @author Alexander Vassilev
*
* @copyright Simplified (2-clause) BSD License.
* You should have received a copy of the license along with this
* program.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* @note
* This file may become part of the mingw-w64 runtime package. If/when this happens,
* the appropriate license will be added, i.e. this code will become dual-licensed,
* and the current BSD 2-clause license will stay.
*/
#ifndef MINGW_CONDITIONAL_VARIABLE_H
#define MINGW_CONDITIONAL_VARIABLE_H
#include <atomic>
#include <assert.h>
#include "mingw.mutex.h"
#include <chrono>
#include <system_error>
#include <windows.h>
#ifdef _GLIBCXX_HAS_GTHREADS
#error This version of MinGW seems to include a win32 port of pthreads, and probably \
already has C++11 std threading classes implemented, based on pthreads. \
It is likely that you will get errors about redefined classes, and unfortunately \
this implementation can not be used standalone and independent of the system <mutex>\
header, since it relies on it for \
std::unique_lock and other utility classes. If you would still like to use this \
implementation (as it is more lightweight), you have to edit the \
c++-config.h system header of your MinGW to not define _GLIBCXX_HAS_GTHREADS. \
This will prevent system headers from defining actual threading classes while still \
defining the necessary utility classes.
#endif
namespace std
{
enum class cv_status { no_timeout, timeout };
class condition_variable_any
{
protected:
recursive_mutex mMutex;
atomic<int> mNumWaiters;
HANDLE mWakeEvent;
#pragma pack(push, 1)
HANDLE mSemaphore;
HANDLE mTimer;
#pragma pack(pop)
public:
typedef HANDLE native_handle_type;
native_handle_type native_handle() {return mSemaphore;}
condition_variable_any(const condition_variable_any&) = delete;
condition_variable_any& operator=(const condition_variable_any&) = delete;
condition_variable_any()
:mNumWaiters(0), mWakeEvent(CreateEvent(NULL, FALSE, FALSE, NULL)),
mSemaphore(CreateSemaphore(NULL, 0, 0xFFFF, NULL)),
mTimer(CreateWaitableTimer(NULL, FALSE, NULL))
{}
~condition_variable_any() { CloseHandle(mWakeEvent); CloseHandle(mSemaphore); CloseHandle(mTimer); }
protected:
template <class M>
bool wait_impl(M& lock, DWORD timeout)
{
{
lock_guard<recursive_mutex> guard(mMutex);
mNumWaiters++;
}
lock.unlock();
DWORD ret = WaitForSingleObject(mSemaphore, timeout);
mNumWaiters--;
SetEvent(mWakeEvent);
lock.lock();
if (ret == WAIT_OBJECT_0)
return true;
else if (ret == WAIT_TIMEOUT)
return false;
//2 possible cases:
//1)The point in notify_all() where we determine the count to
//increment the semaphore with has not been reached yet:
//we just need to decrement mNumWaiters, but setting the event does not hurt
//
//2)Semaphore has just been released with mNumWaiters just before
//we decremented it. This means that the semaphore count
//after all waiters finish won't be 0 - because not all waiters
//woke up by acquiring the semaphore - we woke up by a timeout.
//The notify_all() must handle this grafecully
//
else
throw system_error(EPROTO, generic_category());
}
//SPRING
template <class M>
bool wait_impl_ns(M& lock, DWORD timeout)
{
{
lock_guard<recursive_mutex> guard(mMutex);
mNumWaiters++;
}
lock.unlock();
LARGE_INTEGER liDueTime;
liDueTime.QuadPart = - int64_t(timeout);
SetWaitableTimer(
mTimer, // Handle to the timer object.
&liDueTime, // When timer will become signaled.
0, // signal once.
NULL, // Completion routine.
NULL, // Argument to the completion routine.
FALSE ); // Do not restore a suspended system.
// Notice that mSemapore and mTimer are contigous in the struct so the following works
DWORD ret = WaitForMultipleObjects(2, &mSemaphore, FALSE, INFINITE);
mNumWaiters--;
SetEvent(mWakeEvent);
lock.lock();
if (ret == WAIT_OBJECT_0)
return true;
else if (ret == WAIT_TIMEOUT || ret == (WAIT_OBJECT_0 + 1))
return false;
else
throw system_error(EPROTO, generic_category());
}
public:
template <class M>
void wait(M& lock)
{
wait_impl(lock, INFINITE);
}
template <class M, class Predicate>
void wait(M& lock, Predicate pred)
{
while(!pred())
{
wait(lock);
};
}
void notify_all() noexcept
{
lock_guard<recursive_mutex> lock(mMutex); //block any further wait requests until all current waiters are unblocked
if (mNumWaiters.load() <= 0)
return;
ReleaseSemaphore(mSemaphore, mNumWaiters, NULL);
while(mNumWaiters > 0)
{
auto ret = WaitForSingleObject(mWakeEvent, 1000);
if ((ret == WAIT_FAILED) || (ret == WAIT_ABANDONED))
throw system_error(EPROTO, generic_category());
}
assert(mNumWaiters == 0);
//in case some of the waiters timed out just after we released the
//semaphore by mNumWaiters, it won't be zero now, because not all waiters
//woke up by acquiring the semaphore. So we must zero the semaphore before
//we accept waiters for the next event
//See _wait_impl for details
while(WaitForSingleObject(mSemaphore, 0) == WAIT_OBJECT_0);
}
void notify_one() noexcept
{
lock_guard<recursive_mutex> lock(mMutex);
if (!mNumWaiters)
return;
int targetWaiters = mNumWaiters.load() - 1;
ReleaseSemaphore(mSemaphore, 1, NULL);
while(mNumWaiters > targetWaiters)
{
auto ret = WaitForSingleObject(mWakeEvent, 1000);
if ((ret == WAIT_FAILED) || (ret == WAIT_ABANDONED))
throw system_error(EPROTO, generic_category());
}
assert(mNumWaiters == targetWaiters);
}
template <class M, class Rep, class Period>
std::cv_status wait_for(M& lock,
const std::chrono::duration<Rep, Period>& rel_time)
{
long long timeout = chrono::duration_cast<chrono::milliseconds>(rel_time).count();
if (timeout < 0)
timeout = 0;
bool ret = wait_impl(lock, (DWORD)timeout);
return ret?cv_status::no_timeout:cv_status::timeout;
}
template <class M, class Rep>
std::cv_status wait_for(M& lock,
const std::chrono::duration<Rep, std::nano>& rel_time)
{
int timeout = chrono::duration_cast<chrono::nanoseconds>(rel_time).count() / 100;
if (timeout < 0)
timeout = 0;
bool ret = wait_impl_ns(lock, (DWORD)timeout);
return ret?cv_status::no_timeout:cv_status::timeout;
}
template <class M, class Rep, class Period, class Predicate>
bool wait_for(M& lock,
const std::chrono::duration<Rep, Period>& rel_time, Predicate pred)
{
wait_for(lock, rel_time);
return pred();
}
template <class M, class Clock, class Duration>
cv_status wait_until (M& lock,
const chrono::time_point<Clock,Duration>& abs_time)
{
return wait_for(lock, abs_time - Clock::now());
}
template <class M, class Clock, class Duration, class Predicate>
bool wait_until (M& lock,
const std::chrono::time_point<Clock, Duration>& abs_time,
Predicate pred)
{
auto time = abs_time - Clock::now();
if (time < 0)
return pred();
else
return wait_for(lock, time, pred);
}
};
class condition_variable: protected condition_variable_any
{
protected:
typedef condition_variable_any base;
public:
using base::native_handle_type;
using base::native_handle;
using base::base;
using base::notify_all;
using base::notify_one;
void wait(unique_lock<mutex> &lock)
{ base::wait(lock); }
template <class Predicate>
void wait(unique_lock<mutex>& lock, Predicate pred)
{ base::wait(lock, pred); }
template <class Rep, class Period>
std::cv_status wait_for(unique_lock<mutex>& lock, const std::chrono::duration<Rep, Period>& rel_time)
{ return base::wait_for(lock, rel_time); }
template <class Rep, class Period, class Predicate>
bool wait_for(unique_lock<mutex>& lock, const std::chrono::duration<Rep, Period>& rel_time, Predicate pred)
{ return base::wait_for(lock, rel_time, pred); }
template <class Clock, class Duration>
cv_status wait_until (unique_lock<mutex>& lock, const chrono::time_point<Clock,Duration>& abs_time)
{ return base::wait_for(lock, abs_time); }
template <class Clock, class Duration, class Predicate>
bool wait_until (unique_lock<mutex>& lock, const std::chrono::time_point<Clock, Duration>& abs_time, Predicate pred)
{ return base::wait_until(lock, abs_time, pred); }
};
}
#endif // MINGW_CONDITIONAL_VARIABLE_H
|