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
5 Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
7 Stockfish is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Stockfish is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef THREAD_WIN32_H_INCLUDED
22 #define THREAD_WIN32_H_INCLUDED
24 /// STL thread library used by mingw and gcc when cross compiling for Windows
25 /// relies on libwinpthread. Currently libwinpthread implements mutexes directly
26 /// on top of Windows semaphores. Semaphores, being kernel objects, require kernel
27 /// mode transition in order to lock or unlock, which is very slow compared to
28 /// interlocked operations (about 30% slower on bench test). To work around this
29 /// issue, we define our wrappers to the low level Win32 calls. We use critical
30 /// sections to support Windows XP and older versions. Unfortunately, cond_wait()
31 /// is racy between unlock() and WaitForSingleObject() but they have the same
32 /// speed performance as the SRW locks.
34 #include <condition_variable>
37 #if defined(_WIN32) && !defined(_MSC_VER)
40 # define NOMINMAX // Disable macros min() and max()
43 #define WIN32_LEAN_AND_MEAN
45 #undef WIN32_LEAN_AND_MEAN
48 /// Mutex and ConditionVariable struct are wrappers of the low level locking
49 /// machinery and are modeled after the corresponding C++11 classes.
52 Mutex() { InitializeCriticalSection(&cs); }
53 ~Mutex() { DeleteCriticalSection(&cs); }
54 void lock() { EnterCriticalSection(&cs); }
55 void unlock() { LeaveCriticalSection(&cs); }
61 typedef std::condition_variable_any ConditionVariable;
63 #else // Default case: use STL classes
65 typedef std::mutex Mutex;
66 typedef std::condition_variable ConditionVariable;
70 #endif // #ifndef THREAD_WIN32_H_INCLUDED