From 5b69e5892571b97f72cfd984434543f66302a71c Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Wed, 7 Sep 2016 22:05:20 +0200 Subject: [PATCH] Provide macros and extensions to the task spawn command to set the stack sizes right --- src/platforms/posix/px4_layer/px4_posix_tasks.cpp | 6 +----- src/platforms/px4_posix.h | 10 ++++++++++ src/platforms/qurt/px4_layer/px4_qurt_tasks.cpp | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/platforms/posix/px4_layer/px4_posix_tasks.cpp b/src/platforms/posix/px4_layer/px4_posix_tasks.cpp index 6dd249951f..3f5c227bd1 100644 --- a/src/platforms/posix/px4_layer/px4_posix_tasks.cpp +++ b/src/platforms/posix/px4_layer/px4_posix_tasks.cpp @@ -185,11 +185,7 @@ px4_task_t px4_task_spawn_cmd(const char *name, int scheduler, int priority, int stack_size = PTHREAD_STACK_MIN; } - // The stack size is intended for 32-bit architectures; therefore - // we often run out of stack space when pointers are larger than 4 bytes. - // Double the stack size on posix when we're on a 64-bit architecture. - stack_size *= __SIZEOF_POINTER__ >> 2; - rv = pthread_attr_setstacksize(&attr, stack_size); + rv = pthread_attr_setstacksize(&attr, PX4_STACK_ADJUSTED(stack_size)); if (rv != 0) { PX4_ERR("pthread_attr_setstacksize to %d returned error (%d)", stack_size, rv); diff --git a/src/platforms/px4_posix.h b/src/platforms/px4_posix.h index c3d2eb8233..ed556e63b5 100644 --- a/src/platforms/px4_posix.h +++ b/src/platforms/px4_posix.h @@ -77,11 +77,14 @@ typedef struct pollfd px4_pollfd_struct_t; #define px4_access _GLOBAL access #define px4_getpid _GLOBAL getpid +#define PX4_STACK_OVERHEAD 0 + #elif defined(__PX4_POSIX) #define PX4_F_RDONLY O_RDONLY #define PX4_F_WRONLY O_WRONLY #define PX4_F_CREAT O_CREAT +#define PX4_STACK_OVERHEAD 8192 typedef short pollevent_t; @@ -118,6 +121,13 @@ __END_DECLS #error "No TARGET OS Provided" #endif + +// The stack size is intended for 32-bit architectures; therefore +// we often run out of stack space when pointers are larger than 4 bytes. +// Double the stack size on posix when we're on a 64-bit architecture. +// Most full-scale OS use 1-4K of memory from the stack themselves +#define PX4_STACK_ADJUSTED(_s) (_s * (__SIZEOF_POINTER__ >> 2) + PX4_STACK_OVERHEAD) + __BEGIN_DECLS extern int px4_errno; diff --git a/src/platforms/qurt/px4_layer/px4_qurt_tasks.cpp b/src/platforms/qurt/px4_layer/px4_qurt_tasks.cpp index 6cf342076a..00739dfafb 100644 --- a/src/platforms/qurt/px4_layer/px4_qurt_tasks.cpp +++ b/src/platforms/qurt/px4_layer/px4_qurt_tasks.cpp @@ -202,7 +202,7 @@ px4_task_t px4_task_spawn_cmd(const char *name, int scheduler, int priority, int fixed_stacksize = (fixed_stacksize < (size_t)stack_size) ? (size_t)stack_size : fixed_stacksize; PX4_DEBUG("setting the thread[%s] stack size to[%d]", name, fixed_stacksize); - pthread_attr_setstacksize(&attr, fixed_stacksize); + pthread_attr_setstacksize(&attr, PX4_STACK_ADJUSTED(fixed_stacksize)); PX4_DEBUG("stack address after pthread_attr_setstacksize: 0x%X", attr.stackaddr); param.sched_priority = priority;