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
|
#include "../globals.h"
/*
* A "lock-and-signal" pair. These are necessarily coupled on pthreads
* systems, and artificially coupled (by this file) on win32. Put
* together here to minimize ifdefs elsewhere; you must use them as
* if you're using a pthreads cvar+mutex pair.
*/
#include "lock_and_signal.h"
#if defined(__WIN32__)
lock_and_signal::lock_and_signal()
: alive(true)
{
// FIXME: In order to match the behavior of pthread_cond_broadcast on
// Windows, we create manual reset events. This however breaks the
// behavior of pthread_cond_signal, fixing this is quite involved:
// refer to: http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
_event = CreateEvent(NULL, TRUE, FALSE, NULL);
InitializeCriticalSection(&_cs);
}
#else
lock_and_signal::lock_and_signal()
: _locked(false), alive(true)
{
CHECKED(pthread_cond_init(&_cond, NULL));
CHECKED(pthread_mutex_init(&_mutex, NULL));
}
#endif
lock_and_signal::~lock_and_signal() {
#if defined(__WIN32__)
CloseHandle(_event);
#else
CHECKED(pthread_cond_destroy(&_cond));
CHECKED(pthread_mutex_destroy(&_mutex));
#endif
alive = false;
}
void lock_and_signal::lock() {
#if defined(__WIN32__)
EnterCriticalSection(&_cs);
_holding_thread = GetCurrentThreadId();
#else
CHECKED(pthread_mutex_lock(&_mutex));
_holding_thread = pthread_self();
#endif
_locked = true;
}
void lock_and_signal::unlock() {
_locked = false;
#if defined(__WIN32__)
LeaveCriticalSection(&_cs);
#else
CHECKED(pthread_mutex_unlock(&_mutex));
#endif
}
/**
* Wait indefinitely until condition is signaled.
*/
void lock_and_signal::wait() {
timed_wait(0);
}
bool lock_and_signal::timed_wait(size_t timeout_in_ms) {
_locked = false;
bool rv = true;
#if defined(__WIN32__)
LeaveCriticalSection(&_cs);
DWORD timeout = timeout_in_ms == 0 ? INFINITE : timeout_in_ms;
rv = WaitForSingleObject(_event, timeout) != WAIT_TIMEOUT;
EnterCriticalSection(&_cs);
_holding_thread = GetCurrentThreadId();
#else
if (timeout_in_ms == 0) {
CHECKED(pthread_cond_wait(&_cond, &_mutex));
} else {
timeval time_val;
gettimeofday(&time_val, NULL);
timespec time_spec;
time_spec.tv_sec = time_val.tv_sec + 0;
time_spec.tv_nsec = time_val.tv_usec * 1000 + timeout_in_ms * 1000000;
if(time_spec.tv_nsec >= 1000000000) {
time_spec.tv_sec++;
time_spec.tv_nsec -= 1000000000;
}
int cond_wait_status
= pthread_cond_timedwait(&_cond, &_mutex, &time_spec);
switch(cond_wait_status) {
case 0:
// successfully grabbed the lock.
break;
case ETIMEDOUT:
// Oops, we timed out.
rv = false;
break;
default:
// Error
CHECKED(cond_wait_status);
}
}
_holding_thread = pthread_self();
#endif
_locked = true;
return rv;
}
/**
* Signal condition, and resume the waiting thread.
*/
void lock_and_signal::signal() {
#if defined(__WIN32__)
SetEvent(_event);
#else
CHECKED(pthread_cond_signal(&_cond));
#endif
}
/**
* Signal condition, and resume all waiting threads.
*/
void lock_and_signal::signal_all() {
#if defined(__WIN32__)
SetEvent(_event);
#else
CHECKED(pthread_cond_broadcast(&_cond));
#endif
}
bool lock_and_signal::lock_held_by_current_thread()
{
#if defined(__WIN32__)
return _locked && _holding_thread == GetCurrentThreadId();
#else
return _locked && _holding_thread == pthread_self();
#endif
}
scoped_lock::scoped_lock(lock_and_signal &lock)
: lock(lock)
{
lock.lock();
}
scoped_lock::~scoped_lock()
{
lock.unlock();
}
//
// Local Variables:
// mode: C++
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
|