8236569: -Xss not multiple of 4K does not work for the main thread on macOS

Reviewed-by: dholmes, stuefe
This commit is contained in:
Adam Sotona 2022-06-01 06:13:29 +00:00
parent 7159976335
commit 7846971381
4 changed files with 43 additions and 2 deletions

View file

@ -651,6 +651,20 @@ static void* ThreadJavaMain(void* args) {
return (void*)(intptr_t)JavaMain(args);
}
static size_t adjustStackSize(size_t stack_size) {
long page_size = sysconf(_SC_PAGESIZE);
if (stack_size % page_size == 0) {
return stack_size;
} else {
long pages = stack_size / page_size;
// Ensure we don't go over limit
if (stack_size <= SIZE_MAX - page_size) {
pages++;
}
return page_size * pages;
}
}
/*
* Block current thread and continue execution in a new thread.
*/
@ -661,9 +675,17 @@ CallJavaMainInNewThread(jlong stack_size, void* args) {
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
size_t adjusted_stack_size;
if (stack_size > 0) {
pthread_attr_setstacksize(&attr, stack_size);
if (pthread_attr_setstacksize(&attr, stack_size) == EINVAL) {
// System may require stack size to be multiple of page size
// Retry with adjusted value
adjusted_stack_size = adjustStackSize(stack_size);
if (adjusted_stack_size != (size_t) stack_size) {
pthread_attr_setstacksize(&attr, adjusted_stack_size);
}
}
}
pthread_attr_setguardsize(&attr, 0); // no pthread guard page on java threads