Skip to content

Commit 965985f

Browse files
committed
sched:use tcb inside of pthread_tcb_s, remove all cast
Signed-off-by: anjiahao <[email protected]>
1 parent a668562 commit 965985f

File tree

8 files changed

+57
-60
lines changed

8 files changed

+57
-60
lines changed

Diff for: include/nuttx/sched.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -748,12 +748,8 @@ struct task_tcb_s
748748
*/
749749

750750
#ifndef CONFIG_DISABLE_PTHREAD
751-
struct pthread_tcb_s
751+
struct pthread_entry_s
752752
{
753-
/* Common TCB fields ******************************************************/
754-
755-
struct tcb_s cmn; /* Common TCB fields */
756-
757753
/* Task Management Fields *************************************************/
758754

759755
pthread_trampoline_t trampoline; /* User-space startup function */

Diff for: sched/group/group.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ void task_initialize(void);
6161
int group_initialize(FAR struct task_tcb_s *tcb, uint8_t ttype);
6262
void group_postinitialize(FAR struct task_tcb_s *tcb);
6363
#ifndef CONFIG_DISABLE_PTHREAD
64-
void group_bind(FAR struct pthread_tcb_s *tcb);
65-
void group_join(FAR struct pthread_tcb_s *tcb);
64+
void group_bind(FAR struct tcb_s *tcb);
65+
void group_join(FAR struct tcb_s *tcb);
6666
#endif
6767
void group_leave(FAR struct tcb_s *tcb);
6868
void group_drop(FAR struct task_group_s *group);

Diff for: sched/group/group_join.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@
6161
*
6262
****************************************************************************/
6363

64-
void group_bind(FAR struct pthread_tcb_s *tcb)
64+
void group_bind(FAR struct tcb_s *tcb)
6565
{
6666
FAR struct tcb_s *ptcb = this_task();
6767

68-
DEBUGASSERT(ptcb && tcb && ptcb->group && !tcb->cmn.group);
68+
DEBUGASSERT(ptcb && tcb && ptcb->group && !tcb->group);
6969

7070
/* Copy the group reference from the parent to the child */
7171

72-
tcb->cmn.group = ptcb->group;
72+
tcb->group = ptcb->group;
7373
}
7474

7575
/****************************************************************************
@@ -89,21 +89,21 @@ void group_bind(FAR struct pthread_tcb_s *tcb)
8989
*
9090
****************************************************************************/
9191

92-
void group_join(FAR struct pthread_tcb_s *tcb)
92+
void group_join(FAR struct tcb_s *tcb)
9393
{
9494
FAR struct task_group_s *group;
9595
irqstate_t flags;
9696

97-
DEBUGASSERT(tcb && tcb->cmn.group);
97+
DEBUGASSERT(tcb && tcb->group);
9898

9999
/* Get the group from the TCB */
100100

101-
group = tcb->cmn.group;
101+
group = tcb->group;
102102

103103
/* Add the member to the group */
104104

105105
flags = spin_lock_irqsave(&group->tg_lock);
106-
sq_addfirst(&tcb->cmn.member, &group->tg_members);
106+
sq_addfirst(&tcb->member, &group->tg_members);
107107
spin_unlock_irqrestore(&group->tg_lock, flags);
108108
}
109109

Diff for: sched/group/group_leave.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ void group_drop(FAR struct task_group_s *group)
248248

249249
if (group->tg_flags & GROUP_FLAG_DELETED)
250250
{
251-
tcb = container_of(group, struct task_tcb_s, group);
251+
tcb = (FAR struct tcb_s *)
252+
((uintptr_t)group - sizeof(struct tcb_s));
252253

253254
/* Release the group container itself */
254255

Diff for: sched/pthread/pthread.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ extern "C"
9898
* Public Function Prototypes
9999
****************************************************************************/
100100

101-
struct pthread_tcb_s; /* Forward reference */
102101
struct task_group_s; /* Forward reference */
103102

104-
int pthread_setup_scheduler(FAR struct pthread_tcb_s *tcb, int priority,
103+
int pthread_setup_scheduler(FAR struct tcb_s *tcb, int priority,
105104
start_t start, pthread_startroutine_t entry);
106105

107106
int pthread_completejoin(pid_t pid, FAR void *exit_value);

Diff for: sched/pthread/pthread_create.c

+38-36
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,25 @@ const pthread_attr_t g_default_pthread_attr = PTHREAD_ATTR_INITIALIZER;
8787
*
8888
****************************************************************************/
8989

90-
static inline void pthread_tcb_setup(FAR struct pthread_tcb_s *ptcb,
90+
static inline void pthread_tcb_setup(FAR struct tcb_s *ptcb,
9191
FAR struct tcb_s *parent,
9292
pthread_trampoline_t trampoline,
9393
pthread_addr_t arg)
9494
{
95+
FAR struct pthread_entry_s *entry;
9596
#if CONFIG_TASK_NAME_SIZE > 0
9697
/* Copy the pthread name into the TCB */
9798

98-
strlcpy(ptcb->cmn.name, parent->name, CONFIG_TASK_NAME_SIZE);
99+
strlcpy(ptcb->name, parent->name, CONFIG_TASK_NAME_SIZE);
99100
#endif /* CONFIG_TASK_NAME_SIZE */
100101

101102
/* For pthreads, args are strictly pass-by-value; that actual
102103
* type wrapped by pthread_addr_t is unknown.
103104
*/
104105

105-
ptcb->trampoline = trampoline;
106-
ptcb->arg = arg;
106+
entry = (FAR struct pthread_entry_s *)(ptcb + 1);
107+
entry->trampoline = trampoline;
108+
entry->arg = arg;
107109
}
108110

109111
/****************************************************************************
@@ -119,29 +121,31 @@ static inline void pthread_tcb_setup(FAR struct pthread_tcb_s *ptcb,
119121

120122
static void pthread_start(void)
121123
{
122-
FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)this_task();
124+
FAR struct tcb_s *ptcb = this_task();
125+
FAR struct pthread_entry_s *entry =
126+
(FAR struct pthread_entry_s *)(ptcb + 1);
123127

124128
/* The priority of this thread may have been boosted to avoid priority
125129
* inversion problems. If that is the case, then drop to the correct
126130
* execution priority.
127131
*/
128132

129-
if (ptcb->cmn.sched_priority > ptcb->cmn.init_priority)
133+
if (ptcb->sched_priority > ptcb->init_priority)
130134
{
131-
DEBUGVERIFY(nxsched_set_priority(&ptcb->cmn, ptcb->cmn.init_priority));
135+
DEBUGVERIFY(nxsched_set_priority(ptcb, ptcb->init_priority));
132136
}
133137

134138
/* Pass control to the thread entry point. In the kernel build this has to
135139
* be handled differently if we are starting a user-space pthread; we have
136140
* to switch to user-mode before calling into the pthread.
137141
*/
138142

139-
DEBUGASSERT(ptcb->trampoline != NULL && ptcb->cmn.entry.pthread != NULL);
143+
DEBUGASSERT(entry->trampoline != NULL && ptcb->entry.pthread != NULL);
140144

141145
#ifdef CONFIG_BUILD_FLAT
142-
ptcb->trampoline(ptcb->cmn.entry.pthread, ptcb->arg);
146+
entry->trampoline(ptcb->entry.pthread, entry->arg);
143147
#else
144-
up_pthread_start(ptcb->trampoline, ptcb->cmn.entry.pthread, ptcb->arg);
148+
up_pthread_start(entry->trampoline, ptcb->entry.pthread, entry->arg);
145149
#endif
146150

147151
/* The thread has returned (should never happen) */
@@ -181,7 +185,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
181185
pthread_startroutine_t entry, pthread_addr_t arg)
182186
{
183187
pthread_attr_t default_attr = g_default_pthread_attr;
184-
FAR struct pthread_tcb_s *ptcb;
188+
FAR struct tcb_s *ptcb;
185189
struct sched_param param;
186190
FAR struct tcb_s *parent;
187191
int policy;
@@ -209,21 +213,21 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
209213

210214
/* Allocate a TCB for the new task. */
211215

212-
ptcb = kmm_zalloc(sizeof(struct pthread_tcb_s));
216+
ptcb = kmm_zalloc(sizeof(struct tcb_s) + sizeof(struct pthread_entry_s));
213217
if (!ptcb)
214218
{
215219
serr("ERROR: Failed to allocate TCB\n");
216220
return ENOMEM;
217221
}
218222

219-
ptcb->cmn.flags |= TCB_FLAG_FREE_TCB;
223+
ptcb->flags |= TCB_FLAG_FREE_TCB;
220224

221225
/* Initialize the task join */
222226

223-
nxtask_joininit(&ptcb->cmn);
227+
nxtask_joininit(ptcb);
224228

225229
#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
226-
spin_lock_init(&ptcb->cmn.mutex_lock);
230+
spin_lock_init(&ptcb->mutex_lock);
227231
#endif
228232

229233
/* Bind the parent's group to the new TCB (we have not yet joined the
@@ -235,7 +239,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
235239
#ifdef CONFIG_ARCH_ADDRENV
236240
/* Share the address environment of the parent task group. */
237241

238-
ret = addrenv_join(this_task(), (FAR struct tcb_s *)ptcb);
242+
ret = addrenv_join(this_task(), ptcb);
239243
if (ret < 0)
240244
{
241245
errcode = -ret;
@@ -245,22 +249,20 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
245249

246250
if (attr->detachstate == PTHREAD_CREATE_DETACHED)
247251
{
248-
ptcb->cmn.flags |= TCB_FLAG_DETACHED;
252+
ptcb->flags |= TCB_FLAG_DETACHED;
249253
}
250254

251255
if (attr->stackaddr)
252256
{
253257
/* Use pre-allocated stack */
254258

255-
ret = up_use_stack((FAR struct tcb_s *)ptcb, attr->stackaddr,
256-
attr->stacksize);
259+
ret = up_use_stack(ptcb, attr->stackaddr, attr->stacksize);
257260
}
258261
else
259262
{
260263
/* Allocate the stack for the TCB */
261264

262-
ret = up_create_stack((FAR struct tcb_s *)ptcb, attr->stacksize,
263-
TCB_FLAG_TTYPE_PTHREAD);
265+
ret = up_create_stack(ptcb, attr->stacksize, TCB_FLAG_TTYPE_PTHREAD);
264266
}
265267

266268
if (ret != OK)
@@ -273,7 +275,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
273275
defined(CONFIG_BUILD_KERNEL) && defined(CONFIG_ARCH_KERNEL_STACK)
274276
/* Allocate the kernel stack */
275277

276-
ret = up_addrenv_kstackalloc(&ptcb->cmn);
278+
ret = up_addrenv_kstackalloc(ptcb);
277279
if (ret < 0)
278280
{
279281
errcode = ENOMEM;
@@ -283,7 +285,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
283285

284286
/* Initialize thread local storage */
285287

286-
ret = tls_init_info(&ptcb->cmn);
288+
ret = tls_init_info(ptcb);
287289
if (ret != OK)
288290
{
289291
errcode = -ret;
@@ -358,10 +360,10 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
358360

359361
/* Initialize the sporadic policy */
360362

361-
ret = nxsched_initialize_sporadic(&ptcb->cmn);
363+
ret = nxsched_initialize_sporadic(ptcb);
362364
if (ret >= 0)
363365
{
364-
sporadic = ptcb->cmn.sporadic;
366+
sporadic = ptcb->sporadic;
365367
DEBUGASSERT(sporadic != NULL);
366368

367369
/* Save the sporadic scheduling parameters */
@@ -374,7 +376,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
374376

375377
/* And start the first replenishment interval */
376378

377-
ret = nxsched_start_sporadic(&ptcb->cmn);
379+
ret = nxsched_start_sporadic(ptcb);
378380
}
379381

380382
/* Handle any failures */
@@ -407,7 +409,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
407409

408410
if (attr->affinity != 0)
409411
{
410-
ptcb->cmn.affinity = attr->affinity;
412+
ptcb->affinity = attr->affinity;
411413
}
412414
#endif
413415

@@ -423,25 +425,25 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
423425

424426
/* Set the appropriate scheduling policy in the TCB */
425427

426-
ptcb->cmn.flags &= ~TCB_FLAG_POLICY_MASK;
428+
ptcb->flags &= ~TCB_FLAG_POLICY_MASK;
427429
switch (policy)
428430
{
429431
default:
430432
case SCHED_FIFO:
431-
ptcb->cmn.flags |= TCB_FLAG_SCHED_FIFO;
433+
ptcb->flags |= TCB_FLAG_SCHED_FIFO;
432434
break;
433435

434436
#if CONFIG_RR_INTERVAL > 0
435437
case SCHED_OTHER:
436438
case SCHED_RR:
437-
ptcb->cmn.flags |= TCB_FLAG_SCHED_RR;
438-
ptcb->cmn.timeslice = MSEC2TICK(CONFIG_RR_INTERVAL);
439+
ptcb->flags |= TCB_FLAG_SCHED_RR;
440+
ptcb->timeslice = MSEC2TICK(CONFIG_RR_INTERVAL);
439441
break;
440442
#endif
441443

442444
#ifdef CONFIG_SCHED_SPORADIC
443445
case SCHED_SPORADIC:
444-
ptcb->cmn.flags |= TCB_FLAG_SCHED_SPORADIC;
446+
ptcb->flags |= TCB_FLAG_SCHED_SPORADIC;
445447
break;
446448
#endif
447449
}
@@ -450,21 +452,21 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
450452

451453
if (thread != NULL)
452454
{
453-
*thread = (pthread_t)ptcb->cmn.pid;
455+
*thread = (pthread_t)ptcb->pid;
454456
}
455457

456458
/* Then activate the task */
457459

458-
nxtask_activate((FAR struct tcb_s *)ptcb);
460+
nxtask_activate(ptcb);
459461

460462
return OK;
461463

462464
errout_with_tcb:
463465

464466
/* Since we do not join the group, assign group to NULL to clear binding */
465467

466-
ptcb->cmn.group = NULL;
468+
ptcb->group = NULL;
467469

468-
nxsched_release_tcb((FAR struct tcb_s *)ptcb, TCB_FLAG_TTYPE_PTHREAD);
470+
nxsched_release_tcb(ptcb, TCB_FLAG_TTYPE_PTHREAD);
469471
return errcode;
470472
}

Diff for: sched/task/task_argvstr.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ size_t nxtask_argvstr(FAR struct tcb_s *tcb, FAR char *args, size_t size)
8181
#ifndef CONFIG_DISABLE_PTHREAD
8282
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD)
8383
{
84-
FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)tcb;
84+
FAR struct pthread_entry_s *entry =
85+
(FAR struct pthread_entry_s *)(tcb + 1);
8586

86-
n += snprintf(args, size, " %p %p", ptcb->cmn.entry.main, ptcb->arg);
87+
n += snprintf(args, size, " %p %p", tcb->entry.main, entry->arg);
8788
}
8889
else
8990
#endif

Diff for: sched/task/task_setup.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,7 @@ int nxtask_setup_scheduler(FAR struct task_tcb_s *tcb, int priority,
663663
{
664664
/* Perform common thread setup */
665665

666-
return nxthread_setup_scheduler((FAR struct tcb_s *)tcb, priority,
667-
start, (CODE void *)main, ttype);
666+
return nxthread_setup_scheduler(tcb, priority, start, main, ttype);
668667
}
669668

670669
/****************************************************************************
@@ -692,13 +691,12 @@ int nxtask_setup_scheduler(FAR struct task_tcb_s *tcb, int priority,
692691
****************************************************************************/
693692

694693
#ifndef CONFIG_DISABLE_PTHREAD
695-
int pthread_setup_scheduler(FAR struct pthread_tcb_s *tcb, int priority,
694+
int pthread_setup_scheduler(FAR struct tcb_s *tcb, int priority,
696695
start_t start, pthread_startroutine_t entry)
697696
{
698697
/* Perform common thread setup */
699698

700-
return nxthread_setup_scheduler((FAR struct tcb_s *)tcb, priority,
701-
start, (CODE void *)entry,
699+
return nxthread_setup_scheduler(tcb, priority, start, entry,
702700
TCB_FLAG_TTYPE_PTHREAD);
703701
}
704702
#endif

0 commit comments

Comments
 (0)