|
37 | 37 | #include "os-shared-idmap.h" |
38 | 38 |
|
39 | 39 | /* |
40 | | - * Defines |
| 40 | + * Extra Stack Space for overhead - |
| 41 | + * |
| 42 | + * glibc pthreads implicitly puts its TCB/TLS structures at the base of the stack. |
| 43 | + * The actual size of these structures is highly system and configuration dependent. |
| 44 | + * Experimentation/measurement on an x64-64 system with glibc shows this to consume |
| 45 | + * between 9-10kB of stack space off the top. |
| 46 | + * |
| 47 | + * There does not seem to be a reliable/standardized way of determining how large this |
| 48 | + * is going to be, but PTHREAD_STACK_MIN (if defined) should include adequate space for it. |
| 49 | + * If this is not defined, then just assume 1 page. |
| 50 | + * |
| 51 | + * Importantly - when the user passes a stack size to OS_TaskCreate() - the expectation is |
| 52 | + * that there will be at least this amount of real _usable_ space for the task. If 10kB |
| 53 | + * is used before the entry point is even called, this needs to be accounted for, or else |
| 54 | + * the stack might end up being too small. |
41 | 55 | */ |
42 | | -#ifndef PTHREAD_STACK_MIN |
43 | | -#define PTHREAD_STACK_MIN (8 * 1024) |
| 56 | +#ifdef PTHREAD_STACK_MIN |
| 57 | +#define OS_IMPL_STACK_EXTRA PTHREAD_STACK_MIN |
| 58 | +#else |
| 59 | +#define OS_IMPL_STACK_EXTRA POSIX_GlobalVars.PageSize |
44 | 60 | #endif |
45 | 61 |
|
46 | 62 | /* Tables where the OS object information is stored */ |
@@ -451,20 +467,9 @@ int32 OS_Posix_InternalTaskCreate_Impl(pthread_t *pthr, osal_priority_t priority |
451 | 467 | } |
452 | 468 |
|
453 | 469 | /* |
454 | | - * Adjust the stack size parameter. |
455 | | - * |
456 | | - * POSIX has additional restrictions/limitations on the stack size of tasks that |
457 | | - * other RTOS environments may not have. Specifically POSIX says that the stack |
458 | | - * size must be at least PTHREAD_STACK_MIN and may also need to be a multiple of the |
459 | | - * system page size. |
460 | | - * |
461 | | - * Rounding up means the user might get a bigger stack than they requested, but |
462 | | - * that should not break anything aside from consuming extra memory. |
| 470 | + * Adjust the stack size parameter, add budget for TCB/TLS overhead. |
463 | 471 | */ |
464 | | - if (stacksz < PTHREAD_STACK_MIN) |
465 | | - { |
466 | | - stacksz = PTHREAD_STACK_MIN; |
467 | | - } |
| 472 | + stacksz += OS_IMPL_STACK_EXTRA; |
468 | 473 |
|
469 | 474 | stacksz += POSIX_GlobalVars.PageSize - 1; |
470 | 475 | stacksz -= stacksz % POSIX_GlobalVars.PageSize; |
|
0 commit comments