From b2d4da4b0ab9603908f881ac07b8ed4461760e41 Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Sat, 23 Apr 2016 02:16:54 -0300 Subject: [PATCH] AP_HAL_Linux: Thread: allow to set stack size This allows the code that is creating the thread to set the size of the stack. --- libraries/AP_HAL_Linux/Thread.cpp | 17 +++++++++++++++++ libraries/AP_HAL_Linux/Thread.h | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/libraries/AP_HAL_Linux/Thread.cpp b/libraries/AP_HAL_Linux/Thread.cpp index 34aa32510c..020e5cf11c 100644 --- a/libraries/AP_HAL_Linux/Thread.cpp +++ b/libraries/AP_HAL_Linux/Thread.cpp @@ -154,6 +154,12 @@ bool Thread::start(const char *name, int policy, int prio) } } + if (_stack_size) { + if (pthread_attr_setstacksize(&attr, _stack_size) != 0) { + return false; + } + } + r = pthread_create(&_ctx, &attr, &Thread::_run_trampoline, this); if (r != 0) { AP_HAL::panic("Failed to create thread '%s': %s", @@ -186,6 +192,17 @@ bool PeriodicThread::set_rate(uint32_t rate_hz) return true; } +bool Thread::set_stack_size(size_t stack_size) +{ + if (_started) { + return false; + } + + _stack_size = stack_size; + + return true; +} + bool PeriodicThread::_run() { uint64_t next_run_usec = AP_HAL::micros64() + _period_usec; diff --git a/libraries/AP_HAL_Linux/Thread.h b/libraries/AP_HAL_Linux/Thread.h index 3c20835f9d..4d2bc1e447 100644 --- a/libraries/AP_HAL_Linux/Thread.h +++ b/libraries/AP_HAL_Linux/Thread.h @@ -44,6 +44,8 @@ public: size_t get_stack_usage(); + bool set_stack_size(size_t stack_size); + protected: static void *_run_trampoline(void *arg); @@ -64,6 +66,8 @@ protected: uint32_t *start; uint32_t *end; } _stack_debug; + + size_t _stack_size; }; class PeriodicThread : public Thread {