X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fthread_win32_osx.h;fp=src%2Fthread_win32.h;h=8890054020459b0d57ca445ed4a17b9ab6c4c66d;hp=5c914df39986fe14b1847ef1fd79e034ba43d897;hb=bad18bccb60c874410edd3f61624696d3abc3cbc;hpb=b8efa0daac897e1b8f3efb9ca09ec4151492f1e0 diff --git a/src/thread_win32.h b/src/thread_win32_osx.h similarity index 66% rename from src/thread_win32.h rename to src/thread_win32_osx.h index 5c914df3..88900540 100644 --- a/src/thread_win32.h +++ b/src/thread_win32_osx.h @@ -18,8 +18,8 @@ along with this program. If not, see . */ -#ifndef THREAD_WIN32_H_INCLUDED -#define THREAD_WIN32_H_INCLUDED +#ifndef THREAD_WIN32_OSX_H_INCLUDED +#define THREAD_WIN32_OSX_H_INCLUDED /// STL thread library used by mingw and gcc when cross compiling for Windows /// relies on libwinpthread. Currently libwinpthread implements mutexes directly @@ -33,6 +33,7 @@ #include #include +#include #if defined(_WIN32) && !defined(_MSC_VER) @@ -67,4 +68,45 @@ typedef std::condition_variable ConditionVariable; #endif -#endif // #ifndef THREAD_WIN32_H_INCLUDED +/// On OSX threads other than the main thread are created with a reduced stack +/// size of 512KB by default, this is dangerously low for deep searches, so +/// adjust it to TH_STACK_SIZE. The implementation calls pthread_create() with +/// proper stack size parameter. + +#if defined(__APPLE__) + +#include + +static const size_t TH_STACK_SIZE = 2 * 1024 * 1024; + +template > +void* start_routine(void* ptr) +{ + P* p = reinterpret_cast(ptr); + (p->first->*(p->second))(); // Call member function pointer + delete p; + return NULL; +} + +class NativeThread { + + pthread_t thread; + +public: + template> + explicit NativeThread(void(T::*fun)(), T* obj) { + pthread_attr_t attr_storage, *attr = &attr_storage; + pthread_attr_init(attr); + pthread_attr_setstacksize(attr, TH_STACK_SIZE); + pthread_create(&thread, attr, start_routine, new P(obj, fun)); + } + void join() { pthread_join(thread, NULL); } +}; + +#else // Default case: use STL classes + +typedef std::thread NativeThread; + +#endif + +#endif // #ifndef THREAD_WIN32_OSX_H_INCLUDED