|
25 | 25 | #include <climits> // PTHREAD_STACK_MIN |
26 | 26 | #endif // __POSIX__ |
27 | 27 |
|
| 28 | +#include <algorithm> |
28 | 29 | #include <cstring> |
29 | 30 | #include <sstream> |
30 | 31 | #include <unordered_map> |
@@ -111,12 +112,18 @@ static int StartDebugSignalHandler() { |
111 | 112 | CHECK_EQ(0, uv_sem_init(&start_io_thread_semaphore, 0)); |
112 | 113 | pthread_attr_t attr; |
113 | 114 | CHECK_EQ(0, pthread_attr_init(&attr)); |
114 | | - // Don't shrink the thread's stack on FreeBSD. Said platform decided to |
115 | | - // follow the pthreads specification to the letter rather than in spirit: |
116 | | - // https://lists.freebsd.org/pipermail/freebsd-current/2014-March/048885.html |
117 | | -#ifndef __FreeBSD__ |
118 | | - CHECK_EQ(0, pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN)); |
119 | | -#endif // __FreeBSD__ |
| 115 | +#if defined(PTHREAD_STACK_MIN) && !defined(__FreeBSD__) |
| 116 | + // PTHREAD_STACK_MIN is 2 KB with musl libc, which is too small to safely |
| 117 | + // receive signals. PTHREAD_STACK_MIN + MINSIGSTKSZ is 8 KB on arm64, which |
| 118 | + // is the musl architecture with the biggest MINSIGSTKSZ so let's use that |
| 119 | + // as a lower bound and let's quadruple it just in case. The goal is to avoid |
| 120 | + // creating a big 2 or 4 MB address space gap (problematic on 32 bits |
| 121 | + // because of fragmentation), not squeeze out every last byte. |
| 122 | + // Omitted on FreeBSD because it doesn't seem to like small stacks. |
| 123 | + const size_t stack_size = std::max(static_cast<size_t>(4 * 8192), |
| 124 | + static_cast<size_t>(PTHREAD_STACK_MIN)); |
| 125 | + CHECK_EQ(0, pthread_attr_setstacksize(&attr, stack_size)); |
| 126 | +#endif // defined(PTHREAD_STACK_MIN) && !defined(__FreeBSD__) |
120 | 127 | CHECK_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)); |
121 | 128 | sigset_t sigmask; |
122 | 129 | // Mask all signals. |
|
0 commit comments