-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaitCondition.cpp
319 lines (261 loc) · 9.59 KB
/
WaitCondition.cpp
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/***************************************************************************
WaitCondition.cpp - description
-------------------
begin : Sat Mar 12 2011
copyright : (C) 2011 by Knut-Helge Vik
email : knuthelge@vik.gs
***************************************************************************/
#include <stdio.h>
#include <exception>
#include <iostream>
#include "BaseLib/WaitCondition.h"
#include "BaseLib/Thread.h"
#include "BaseLib/Debug.h"
#include "BaseLib/Utility.h"
#include "BaseLib/ReadWriteLock.h"
#include "BaseLib/Timestamp.h"
#include "Exception.h"
namespace BaseLib
{
static void report_error(int code, const char *where, const char *what)
{
if (code != 0)
iWarning("%s: %s failure: %i", where, what, code);
}
static void deleteSelf(WaitConditionPrivate *self);
#ifdef USE_GCC
class WaitConditionPrivate
{
public:
WaitConditionPrivate()
: waiters(0)
, wakeups(0)
{}
virtual ~WaitConditionPrivate()
{
deleteSelf(this);
}
pthread_mutex_t mutex;
pthread_cond_t cond;
int waiters;
int wakeups;
bool wait(int64 msecs)
{
ElapsedTimer timer(msecs);
int code = -1;
bool waited = false;
while((wakeups <= 0 && code != 0) && !timer.HasExpired() && !Thread::isInterrupted())
{
// if(msecs != LONG_MAX)
// {
struct timespec ts = {0,0};
int init = Utility::ClockInitEpochWaitTimeMsec(ts, std::min<int64>(5000, timer.Remaining().InMillis()));
ASSERT(init == 0);
/**
Except in the case of [ETIMEDOUT], all these error checks shall act as if they were performed
immediately at the beginning of processing for the function and shall cause an error return,
in effect, prior to modifying the state of the mutex specified by mutex or the condition variable
specified by cond.
Upon successful completion, a value of zero shall be returned; otherwise, an error number shall
be returned to indicate the error.
*/
code = pthread_cond_timedwait(&cond, &mutex, &ts);
// -- debug --
//if(code == ETIMEDOUT)
// IDEBUG() << "The time specified by abstime to pthread_cond_timedwait() has passed";
if(code == EINVAL) {
ICRITICAL() << "Wait time: " << msecs << ". Remining " << timer.Remaining() << ". Error: The value specified by abstime is invalid: ts(" << ts.tv_sec << "," << ts.tv_nsec << ")";
ASSERT(code != EINVAL);
}
else if(code == EPERM) {
iCritical("The mutex was not owned by the current thread at the time of the call");
ASSERT(code != EPERM);
}
else if(code == EDEADLK) {
iCritical("Thread::wait(): EDEADLK A deadlock was detected or the value of thread specifies the calling thread.");
ASSERT(code != EDEADLK);
}
// -- debug --
// }
// else
// {
// code = pthread_cond_wait(&cond, &mutex);
// }
waited = true;
}
ASSERT(waiters > 0);
--waiters;
if(code == 0 && !Thread::isInterrupted())
{
ASSERT(wakeups > 0);
wakeups = std::max<int>(0, --wakeups);
}
report_error(pthread_mutex_unlock(&mutex), "WaitCondition::wait()", "mutex unlock");
if (code && code != ETIMEDOUT && waited)
report_error(code, "WaitCondition::wait()", "cv wait");
return (code == 0);
}
};
#elif WIN32
class WaitConditionPrivate
{
public:
WaitConditionPrivate()
: waiters(0)
, wakeups(0)
{}
virtual ~WaitConditionPrivate()
{
deleteSelf(this);
}
CRITICAL_SECTION criticalSection;
CONDITION_VARIABLE conditionVariable;
int waiters;
int wakeups;
bool wait(int64 msecs)
{
ElapsedTimer timer(msecs);
BOOL code = 0;
while( (wakeups <= 0 && code == 0) && !timer.HasExpired() && !Thread::isInterrupted())
{
/*--------------------------------------------------------------
BOOL WINAPI SleepConditionVariableCS(
__inout PCONDITION_VARIABLE ConditionVariable,
__inout PCRITICAL_SECTION CriticalSection,
__in DWORD dwMilliseconds
);
If the function succeeds, the return value is nonzero.
If the function fails or the time-out interval elapses, the return value is zero.
To get extended error information, call GetLastError. Possible error codes include
ERROR_TIMEOUT, which indicates that the time-out interval has elapsed before another
thread has attempted to wake the sleeping thread.
--------------------------------------------------------------*/
//if(msecs == LONG_MAX)
//{
// code = SleepConditionVariableCS(&conditionVariable, &criticalSection, INFINITE);
//}
//else
//{
code = SleepConditionVariableCS(&conditionVariable, &criticalSection, std::min<int64>(5000, timer.Remaining().InMillis()));
//}
//IDEBUG() << "waiters = " << waiters << ", wakeups = " << wakeups << ", code = " << code;
}
ASSERT(waiters > 0);
--waiters;
ASSERT(waiters >= 0);
if(code != 0)
{
ASSERT(wakeups > 0);
wakeups = std::max<int>(0, --wakeups);
}
LeaveCriticalSection(&criticalSection);
return (code == 1);
}
};
#endif
static void deleteSelf(WaitConditionPrivate *self)
{
#ifdef USE_GCC
report_error(pthread_cond_destroy(&self->cond), "WaitCondition", "cv destroy");
report_error(pthread_mutex_destroy(&self->mutex), "WaitCondition", "mutex destroy");
#elif WIN32
//DeleteConditionVariable(&self->conditionVariable);
DeleteCriticalSection(&self->criticalSection);
#endif
}
WaitCondition::WaitCondition()
: self(new WaitConditionPrivate())
{
#ifdef USE_GCC
report_error(pthread_mutex_init(&self->mutex, NULL), "WaitCondition", "mutex init");
report_error(pthread_cond_init(&self->cond, NULL), "WaitCondition", "cv init");
self->waiters = self->wakeups = 0;
#elif WIN32
InitializeConditionVariable(&self->conditionVariable);
InitializeCriticalSection(&self->criticalSection);
self->waiters = self->wakeups = 0;
#endif
}
WaitCondition::~WaitCondition()
{ }
void WaitCondition::wakeOne()
{
#ifdef USE_GCC
report_error(pthread_mutex_lock(&self->mutex), "WaitCondition::wakeOne()", "mutex lock");
self->wakeups = std::min(self->wakeups + 1, self->waiters);
report_error(pthread_cond_signal(&self->cond), "WaitCondition::wakeOne()", "cv signal");
report_error(pthread_mutex_unlock(&self->mutex), "WaitCondition::wakeOne()", "mutex unlock");
#elif WIN32
EnterCriticalSection(&self->criticalSection);
self->wakeups = std::min(self->wakeups + 1, self->waiters);
WakeConditionVariable(&self->conditionVariable);
LeaveCriticalSection(&self->criticalSection);
#endif
}
void WaitCondition::wakeAll()
{
#ifdef USE_GCC
report_error(pthread_mutex_lock(&self->mutex), "WaitCondition::wakeAll()", "mutex lock");
self->wakeups = self->waiters;
report_error(pthread_cond_broadcast(&self->cond), "WaitCondition::wakeAll()", "cv broadcast");
report_error(pthread_mutex_unlock(&self->mutex), "WaitCondition::wakeAll()", "mutex unlock");
#elif WIN32
EnterCriticalSection(&self->criticalSection);
self->wakeups = self->waiters;
WakeAllConditionVariable(&self->conditionVariable);
LeaveCriticalSection(&self->criticalSection);
#endif
}
bool WaitCondition::wait(Mutex *mutex, int64 msecs)
{
if(!mutex) return false;
#ifdef USE_GCC
report_error(pthread_mutex_lock(&self->mutex), "WaitCondition::wait()", "mutex lock");
#elif WIN32
EnterCriticalSection(&self->criticalSection);
#endif
++self->waiters;
mutex->unlock();
if(mutex->isDebug())
{
Thread* thread_self = Thread::currentThread();
if(thread_self)
thread_self->setThreadState(ThreadDetails::MonitoringState);
}
bool isWoken = self->wait(msecs);
mutex->lock();
return isWoken;
}
bool WaitCondition::wait(ReadWriteLock *readWriteLock, int64 msecs)
{
if(!readWriteLock || readWriteLock->accessCount_ == 0) return false;
#ifdef USE_GCC
report_error(pthread_mutex_lock(&self->mutex), "WaitCondition::wait()", "mutex lock");
#elif WIN32
EnterCriticalSection(&self->criticalSection);
#endif
++self->waiters;
int previousAccessCount = readWriteLock->accessCount_;
readWriteLock->unlock();
bool isWoken = self->wait(msecs);
if (previousAccessCount < 0)
readWriteLock->lockForWrite();
else
readWriteLock->lockForRead();
return isWoken;
}
int WaitCondition::numWaiters() const
{
int numWaiters = 0;
#ifdef USE_GCC
report_error(pthread_mutex_lock(&self->mutex), "WaitCondition::numWaiters() const", "mutex lock");
numWaiters = self->waiters;
report_error(pthread_mutex_unlock(&self->mutex), "WaitCondition::numWaiters() const", "mutex unlock");
#elif WIN32
EnterCriticalSection(&self->criticalSection);
numWaiters = self->waiters;
LeaveCriticalSection(&self->criticalSection);
#endif
return numWaiters;
}
}