From 73bfc2a15db2b59b56dcb677b349a67d0ddabec3 Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Mon, 18 Sep 2023 14:07:12 +0200 Subject: [PATCH] Set proper thread stack size also when using the Zephyr RTOS --- openrtx/include/core/threads.h | 5 +++++ openrtx/src/core/audio_codec.c | 31 ++++++++++++++++++++----------- openrtx/src/core/threads.c | 24 +++++++++++++++--------- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/openrtx/include/core/threads.h b/openrtx/include/core/threads.h index 9fd62cc4..ef6e8674 100644 --- a/openrtx/include/core/threads.h +++ b/openrtx/include/core/threads.h @@ -43,4 +43,9 @@ void create_threads(); */ #define RTX_TASK_STKSIZE 512 +/** + * Stack size for codec2 task, in bytes. + */ +#define CODEC2_TASK_STKSIZE 16384 + #endif /* THREADS_H */ diff --git a/openrtx/src/core/audio_codec.c b/openrtx/src/core/audio_codec.c index fad1af83..60211393 100644 --- a/openrtx/src/core/audio_codec.c +++ b/openrtx/src/core/audio_codec.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -37,6 +38,7 @@ static bool running; static bool reqStop; static pthread_t codecThread; +static pthread_attr_t codecAttr; static pthread_mutex_t data_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t wakeup_cond = PTHREAD_COND_INITIALIZER; @@ -387,25 +389,24 @@ static bool startThread(const pathId path, void *(*func) (void *)) numElements = 0; reqStop = false; - int ret = 0; - - #ifdef _MIOSIX - // Set stack size of CODEC2 thread to 16kB. - pthread_attr_t codecAttr; pthread_attr_init(&codecAttr); - pthread_attr_setstacksize(&codecAttr, 16384); + + #if defined(_MIOSIX) + // Set stack size of CODEC2 thread to 16kB. + pthread_attr_setstacksize(&codecAttr, CODEC2_TASK_STKSIZE); // Set priority of CODEC2 thread to the maximum one, the same of RTX thread. struct sched_param param; param.sched_priority = sched_get_priority_max(0); pthread_attr_setschedparam(&codecAttr, ¶m); - - // Start thread - ret = pthread_create(&codecThread, &codecAttr, func, ((void *) audioPath)); - #else - ret = pthread_create(&codecThread, NULL, func, ((void *) audioPath)); + #elif defined(__ZEPHYR__) + // Allocate and set the stack for CODEC2 thread + void *codec_thread_stack = malloc(CODEC2_TASK_STKSIZE * sizeof(uint8_t)); + pthread_attr_setstack(&codecAttr, codec_thread_stack, CODEC2_TASK_STKSIZE); #endif + // Start thread + int ret = pthread_create(&codecThread, &codecAttr, func, ((void *) audioPath)); if(ret < 0) running = false; @@ -417,4 +418,12 @@ static void stopThread() reqStop = true; pthread_join(codecThread, NULL); running = false; + + #ifdef __ZEPHYR__ + void *addr; + size_t size; + + pthread_attr_getstack(&codecAttr, &addr, &size); + free(addr); + #endif } diff --git a/openrtx/src/core/threads.c b/openrtx/src/core/threads.c index d8455fbd..2ea2c363 100644 --- a/openrtx/src/core/threads.c +++ b/openrtx/src/core/threads.c @@ -198,17 +198,19 @@ void *rtx_threadFunc(void *arg) */ void create_threads() { - pthread_t rtx_thread; - pthread_t ui_thread; - // Create RTX state mutex pthread_mutex_init(&rtx_mutex, NULL); -#ifndef __ZEPHYR__ // Create rtx radio thread pthread_attr_t rtx_attr; pthread_attr_init(&rtx_attr); + + #ifndef __ZEPHYR__ pthread_attr_setstacksize(&rtx_attr, RTX_TASK_STKSIZE); + #else + void *rtx_thread_stack = malloc(RTX_TASK_STKSIZE * sizeof(uint8_t)); + pthread_attr_setstack(&rtx_attr, rtx_thread_stack, RTX_TASK_STKSIZE); + #endif #ifdef _MIOSIX // Max priority for RTX thread when running with miosix rtos @@ -217,16 +219,20 @@ void create_threads() pthread_attr_setschedparam(&rtx_attr, ¶m); #endif + pthread_t rtx_thread; pthread_create(&rtx_thread, &rtx_attr, rtx_threadFunc, NULL); // Create UI thread pthread_attr_t ui_attr; pthread_attr_init(&ui_attr); + + #ifndef __ZEPHYR__ pthread_attr_setstacksize(&ui_attr, UI_TASK_STKSIZE); + #else + void *ui_thread_stack = malloc(UI_TASK_STKSIZE * sizeof(uint8_t)); + pthread_attr_setstack(&ui_attr, ui_thread_stack, UI_TASK_STKSIZE); + #endif + + pthread_t ui_thread; pthread_create(&ui_thread, &ui_attr, ui_threadFunc, NULL); -#else - // On zephyr just spawn the threads without setting the attributes - pthread_create(&rtx_thread, NULL, rtx_threadFunc, NULL); - pthread_create(&ui_thread, NULL, ui_threadFunc, NULL); -#endif }