From b9042f590071f4e60c720c5f8d14c276f99e477f Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Wed, 27 May 2020 09:32:14 -0600 Subject: [PATCH] sched/task/task_init.c: Add nxtask_uninit() Add trivial function nxtask_uninit(). This function will undo all operations on a TCB performed by task_init() and release the TCB by calling kmm_free(). This is intended primarily to support error recovery operations after a successful call to task_init() such was when a subsequent call to task_activate fails. That error recovery is trivial but not obvious. This helper function should eliminate confusion about what to do to recover after calling nxtask_init() --- include/nuttx/sched.h | 21 +++++++++++++++++++++ sched/task/task_init.c | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index 824a44eb05..b19cf6898e 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -944,6 +944,27 @@ int nxtask_init(FAR struct tcb_s *tcb, const char *name, int priority, FAR uint32_t *stack, uint32_t stack_size, main_t entry, FAR char * const argv[]); +/******************************************************************************** + * Name: nxtask_uninit + * + * Description: + * Undo all operations on a TCB performed by task_init() and release the + * TCB by calling kmm_free(). This is intended primarily to support + * error recovery operations after a successful call to task_init() such + * was when a subsequent call to task_activate fails. + * + * Caution: Freeing of the TCB itself might be an unexpected side-effect. + * + * Input Parameters: + * tcb - Address of the TCB initialized by task_init() + * + * Returned Value: + * OK on success; negative error value on failure appropriately. + * + ********************************************************************************/ + +void nxtask_uninit(FAR struct tcb_s *tcb); + /******************************************************************************** * Name: nxtask_activate * diff --git a/sched/task/task_init.c b/sched/task/task_init.c index 7068dc4b55..315e6b6f6d 100644 --- a/sched/task/task_init.c +++ b/sched/task/task_init.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -74,8 +75,8 @@ ****************************************************************************/ int nxtask_init(FAR struct tcb_s *tcb, const char *name, int priority, - FAR uint32_t *stack, uint32_t stack_size, - main_t entry, FAR char * const argv[]) + FAR uint32_t *stack, uint32_t stack_size, + main_t entry, FAR char * const argv[]) { FAR struct task_tcb_s *ttcb = (FAR struct task_tcb_s *)tcb; int ret; @@ -136,3 +137,38 @@ errout_with_group: errout: return ret; } + +/**************************************************************************** + * Name: nxtask_uninit + * + * Description: + * Undo all operations on a TCB performed by task_init() and release the + * TCB by calling kmm_free(). This is intended primarily to support + * error recovery operations after a successful call to task_init() such + * was when a subsequent call to task_activate fails. + * + * Caution: Freeing of the TCB itself might be an unexpected side-effect. + * + * Input Parameters: + * tcb - Address of the TCB initialized by task_init() + * + * Returned Value: + * OK on success; negative error value on failure appropriately. + * + ****************************************************************************/ + +void nxtask_uninit(FAR struct tcb_s *tcb) +{ + /* The TCB was added to the inactive task list by + * nxtask_setup_scheduler(). + */ + + dq_rem((FAR dq_entry_t *)tcb, (FAR dq_queue_t *)&g_inactivetasks); + + /* Release all resources associated with the TCB... Including the TCB + * itself. + */ + + nxsched_release_tcb((FAR struct tcb_s *)tcb, + tcb->flags & TCB_FLAG_TTYPE_MASK); +}