kernel: move essential flag related routines out

The functions to manipulate the essential flag indeed operate on
threads, but they are misplaced in the thread implementation file. Put
them alongside other routines setting other thread flags and cleanup
headers a bit.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
This commit is contained in:
Anas Nashif 2024-02-22 22:24:36 -05:00
commit 8791012ed1
9 changed files with 53 additions and 53 deletions

View file

@ -110,11 +110,6 @@ static inline void *z_thread_malloc(size_t size)
return z_thread_aligned_alloc(0, size);
}
/* set and clear essential thread flag */
extern void z_thread_essential_set(void);
extern void z_thread_essential_clear(void);
/* clean up when a thread is aborted */
#if defined(CONFIG_THREAD_MONITOR)

View file

@ -13,8 +13,6 @@
#include <zephyr/tracing/tracing.h>
#include <stdbool.h>
bool z_is_thread_essential(void);
BUILD_ASSERT(K_LOWEST_APPLICATION_THREAD_PRIO
>= K_HIGHEST_APPLICATION_THREAD_PRIO);
@ -169,6 +167,36 @@ static inline void z_mark_thread_as_not_pending(struct k_thread *thread)
thread->base.thread_state &= ~_THREAD_PENDING;
}
/*
* This function tags the current thread as essential to system operation.
* Exceptions raised by this thread will be treated as a fatal system error.
*/
static inline void z_thread_essential_set(struct k_thread *thread)
{
thread->base.user_options |= K_ESSENTIAL;
}
/*
* This function tags the current thread as not essential to system operation.
* Exceptions raised by this thread may be recoverable.
* (This is the default tag for a thread.)
*/
static inline void z_thread_essential_clear(struct k_thread *thread)
{
thread->base.user_options &= ~K_ESSENTIAL;
}
/*
* This routine indicates if the current thread is an essential system thread.
*
* Returns true if current thread is essential, false if it is not.
*/
static inline bool z_is_thread_essential(struct k_thread *thread)
{
return (thread->base.user_options & K_ESSENTIAL) == K_ESSENTIAL;
}
static inline void z_set_thread_states(struct k_thread *thread, uint32_t states)
{
thread->base.thread_state |= states;

View file

@ -370,8 +370,8 @@ static void bg_thread_main(void *unused1, void *unused2, void *unused3)
(void)main();
/* Mark nonessential since main() has no more work to do */
z_main_thread.base.user_options &= ~K_ESSENTIAL;
/* Mark non-essential since main() has no more work to do */
z_thread_essential_clear(&z_main_thread);
#ifdef CONFIG_COVERAGE_DUMP
/* Dump coverage data once the main() has exited. */

View file

@ -1732,7 +1732,7 @@ void z_thread_abort(struct k_thread *thread)
{
k_spinlock_key_t key = k_spin_lock(&sched_spinlock);
if ((thread->base.user_options & K_ESSENTIAL) != 0) {
if (z_is_thread_essential(thread)) {
k_spin_unlock(&sched_spinlock, key);
__ASSERT(false, "aborting essential thread %p", thread);
k_panic();
@ -1836,7 +1836,7 @@ static inline void z_vrfy_k_thread_abort(k_tid_t thread)
return;
}
K_OOPS(K_SYSCALL_VERIFY_MSG(!(thread->base.user_options & K_ESSENTIAL),
K_OOPS(K_SYSCALL_VERIFY_MSG(!z_is_thread_essential(thread),
"aborting essential thread %p", thread));
z_impl_k_thread_abort((struct k_thread *)thread);

View file

@ -143,35 +143,6 @@ bool k_is_in_isr(void)
}
EXPORT_SYMBOL(k_is_in_isr);
/*
* This function tags the current thread as essential to system operation.
* Exceptions raised by this thread will be treated as a fatal system error.
*/
void z_thread_essential_set(void)
{
_current->base.user_options |= K_ESSENTIAL;
}
/*
* This function tags the current thread as not essential to system operation.
* Exceptions raised by this thread may be recoverable.
* (This is the default tag for a thread.)
*/
void z_thread_essential_clear(void)
{
_current->base.user_options &= ~K_ESSENTIAL;
}
/*
* This routine indicates if the current thread is an essential system thread.
*
* Returns true if current thread is essential, false if it is not.
*/
bool z_is_thread_essential(void)
{
return (_current->base.user_options & K_ESSENTIAL) == K_ESSENTIAL;
}
#ifdef CONFIG_THREAD_CUSTOM_DATA
void z_impl_k_thread_custom_data_set(void *value)
{
@ -918,7 +889,7 @@ FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
SYS_PORT_TRACING_FUNC(k_thread, user_mode_enter);
_current->base.user_options |= K_USER;
z_thread_essential_clear();
z_thread_essential_clear(_current);
#ifdef CONFIG_THREAD_MONITOR
_current->entry.pEntry = entry;
_current->entry.parameter1 = p1;

View file

@ -6,3 +6,8 @@ project(message_capture)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
target_include_directories(app PRIVATE
${ZEPHYR_BASE}/kernel/include
${ZEPHYR_BASE}/arch/${ARCH}/include
)

View file

@ -6,10 +6,11 @@
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <ksched.h>
static volatile int expected_reason = -1;
void z_thread_essential_clear(void);
void z_thread_essential_clear(struct k_thread *thread);
void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf)
{
@ -85,7 +86,7 @@ int main(void)
* panic and not an oops). Set the thread non-essential as a
* workaround.
*/
z_thread_essential_clear();
z_thread_essential_clear(_current);
test_message_capture();
return 0;

View file

@ -229,7 +229,7 @@ static void umode_entry(void *thread_id, void *p2, void *p3)
ARG_UNUSED(p2);
ARG_UNUSED(p3);
if (!z_is_thread_essential() &&
if (!z_is_thread_essential(_current) &&
(k_current_get() == (k_tid_t)thread_id)) {
ztest_test_pass();
} else {
@ -246,9 +246,9 @@ static void umode_entry(void *thread_id, void *p2, void *p3)
*/
static void enter_user_mode_entry(void *p1, void *p2, void *p3)
{
z_thread_essential_set();
z_thread_essential_set(_current);
zassert_true(z_is_thread_essential(), "Thread isn't set"
zassert_true(z_is_thread_essential(_current), "Thread isn't set"
" as essential\n");
k_thread_user_mode_enter(umode_entry,

View file

@ -26,16 +26,16 @@ static void thread_entry(void *p1, void *p2, void *p3)
ARG_UNUSED(p2);
ARG_UNUSED(p3);
z_thread_essential_set();
z_thread_essential_set(_current);
if (z_is_thread_essential()) {
if (z_is_thread_essential(_current)) {
k_busy_wait(100);
} else {
zassert_unreachable("The thread is not set as essential");
}
z_thread_essential_clear();
zassert_false(z_is_thread_essential(),
z_thread_essential_clear(_current);
zassert_false(z_is_thread_essential(_current),
"Essential flag of the thread is not cleared");
k_sem_give(&sync_sem);
@ -67,7 +67,7 @@ void k_sys_fatal_error_handler(unsigned int reason,
fatal_error_signaled = true;
z_thread_essential_clear();
z_thread_essential_clear(_current);
}
static void abort_thread_entry(void *p1, void *p2, void *p3)
@ -76,9 +76,9 @@ static void abort_thread_entry(void *p1, void *p2, void *p3)
ARG_UNUSED(p2);
ARG_UNUSED(p3);
z_thread_essential_set();
z_thread_essential_set(_current);
if (z_is_thread_essential()) {
if (z_is_thread_essential(_current)) {
k_busy_wait(100);
} else {
zassert_unreachable("The thread is not set as essential");