Skip to content

Commit bf2eb55

Browse files
benwrsnashif
authored andcommitted
kernel: remove K_TIMING thread flag
The fact that a thread is timing out was tracked via two flags: the K_TIMING thread flag bit, and the thread's timeout's delta_ticks_from_prev being -1 or not. This duplication could potentially cause discrepancies if the two flags got out-of-sync, and there was no benfits to having both. Since timeouts that are not parts of a thread rely on the value of delta_ticks_from_prev, standardize on it. Since the K_TIMING bit is removed from the thread's flags, K_READY would not reflect the reality anymore. It is removed and replaced by _is_thread_prevented_froM_running(), which looks at the state flags that are relevant. A thread that is ready now is not prevented from running and does not have an active timeout. Change-Id: I902ef9fb7801b00626df491f5108971817750daa Signed-off-by: Benjamin Walsh <[email protected]>
1 parent 51859b8 commit bf2eb55

File tree

8 files changed

+60
-61
lines changed

8 files changed

+60
-61
lines changed

kernel/unified/include/kernel_structs.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@
3333
/* thread is defined statically */
3434
#define K_STATIC (1 << 8)
3535

36-
#define K_READY 0
37-
38-
/* Thread is waiting on a timeout */
39-
#define K_TIMING (1 << 12)
40-
4136
/* Thread is waiting on an object */
4237
#define K_PENDING (1 << 13)
4338

@@ -53,9 +48,6 @@
5348
/* Not a real thread */
5449
#define K_DUMMY (1 << 17)
5550

56-
#define K_EXECUTION_MASK \
57-
(K_TIMING | K_PENDING | K_PRESTART | K_DEAD | K_SUSPENDED | K_DUMMY)
58-
5951
#if defined(CONFIG_FP_SHARING)
6052
/* thread uses floating point registers */
6153
#define K_FP_REGS (1 << 4)

kernel/unified/include/ksched.h

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -258,33 +258,34 @@ static inline void _mark_thread_as_not_suspended(struct k_thread *thread)
258258
thread->base.flags &= ~K_SUSPENDED;
259259
}
260260

261-
/* mark a thread as being in the timer queue */
262-
static inline void _mark_thread_as_timing(struct k_thread *thread)
261+
/* check if a thread is on the timeout queue */
262+
static inline int _is_thread_timeout_active(struct k_thread *thread)
263263
{
264-
thread->base.flags |= K_TIMING;
264+
#ifdef CONFIG_SYS_CLOCK_EXISTS
265+
return thread->base.timeout.delta_ticks_from_prev != -1;
266+
#else
267+
return 0;
268+
#endif
265269
}
266270

267-
/* mark a thread as not being in the timer queue */
268-
static inline void _mark_thread_as_not_timing(struct k_thread *thread)
271+
static inline int _has_thread_started(struct k_thread *thread)
269272
{
270-
thread->base.flags &= ~K_TIMING;
273+
return !(thread->base.flags & K_PRESTART);
271274
}
272275

273-
/* check if a thread is on the timer queue */
274-
static inline int _is_thread_timing(struct k_thread *thread)
276+
static inline int _is_thread_prevented_from_running(struct k_thread *thread)
275277
{
276-
return !!(thread->base.flags & K_TIMING);
277-
}
278+
return thread->base.flags & (K_PENDING | K_PRESTART |
279+
K_DEAD | K_DUMMY |
280+
K_SUSPENDED);
278281

279-
static inline int _has_thread_started(struct k_thread *thread)
280-
{
281-
return !(thread->base.flags & K_PRESTART);
282282
}
283283

284284
/* check if a thread is ready */
285285
static inline int _is_thread_ready(struct k_thread *thread)
286286
{
287-
return (thread->base.flags & K_EXECUTION_MASK) == K_READY;
287+
return !(_is_thread_prevented_from_running(thread) ||
288+
_is_thread_timeout_active(thread));
288289
}
289290

290291
/* mark a thread as pending in its TCS */
@@ -305,11 +306,22 @@ static inline int _is_thread_pending(struct k_thread *thread)
305306
return !!(thread->base.flags & K_PENDING);
306307
}
307308

309+
/**
310+
* @brief Mark a thread as started
311+
*
312+
* This routine must be called with interrupts locked.
313+
*/
314+
static inline void _mark_thread_as_started(struct k_thread *thread)
315+
{
316+
thread->base.flags &= ~K_PRESTART;
317+
}
318+
308319
/*
309-
* Mark the thread as not being in the timer queue. If this makes it ready,
310-
* then add it to the ready queue according to its priority.
320+
* Put the thread in the ready queue according to its priority if it is not
321+
* blocked for another reason (eg. suspended).
322+
*
323+
* Must be called with interrupts locked.
311324
*/
312-
/* must be called with interrupts locked */
313325
static inline void _ready_thread(struct k_thread *thread)
314326
{
315327
__ASSERT(_is_prio_higher(thread->base.prio, K_LOWEST_THREAD_PRIO) ||
@@ -324,24 +336,14 @@ static inline void _ready_thread(struct k_thread *thread)
324336
"thread %p prio too high (id %d, cannot be higher than %d)",
325337
thread, thread->base.prio, K_HIGHEST_THREAD_PRIO);
326338

327-
/* K_PRESTART is needed to handle the start-with-delay case */
328-
_reset_thread_states(thread, K_TIMING|K_PRESTART);
339+
/* needed to handle the start-with-delay case */
340+
_mark_thread_as_started(thread);
329341

330342
if (_is_thread_ready(thread)) {
331343
_add_thread_to_ready_q(thread);
332344
}
333345
}
334346

