]> git.sesse.net Git - stockfish/blob - src/thread_win32.h
Simplify outpost code
[stockfish] / src / thread_win32.h
1 /*
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
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.
10
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.
15
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/>.
18 */
19
20 #ifndef THREAD_WIN32_H_INCLUDED
21 #define THREAD_WIN32_H_INCLUDED
22
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>
35
36 #if defined(_WIN32) && !defined(_MSC_VER)
37
38 #ifndef NOMINMAX
39 #  define NOMINMAX // Disable macros min() and max()
40 #endif
41
42 #define WIN32_LEAN_AND_MEAN
43 #include <windows.h>
44 #undef WIN32_LEAN_AND_MEAN
45 #undef NOMINMAX
46
47 /// Mutex and ConditionVariable struct are wrappers of the low level locking
48 /// machinery and are modeled after the corresponding C++11 classes.
49
50 struct Mutex {
51   Mutex() { InitializeCriticalSection(&cs); }
52  ~Mutex() { DeleteCriticalSection(&cs); }
53   void lock() { EnterCriticalSection(&cs); }
54   void unlock() { LeaveCriticalSection(&cs); }
55
56 private:
57   CRITICAL_SECTION cs;
58 };
59
60 typedef std::condition_variable_any ConditionVariable;
61
62 #else // Default case: use STL classes
63
64 typedef std::mutex Mutex;
65 typedef std::condition_variable ConditionVariable;
66
67 #endif
68
69 #endif // #ifndef THREAD_WIN32_H_INCLUDED