forked from meganz/mingw-std-threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmingw.condition_variable.h
192 lines (187 loc) · 6.88 KB
/
mingw.condition_variable.h
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
/**
* @file mingw.condition_variable.h
* @brief std::condition_vaiable implementation for MinGW
*
* (c) 2014 by Mega Limited, Wellsford, New Zealand
*
* This file is part of the standard threads implementation for MinGW.
*
* 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.
*
* @copyright GNU LGPL version 2.1 License.
*
* You should have received a copy of the license along with this
* program.
*/
#ifndef MINGW_CONDITIONAL_VARIABLE_H
#define MINGW_CONDITIONAL_VARIABLE_H
#include <atomic>
#include <assert.h>
#include <condition_variable>
namespace std
{
enum class cv_status { no_timeout, timeout };
class condition_variable_any
{
protected:
recursive_mutex mMutex;
HANDLE mSemaphore;
HANDLE mWakeEvent;
atomic<int> mNumWaiters;
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), mSemaphore(CreateSemaphore(NULL, 0, 0xFFFF, NULL)),
mWakeEvent(CreateEvent(NULL, FALSE, FALSE, NULL))
{}
~condition_variable_any() { CloseHandle(mWakeEvent); CloseHandle(mSemaphore); }
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());
}
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, 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