Skip to content

Commit 6027652

Browse files
committed
Cleanup thread_win.h
No functional change.
1 parent 0437231 commit 6027652

1 file changed

Lines changed: 33 additions & 42 deletions

File tree

src/thread_win32.h

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,74 +20,65 @@
2020
#ifndef THREAD_WIN32_H_INCLUDED
2121
#define THREAD_WIN32_H_INCLUDED
2222

23-
/// STL thread library uded by gcc and mingw compilers is implemented above
24-
/// POSIX pthread. Unfortunatly this yields to a much slower speed (about 30%)
25-
/// than the native Win32 calls. So use our own implementation that relies on
26-
/// the Windows specific low level calls.
23+
/// STL thread library used by mingw and gcc when cross compiling for Windows
24+
/// relies on libwinpthread. Currently libwinpthread implements mutexes directly
25+
/// on top of Windows semaphores. Semaphores, being kernel objects, require kernel
26+
/// mode transition in order to lock or unlock, which is very slow compared to
27+
/// interlocked operations (about 30% slower on bench test). To workaround this
28+
/// issue, we define our wrappers to the low level Win32 calls. We use critical
29+
/// sections to support Windows XP and older versions. Unfortunately, cond_wait()
30+
/// is racy between unlock() and WaitForSingleObject() but they have the same
31+
/// speed performance of SRW locks.
32+
33+
#include <condition_variable>
34+
#include <mutex>
2735

2836
#if defined(_WIN32) && !defined(_MSC_VER)
2937

3038
#ifndef NOMINMAX
31-
# define NOMINMAX // disable macros min() and max()
39+
# define NOMINMAX // Disable macros min() and max()
3240
#endif
3341

3442
#define WIN32_LEAN_AND_MEAN
3543
#include <windows.h>
3644
#undef WIN32_LEAN_AND_MEAN
3745
#undef NOMINMAX
3846

39-
// We use critical sections on Windows to support Windows XP and older versions.
40-
// Unfortunately, cond_wait() is racy between lock_release() and WaitForSingleObject()
41-
// but apart from this they have the same speed performance of SRW locks.
42-
typedef CRITICAL_SECTION Lock;
43-
typedef HANDLE WaitCondition;
44-
typedef HANDLE NativeHandle;
45-
46-
// On Windows 95 and 98 parameter lpThreadId may not be null
47-
inline DWORD* dwWin9xKludge() { static DWORD dw; return &dw; }
48-
49-
# define lock_init(x) InitializeCriticalSection(&(x))
50-
# define lock_grab(x) EnterCriticalSection(&(x))
51-
# define lock_release(x) LeaveCriticalSection(&(x))
52-
# define lock_destroy(x) DeleteCriticalSection(&(x))
53-
# define cond_init(x) { x = CreateEvent(0, FALSE, FALSE, 0); }
54-
# define cond_destroy(x) CloseHandle(x)
55-
# define cond_signal(x) SetEvent(x)
56-
# define cond_wait(x,y) { lock_release(y); WaitForSingleObject(x, INFINITE); lock_grab(y); }
57-
# define cond_timedwait(x,y,z) { lock_release(y); WaitForSingleObject(x,z); lock_grab(y); }
58-
5947
/// Mutex and ConditionVariable struct are wrappers of the low level locking
6048
/// machinery and are modeled after the corresponding C++11 classes.
6149

6250
struct Mutex {
63-
Mutex() { lock_init(l); }
64-
~Mutex() { lock_destroy(l); }
65-
66-
void lock() { lock_grab(l); }
67-
void unlock() { lock_release(l); }
51+
Mutex() { InitializeCriticalSection(&cs); }
52+
~Mutex() { DeleteCriticalSection(&cs); }
53+
void lock() { EnterCriticalSection(&cs); }
54+
void unlock() { LeaveCriticalSection(&cs); }
6855

6956
private:
70-
friend struct ConditionVariable;
71-
72-
Lock l;
57+
CRITICAL_SECTION cs;
7358
};
7459

7560
struct ConditionVariable {
76-
ConditionVariable() { cond_init(c); }
77-
~ConditionVariable() { cond_destroy(c); }
61+
ConditionVariable() { hn = CreateEvent(0, FALSE, FALSE, 0); }
62+
~ConditionVariable() { CloseHandle(hn); }
63+
void notify_one() { SetEvent(hn); }
64+
65+
void wait(std::unique_lock<Mutex>& lk) {
66+
lk.unlock();
67+
WaitForSingleObject(hn, INFINITE);
68+
lk.lock();
69+
}
7870

79-
void notify_one() { cond_signal(c); }
80-
void wait(std::unique_lock<Mutex>& lk) { cond_wait(c, lk.mutex()->l); }
71+
void wait_for(std::unique_lock<Mutex>& lk, const std::chrono::milliseconds& ms) {
72+
lk.unlock();
73+
WaitForSingleObject(hn, ms.count());
74+
lk.lock();
75+
}
8176

8277
template<class Predicate>
8378
void wait(std::unique_lock<Mutex>& lk, Predicate p) { while (!p()) this->wait(lk); }
8479

85-
void wait_for(std::unique_lock<Mutex>& lk, const std::chrono::milliseconds& ms) {
86-
cond_timedwait(c, lk.mutex()->l, ms.count());
87-
}
88-
8980
private:
90-
WaitCondition c;
81+
HANDLE hn;
9182
};
9283

9384
#else // Default case: use STL classes

0 commit comments

Comments
 (0)