tests: kernel: Add ISR tests for k_thread_priority_set()

Adds tests demonstrating k_thread_priority_set() being called
from the context of an ISR.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
This commit is contained in:
Peter Mitsis 2024-07-30 19:14:15 -07:00 committed by Anas Nashif
commit 1bb5f3a52b

View file

@ -6,6 +6,8 @@
#include <zephyr/ztest.h>
#include <zephyr/irq_offload.h>
#include "tests_thread_apis.h"
static int thread2_data;
@ -13,6 +15,23 @@ static int thread2_data;
K_SEM_DEFINE(sem_thread2, 0, 1);
K_SEM_DEFINE(sem_thread1, 0, 1);
struct isr_arg {
k_tid_t thread;
int prio;
};
static struct isr_arg prio_args;
/**
* @brief Test ISR used to change a thread's priority
*/
static void test_isr(const void *arg)
{
const struct isr_arg *data = arg;
k_thread_priority_set(data->thread, data->prio);
}
/**
*
* @brief thread2 portion to test setting the priority
@ -103,4 +122,80 @@ ZTEST(threads_lifecycle, test_threads_priority_set)
zassert_equal(thread2_data, thread2_prio,
"Expected priority to be changed to %d, not %d\n",
thread2_prio, thread2_data);
k_thread_join(thread2_id, K_FOREVER);
}
/**
* @ingroup kernel_thread_tests
* @brief Test the k_thread_priority_set() API from an ISR
*
* @see k_thread_priority_set(), k_thread_priority_get()
*/
ZTEST(threads_lifecycle, test_isr_threads_priority_set_)
{
int rv;
int prio = k_thread_priority_get(k_current_get());
/* Lower the priority of the current thread (thread1) */
prio_args.thread = k_current_get();
prio_args.prio = prio + 2;
irq_offload(test_isr, &prio_args);
rv = k_thread_priority_get(k_current_get());
zassert_equal(rv, prio + 2,
"Expected priority to be changed to %d, not %d\n",
prio + 2, rv);
/* Raise the priority of the current thread (thread1) */
prio_args.prio = prio - 2;
irq_offload(test_isr, &prio_args);
rv = k_thread_priority_get(k_current_get());
zassert_equal(rv, prio - 2,
"Expected priority to be changed to %d, not %d\n",
prio - 2, rv);
/* Restore the priority of the current thread (thread1) */
prio_args.prio = prio;
irq_offload(test_isr, &prio_args);
rv = k_thread_priority_get(k_current_get());
zassert_equal(rv, prio,
"Expected priority to be changed to %d, not %d\n",
prio, rv);
/* create thread with lower priority */
int thread2_prio = prio + 1;
k_tid_t thread2_id = k_thread_create(&tdata, tstack, STACK_SIZE,
thread2_set_prio_test,
NULL, NULL, NULL, thread2_prio, 0,
K_NO_WAIT);
/* Lower the priority of thread2 */
prio_args.thread = thread2_id;
prio_args.prio = thread2_prio + 2;
irq_offload(test_isr, &prio_args);
k_sem_give(&sem_thread2);
k_sem_take(&sem_thread1, K_FOREVER);
zassert_equal(thread2_data, thread2_prio + 2,
"Expected priority to be changed to %d, not %d\n",
thread2_prio + 2, thread2_data);
/* Raise the priority of thread2 */
prio_args.prio = thread2_prio - 2;
irq_offload(test_isr, &prio_args);
k_sem_give(&sem_thread2);
k_sem_take(&sem_thread1, K_FOREVER);
zassert_equal(thread2_data, thread2_prio - 2,
"Expected priority to be changed to %d, not %d\n",
thread2_prio - 2, thread2_data);
/* Restore the priority of thread2 */
prio_args.prio = thread2_prio;
irq_offload(test_isr, &prio_args);
k_sem_give(&sem_thread2);
k_sem_take(&sem_thread1, K_FOREVER);
zassert_equal(thread2_data, thread2_prio,
"Expected priority to be changed to %d, not %d\n",
thread2_prio, thread2_data);
k_thread_join(thread2_id, K_FOREVER);
}