Skip to content

Commit bad18bc

Browse files
authored
Increase thread stack for OS X (#2035)
On OS X 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. Verified for no regression at STC enabling the patch on all platforms where pthread is supported. LLR: 2.95 (-2.94,2.94) [-3.00,1.00] Total: 50873 W: 9768 L: 9700 D: 31405 No functional change.
1 parent b8efa0d commit bad18bc

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

src/syzygy/tbprobe.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "../movegen.h"
3333
#include "../position.h"
3434
#include "../search.h"
35-
#include "../thread_win32.h"
35+
#include "../thread_win32_osx.h"
3636
#include "../types.h"
3737
#include "../uci.h"
3838

src/thread.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "pawns.h"
3333
#include "position.h"
3434
#include "search.h"
35-
#include "thread_win32.h"
35+
#include "thread_win32_osx.h"
3636

3737

3838
/// Thread class keeps together all the thread-related stuff. We use
@@ -46,7 +46,7 @@ class Thread {
4646
ConditionVariable cv;
4747
size_t idx;
4848
bool exit = false, searching = true; // Set before starting std::thread
49-
std::thread stdThread;
49+
NativeThread stdThread;
5050

5151
public:
5252
explicit Thread(size_t);
Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*/
2020

21-
#ifndef THREAD_WIN32_H_INCLUDED
22-
#define THREAD_WIN32_H_INCLUDED
21+
#ifndef THREAD_WIN32_OSX_H_INCLUDED
22+
#define THREAD_WIN32_OSX_H_INCLUDED
2323

2424
/// STL thread library used by mingw and gcc when cross compiling for Windows
2525
/// relies on libwinpthread. Currently libwinpthread implements mutexes directly
@@ -33,6 +33,7 @@
3333

3434
#include <condition_variable>
3535
#include <mutex>
36+
#include <thread>
3637

3738
#if defined(_WIN32) && !defined(_MSC_VER)
3839

@@ -67,4 +68,45 @@ typedef std::condition_variable ConditionVariable;
6768

6869
#endif
6970

70-
#endif // #ifndef THREAD_WIN32_H_INCLUDED
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

0 commit comments

Comments
 (0)