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 | #if defined(__APPLE__) |
77 | |
78 | #include <pthread.h> |
79 | |
80 | static const size_t TH_STACK_SIZE = 2 * 1024 * 1024; |
81 | |
82 | template <class T, class P = std::pair<T*, void(T::*)()>> |
83 | void* start_routine(void* ptr) |
84 | { |
85 | P* p = reinterpret_cast<P*>(ptr); |
86 | (p->first->*(p->second))(); // Call member function pointer |
87 | delete p; |
88 | return NULL; |
89 | } |
90 | |
91 | class NativeThread { |
92 | |
93 | pthread_t thread; |
94 | |
95 | public: |
96 | template<class T, class P = std::pair<T*, void(T::*)()>> |
97 | explicit NativeThread(void(T::*fun)(), T* obj) { |
98 | pthread_attr_t attr_storage, *attr = &attr_storage; |
99 | pthread_attr_init(attr); |
100 | pthread_attr_setstacksize(attr, TH_STACK_SIZE); |
101 | pthread_create(&thread, attr, start_routine<T>, new P(obj, fun)); |
102 | } |
103 | void join() { pthread_join(thread, NULL); } |
104 | }; |
105 | |
106 | #else // Default case: use STL classes |
107 | |
108 | typedef std::thread NativeThread; |
109 | |
110 | #endif |
111 | |
112 | #endif // #ifndef THREAD_WIN32_OSX_H_INCLUDED |
113 | |