Skip to content

Commit e069ce2

Browse files
TaiJuWuhenrikbrixandersen
authored andcommitted
kernel: Consolidate thread state checking functions
This patch moves `is_aborting()` and `is_halting()` from `kernel/sched.c` to `kernel/include/kthread.h` and renames them to `z_is_thread_aborting()` and `z_is_thread_halting()`, for consistency with other internal kernel APIs. It replaces the previous inline function definitions in `sched.c` with calls to the new header functions. Additionally, direct bitwise checks like `(thread->base.thread_state & _THREAD_DEAD) != 0U` are updated to use the new `z_is_thread_dead()` helper function. This enhances code readability and maintainability. Signed-off-by: TaiJu Wu <[email protected]>
1 parent 979adfd commit e069ce2

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

kernel/include/kthread.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,25 @@ static inline bool z_is_thread_pending(struct k_thread *thread)
9898
return (thread->base.thread_state & _THREAD_PENDING) != 0U;
9999
}
100100

101+
static inline bool z_is_thread_dead(struct k_thread *thread)
102+
{
103+
return (thread->base.thread_state & _THREAD_DEAD) != 0U;
104+
}
105+
106+
/* Return true if the thread is aborting, else false */
107+
static inline bool z_is_thread_aborting(struct k_thread *thread)
108+
{
109+
return (thread->base.thread_state & _THREAD_ABORTING) != 0U;
110+
}
111+
112+
/* Return true if the thread is aborting or suspending, else false */
113+
static inline bool z_is_thread_halting(struct k_thread *thread)
114+
{
115+
return (thread->base.thread_state &
116+
(_THREAD_ABORTING | _THREAD_SUSPENDING)) != 0U;
117+
}
118+
119+
101120
static inline bool z_is_thread_prevented_from_running(struct k_thread *thread)
102121
{
103122
uint8_t state = thread->base.thread_state;

kernel/sched.c

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,6 @@ void z_requeue_current(struct k_thread *thread)
142142
signal_pending_ipi();
143143
}
144144

145-
/* Return true if the thread is aborting, else false */
146-
static inline bool is_aborting(struct k_thread *thread)
147-
{
148-
return (thread->base.thread_state & _THREAD_ABORTING) != 0U;
149-
}
150-
151-
/* Return true if the thread is aborting or suspending, else false */
152-
static inline bool is_halting(struct k_thread *thread)
153-
{
154-
return (thread->base.thread_state &
155-
(_THREAD_ABORTING | _THREAD_SUSPENDING)) != 0U;
156-
}
157-
158145
/* Clear the halting bits (_THREAD_ABORTING and _THREAD_SUSPENDING) */
159146
static inline void clear_halting(struct k_thread *thread)
160147
{
@@ -167,8 +154,8 @@ static inline void clear_halting(struct k_thread *thread)
167154
static ALWAYS_INLINE struct k_thread *next_up(void)
168155
{
169156
#ifdef CONFIG_SMP
170-
if (is_halting(_current)) {
171-
halt_thread(_current, is_aborting(_current) ?
157+
if (z_is_thread_halting(_current)) {
158+
halt_thread(_current, z_is_thread_aborting(_current) ?
172159
_THREAD_DEAD : _THREAD_SUSPENDED);
173160
}
174161
#endif /* CONFIG_SMP */
@@ -378,12 +365,12 @@ void z_move_thread_to_end_of_prio_q(struct k_thread *thread)
378365
*/
379366
static void thread_halt_spin(struct k_thread *thread, k_spinlock_key_t key)
380367
{
381-
if (is_halting(_current)) {
368+
if (z_is_thread_halting(_current)) {
382369
halt_thread(_current,
383-
is_aborting(_current) ? _THREAD_DEAD : _THREAD_SUSPENDED);
370+
z_is_thread_aborting(_current) ? _THREAD_DEAD : _THREAD_SUSPENDED);
384371
}
385372
k_spin_unlock(&_sched_spinlock, key);
386-
while (is_halting(thread)) {
373+
while (z_is_thread_halting(thread)) {
387374
unsigned int k = arch_irq_lock();
388375

389376
arch_spin_relax(); /* Requires interrupts be masked */
@@ -1354,7 +1341,7 @@ void z_thread_abort(struct k_thread *thread)
13541341
bool essential = z_is_thread_essential(thread);
13551342
k_spinlock_key_t key = k_spin_lock(&_sched_spinlock);
13561343

1357-
if ((thread->base.thread_state & _THREAD_DEAD) != 0U) {
1344+
if (z_is_thread_dead(thread)) {
13581345
k_spin_unlock(&_sched_spinlock, key);
13591346
return;
13601347
}
@@ -1374,7 +1361,7 @@ void z_impl_k_thread_abort(k_tid_t thread)
13741361

13751362
z_thread_abort(thread);
13761363

1377-
__ASSERT_NO_MSG((thread->base.thread_state & _THREAD_DEAD) != 0);
1364+
__ASSERT_NO_MSG(z_is_thread_dead(thread));
13781365

13791366
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_thread, abort, thread);
13801367
}
@@ -1387,7 +1374,7 @@ int z_impl_k_thread_join(struct k_thread *thread, k_timeout_t timeout)
13871374

13881375
SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_thread, join, thread, timeout);
13891376

1390-
if ((thread->base.thread_state & _THREAD_DEAD) != 0U) {
1377+
if (z_is_thread_dead(thread)) {
13911378
z_sched_switch_spin(thread);
13921379
ret = 0;
13931380
} else if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {

0 commit comments

Comments
 (0)