335-
/**
336-
* @brief Mark a thread as started
337-
*
338-
* This routine must be called with interrupts locked.
339-
*/
340-
static inline void _mark_thread_as_started(struct k_thread *thread)
341-
{
342-
thread->base.flags &= ~K_PRESTART;
343-
}
344-
345347
/**
346348
* @brief Mark thread as dead
347349
*

kernel/unified/include/timeout_q.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,15 @@ static inline struct _timeout *_handle_one_timeout(
9898
struct _timeout *t = (void *)sys_dlist_get(timeout_q);
9999
struct k_thread *thread = t->thread;
100100

101+
t->delta_ticks_from_prev = -1;
102+
101103
K_DEBUG("timeout %p\n", t);
102104
if (thread != NULL) {
103105
_unpend_thread_timing_out(thread, t);
104106
_ready_thread(thread);
105107
} else if (t->func) {
106108
t->func(t);
107109
}
108-
/*
109-
* Note: t->func() may add timeout again. Make sure that
110-
* delta_ticks_from_prev is set to -1 only if timeout is
111-
* still expired (delta_ticks_from_prev == 0)
112-
*/
113-
if (t->delta_ticks_from_prev == 0) {
114-
t->delta_ticks_from_prev = -1;
115-
}
116110

117111
return (struct _timeout *)sys_dlist_peek_head(timeout_q);
118112
}

kernel/unified/include/wait_q.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,21 @@ static ALWAYS_INLINE void _init_thread_timeout(struct _thread_base *thread_base)
3434
{
3535
ARG_UNUSED(thread_base);
3636
}
37-
#define _add_thread_timeout(thread, wait_q, timeout) do { } while (0)
38-
static inline int _abort_thread_timeout(struct k_thread *thread) { return 0; }
37+
38+
static ALWAYS_INLINE void
39+
_add_thread_timeout(struct k_thread *thread, _wait_q_t *wait_q, int32_t timeout)
40+
{
41+
ARG_UNUSED(thread);
42+
ARG_UNUSED(wait_q);
43+
ARG_UNUSED(timeout);
44+
}
45+
46+
static ALWAYS_INLINE int _abort_thread_timeout(struct k_thread *thread)
47+
{
48+
ARG_UNUSED(thread);
49+
50+
return 0;
51+
}
3952
#define _get_next_timeout_expiry() (K_FOREVER)
4053
#endif
4154

kernel/unified/legacy_timer.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ void _legacy_sleep(int32_t ticks)
3333

3434
int key = irq_lock();
3535

36-
_mark_thread_as_timing(_current);
3736
_remove_thread_from_ready_q(_current);
3837
_add_thread_timeout(_current, NULL, ticks);
3938

kernel/unified/sched.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ void _pend_thread(struct k_thread *thread, _wait_q_t *wait_q, int32_t timeout)
168168
_mark_thread_as_pending(thread);
169169

170170
if (timeout != K_FOREVER) {
171-
_mark_thread_as_timing(thread);
172-
_add_thread_timeout(thread, wait_q,
173-
_TICK_ALIGN + _ms_to_ticks(timeout));
171+
int32_t ticks = _TICK_ALIGN + _ms_to_ticks(timeout);
172+
173+
_add_thread_timeout(thread, wait_q, ticks);
174174
}
175175
}
176176

@@ -306,12 +306,11 @@ void k_sleep(int32_t duration)
306306
return;
307307
}
308308

309+
int32_t ticks = _TICK_ALIGN + _ms_to_ticks(duration);
309310
int key = irq_lock();
310311

311-
_mark_thread_as_timing(_current);
312312
_remove_thread_from_ready_q(_current);
313-
_add_thread_timeout(_current, NULL,
314-
_TICK_ALIGN + _ms_to_ticks(duration));
313+
_add_thread_timeout(_current, NULL, ticks);
315314

316315
_Swap(key);
317316
}

kernel/unified/sem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ static int handle_sem_group(struct k_sem *sem, struct k_thread *thread)
222222
*/
223223

224224
if (!_is_thread_ready(desc->thread)) {
225-
_reset_thread_states(desc->thread, K_PENDING | K_TIMING);
226225
_abort_thread_timeout(desc->thread);
226+
_mark_thread_as_not_pending(desc->thread);
227227
if (_is_thread_ready(desc->thread)) {
228228
_add_thread_to_ready_q(desc->thread);
229229
}

kernel/unified/thread.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ static void schedule_new_thread(struct k_thread *thread, int32_t delay)
220220
if (delay == 0) {
221221
start_thread(thread);
222222
} else {
223-
_mark_thread_as_timing(thread);
224-
_add_thread_timeout(thread, NULL,
225-
_TICK_ALIGN + _ms_to_ticks(delay));
223+
int32_t ticks = _TICK_ALIGN + _ms_to_ticks(delay);
224+
225+
_add_thread_timeout(thread, NULL, ticks);
226226
}
227227
#else
228228
ARG_UNUSED(delay);
@@ -252,7 +252,8 @@ int k_thread_cancel(k_tid_t tid)
252252

253253
int key = irq_lock();
254254

255-
if (_has_thread_started(thread) || !_is_thread_timing(thread)) {
255+
if (_has_thread_started(thread) ||
256+
!_is_thread_timeout_active(thread)) {
256257
irq_unlock(key);
257258
return -EINVAL;
258259
}
@@ -365,9 +366,8 @@ void _k_thread_single_abort(struct k_thread *thread)
365366
if (_is_thread_pending(thread)) {
366367
_unpend_thread(thread);
367368
}
368-
if (_is_thread_timing(thread)) {
369+
if (_is_thread_timeout_active(thread)) {
369370
_abort_thread_timeout(thread);
370-
_mark_thread_as_not_timing(thread);
371371
}
372372
}
373373
_mark_thread_as_dead(thread);

0 commit comments

Comments
 (0)