From 94899532300a609251de19d0d492ebbf8c0e1330 Mon Sep 17 00:00:00 2001 From: chao an Date: Mon, 26 Feb 2024 15:50:56 +0800 Subject: [PATCH] sched/tcb: add free tcb flag to support static tcb Add support for static tcb, applications in some special case can initialize system resources in advance through static tcb. | static struct task_tcb_s g_tcb; | | memset(&g_tcb, 0, sizeof(struct task_tcb_s)); | g_tcb.cmn.flags = TCB_FLAG_TTYPE_KERNEL; | nxtask_init(&g_tcb, "PTCB", 101, NULL, 1024, ptcb_task, NULL, NULL, NULL); | | ... | nxtask_activate(&g_tcb.cmn); Signed-off-by: chao an --- binfmt/binfmt_execmodule.c | 6 ++++-- include/nuttx/sched.h | 1 + sched/sched/sched_releasetcb.c | 5 ++++- sched/task/task_create.c | 2 +- sched/task/task_spawn.c | 2 +- 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/binfmt/binfmt_execmodule.c b/binfmt/binfmt_execmodule.c index 3859a0bfe47..85978b411be 100644 --- a/binfmt/binfmt_execmodule.c +++ b/binfmt/binfmt_execmodule.c @@ -303,9 +303,11 @@ int exec_module(FAR struct binary_s *binp, } #endif - /* Note that tcb->flags are not modified. 0=normal task */ + /* Note that tcb->cmn.flags are not modified. 0=normal task */ - /* tcb->flags |= TCB_FLAG_TTYPE_TASK; */ + /* tcb->cmn.flags |= TCB_FLAG_TTYPE_TASK; */ + + tcb->cmn.flags |= TCB_FLAG_FREE_TCB; /* Initialize the task */ diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index 16e66ccc0a3..820436669df 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -104,6 +104,7 @@ #define TCB_FLAG_HEAP_DUMP (1 << 11) /* Bit 11: Heap dump */ #define TCB_FLAG_DETACHED (1 << 12) /* Bit 12: Pthread detached */ #define TCB_FLAG_FORCED_CANCEL (1 << 13) /* Bit 13: Pthread cancel is forced */ +#define TCB_FLAG_FREE_TCB (1 << 14) /* Bit 14: Free tcb after exit */ /* Values for struct task_group tg_flags */ diff --git a/sched/sched/sched_releasetcb.c b/sched/sched/sched_releasetcb.c index dc996af9466..6ba7aaf87d2 100644 --- a/sched/sched/sched_releasetcb.c +++ b/sched/sched/sched_releasetcb.c @@ -163,7 +163,10 @@ int nxsched_release_tcb(FAR struct tcb_s *tcb, uint8_t ttype) /* And, finally, release the TCB itself */ - kmm_free(tcb); + if (tcb->flags & TCB_FLAG_FREE_TCB) + { + kmm_free(tcb); + } } return ret; diff --git a/sched/task/task_create.c b/sched/task/task_create.c index 17a84202d08..f934078a374 100644 --- a/sched/task/task_create.c +++ b/sched/task/task_create.c @@ -91,7 +91,7 @@ int nxthread_create(FAR const char *name, uint8_t ttype, int priority, /* Setup the task type */ - tcb->cmn.flags = ttype; + tcb->cmn.flags = ttype | TCB_FLAG_FREE_TCB; /* Initialize the task */ diff --git a/sched/task/task_spawn.c b/sched/task/task_spawn.c index 9554fdaad24..c2f972341c0 100644 --- a/sched/task/task_spawn.c +++ b/sched/task/task_spawn.c @@ -99,7 +99,7 @@ static int nxtask_spawn_create(FAR const char *name, int priority, /* Setup the task type */ - tcb->cmn.flags = TCB_FLAG_TTYPE_TASK; + tcb->cmn.flags = TCB_FLAG_TTYPE_TASK | TCB_FLAG_FREE_TCB; /* Initialize the task */