Skip to content

Commit 27a4243

Browse files
ystradmannDavid Holmes
authored andcommitted
8354560: Exponentially delay subsequent native thread creation in case of EAGAIN
Reviewed-by: dholmes, fbredberg
1 parent bd99525 commit 27a4243

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

src/hotspot/os/aix/os_aix.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,10 +760,23 @@ bool os::create_thread(Thread* thread, ThreadType thr_type,
760760
pthread_t tid = 0;
761761

762762
if (ret == 0) {
763-
int limit = 3;
764-
do {
763+
int trials_remaining = 4;
764+
useconds_t next_delay = 1000;
765+
while (true) {
765766
ret = pthread_create(&tid, &attr, (void* (*)(void*)) thread_native_entry, thread);
766-
} while (ret == EAGAIN && limit-- > 0);
767+
768+
if (ret != EAGAIN) {
769+
break;
770+
}
771+
772+
if (--trials_remaining <= 0) {
773+
break;
774+
}
775+
776+
log_debug(os, thread)("Failed to start native thread (%s), retrying after %dus.", os::errno_name(ret), next_delay);
777+
::usleep(next_delay);
778+
next_delay *= 2;
779+
}
767780
}
768781

769782
if (ret == 0) {

src/hotspot/os/bsd/os_bsd.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,10 +643,23 @@ bool os::create_thread(Thread* thread, ThreadType thr_type,
643643
ResourceMark rm;
644644
pthread_t tid;
645645
int ret = 0;
646-
int limit = 3;
647-
do {
646+
int trials_remaining = 4;
647+
useconds_t next_delay = 1000;
648+
while (true) {
648649
ret = pthread_create(&tid, &attr, (void* (*)(void*)) thread_native_entry, thread);
649-
} while (ret == EAGAIN && limit-- > 0);
650+
651+
if (ret != EAGAIN) {
652+
break;
653+
}
654+
655+
if (--trials_remaining <= 0) {
656+
break;
657+
}
658+
659+
log_debug(os, thread)("Failed to start native thread (%s), retrying after %dus.", os::errno_name(ret), next_delay);
660+
::usleep(next_delay);
661+
next_delay *= 2;
662+
}
650663

651664
char buf[64];
652665
if (ret == 0) {

src/hotspot/os/linux/os_linux.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,10 +1056,23 @@ bool os::create_thread(Thread* thread, ThreadType thr_type,
10561056
ResourceMark rm;
10571057
pthread_t tid;
10581058
int ret = 0;
1059-
int limit = 3;
1060-
do {
1059+
int trials_remaining = 4;
1060+
useconds_t next_delay = 1000;
1061+
while (true) {
10611062
ret = pthread_create(&tid, &attr, (void* (*)(void*)) thread_native_entry, thread);
1062-
} while (ret == EAGAIN && limit-- > 0);
1063+
1064+
if (ret != EAGAIN) {
1065+
break;
1066+
}
1067+
1068+
if (--trials_remaining <= 0) {
1069+
break;
1070+
}
1071+
1072+
log_debug(os, thread)("Failed to start native thread (%s), retrying after %dus.", os::errno_name(ret), next_delay);
1073+
::usleep(next_delay);
1074+
next_delay *= 2;
1075+
}
10631076

10641077
char buf[64];
10651078
if (ret == 0) {

src/hotspot/os/windows/os_windows.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -757,16 +757,33 @@ bool os::create_thread(Thread* thread, ThreadType thr_type,
757757

758758
const unsigned initflag = CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION;
759759
HANDLE thread_handle;
760-
int limit = 3;
761-
do {
760+
int trials_remaining = 4;
761+
DWORD next_delay_ms = 1;
762+
while (true) {
762763
thread_handle =
763764
(HANDLE)_beginthreadex(nullptr,
764765
(unsigned)stack_size,
765766
&thread_native_entry,
766767
thread,
767768
initflag,
768769
&thread_id);
769-
} while (thread_handle == nullptr && errno == EAGAIN && limit-- > 0);
770+
771+
if (thread_handle != nullptr) {
772+
break;
773+
}
774+
775+
if (errno != EAGAIN) {
776+
break;
777+
}
778+
779+
if (--trials_remaining <= 0) {
780+
break;
781+
}
782+
783+
log_debug(os, thread)("Failed to start native thread (%s), retrying after %dms.", os::errno_name(errno), next_delay_ms);
784+
Sleep(next_delay_ms);
785+
next_delay_ms *= 2;
786+
}
770787

771788
ResourceMark rm;
772789
char buf[64];

0 commit comments

Comments
 (0)