]> git.sesse.net Git - stockfish/blob - src/thread_win32_osx.h
Simplify connected pawn scoring
[stockfish] / src / thread_win32_osx.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   Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
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.
11
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.
16
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/>.
19 */
20
21 #ifndef THREAD_WIN32_OSX_H_INCLUDED
22 #define THREAD_WIN32_OSX_H_INCLUDED
23
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.
33
34 #include <condition_variable>
35 #include <mutex>
36 #include <thread>
37
38 #if defined(_WIN32) && !defined(_MSC_VER)
39
40 #ifndef NOMINMAX
41 #  define NOMINMAX // Disable macros min() and max()
42 #endif
43
44 #define WIN32_LEAN_AND_MEAN
45 #include <windows.h>
46 #undef WIN32_LEAN_AND_MEAN
47 #undef NOMINMAX
48
49 /// Mutex and ConditionVariable struct are wrappers of the low level locking
50 /// machinery and are modeled after the corresponding C++11 classes.
51
52 struct Mutex {
53   Mutex() { InitializeCriticalSection(&cs); }
54  ~Mutex() { DeleteCriticalSection(&cs); }
55   void lock() { EnterCriticalSection(&cs); }
56   void unlock() { LeaveCriticalSection(&cs); }
57
58 private:
59   CRITICAL_SECTION cs;
60 };
61
62 typedef std::condition_variable_any ConditionVariable;
63
64 #else // Default case: use STL classes
65
66 typedef std::mutex Mutex;
67 typedef std::condition_variable ConditionVariable;
68
69 #endif
70
71 /// On OSX threads other than the main thread are created with a reduced stack
72 /// size of 512KB by default, this is dangerously low for deep searches, so
73 /// adjust it to TH_STACK_SIZE. The implementation calls pthread_create() with
74 /// proper stack size parameter.
75
76 /// On toolchains where pthread is always available, ensure minimum stack size
77 /// instead of relying on linker defaults which may be platform-specific.
78
79 #if defined(__APPLE__) || defined(__MINGW32__) || defined(__MINGW64__)
80
81 #include <pthread.h>
82
83 static const size_t TH_STACK_SIZE = 8 * 1024 * 1024;
84
85 template <class T, class P = std::pair<T*, void(T::*)()>>
86 void* start_routine(void* ptr)
87 {
88    P* p = reinterpret_cast<P*>(ptr);
89    (p->first->*(p->second))(); // Call member function pointer
90    delete p;
91    return NULL;
92 }
93
94 class NativeThread {
95
96    pthread_t thread;
97
98 public:
99   template<class T, class P = std::pair<T*, void(T::*)()>>
100   explicit NativeThread(void(T::*fun)(), T* obj) {
101     pthread_attr_t attr_storage, *attr = &attr_storage;
102     pthread_attr_init(attr);
103     pthread_attr_setstacksize(attr, TH_STACK_SIZE);
104     pthread_create(&thread, attr, start_routine<T>, new P(obj, fun));
105   }
106   void join() { pthread_join(thread, NULL); }
107 };
108
109 #else // Default case: use STL classes
110
111 typedef std::thread NativeThread;
112
113 #endif
114
115 #endif // #ifndef THREAD_WIN32_OSX_H_INCLUDED