Skip to content

Commit 85bc0a3

Browse files
Andy RossAnas Nashif
authored andcommitted
kernel: Cleanup, unify _add_thread_to_ready_q() and _ready_thread()
The scheduler exposed two APIs to do the same thing: _add_thread_to_ready_q() was a low level primitive that in most cases was wrapped by _ready_thread(), which also (1) checks that the thread _is_ready() or exits, (2) flags the thread as "started" to handle the case of a thread running for the first time out of a waitq timeout, and (3) signals a logger event. As it turns out, all existing usage was already checking case #1. Case #2 can be better handled in the timeout resume path instead of on every call. And case #3 was probably wrong to have been skipping anyway (there were paths that could make a thread runnable without logging). Now _add_thread_to_ready_q() is an internal scheduler API, as it probably always should have been. This also moves some asserts from the inline _ready_thread() wrapper to the underlying true function for code size reasons, otherwise the extra use of the inline added by this patch blows past code size limits on Quark D2000. Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
1 parent c7ceef6 commit 85bc0a3

File tree

6 files changed

+19
-32
lines changed

6 files changed

+19
-32
lines changed

kernel/include/ksched.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -418,21 +418,6 @@ static inline void _mark_thread_as_started(struct k_thread *thread)
418418
*/
419419
static inline void _ready_thread(struct k_thread *thread)
420420
{
421-
__ASSERT(_is_prio_higher(thread->base.prio, K_LOWEST_THREAD_PRIO) ||
422-
((thread->base.prio == K_LOWEST_THREAD_PRIO) &&
423-
(thread == _idle_thread)),
424-
"thread %p prio too low (is %d, cannot be lower than %d)",
425-
thread, thread->base.prio,
426-
thread == _idle_thread ? K_LOWEST_THREAD_PRIO :
427-
K_LOWEST_APPLICATION_THREAD_PRIO);
428-
429-
__ASSERT(!_is_prio_higher(thread->base.prio, K_HIGHEST_THREAD_PRIO),
430-
"thread %p prio too high (id %d, cannot be higher than %d)",
431-
thread, thread->base.prio, K_HIGHEST_THREAD_PRIO);
432-
433-
/* needed to handle the start-with-delay case */
434-
_mark_thread_as_started(thread);
435-
436421
if (_is_thread_ready(thread)) {
437422
_add_thread_to_ready_q(thread);
438423
}

kernel/include/timeout_q.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ static inline void _handle_one_expired_timeout(struct _timeout *timeout)
9090
K_DEBUG("timeout %p\n", timeout);
9191
if (thread) {
9292
_unpend_thread_timing_out(thread, timeout);
93+
_mark_thread_as_started(thread);
9394
_ready_thread(thread);
9495
irq_unlock(key);
9596
} else {

kernel/init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ static void init_idle_thread(struct k_thread *thr, k_thread_stack_t *stack)
274274
IDLE_STACK_SIZE, idle, NULL, NULL, NULL,
275275
K_LOWEST_THREAD_PRIO, K_ESSENTIAL);
276276
_mark_thread_as_started(thr);
277-
_add_thread_to_ready_q(thr);
277+
_ready_thread(thr);
278278
}
279279
#endif
280280

@@ -352,7 +352,7 @@ static void prepare_multithreading(struct k_thread *dummy_thread)
352352
NULL, NULL, NULL,
353353
CONFIG_MAIN_THREAD_PRIORITY, K_ESSENTIAL);
354354
_mark_thread_as_started(_main_thread);
355-
_add_thread_to_ready_q(_main_thread);
355+
_ready_thread(_main_thread);
356356

357357
#ifdef CONFIG_MULTITHREADING
358358
init_idle_thread(_idle_thread, _idle_stack);

kernel/poll.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ static int signal_poll_event(struct k_poll_event *event, u32_t state,
299299
goto ready_event;
300300
}
301301

302-
_add_thread_to_ready_q(thread);
302+
_ready_thread(thread);
303303
*must_reschedule = !_is_in_isr() && _must_switch_threads();
304304

305305
ready_event:

kernel/sched.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ static struct k_thread *get_ready_q_head(void)
7070

7171
void _add_thread_to_ready_q(struct k_thread *thread)
7272
{
73+
__ASSERT(_is_prio_higher(thread->base.prio, K_LOWEST_THREAD_PRIO) ||
74+
((thread->base.prio == K_LOWEST_THREAD_PRIO) &&
75+
(thread == _idle_thread)),
76+
"thread %p prio too low (is %d, cannot be lower than %d)",
77+
thread, thread->base.prio,
78+
thread == _idle_thread ? K_LOWEST_THREAD_PRIO :
79+
K_LOWEST_APPLICATION_THREAD_PRIO);
80+
81+
__ASSERT(!_is_prio_higher(thread->base.prio, K_HIGHEST_THREAD_PRIO),
82+
"thread %p prio too high (id %d, cannot be higher than %d)",
83+
thread, thread->base.prio, K_HIGHEST_THREAD_PRIO);
84+
7385
#ifdef CONFIG_MULTITHREADING
7486
int q_index = _get_ready_q_q_index(thread->base.prio);
7587
sys_dlist_t *q = &_ready_q.q[q_index];

kernel/thread.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,8 @@ void _impl_k_thread_start(struct k_thread *thread)
227227
}
228228

229229
_mark_thread_as_started(thread);
230-
231-
if (_is_thread_ready(thread)) {
232-
_add_thread_to_ready_q(thread);
233-
if (_must_switch_threads()) {
234-
_Swap(key);
235-
return;
236-
}
237-
}
238-
239-
irq_unlock(key);
230+
_ready_thread(thread);
231+
_reschedule_threads(key);
240232
}
241233

242234
#ifdef CONFIG_USERSPACE
@@ -462,10 +454,7 @@ _SYSCALL_HANDLER1_SIMPLE_VOID(k_thread_suspend, K_OBJ_THREAD, k_tid_t);
462454
void _k_thread_single_resume(struct k_thread *thread)
463455
{
464456
_mark_thread_as_not_suspended(thread);
465-
466-
if (_is_thread_ready(thread)) {
467-
_add_thread_to_ready_q(thread);
468-
}
457+
_ready_thread(thread);
469458
}
470459

471460
void _impl_k_thread_resume(struct k_thread *thread)

0 commit comments

Comments
 (0)