mirror of
https://github.com/apache/nuttx.git
synced 2026-05-23 23:28:29 +08:00
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:
@@ -741,12 +741,8 @@ struct tcb_s
|
||||
*/
|
||||
|
||||
#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 *************************************************/
|
||||
|
||||
pthread_trampoline_t trampoline; /* User-space startup function */
|
||||
|
||||
+2
-2
@@ -61,8 +61,8 @@ void task_initialize(void);
|
||||
int group_allocate(FAR struct tcb_s *tcb, uint8_t ttype);
|
||||
void group_initialize(FAR struct tcb_s *tcb);
|
||||
#ifndef CONFIG_DISABLE_PTHREAD
|
||||
void group_bind(FAR struct pthread_tcb_s *tcb);
|
||||
void group_join(FAR struct pthread_tcb_s *tcb);
|
||||
void group_bind(FAR struct tcb_s *tcb);
|
||||
void group_join(FAR struct tcb_s *tcb);
|
||||
#endif
|
||||
void group_leave(FAR struct tcb_s *tcb);
|
||||
void group_drop(FAR struct task_group_s *group);
|
||||
|
||||
@@ -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();
|
||||
|
||||
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 */
|
||||
|
||||
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;
|
||||
irqstate_t flags;
|
||||
|
||||
DEBUGASSERT(tcb && tcb->cmn.group);
|
||||
DEBUGASSERT(tcb && tcb->group);
|
||||
|
||||
/* Get the group from the TCB */
|
||||
|
||||
group = tcb->cmn.group;
|
||||
group = tcb->group;
|
||||
|
||||
/* Add the member to the group */
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -98,10 +98,9 @@ extern "C"
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
struct pthread_tcb_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);
|
||||
|
||||
int pthread_completejoin(pid_t pid, FAR void *exit_value);
|
||||
|
||||
@@ -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,
|
||||
pthread_trampoline_t trampoline,
|
||||
pthread_addr_t arg)
|
||||
{
|
||||
FAR struct pthread_entry_s *entry;
|
||||
#if CONFIG_TASK_NAME_SIZE > 0
|
||||
/* 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 */
|
||||
|
||||
/* For pthreads, args are strictly pass-by-value; that actual
|
||||
* type wrapped by pthread_addr_t is unknown.
|
||||
*/
|
||||
|
||||
ptcb->trampoline = trampoline;
|
||||
ptcb->arg = arg;
|
||||
entry = (FAR struct pthread_entry_s *)(ptcb + 1);
|
||||
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)
|
||||
{
|
||||
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
|
||||
* inversion problems. If that is the case, then drop to the correct
|
||||
* 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
|
||||
@@ -136,12 +140,12 @@ static void pthread_start(void)
|
||||
* 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
|
||||
ptcb->trampoline(ptcb->cmn.entry.pthread, ptcb->arg);
|
||||
entry->trampoline(ptcb->entry.pthread, entry->arg);
|
||||
#else
|
||||
up_pthread_start(ptcb->trampoline, ptcb->cmn.entry.pthread, ptcb->arg);
|
||||
up_pthread_start(entry->trampoline, ptcb->entry.pthread, entry->arg);
|
||||
#endif
|
||||
|
||||
/* 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_attr_t default_attr = g_default_pthread_attr;
|
||||
FAR struct pthread_tcb_s *ptcb;
|
||||
FAR struct tcb_s *ptcb;
|
||||
struct sched_param param;
|
||||
FAR struct tcb_s *parent;
|
||||
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. */
|
||||
|
||||
ptcb = (FAR struct pthread_tcb_s *)
|
||||
kmm_zalloc(sizeof(struct pthread_tcb_s));
|
||||
ptcb = kmm_zalloc(sizeof(struct tcb_s) + sizeof(struct pthread_entry_s));
|
||||
if (!ptcb)
|
||||
{
|
||||
serr("ERROR: Failed to allocate TCB\n");
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
ptcb->cmn.flags |= TCB_FLAG_FREE_TCB;
|
||||
ptcb->flags |= TCB_FLAG_FREE_TCB;
|
||||
|
||||
/* Initialize the task join */
|
||||
|
||||
nxtask_joininit(&ptcb->cmn);
|
||||
nxtask_joininit(ptcb);
|
||||
|
||||
#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
|
||||
spin_lock_init(&ptcb->cmn.mhead_lock);
|
||||
spin_lock_init(&ptcb->mhead_lock);
|
||||
#endif
|
||||
|
||||
/* 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
|
||||
/* 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)
|
||||
{
|
||||
errcode = -ret;
|
||||
@@ -246,21 +249,20 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
|
||||
|
||||
if (attr->detachstate == PTHREAD_CREATE_DETACHED)
|
||||
{
|
||||
ptcb->cmn.flags |= TCB_FLAG_DETACHED;
|
||||
ptcb->flags |= TCB_FLAG_DETACHED;
|
||||
}
|
||||
|
||||
if (attr->stackaddr)
|
||||
{
|
||||
/* Use pre-allocated stack */
|
||||
|
||||
ret = up_use_stack((FAR struct tcb_s *)ptcb, attr->stackaddr,
|
||||
attr->stacksize);
|
||||
ret = up_use_stack(ptcb, attr->stackaddr, attr->stacksize);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Allocate the stack for the TCB */
|
||||
|
||||
ret = up_create_stack((FAR struct tcb_s *)ptcb,
|
||||
ret = up_create_stack(ptcb,
|
||||
attr->stacksize + attr->guardsize,
|
||||
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)
|
||||
/* Allocate the kernel stack */
|
||||
|
||||
ret = up_addrenv_kstackalloc(&ptcb->cmn);
|
||||
ret = up_addrenv_kstackalloc(ptcb);
|
||||
if (ret < 0)
|
||||
{
|
||||
errcode = ENOMEM;
|
||||
@@ -351,10 +353,10 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
|
||||
|
||||
/* Initialize the sporadic policy */
|
||||
|
||||
ret = nxsched_initialize_sporadic(&ptcb->cmn);
|
||||
ret = nxsched_initialize_sporadic(ptcb);
|
||||
if (ret >= 0)
|
||||
{
|
||||
sporadic = ptcb->cmn.sporadic;
|
||||
sporadic = ptcb->sporadic;
|
||||
DEBUGASSERT(sporadic != NULL);
|
||||
|
||||
/* 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 */
|
||||
|
||||
ret = nxsched_start_sporadic(&ptcb->cmn);
|
||||
ret = nxsched_start_sporadic(ptcb);
|
||||
}
|
||||
|
||||
/* Handle any failures */
|
||||
@@ -392,7 +394,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
|
||||
|
||||
/* Initialize thread local storage */
|
||||
|
||||
ret = tls_init_info(&ptcb->cmn);
|
||||
ret = tls_init_info(ptcb);
|
||||
if (ret != OK)
|
||||
{
|
||||
errcode = -ret;
|
||||
@@ -409,7 +411,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
|
||||
|
||||
if (attr->affinity != 0)
|
||||
{
|
||||
ptcb->cmn.affinity = attr->affinity;
|
||||
ptcb->affinity = attr->affinity;
|
||||
}
|
||||
#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 */
|
||||
|
||||
ptcb->cmn.flags &= ~TCB_FLAG_POLICY_MASK;
|
||||
ptcb->flags &= ~TCB_FLAG_POLICY_MASK;
|
||||
switch (policy)
|
||||
{
|
||||
default:
|
||||
case SCHED_FIFO:
|
||||
ptcb->cmn.flags |= TCB_FLAG_SCHED_FIFO;
|
||||
ptcb->flags |= TCB_FLAG_SCHED_FIFO;
|
||||
break;
|
||||
|
||||
#if CONFIG_RR_INTERVAL > 0
|
||||
case SCHED_OTHER:
|
||||
case SCHED_RR:
|
||||
ptcb->cmn.flags |= TCB_FLAG_SCHED_RR;
|
||||
ptcb->cmn.timeslice = MSEC2TICK(CONFIG_RR_INTERVAL);
|
||||
ptcb->flags |= TCB_FLAG_SCHED_RR;
|
||||
ptcb->timeslice = MSEC2TICK(CONFIG_RR_INTERVAL);
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_SCHED_SPORADIC
|
||||
case SCHED_SPORADIC:
|
||||
ptcb->cmn.flags |= TCB_FLAG_SCHED_SPORADIC;
|
||||
ptcb->flags |= TCB_FLAG_SCHED_SPORADIC;
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
@@ -452,12 +454,12 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
|
||||
|
||||
if (thread != NULL)
|
||||
{
|
||||
*thread = (pthread_t)ptcb->cmn.pid;
|
||||
*thread = (pthread_t)ptcb->pid;
|
||||
}
|
||||
|
||||
/* Then activate the task */
|
||||
|
||||
nxtask_activate((FAR struct tcb_s *)ptcb);
|
||||
nxtask_activate(ptcb);
|
||||
|
||||
return OK;
|
||||
|
||||
@@ -465,8 +467,8 @@ errout_with_tcb:
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
@@ -81,9 +81,10 @@ size_t nxtask_argvstr(FAR struct tcb_s *tcb, FAR char *args, size_t size)
|
||||
#ifndef CONFIG_DISABLE_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
|
||||
#endif
|
||||
|
||||
@@ -663,8 +663,7 @@ int nxtask_setup_scheduler(FAR struct tcb_s *tcb, int priority,
|
||||
{
|
||||
/* Perform common thread setup */
|
||||
|
||||
return nxthread_setup_scheduler((FAR struct tcb_s *)tcb, priority,
|
||||
start, (CODE void *)main, ttype);
|
||||
return nxthread_setup_scheduler(tcb, priority, start, main, ttype);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@@ -692,13 +691,12 @@ int nxtask_setup_scheduler(FAR struct tcb_s *tcb, int priority,
|
||||
****************************************************************************/
|
||||
|
||||
#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)
|
||||
{
|
||||
/* Perform common thread setup */
|
||||
|
||||
return nxthread_setup_scheduler((FAR struct tcb_s *)tcb, priority,
|
||||
start, (CODE void *)entry,
|
||||
return nxthread_setup_scheduler(tcb, priority, start, entry,
|
||||
TCB_FLAG_TTYPE_PTHREAD);
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user