Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase thread stack for OS X #2035

Merged
merged 5 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Thread {
ConditionVariable cv;
size_t idx;
bool exit = false, searching = true; // Set before starting std::thread
std::thread stdThread;
NativeThread stdThread;

public:
explicit Thread(size_t);
Expand Down
43 changes: 43 additions & 0 deletions src/thread_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,47 @@ typedef std::condition_variable ConditionVariable;

#endif

#if !defined(_MSC_VER) // defined(__APPLE__) FIXME when done with testing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXME... APPLE only?


#include <pthread.h>

/// 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.

static const size_t TH_STACK_SIZE = 2 * 1024 * 1024;

template <class Fun, class Obj, class P = std::pair<Fun, Obj*>>
void* start_routine(void* ptr)
{
P* p = reinterpret_cast<P*>(ptr);
(p->second->*(p->first))(); // Call member function pointer
delete p;
return NULL;
}

class NativeThread {

pthread_t thread;

public:
template<class Fun, class Obj>
explicit NativeThread(Fun fun, Obj* obj) {
auto ptr = new std::pair<Fun, Obj*>(fun, obj); // Should survive to us
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<Fun, Obj>, (void*)ptr);
mcostalba marked this conversation as resolved.
Show resolved Hide resolved
}
void join() { pthread_join(thread, NULL); }
};

#else // Default case: use STL classes

typedef std::thread NativeThread;

#endif


#endif // #ifndef THREAD_WIN32_H_INCLUDED