mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 16:53:09 +00:00
Cleanup thread_win.h
No functional change.
This commit is contained in:
parent
04372316b3
commit
6027652773
1 changed files with 33 additions and 42 deletions
|
@ -20,15 +20,23 @@
|
||||||
#ifndef THREAD_WIN32_H_INCLUDED
|
#ifndef THREAD_WIN32_H_INCLUDED
|
||||||
#define THREAD_WIN32_H_INCLUDED
|
#define THREAD_WIN32_H_INCLUDED
|
||||||
|
|
||||||
/// STL thread library uded by gcc and mingw compilers is implemented above
|
/// STL thread library used by mingw and gcc when cross compiling for Windows
|
||||||
/// POSIX pthread. Unfortunatly this yields to a much slower speed (about 30%)
|
/// relies on libwinpthread. Currently libwinpthread implements mutexes directly
|
||||||
/// than the native Win32 calls. So use our own implementation that relies on
|
/// on top of Windows semaphores. Semaphores, being kernel objects, require kernel
|
||||||
/// the Windows specific low level calls.
|
/// mode transition in order to lock or unlock, which is very slow compared to
|
||||||
|
/// interlocked operations (about 30% slower on bench test). To workaround this
|
||||||
|
/// issue, we define our wrappers to the low level Win32 calls. We use critical
|
||||||
|
/// sections to support Windows XP and older versions. Unfortunately, cond_wait()
|
||||||
|
/// is racy between unlock() and WaitForSingleObject() but they have the same
|
||||||
|
/// speed performance of SRW locks.
|
||||||
|
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#if defined(_WIN32) && !defined(_MSC_VER)
|
#if defined(_WIN32) && !defined(_MSC_VER)
|
||||||
|
|
||||||
#ifndef NOMINMAX
|
#ifndef NOMINMAX
|
||||||
# define NOMINMAX // disable macros min() and max()
|
# define NOMINMAX // Disable macros min() and max()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
@ -36,58 +44,41 @@
|
||||||
#undef WIN32_LEAN_AND_MEAN
|
#undef WIN32_LEAN_AND_MEAN
|
||||||
#undef NOMINMAX
|
#undef NOMINMAX
|
||||||
|
|
||||||
// We use critical sections on Windows to support Windows XP and older versions.
|
|
||||||
// Unfortunately, cond_wait() is racy between lock_release() and WaitForSingleObject()
|
|
||||||
// but apart from this they have the same speed performance of SRW locks.
|
|
||||||
typedef CRITICAL_SECTION Lock;
|
|
||||||
typedef HANDLE WaitCondition;
|
|
||||||
typedef HANDLE NativeHandle;
|
|
||||||
|
|
||||||
// On Windows 95 and 98 parameter lpThreadId may not be null
|
|
||||||
inline DWORD* dwWin9xKludge() { static DWORD dw; return &dw; }
|
|
||||||
|
|
||||||
# define lock_init(x) InitializeCriticalSection(&(x))
|
|
||||||
# define lock_grab(x) EnterCriticalSection(&(x))
|
|
||||||
# define lock_release(x) LeaveCriticalSection(&(x))
|
|
||||||
# define lock_destroy(x) DeleteCriticalSection(&(x))
|
|
||||||
# define cond_init(x) { x = CreateEvent(0, FALSE, FALSE, 0); }
|
|
||||||
# define cond_destroy(x) CloseHandle(x)
|
|
||||||
# define cond_signal(x) SetEvent(x)
|
|
||||||
# define cond_wait(x,y) { lock_release(y); WaitForSingleObject(x, INFINITE); lock_grab(y); }
|
|
||||||
# define cond_timedwait(x,y,z) { lock_release(y); WaitForSingleObject(x,z); lock_grab(y); }
|
|
||||||
|
|
||||||
/// Mutex and ConditionVariable struct are wrappers of the low level locking
|
/// Mutex and ConditionVariable struct are wrappers of the low level locking
|
||||||
/// machinery and are modeled after the corresponding C++11 classes.
|
/// machinery and are modeled after the corresponding C++11 classes.
|
||||||
|
|
||||||
struct Mutex {
|
struct Mutex {
|
||||||
Mutex() { lock_init(l); }
|
Mutex() { InitializeCriticalSection(&cs); }
|
||||||
~Mutex() { lock_destroy(l); }
|
~Mutex() { DeleteCriticalSection(&cs); }
|
||||||
|
void lock() { EnterCriticalSection(&cs); }
|
||||||
void lock() { lock_grab(l); }
|
void unlock() { LeaveCriticalSection(&cs); }
|
||||||
void unlock() { lock_release(l); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend struct ConditionVariable;
|
CRITICAL_SECTION cs;
|
||||||
|
|
||||||
Lock l;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ConditionVariable {
|
struct ConditionVariable {
|
||||||
ConditionVariable() { cond_init(c); }
|
ConditionVariable() { hn = CreateEvent(0, FALSE, FALSE, 0); }
|
||||||
~ConditionVariable() { cond_destroy(c); }
|
~ConditionVariable() { CloseHandle(hn); }
|
||||||
|
void notify_one() { SetEvent(hn); }
|
||||||
|
|
||||||
void notify_one() { cond_signal(c); }
|
void wait(std::unique_lock<Mutex>& lk) {
|
||||||
void wait(std::unique_lock<Mutex>& lk) { cond_wait(c, lk.mutex()->l); }
|
lk.unlock();
|
||||||
|
WaitForSingleObject(hn, INFINITE);
|
||||||
|
lk.lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void wait_for(std::unique_lock<Mutex>& lk, const std::chrono::milliseconds& ms) {
|
||||||
|
lk.unlock();
|
||||||
|
WaitForSingleObject(hn, ms.count());
|
||||||
|
lk.lock();
|
||||||
|
}
|
||||||
|
|
||||||
template<class Predicate>
|
template<class Predicate>
|
||||||
void wait(std::unique_lock<Mutex>& lk, Predicate p) { while (!p()) this->wait(lk); }
|
void wait(std::unique_lock<Mutex>& lk, Predicate p) { while (!p()) this->wait(lk); }
|
||||||
|
|
||||||
void wait_for(std::unique_lock<Mutex>& lk, const std::chrono::milliseconds& ms) {
|
|
||||||
cond_timedwait(c, lk.mutex()->l, ms.count());
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
WaitCondition c;
|
HANDLE hn;
|
||||||
};
|
};
|
||||||
|
|
||||||
#else // Default case: use STL classes
|
#else // Default case: use STL classes
|
||||||
|
|
Loading…
Add table
Reference in a new issue