123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- #ifndef ZEPHYR_KERNEL_INCLUDE_KSCHED_H_
- #define ZEPHYR_KERNEL_INCLUDE_KSCHED_H_
- #include <kernel_structs.h>
- #include <kernel_internal.h>
- #include <timeout_q.h>
- #include <tracing/tracing.h>
- #include <stdbool.h>
- BUILD_ASSERT(K_LOWEST_APPLICATION_THREAD_PRIO
- >= K_HIGHEST_APPLICATION_THREAD_PRIO);
- #ifdef CONFIG_MULTITHREADING
- #define Z_VALID_PRIO(prio, entry_point) \
- (((prio) == K_IDLE_PRIO && z_is_idle_thread_entry(entry_point)) || \
- ((K_LOWEST_APPLICATION_THREAD_PRIO \
- >= K_HIGHEST_APPLICATION_THREAD_PRIO) \
- && (prio) >= K_HIGHEST_APPLICATION_THREAD_PRIO \
- && (prio) <= K_LOWEST_APPLICATION_THREAD_PRIO))
- #define Z_ASSERT_VALID_PRIO(prio, entry_point) do { \
- __ASSERT(Z_VALID_PRIO((prio), (entry_point)), \
- "invalid priority (%d); allowed range: %d to %d", \
- (prio), \
- K_LOWEST_APPLICATION_THREAD_PRIO, \
- K_HIGHEST_APPLICATION_THREAD_PRIO); \
- } while (false)
- #else
- #define Z_VALID_PRIO(prio, entry_point) ((prio) == -1)
- #define Z_ASSERT_VALID_PRIO(prio, entry_point) __ASSERT((prio) == -1, "")
- #endif
- void z_sched_init(void);
- void z_move_thread_to_end_of_prio_q(struct k_thread *thread);
- int z_is_thread_time_slicing(struct k_thread *thread);
- void z_unpend_thread_no_timeout(struct k_thread *thread);
- struct k_thread *z_unpend1_no_timeout(_wait_q_t *wait_q);
- int z_pend_curr(struct k_spinlock *lock, k_spinlock_key_t key,
- _wait_q_t *wait_q, k_timeout_t timeout);
- int z_pend_curr_irqlock(uint32_t key, _wait_q_t *wait_q, k_timeout_t timeout);
- void z_pend_thread(struct k_thread *thread, _wait_q_t *wait_q,
- k_timeout_t timeout);
- void z_reschedule(struct k_spinlock *lock, k_spinlock_key_t key);
- void z_reschedule_irqlock(uint32_t key);
- struct k_thread *z_unpend_first_thread(_wait_q_t *wait_q);
- void z_unpend_thread(struct k_thread *thread);
- int z_unpend_all(_wait_q_t *wait_q);
- void z_thread_priority_set(struct k_thread *thread, int prio);
- bool z_set_prio(struct k_thread *thread, int prio);
- void *z_get_next_switch_handle(void *interrupted);
- void idle(void *unused1, void *unused2, void *unused3);
- void z_time_slice(int ticks);
- void z_reset_time_slice(void);
- void z_sched_abort(struct k_thread *thread);
- void z_sched_ipi(void);
- void z_sched_start(struct k_thread *thread);
- void z_ready_thread(struct k_thread *thread);
- void z_requeue_current(struct k_thread *curr);
- struct k_thread *z_swap_next_thread(void);
- void z_thread_abort(struct k_thread *thread);
- static inline void z_pend_curr_unlocked(_wait_q_t *wait_q, k_timeout_t timeout)
- {
- (void) z_pend_curr_irqlock(arch_irq_lock(), wait_q, timeout);
- }
- static inline void z_reschedule_unlocked(void)
- {
- (void) z_reschedule_irqlock(arch_irq_lock());
- }
- static inline bool z_is_idle_thread_entry(void *entry_point)
- {
- return entry_point == idle;
- }
- static inline bool z_is_idle_thread_object(struct k_thread *thread)
- {
- #ifdef CONFIG_MULTITHREADING
- #ifdef CONFIG_SMP
- return thread->base.is_idle;
- #else
- return thread == &z_idle_threads[0];
- #endif
- #else
- return false;
- #endif
- }
- static inline bool z_is_thread_suspended(struct k_thread *thread)
- {
- return (thread->base.thread_state & _THREAD_SUSPENDED) != 0U;
- }
- static inline bool z_is_thread_pending(struct k_thread *thread)
- {
- return (thread->base.thread_state & _THREAD_PENDING) != 0U;
- }
- static inline bool z_is_thread_prevented_from_running(struct k_thread *thread)
- {
- uint8_t state = thread->base.thread_state;
- return (state & (_THREAD_PENDING | _THREAD_PRESTART | _THREAD_DEAD |
- _THREAD_DUMMY | _THREAD_SUSPENDED)) != 0U;
- }
- static inline bool z_is_thread_timeout_active(struct k_thread *thread)
- {
- return !z_is_inactive_timeout(&thread->base.timeout);
- }
- static inline bool z_is_thread_ready(struct k_thread *thread)
- {
- return !((z_is_thread_prevented_from_running(thread)) != 0U ||
- z_is_thread_timeout_active(thread));
- }
- static inline bool z_has_thread_started(struct k_thread *thread)
- {
- return (thread->base.thread_state & _THREAD_PRESTART) == 0U;
- }
- static inline bool z_is_thread_state_set(struct k_thread *thread, uint32_t state)
- {
- return (thread->base.thread_state & state) != 0U;
- }
- static inline bool z_is_thread_queued(struct k_thread *thread)
- {
- return z_is_thread_state_set(thread, _THREAD_QUEUED);
- }
- static inline void z_mark_thread_as_suspended(struct k_thread *thread)
- {
- thread->base.thread_state |= _THREAD_SUSPENDED;
- SYS_PORT_TRACING_FUNC(k_thread, sched_suspend, thread);
- }
- static inline void z_mark_thread_as_not_suspended(struct k_thread *thread)
- {
- thread->base.thread_state &= ~_THREAD_SUSPENDED;
- SYS_PORT_TRACING_FUNC(k_thread, sched_resume, thread);
- }
- static inline void z_mark_thread_as_started(struct k_thread *thread)
- {
- thread->base.thread_state &= ~_THREAD_PRESTART;
- }
- static inline void z_mark_thread_as_pending(struct k_thread *thread)
- {
- thread->base.thread_state |= _THREAD_PENDING;
- }
- static inline void z_mark_thread_as_not_pending(struct k_thread *thread)
- {
- thread->base.thread_state &= ~_THREAD_PENDING;
- }
- static inline void z_set_thread_states(struct k_thread *thread, uint32_t states)
- {
- thread->base.thread_state |= states;
- }
- static inline void z_reset_thread_states(struct k_thread *thread,
- uint32_t states)
- {
- thread->base.thread_state &= ~states;
- }
- static inline bool z_is_under_prio_ceiling(int prio)
- {
- return prio >= CONFIG_PRIORITY_CEILING;
- }
- static inline int z_get_new_prio_with_ceiling(int prio)
- {
- return z_is_under_prio_ceiling(prio) ? prio : CONFIG_PRIORITY_CEILING;
- }
- static inline bool z_is_prio1_higher_than_or_equal_to_prio2(int prio1, int prio2)
- {
- return prio1 <= prio2;
- }
- static inline bool z_is_prio_higher_or_equal(int prio1, int prio2)
- {
- return z_is_prio1_higher_than_or_equal_to_prio2(prio1, prio2);
- }
- static inline bool z_is_prio1_lower_than_or_equal_to_prio2(int prio1, int prio2)
- {
- return prio1 >= prio2;
- }
- static inline bool z_is_prio1_higher_than_prio2(int prio1, int prio2)
- {
- return prio1 < prio2;
- }
- static inline bool z_is_prio_higher(int prio, int test_prio)
- {
- return z_is_prio1_higher_than_prio2(prio, test_prio);
- }
- static inline bool z_is_prio_lower_or_equal(int prio1, int prio2)
- {
- return z_is_prio1_lower_than_or_equal_to_prio2(prio1, prio2);
- }
- int32_t z_sched_prio_cmp(struct k_thread *thread_1, struct k_thread *thread_2);
- static inline bool _is_valid_prio(int prio, void *entry_point)
- {
- if (prio == K_IDLE_PRIO && z_is_idle_thread_entry(entry_point)) {
- return true;
- }
- if (!z_is_prio_higher_or_equal(prio,
- K_LOWEST_APPLICATION_THREAD_PRIO)) {
- return false;
- }
- if (!z_is_prio_lower_or_equal(prio,
- K_HIGHEST_APPLICATION_THREAD_PRIO)) {
- return false;
- }
- return true;
- }
- static inline void _ready_one_thread(_wait_q_t *wq)
- {
- struct k_thread *thread = z_unpend_first_thread(wq);
- if (thread != NULL) {
- z_ready_thread(thread);
- }
- }
- static inline void z_sched_lock(void)
- {
- __ASSERT(!arch_is_in_isr(), "");
- __ASSERT(_current->base.sched_locked != 1U, "");
- --_current->base.sched_locked;
- compiler_barrier();
- }
- static ALWAYS_INLINE void z_sched_unlock_no_reschedule(void)
- {
- __ASSERT(!arch_is_in_isr(), "");
- __ASSERT(_current->base.sched_locked != 0U, "");
- compiler_barrier();
- ++_current->base.sched_locked;
- }
- static ALWAYS_INLINE bool z_is_thread_timeout_expired(struct k_thread *thread)
- {
- #ifdef CONFIG_SYS_CLOCK_EXISTS
- return thread->base.timeout.dticks == _EXPIRED;
- #else
- return 0;
- #endif
- }
- bool z_sched_wake(_wait_q_t *wait_q, int swap_retval, void *swap_data);
- static inline bool z_sched_wake_all(_wait_q_t *wait_q, int swap_retval,
- void *swap_data)
- {
- bool woken = false;
- while (z_sched_wake(wait_q, swap_retval, swap_data)) {
- woken = true;
- }
-
- return woken;
- }
- int z_sched_wait(struct k_spinlock *lock, k_spinlock_key_t key,
- _wait_q_t *wait_q, k_timeout_t timeout, void **data);
- #endif
|