2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef THREAD_WIN32_H_INCLUDED
21 #define THREAD_WIN32_H_INCLUDED
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.
28 #if defined(_WIN32) && !defined(_MSC_VER)
31 # define NOMINMAX // disable macros min() and max()
34 #define WIN32_LEAN_AND_MEAN
36 #undef WIN32_LEAN_AND_MEAN
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;
46 // On Windows 95 and 98 parameter lpThreadId may not be null
47 inline DWORD* dwWin9xKludge() { static DWORD dw; return &dw; }
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); }
59 /// Mutex and ConditionVariable struct are wrappers of the low level locking
60 /// machinery and are modeled after the corresponding C++11 classes.
63 Mutex() { lock_init(l); }
64 ~Mutex() { lock_destroy(l); }
66 void lock() { lock_grab(l); }
67 void unlock() { lock_release(l); }
70 friend struct ConditionVariable;
75 struct ConditionVariable {
76 ConditionVariable() { cond_init(c); }
77 ~ConditionVariable() { cond_destroy(c); }
79 void notify_one() { cond_signal(c); }
80 void wait(std::unique_lock<Mutex>& lk) { cond_wait(c, lk.mutex()->l); }
82 template<class Predicate>
83 void wait(std::unique_lock<Mutex>& lk, Predicate p) { while (!p()) this->wait(lk); }
85 void wait_for(std::unique_lock<Mutex>& lk, const std::chrono::milliseconds& ms) {
86 cond_timedwait(c, lk.mutex()->l, ms.count());
93 #else // Default case: use STL classes
95 typedef std::mutex Mutex;
96 typedef std::condition_variable ConditionVariable;
100 #endif // #ifndef THREAD_WIN32_H_INCLUDED