sched:use tcb inside of pthread_tcb_s, remove all cast

Simplify the code and remove different tcb

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao
2025-02-18 15:19:02 +08:00
committed by GUIDINGLI
parent 0a2b01950c
commit 994c9d8929
7 changed files with 55 additions and 59 deletions
+1 -5
View File
@@ -741,12 +741,8 @@ struct tcb_s
*/ */
#ifndef CONFIG_DISABLE_PTHREAD #ifndef CONFIG_DISABLE_PTHREAD
struct pthread_tcb_s struct pthread_entry_s
{ {
/* Common TCB fields ******************************************************/
struct tcb_s cmn; /* Common TCB fields */
/* Task Management Fields *************************************************/ /* Task Management Fields *************************************************/
pthread_trampoline_t trampoline; /* User-space startup function */ pthread_trampoline_t trampoline; /* User-space startup function */
+2 -2
View File
@@ -61,8 +61,8 @@ void task_initialize(void);
int group_allocate(FAR struct tcb_s *tcb, uint8_t ttype); int group_allocate(FAR struct tcb_s *tcb, uint8_t ttype);
void group_initialize(FAR struct tcb_s *tcb); void group_initialize(FAR struct tcb_s *tcb);
#ifndef CONFIG_DISABLE_PTHREAD #ifndef CONFIG_DISABLE_PTHREAD
void group_bind(FAR struct pthread_tcb_s *tcb); void group_bind(FAR struct tcb_s *tcb);
void group_join(FAR struct pthread_tcb_s *tcb); void group_join(FAR struct tcb_s *tcb);
#endif #endif
void group_leave(FAR struct tcb_s *tcb); void group_leave(FAR struct tcb_s *tcb);
void group_drop(FAR struct task_group_s *group); void group_drop(FAR struct task_group_s *group);
+7 -7
View File
@@ -61,15 +61,15 @@
* *
****************************************************************************/ ****************************************************************************/
void group_bind(FAR struct pthread_tcb_s *tcb) void group_bind(FAR struct tcb_s *tcb)
{ {
FAR struct tcb_s *ptcb = this_task(); FAR struct tcb_s *ptcb = this_task();
DEBUGASSERT(ptcb && tcb && ptcb->group && !tcb->cmn.group); DEBUGASSERT(ptcb && tcb && ptcb->group && !tcb->group);
/* Copy the group reference from the parent to the child */ /* Copy the group reference from the parent to the child */
tcb->cmn.group = ptcb->group; tcb->group = ptcb->group;
} }
/**************************************************************************** /****************************************************************************
@@ -89,21 +89,21 @@ void group_bind(FAR struct pthread_tcb_s *tcb)
* *
****************************************************************************/ ****************************************************************************/
void group_join(FAR struct pthread_tcb_s *tcb) void group_join(FAR struct tcb_s *tcb)
{ {
FAR struct task_group_s *group; FAR struct task_group_s *group;
irqstate_t flags; irqstate_t flags;
DEBUGASSERT(tcb && tcb->cmn.group); DEBUGASSERT(tcb && tcb->group);
/* Get the group from the TCB */ /* Get the group from the TCB */
group = tcb->cmn.group; group = tcb->group;
/* Add the member to the group */ /* Add the member to the group */
flags = spin_lock_irqsave(&group->tg_lock); flags = spin_lock_irqsave(&group->tg_lock);
sq_addfirst(&tcb->cmn.member, &group->tg_members); sq_addfirst(&tcb->member, &group->tg_members);
spin_unlock_irqrestore(&group->tg_lock, flags); spin_unlock_irqrestore(&group->tg_lock, flags);
} }
+1 -2
View File
@@ -98,10 +98,9 @@ extern "C"
* Public Function Prototypes * Public Function Prototypes
****************************************************************************/ ****************************************************************************/
struct pthread_tcb_s; /* Forward reference */
struct task_group_s; /* Forward reference */ struct task_group_s; /* Forward reference */
int pthread_setup_scheduler(FAR struct pthread_tcb_s *tcb, int priority, int pthread_setup_scheduler(FAR struct tcb_s *tcb, int priority,
start_t start, pthread_startroutine_t entry); start_t start, pthread_startroutine_t entry);
int pthread_completejoin(pid_t pid, FAR void *exit_value); int pthread_completejoin(pid_t pid, FAR void *exit_value);
+38 -36
View File
@@ -87,23 +87,25 @@ const pthread_attr_t g_default_pthread_attr = PTHREAD_ATTR_INITIALIZER;
* *
****************************************************************************/ ****************************************************************************/
static inline void pthread_tcb_setup(FAR struct pthread_tcb_s *ptcb, static inline void pthread_tcb_setup(FAR struct tcb_s *ptcb,
FAR struct tcb_s *parent, FAR struct tcb_s *parent,
pthread_trampoline_t trampoline, pthread_trampoline_t trampoline,
pthread_addr_t arg) pthread_addr_t arg)
{ {
FAR struct pthread_entry_s *entry;
#if CONFIG_TASK_NAME_SIZE > 0 #if CONFIG_TASK_NAME_SIZE > 0
/* Copy the pthread name into the TCB */ /* Copy the pthread name into the TCB */
strlcpy(ptcb->cmn.name, parent->name, CONFIG_TASK_NAME_SIZE); strlcpy(ptcb->name, parent->name, CONFIG_TASK_NAME_SIZE);
#endif /* CONFIG_TASK_NAME_SIZE */ #endif /* CONFIG_TASK_NAME_SIZE */
/* For pthreads, args are strictly pass-by-value; that actual /* For pthreads, args are strictly pass-by-value; that actual
* type wrapped by pthread_addr_t is unknown. * type wrapped by pthread_addr_t is unknown.
*/ */
ptcb->trampoline = trampoline; entry = (FAR struct pthread_entry_s *)(ptcb + 1);
ptcb->arg = arg; entry->trampoline = trampoline;
entry->arg = arg;
} }
/**************************************************************************** /****************************************************************************
@@ -119,16 +121,18 @@ static inline void pthread_tcb_setup(FAR struct pthread_tcb_s *ptcb,
static void pthread_start(void) static void pthread_start(void)
{ {
FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)this_task(); FAR struct tcb_s *ptcb = this_task();
FAR struct pthread_entry_s *entry =
(FAR struct pthread_entry_s *)(ptcb + 1);
/* The priority of this thread may have been boosted to avoid priority /* The priority of this thread may have been boosted to avoid priority
* inversion problems. If that is the case, then drop to the correct * inversion problems. If that is the case, then drop to the correct
* execution priority. * execution priority.
*/ */
if (ptcb->cmn.sched_priority > ptcb->cmn.init_priority) if (ptcb->sched_priority > ptcb->init_priority)
{ {
DEBUGVERIFY(nxsched_set_priority(&ptcb->cmn, ptcb->cmn.init_priority)); DEBUGVERIFY(nxsched_set_priority(ptcb, ptcb->init_priority));
} }
/* Pass control to the thread entry point. In the kernel build this has to /* Pass control to the thread entry point. In the kernel build this has to
@@ -136,12 +140,12 @@ static void pthread_start(void)
* to switch to user-mode before calling into the pthread. * to switch to user-mode before calling into the pthread.
*/ */
DEBUGASSERT(ptcb->trampoline != NULL && ptcb->cmn.entry.pthread != NULL); DEBUGASSERT(entry->trampoline != NULL && ptcb->entry.pthread != NULL);
#ifdef CONFIG_BUILD_FLAT #ifdef CONFIG_BUILD_FLAT
ptcb->trampoline(ptcb->cmn.entry.pthread, ptcb->arg); entry->trampoline(ptcb->entry.pthread, entry->arg);
#else #else
up_pthread_start(ptcb->trampoline, ptcb->cmn.entry.pthread, ptcb->arg); up_pthread_start(entry->trampoline, ptcb->entry.pthread, entry->arg);
#endif #endif
/* The thread has returned (should never happen) */ /* The thread has returned (should never happen) */
@@ -181,7 +185,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
pthread_startroutine_t entry, pthread_addr_t arg) pthread_startroutine_t entry, pthread_addr_t arg)
{ {
pthread_attr_t default_attr = g_default_pthread_attr; pthread_attr_t default_attr = g_default_pthread_attr;
FAR struct pthread_tcb_s *ptcb; FAR struct tcb_s *ptcb;
struct sched_param param; struct sched_param param;
FAR struct tcb_s *parent; FAR struct tcb_s *parent;
int policy; int policy;
@@ -209,22 +213,21 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
/* Allocate a TCB for the new task. */ /* Allocate a TCB for the new task. */
ptcb = (FAR struct pthread_tcb_s *) ptcb = kmm_zalloc(sizeof(struct tcb_s) + sizeof(struct pthread_entry_s));
kmm_zalloc(sizeof(struct pthread_tcb_s));
if (!ptcb) if (!ptcb)
{ {
serr("ERROR: Failed to allocate TCB\n"); serr("ERROR: Failed to allocate TCB\n");
return ENOMEM; return ENOMEM;
} }
ptcb->cmn.flags |= TCB_FLAG_FREE_TCB; ptcb->flags |= TCB_FLAG_FREE_TCB;
/* Initialize the task join */ /* Initialize the task join */
nxtask_joininit(&ptcb->cmn); nxtask_joininit(ptcb);
#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE #ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
spin_lock_init(&ptcb->cmn.mhead_lock); spin_lock_init(&ptcb->mhead_lock);
#endif #endif
/* Bind the parent's group to the new TCB (we have not yet joined the /* Bind the parent's group to the new TCB (we have not yet joined the
@@ -236,7 +239,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
#ifdef CONFIG_ARCH_ADDRENV #ifdef CONFIG_ARCH_ADDRENV
/* Share the address environment of the parent task group. */ /* Share the address environment of the parent task group. */
ret = addrenv_join(this_task(), (FAR struct tcb_s *)ptcb); ret = addrenv_join(this_task(), ptcb);
if (ret < 0) if (ret < 0)
{ {
errcode = -ret; errcode = -ret;
@@ -246,21 +249,20 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
if (attr->detachstate == PTHREAD_CREATE_DETACHED) if (attr->detachstate == PTHREAD_CREATE_DETACHED)
{ {
ptcb->cmn.flags |= TCB_FLAG_DETACHED; ptcb->flags |= TCB_FLAG_DETACHED;
} }
if (attr->stackaddr) if (attr->stackaddr)
{ {
/* Use pre-allocated stack */ /* Use pre-allocated stack */
ret = up_use_stack((FAR struct tcb_s *)ptcb, attr->stackaddr, ret = up_use_stack(ptcb, attr->stackaddr, attr->stacksize);
attr->stacksize);
} }
else else
{ {
/* Allocate the stack for the TCB */ /* Allocate the stack for the TCB */
ret = up_create_stack((FAR struct tcb_s *)ptcb, ret = up_create_stack(ptcb,
attr->stacksize + attr->guardsize, attr->stacksize + attr->guardsize,
TCB_FLAG_TTYPE_PTHREAD); TCB_FLAG_TTYPE_PTHREAD);
} }
@@ -275,7 +277,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
defined(CONFIG_BUILD_KERNEL) && defined(CONFIG_ARCH_KERNEL_STACK) defined(CONFIG_BUILD_KERNEL) && defined(CONFIG_ARCH_KERNEL_STACK)
/* Allocate the kernel stack */ /* Allocate the kernel stack */
ret = up_addrenv_kstackalloc(&ptcb->cmn); ret = up_addrenv_kstackalloc(ptcb);
if (ret < 0) if (ret < 0)
{ {
errcode = ENOMEM; errcode = ENOMEM;
@@ -351,10 +353,10 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
/* Initialize the sporadic policy */ /* Initialize the sporadic policy */
ret = nxsched_initialize_sporadic(&ptcb->cmn); ret = nxsched_initialize_sporadic(ptcb);
if (ret >= 0) if (ret >= 0)
{ {
sporadic = ptcb->cmn.sporadic; sporadic = ptcb->sporadic;
DEBUGASSERT(sporadic != NULL); DEBUGASSERT(sporadic != NULL);
/* Save the sporadic scheduling parameters */ /* Save the sporadic scheduling parameters */
@@ -367,7 +369,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
/* And start the first replenishment interval */ /* And start the first replenishment interval */
ret = nxsched_start_sporadic(&ptcb->cmn); ret = nxsched_start_sporadic(ptcb);
} }
/* Handle any failures */ /* Handle any failures */
@@ -392,7 +394,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
/* Initialize thread local storage */ /* Initialize thread local storage */
ret = tls_init_info(&ptcb->cmn); ret = tls_init_info(ptcb);
if (ret != OK) if (ret != OK)
{ {
errcode = -ret; errcode = -ret;
@@ -409,7 +411,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
if (attr->affinity != 0) if (attr->affinity != 0)
{ {
ptcb->cmn.affinity = attr->affinity; ptcb->affinity = attr->affinity;
} }
#endif #endif
@@ -425,25 +427,25 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
/* Set the appropriate scheduling policy in the TCB */ /* Set the appropriate scheduling policy in the TCB */
ptcb->cmn.flags &= ~TCB_FLAG_POLICY_MASK; ptcb->flags &= ~TCB_FLAG_POLICY_MASK;
switch (policy) switch (policy)
{ {
default: default:
case SCHED_FIFO: case SCHED_FIFO:
ptcb->cmn.flags |= TCB_FLAG_SCHED_FIFO; ptcb->flags |= TCB_FLAG_SCHED_FIFO;
break; break;
#if CONFIG_RR_INTERVAL > 0 #if CONFIG_RR_INTERVAL > 0
case SCHED_OTHER: case SCHED_OTHER:
case SCHED_RR: case SCHED_RR:
ptcb->cmn.flags |= TCB_FLAG_SCHED_RR; ptcb->flags |= TCB_FLAG_SCHED_RR;
ptcb->cmn.timeslice = MSEC2TICK(CONFIG_RR_INTERVAL); ptcb->timeslice = MSEC2TICK(CONFIG_RR_INTERVAL);
break; break;
#endif #endif
#ifdef CONFIG_SCHED_SPORADIC #ifdef CONFIG_SCHED_SPORADIC
case SCHED_SPORADIC: case SCHED_SPORADIC:
ptcb->cmn.flags |= TCB_FLAG_SCHED_SPORADIC; ptcb->flags |= TCB_FLAG_SCHED_SPORADIC;
break; break;
#endif #endif
} }
@@ -452,12 +454,12 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
if (thread != NULL) if (thread != NULL)
{ {
*thread = (pthread_t)ptcb->cmn.pid; *thread = (pthread_t)ptcb->pid;
} }
/* Then activate the task */ /* Then activate the task */
nxtask_activate((FAR struct tcb_s *)ptcb); nxtask_activate(ptcb);
return OK; return OK;
@@ -465,8 +467,8 @@ errout_with_tcb:
/* Since we do not join the group, assign group to NULL to clear binding */ /* Since we do not join the group, assign group to NULL to clear binding */
ptcb->cmn.group = NULL; ptcb->group = NULL;
nxsched_release_tcb((FAR struct tcb_s *)ptcb, TCB_FLAG_TTYPE_PTHREAD); nxsched_release_tcb(ptcb, TCB_FLAG_TTYPE_PTHREAD);
return errcode; return errcode;
} }
+3 -2
View File
@@ -81,9 +81,10 @@ size_t nxtask_argvstr(FAR struct tcb_s *tcb, FAR char *args, size_t size)
#ifndef CONFIG_DISABLE_PTHREAD #ifndef CONFIG_DISABLE_PTHREAD
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD) if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD)
{ {
FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)tcb; FAR struct pthread_entry_s *entry =
(FAR struct pthread_entry_s *)(tcb + 1);
n += snprintf(args, size, " %p %p", ptcb->cmn.entry.main, ptcb->arg); n += snprintf(args, size, " %p %p", tcb->entry.main, entry->arg);
} }
else else
#endif #endif
+3 -5
View File
@@ -663,8 +663,7 @@ int nxtask_setup_scheduler(FAR struct tcb_s *tcb, int priority,
{ {
/* Perform common thread setup */ /* Perform common thread setup */
return nxthread_setup_scheduler((FAR struct tcb_s *)tcb, priority, return nxthread_setup_scheduler(tcb, priority, start, main, ttype);
start, (CODE void *)main, ttype);
} }
/**************************************************************************** /****************************************************************************
@@ -692,13 +691,12 @@ int nxtask_setup_scheduler(FAR struct tcb_s *tcb, int priority,
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_DISABLE_PTHREAD #ifndef CONFIG_DISABLE_PTHREAD
int pthread_setup_scheduler(FAR struct pthread_tcb_s *tcb, int priority, int pthread_setup_scheduler(FAR struct tcb_s *tcb, int priority,
start_t start, pthread_startroutine_t entry) start_t start, pthread_startroutine_t entry)
{ {
/* Perform common thread setup */ /* Perform common thread setup */
return nxthread_setup_scheduler((FAR struct tcb_s *)tcb, priority, return nxthread_setup_scheduler(tcb, priority, start, entry,
start, (CODE void *)entry,
TCB_FLAG_TTYPE_PTHREAD); TCB_FLAG_TTYPE_PTHREAD);
} }
#endif #endif