]> git.sesse.net Git - stockfish/blob - src/thread_win32.h
f0c33fb5a96110e92b312a9deb8526d31dae34db
[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 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.
27
28 #if defined(_WIN32) && !defined(_MSC_VER)
29
30 #ifndef NOMINMAX
31 #  define NOMINMAX // disable macros min() and max()
32 #endif
33
34 #define WIN32_LEAN_AND_MEAN
35 #include <windows.h>
36 #undef WIN32_LEAN_AND_MEAN
37 #undef NOMINMAX
38
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
59 /// Mutex and ConditionVariable struct are wrappers of the low level locking
60 /// machinery and are modeled after the corresponding C++11 classes.
61
62 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); }
68
69 private:
70   friend struct ConditionVariable;
71
72   Lock l;
73 };
74
75 struct ConditionVariable {
76   ConditionVariable() { cond_init(c); }
77  ~ConditionVariable() { cond_destroy(c); }
78
79   void notify_one() { cond_signal(c); }
80   void wait(std::unique_lock<Mutex>& lk) { cond_wait(c, lk.mutex()->l); }
81
82   template<class Predicate>
83   void wait(std::unique_lock<Mutex>& lk, Predicate p) { while (!p()) this->wait(lk); }
84
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
89 private:
90   WaitCondition c;
91 };
92
93 #else // Default case: use STL classes
94
95 typedef std::mutex Mutex;
96 typedef std::condition_variable ConditionVariable;
97
98 #endif
99
100 #endif // #ifndef THREAD_WIN32_H_INCLUDED