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
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_OSX_H_INCLUDED
22 #define THREAD_WIN32_OSX_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>
38 #if defined(_WIN32) && !defined(_MSC_VER)
41 # define NOMINMAX // Disable macros min() and max()
44 #define WIN32_LEAN_AND_MEAN
46 #undef WIN32_LEAN_AND_MEAN
49 /// Mutex and ConditionVariable struct are wrappers of the low level locking
50 /// machinery and are modeled after the corresponding C++11 classes.
53 Mutex() { InitializeCriticalSection(&cs); }
54 ~Mutex() { DeleteCriticalSection(&cs); }
55 void lock() { EnterCriticalSection(&cs); }
56 void unlock() { LeaveCriticalSection(&cs); }
62 typedef std::condition_variable_any ConditionVariable;
64 #else // Default case: use STL classes
66 typedef std::mutex Mutex;
67 typedef std::condition_variable ConditionVariable;
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.
76 #if defined(__APPLE__)
80 static const size_t TH_STACK_SIZE = 2 * 1024 * 1024;
82 template <class T, class P = std::pair<T*, void(T::*)()>>
83 void* start_routine(void* ptr)
85 P* p = reinterpret_cast<P*>(ptr);
86 (p->first->*(p->second))(); // Call member function pointer
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));
103 void join() { pthread_join(thread, NULL); }
106 #else // Default case: use STL classes
108 typedef std::thread NativeThread;
112 #endif // #ifndef THREAD_WIN32_OSX_H_INCLUDED