From 557fcbd3297405238c9529c137ed1f763bac5ac9 Mon Sep 17 00:00:00 2001 From: pangzhen1 Date: Fri, 21 Feb 2025 20:13:35 +0800 Subject: [PATCH] sched/wqueue: add configuring the stack of an hpwork/lpwork as static Allows configuring a static stack for hpwork/lpwork. Signed-off-by: pangzhen1 --- sched/Kconfig | 12 ++++++++++++ sched/wqueue/CMakeLists.txt | 12 ++++++++++++ sched/wqueue/Make.defs | 8 ++++++++ sched/wqueue/kwork_thread.c | 39 ++++++++++++++++++++++++++++++++++--- 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/sched/Kconfig b/sched/Kconfig index c9868fb70ae98..1f57185b1de22 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -1855,6 +1855,12 @@ config SCHED_HPWORKSTACKSIZE ---help--- The stack size allocated for the worker thread. Default: 2K. +config SCHED_HPWORKSTACKSECTION + string "The section where hpwork stack is located" + default "" + ---help--- + The section where hpwork stack is located. + endif # SCHED_HPWORK config SCHED_LPWORK @@ -1954,6 +1960,12 @@ config SCHED_LPWORKSTACKSIZE ---help--- The stack size allocated for the lower priority worker thread. Default: 2K. +config SCHED_LPWORKSTACKSECTION + string "The section where lpwork stack is located" + default "" + ---help--- + The section where lpwork stack is located. + endif # SCHED_LPWORK endmenu # Work Queue Support diff --git a/sched/wqueue/CMakeLists.txt b/sched/wqueue/CMakeLists.txt index 7b325beae46bf..90e012b098e72 100644 --- a/sched/wqueue/CMakeLists.txt +++ b/sched/wqueue/CMakeLists.txt @@ -38,6 +38,18 @@ if(CONFIG_SCHED_WORKQUEUE) list(APPEND SRCS kwork_notifier.c) endif() + if(CONFIG_SCHED_HPWORKSTACKSECTION) + target_compile_definitions( + sched + PRIVATE -DSCHED_HPWORKSTACKSECTION="${CONFIG_SCHED_HPWORKSTACKSECTION}") + endif() + + if(CONFIG_SCHED_LPWORKSTACKSECTION) + target_compile_definitions( + sched + PRIVATE -DSCHED_LPWORKSTACKSECTION="${CONFIG_SCHED_LPWORKSTACKSECTION}") + endif() + target_sources(sched PRIVATE ${SRCS}) endif() diff --git a/sched/wqueue/Make.defs b/sched/wqueue/Make.defs index baa93f72e6f69..04e735dae9237 100644 --- a/sched/wqueue/Make.defs +++ b/sched/wqueue/Make.defs @@ -36,6 +36,14 @@ ifeq ($(CONFIG_WQUEUE_NOTIFIER),y) CSRCS += kwork_notifier.c endif +ifneq ($(CONFIG_SCHED_HPWORKSTACKSECTION),"") + CFLAGS += ${DEFINE_PREFIX}SCHED_HPWORKSTACKSECTION=CONFIG_SCHED_HPWORKSTACKSECTION +endif + +ifneq ($(CONFIG_SCHED_LPWORKSTACKSECTION),"") + CFLAGS += ${DEFINE_PREFIX}SCHED_LPWORKSTACKSECTION=CONFIG_SCHED_LPWORKSTACKSECTION +endif + # Include wqueue build support DEPPATH += --dep-path wqueue diff --git a/sched/wqueue/kwork_thread.c b/sched/wqueue/kwork_thread.c index 725461d76d28a..7461ccdc8d75e 100644 --- a/sched/wqueue/kwork_thread.c +++ b/sched/wqueue/kwork_thread.c @@ -308,6 +308,7 @@ static int work_thread_create(FAR const char *name, int priority, char arg1[32]; int wndx; int pid; + FAR void *stack = NULL; /* Don't permit any of the threads to run until we have fully initialized * all of them. @@ -325,8 +326,15 @@ static int work_thread_create(FAR const char *name, int priority, argv[1] = arg1; argv[2] = NULL; - pid = kthread_create_with_stack(name, priority, stack_addr, stack_size, - work_thread, argv); + /* In case of the stack_addr is NULL */ + + if (stack_addr) + { + stack = (FAR void *)((uintptr_t)stack_addr + wndx * stack_size); + } + + pid = kthread_create_with_stack(name, priority, stack, + stack_size, work_thread, argv); DEBUGASSERT(pid > 0); if (pid < 0) @@ -550,9 +558,21 @@ int work_start_highpri(void) sinfo("Starting high-priority kernel worker thread(s)\n"); +#ifdef SCHED_HPWORKSTACKSECTION + static uint8_t hp_work_stack[CONFIG_SCHED_HPNTHREADS] + [CONFIG_SCHED_HPWORKSTACKSIZE] + locate_data(CONFIG_SCHED_HPWORKSTACKSECTION); + + return work_thread_create(HPWORKNAME, + CONFIG_SCHED_HPWORKPRIORITY, + hp_work_stack, + CONFIG_SCHED_HPWORKSTACKSIZE, + (FAR struct kwork_wqueue_s *)&g_hpwork); +#else return work_thread_create(HPWORKNAME, CONFIG_SCHED_HPWORKPRIORITY, NULL, CONFIG_SCHED_HPWORKSTACKSIZE, (FAR struct kwork_wqueue_s *)&g_hpwork); +#endif } #endif /* CONFIG_SCHED_HPWORK */ @@ -578,9 +598,22 @@ int work_start_lowpri(void) sinfo("Starting low-priority kernel worker thread(s)\n"); - return work_thread_create(LPWORKNAME, CONFIG_SCHED_LPWORKPRIORITY, NULL, +#ifdef SCHED_LPWORKSTACKSECTION + static uint8_t lp_work_stack[CONFIG_SCHED_LPNTHREADS] + [CONFIG_SCHED_LPWORKSTACKSIZE] + locate_data(CONFIG_SCHED_LPWORKSTACKSECTION); + + return work_thread_create(LPWORKNAME, + CONFIG_SCHED_LPWORKPRIORITY, + lp_work_stack, CONFIG_SCHED_LPWORKSTACKSIZE, (FAR struct kwork_wqueue_s *)&g_lpwork); +#else + return work_thread_create(LPWORKNAME, + CONFIG_SCHED_LPWORKPRIORITY, NULL, + CONFIG_SCHED_LPWORKSTACKSIZE, + (FAR struct kwork_wqueue_s *)&g_lpwork); +#endif } #endif /* CONFIG_SCHED_LPWORK */