| 1 | /* pthread initialization called from __libc_early_init. NPTL version. |
|---|---|
| 2 | Copyright (C) 2021-2022 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #ifndef _PTHREAD_EARLY_INIT_H |
| 20 | #define _PTHREAD_EARLY_INIT_H 1 |
| 21 | |
| 22 | #include <nptl/nptl-stack.h> |
| 23 | #include <pthreadP.h> |
| 24 | #include <pthread_mutex_conf.h> |
| 25 | #include <sys/resource.h> |
| 26 | |
| 27 | static inline void |
| 28 | __pthread_early_init (void) |
| 29 | { |
| 30 | /* Determine the default allowed stack size. This is the size used |
| 31 | in case the user does not specify one. */ |
| 32 | struct rlimit limit; |
| 33 | if (__getrlimit (RLIMIT_STACK, &limit) != 0 |
| 34 | || limit.rlim_cur == RLIM_INFINITY) |
| 35 | /* The system limit is not usable. Use an architecture-specific |
| 36 | default. */ |
| 37 | limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE; |
| 38 | else if (limit.rlim_cur < PTHREAD_STACK_MIN) |
| 39 | /* The system limit is unusably small. |
| 40 | Use the minimal size acceptable. */ |
| 41 | limit.rlim_cur = PTHREAD_STACK_MIN; |
| 42 | |
| 43 | /* Make sure it meets the minimum size that allocate_stack |
| 44 | (allocatestack.c) will demand, which depends on the page size. */ |
| 45 | const uintptr_t pagesz = GLRO(dl_pagesize); |
| 46 | const size_t minstack = (pagesz + __nptl_tls_static_size_for_stack () |
| 47 | + MINIMAL_REST_STACK); |
| 48 | if (limit.rlim_cur < minstack) |
| 49 | limit.rlim_cur = minstack; |
| 50 | |
| 51 | /* Round the resource limit up to page size. */ |
| 52 | limit.rlim_cur = ALIGN_UP (limit.rlim_cur, pagesz); |
| 53 | __default_pthread_attr.internal.stacksize = limit.rlim_cur; |
| 54 | __default_pthread_attr.internal.guardsize = GLRO (dl_pagesize); |
| 55 | |
| 56 | #if HAVE_TUNABLES |
| 57 | __pthread_tunables_init (); |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | #endif /* _PTHREAD_EARLY_INIT_H */ |
| 62 |