diff --git a/lib/posix/pthread.c b/lib/posix/pthread.c index fa08f5f53e7..3a21d373989 100644 --- a/lib/posix/pthread.c +++ b/lib/posix/pthread.c @@ -227,7 +227,8 @@ static bool is_posix_policy_prio_valid(uint32_t priority, int policy) return false; } -static int zephyr_to_posix_priority(int z_prio, int *policy) +/* Non-static so that they can be tested in ztest */ +int zephyr_to_posix_priority(int z_prio, int *policy) { if (z_prio < 0) { __ASSERT_NO_MSG(-z_prio <= CONFIG_NUM_COOP_PRIORITIES); @@ -239,7 +240,8 @@ static int zephyr_to_posix_priority(int z_prio, int *policy) return ZEPHYR_TO_POSIX_PRIORITY(z_prio); } -static int posix_to_zephyr_priority(int priority, int policy) +/* Non-static so that they can be tested in ztest */ +int posix_to_zephyr_priority(int priority, int policy) { if (policy == SCHED_FIFO) { /* COOP: highest [-CONFIG_NUM_COOP_PRIORITIES, -1] lowest */ diff --git a/tests/posix/common/src/pthread.c b/tests/posix/common/src/pthread.c index feab8d64d59..d4038eba629 100644 --- a/tests/posix/common/src/pthread.c +++ b/tests/posix/common/src/pthread.c @@ -215,6 +215,39 @@ static void *thread_top_term(void *p1) return NULL; } +/* Test the internal priority conversion functions */ +int zephyr_to_posix_priority(int z_prio, int *policy); +int posix_to_zephyr_priority(int priority, int policy); +ZTEST(pthread, test_pthread_priority_conversion) +{ + /* + * ZEPHYR [-CONFIG_NUM_COOP_PRIORITIES, -1] + * TO + * POSIX(FIFO) [0, CONFIG_NUM_COOP_PRIORITIES - 1] + */ + for (int z_prio = -CONFIG_NUM_COOP_PRIORITIES, prio = CONFIG_NUM_COOP_PRIORITIES - 1, + p_prio, policy; + z_prio <= -1; z_prio++, prio--) { + p_prio = zephyr_to_posix_priority(z_prio, &policy); + zassert_equal(policy, SCHED_FIFO); + zassert_equal(p_prio, prio, "%d %d\n", p_prio, prio); + zassert_equal(z_prio, posix_to_zephyr_priority(p_prio, SCHED_FIFO)); + } + + /* + * ZEPHYR [0, CONFIG_NUM_PREEMPT_PRIORITIES - 1] + * TO + * POSIX(RR) [0, CONFIG_NUM_PREEMPT_PRIORITIES - 1] + */ + for (int z_prio = 0, prio = CONFIG_NUM_PREEMPT_PRIORITIES - 1, p_prio, policy; + z_prio < CONFIG_NUM_PREEMPT_PRIORITIES; z_prio++, prio--) { + p_prio = zephyr_to_posix_priority(z_prio, &policy); + zassert_equal(policy, SCHED_RR); + zassert_equal(p_prio, prio, "%d %d\n", p_prio, prio); + zassert_equal(z_prio, posix_to_zephyr_priority(p_prio, SCHED_RR)); + } +} + ZTEST(pthread, test_pthread_execution) { int i, ret;