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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
// mt - thread library to create C++ experiments -*- C++ -*-
// Copyright (C) 2010, 2012 David Capello
//
// Distributed under the terms of the New BSD License,
// see LICENSE.md for more details.
#ifndef MT_THREAD_HEADER_FILE_INCLUDED
#define MT_THREAD_HEADER_FILE_INCLUDED
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400 // From Windows 2000
#endif
#include <windows.h>
#include <cassert>
#include <string>
#include <exception>
namespace mt {
//////////////////////////////////////////////////////////////////////
// lock_guard class
template<class Mutex>
class lock_guard {
public:
typedef Mutex mutex_type;
explicit lock_guard(mutex_type& mutex) : m_mutex(mutex) {
m_mutex.lock();
}
~lock_guard() {
m_mutex.unlock();
}
void lock() {
m_mutex.lock();
}
void unlock() {
m_mutex.unlock();
}
private:
mutex_type& m_mutex;
// Non-copyable
lock_guard(const lock_guard&);
lock_guard& operator=(const lock_guard&);
};
//////////////////////////////////////////////////////////////////////
// mutex class
class mutex {
public:
mutex() {
InitializeCriticalSection(&m_cs);
}
~mutex() {
DeleteCriticalSection(&m_cs);
}
void lock() {
EnterCriticalSection(&m_cs);
}
bool try_lock() {
return TryEnterCriticalSection(&m_cs) ? true: false;
}
void unlock() {
LeaveCriticalSection(&m_cs);
}
private:
CRITICAL_SECTION m_cs;
// Non-copyable
mutex(const mutex&);
mutex& operator=(const mutex&);
};
//////////////////////////////////////////////////////////////////////
// condition_variable class
class condition_variable {
public:
condition_variable() {
m_waiting_queue =
CreateSemaphore(NULL, // Security attributes
0, // Initial count
1000, // Maximum number of waiters (TODO)
NULL); // Unnamed semaphore
m_waiters = 0;
}
~condition_variable() {
// Here we could call notify_all, but, if a condition variable
// is destroyed, "there shall be no thread blocked on *this"
// (see 30.5.1 C++ working draft n3035)
assert(m_waiters == 0);
CloseHandle(m_waiting_queue);
}
void wait(lock_guard<mutex>& external_monitor) {
{
lock_guard<mutex> lock(m_monitor);
assert(m_waiters >= 0);
++m_waiters;
external_monitor.unlock();
}
::WaitForSingleObject(m_waiting_queue, INFINITE);
external_monitor.lock();
}
void notify_one() {
lock_guard<mutex> lock(m_monitor);
assert(m_waiters >= 0);
// If there are one or more waiters...
if (m_waiters > 0) {
// Increment semaphore count to unlock a waiting thread
ReleaseSemaphore(m_waiting_queue, 1, NULL);
--m_waiters;
}
}
void notify_all() {
lock_guard<mutex> lock(m_monitor);
assert(m_waiters >= 0);
// If there are one or more waiters...
if (m_waiters > 0) {
// Increment the semaphore to the number of waiting threads
ReleaseSemaphore(m_waiting_queue, m_waiters, NULL);
m_waiters = 0;
}
}
private:
mutex m_monitor; // To avoid running two condition_variable member function at the same time
HANDLE m_waiting_queue; // Queue of waiting threads
LONG m_waiters; // Number of waiters in the queue
// Non-copyable
condition_variable(const condition_variable&);
condition_variable& operator=(const condition_variable&);
};
//////////////////////////////////////////////////////////////////////
// ultra-simplistic thread class implementation based on C++0x
class thread {
public:
class details;
class id {
friend class thread;
friend class details;
DWORD m_native_id;
id(DWORD id) : m_native_id(id) { }
public:
id() : m_native_id(0) { }
bool operator==(const id& y) const { return m_native_id == y.m_native_id; }
bool operator!=(const id& y) const { return m_native_id != y.m_native_id; }
bool operator< (const id& y) const { return m_native_id < y.m_native_id; }
bool operator<=(const id& y) const { return m_native_id <= y.m_native_id; }
bool operator> (const id& y) const { return m_native_id > y.m_native_id; }
bool operator>=(const id& y) const { return m_native_id >= y.m_native_id; }
// TODO should we replace this with support for iostreams?
DWORD get_native_id() { return m_native_id; }
};
typedef HANDLE native_handle_type;
private:
template<class Callable>
struct f_wrapper0 {
Callable f;
f_wrapper0(const Callable& f) : f(f) { }
void operator()() { f(); }
};
template<class Callable, class A>
struct f_wrapper1 {
Callable f;
A a;
f_wrapper1(const Callable& f, A a) : f(f), a(a) { }
void operator()() { f(a); }
};
template<class Callable, class A, class B>
struct f_wrapper2 {
Callable f;
A a;
B b;
f_wrapper2(const Callable& f, A a, B b) : f(f), a(a), b(b) { }
void operator()() { f(a, b); }
};
template<class T>
static DWORD WINAPI thread_proxy(LPVOID data) {
T* t = (T*)data;
(*t)();
delete t;
return 0;
}
native_handle_type m_native_handle;
id m_id;
public:
// Create an instance to represent the current thread
thread()
: m_native_handle(NULL)
, m_id() {
}
// Create a new thread without arguments
template<class Callable>
thread(const Callable& f) {
m_native_handle =
CreateThread(NULL, 0,
thread_proxy<f_wrapper0<Callable> >,
(LPVOID)new f_wrapper0<Callable>(f),
CREATE_SUSPENDED, &m_id.m_native_id);
ResumeThread(m_native_handle);
}
// Create a new thread with one argument
template<class Callable, class A>
thread(const Callable& f, A a) {
m_native_handle =
CreateThread(NULL, 0,
thread_proxy<f_wrapper1<Callable, A> >,
(LPVOID)new f_wrapper1<Callable, A>(f, a),
CREATE_SUSPENDED, &m_id.m_native_id);
ResumeThread(m_native_handle);
}
// Create a new thread with two arguments
template<class Callable, class A, class B>
thread(const Callable& f, A a, B b) {
m_native_handle =
CreateThread(NULL, 0,
thread_proxy<f_wrapper2<Callable, A, B> >,
(LPVOID)new f_wrapper2<Callable, A, B>(f, a, b),
CREATE_SUSPENDED, &m_id.m_native_id);
ResumeThread(m_native_handle);
}
~thread() {
if (joinable())
detach();
}
bool joinable() const {
return
m_native_handle != NULL &&
m_id.m_native_id != ::GetCurrentThreadId();
}
void join() {
if (joinable()) {
::WaitForSingleObject(m_native_handle, INFINITE);
detach();
}
}
void detach() {
::CloseHandle(m_native_handle);
m_native_handle = NULL;
m_id = id();
}
id get_id() const {
return m_id;
}
native_handle_type native_handle() {
return m_native_handle;
}
class details {
public:
static id get_current_thread_id() {
return id(::GetCurrentThreadId());
}
};
};
//////////////////////////////////////////////////////////////////////
// this_thread namespace
namespace this_thread {
inline thread::id get_id() {
return thread::details::get_current_thread_id();
}
inline void yield() {
::Sleep(0);
}
// Simplified API (here we do not implement duration/time_point C++0x classes
inline void sleep_for(int milliseconds) {
::Sleep(milliseconds);
}
} // namespace this_thread
//////////////////////////////////////////////////////////////////////
// thread_guard class
class thread_guard {
public:
explicit thread_guard(thread& t) : m_thread(t) { }
~thread_guard() {
if (m_thread.joinable())
m_thread.join();
}
private:
thread& m_thread;
};
} // namespace mt
#endif // MT_THREAD_HEADER_FILE_INCLUDED
|