diff options
| author | Rbb666 <[email protected]> | 2025-09-01 15:30:09 +0800 |
|---|---|---|
| committer | R b b666 <[email protected]> | 2025-09-02 16:46:55 +0800 |
| commit | 3c17851fe2316b8cb4434dfe0d6046fcede3b0c7 (patch) | |
| tree | dee22d634e04eb8d34a8a69a8cbb0fc815b2cd0c /src | |
| parent | f8a37bc42ed754c461bfe9ea02d8638313cb9fe0 (diff) | |
[Add][kernel/thread]Add thread overflow hook and testcases:thread_overflow_tc.c.
Diffstat (limited to 'src')
| -rw-r--r-- | src/scheduler_comm.c | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/src/scheduler_comm.c b/src/scheduler_comm.c index 3a9332ddec..e68c2a17d3 100644 --- a/src/scheduler_comm.c +++ b/src/scheduler_comm.c @@ -8,6 +8,7 @@ * Change Logs: * Date Author Notes * 2024-01-18 Shell Separate scheduling related codes from thread.c, scheduler_.* + * 2025-09-01 Rbb666 Add thread stack overflow hook. */ #define DBG_TAG "kernel.sched" @@ -411,6 +412,35 @@ rt_err_t rt_sched_thread_reset_priority(struct rt_thread *thread, rt_uint8_t pri } #ifdef RT_USING_OVERFLOW_CHECK + +#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR) +static rt_err_t (*rt_stack_overflow_hook)(struct rt_thread *thread); + +/** + * @brief Set a hook function to be called when stack overflow is detected + * + * @param hook The function pointer to be called when stack overflow is detected. + * Pass RT_NULL to disable the hook. + * The hook function should return RT_EOK if overflow is handled, + * otherwise the system will halt in an infinite loop. + * + * @note The hook function must be simple and never be blocked or suspended. + * This function is typically used for error logging, recovery, or graceful shutdown. + * + * @details Hook function behavior: + * - Return RT_EOK: System continues execution after overflow handling + * - Return any other value: System enters infinite loop (halt) + * - Hook is called from rt_scheduler_stack_check() when overflow is detected + * - Hook execution context depends on when stack check is performed + * + * @see rt_scheduler_stack_check() + */ +void rt_scheduler_stack_overflow_sethook(rt_err_t (*hook)(struct rt_thread *thread)) +{ + rt_stack_overflow_hook = hook; +} +#endif /* RT_USING_HOOK */ + /** * @brief Check thread stack for overflow or near-overflow conditions * @@ -452,10 +482,22 @@ void rt_scheduler_stack_check(struct rt_thread *thread) (rt_uintptr_t)thread->stack_addr + (rt_uintptr_t)thread->stack_size) { rt_base_t dummy = 1; + rt_err_t hook_result = -RT_ERROR; LOG_E("thread:%s stack overflow\n", thread->parent.name); - while (dummy); +#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR) + if (rt_stack_overflow_hook != RT_NULL) + { + hook_result = rt_stack_overflow_hook(thread); + } +#endif /* RT_USING_HOOK */ + + /* If hook handled the overflow successfully, don't enter infinite loop */ + if (hook_result != RT_EOK) + { + while (dummy); + } } #endif /* RT_USING_HW_STACK_GUARD */ #ifdef ARCH_CPU_STACK_GROWS_UPWARD |
