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

Use RLIMIT_STACK's current limit for Pthreads stack size if it is sane #2852

Merged
merged 1 commit into from
Aug 8, 2018
Merged
Changes from all 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
58 changes: 32 additions & 26 deletions src/libponyrt/platform/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ uint32_t ponyint_numa_node_of_cpu(uint32_t cpu)
bool ponyint_thread_create(pony_thread_id_t* thread, thread_fn start,
uint32_t cpu, void* arg)
{
bool ret = true;
(void)cpu;

#if defined(PLATFORM_IS_WINDOWS)
Expand All @@ -168,41 +169,46 @@ bool ponyint_thread_create(pony_thread_id_t* thread, thread_fn start,
return false;

*thread = (HANDLE)p;
#elif defined(PLATFORM_IS_LINUX)
#else
bool setstack_called = false;
struct rlimit limit;
pthread_attr_t attr;
pthread_attr_init(&attr);

if(cpu != (uint32_t)-1)
pthread_attr_t* attr_p = &attr;
pthread_attr_init(attr_p);

// Some systems, e.g., macOS, hav a different default default
// stack size than the typical system's RLIMIT_STACK.
// Let's use RLIMIT_STACK's current limit if it is sane.
if(getrlimit(RLIMIT_STACK, &limit) == 0 &&
limit.rlim_cur != RLIM_INFINITY &&
limit.rlim_cur >= PTHREAD_STACK_MIN)
{
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(cpu, &set);

if(use_numa)
#if defined(PLATFORM_IS_LINUX)
if(cpu != (uint32_t)-1 && use_numa)
{
struct rlimit limit;

if(getrlimit(RLIMIT_STACK, &limit) == 0)
{
int node = _numa_node_of_cpu(cpu);
void* stack = _numa_alloc_onnode((size_t)limit.rlim_cur, node);
if (stack != NULL) {
pthread_attr_setstack(&attr, stack, (size_t)limit.rlim_cur);
}
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(cpu, &set);

int node = _numa_node_of_cpu(cpu);
void* stack = _numa_alloc_onnode((size_t)limit.rlim_cur, node);
if (stack != NULL) {
pthread_attr_setstack(&attr, stack, (size_t)limit.rlim_cur);
setstack_called = true;
}
}
#endif
if(! setstack_called)
pthread_attr_setstacksize(&attr, (size_t)limit.rlim_cur);
} else {
attr_p = NULL;
}

if(pthread_create(thread, &attr, start, arg))
return false;

if(pthread_create(thread, attr_p, start, arg))
ret = false;
pthread_attr_destroy(&attr);
#else
if(pthread_create(thread, NULL, start, arg))
return false;
#endif

return true;
return ret;
}

bool ponyint_thread_join(pony_thread_id_t thread)
Expand Down