diff --git a/kernel/Kconfig b/kernel/Kconfig index 9a326bf546..db3990ee9a 100644 --- a/kernel/Kconfig +++ b/kernel/Kconfig @@ -208,6 +208,13 @@ config THREAD_USERSPACE_LOCAL_DATA depends on USERSPACE default y if ERRNO && !ERRNO_IN_TLS && !LIBC_ERRNO +config USERSPACE_THREAD_MAY_RAISE_PRIORITY + bool "Thread can raise own priority" + depends on USERSPACE + depends on TEST # This should only be enabled by tests. + help + Thread can raise its own priority in userspace mode. + config DYNAMIC_THREAD bool "Support for dynamic threads [EXPERIMENTAL]" select EXPERIMENTAL diff --git a/kernel/include/ksched.h b/kernel/include/ksched.h index e2929b8971..499c00d0c3 100644 --- a/kernel/include/ksched.h +++ b/kernel/include/ksched.h @@ -51,7 +51,6 @@ 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); diff --git a/kernel/sched.c b/kernel/sched.c index deb045ed59..703b6adc6a 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -1013,17 +1013,6 @@ bool z_set_prio(struct k_thread *thread, int prio) return need_sched; } -void z_thread_priority_set(struct k_thread *thread, int prio) -{ - bool need_sched = z_set_prio(thread, prio); - - flag_ipi(); - - if (need_sched && _current->base.sched_locked == 0U) { - z_reschedule_unlocked(); - } -} - static inline bool resched(uint32_t key) { #ifdef CONFIG_SMP @@ -1286,9 +1275,12 @@ void z_impl_k_thread_priority_set(k_tid_t thread, int prio) Z_ASSERT_VALID_PRIO(prio, NULL); __ASSERT(!arch_is_in_isr(), ""); - struct k_thread *th = (struct k_thread *)thread; + bool need_sched = z_set_prio((struct k_thread *)thread, prio); - z_thread_priority_set(th, prio); + flag_ipi(); + if (need_sched && _current->base.sched_locked == 0U) { + z_reschedule_unlocked(); + } } #ifdef CONFIG_USERSPACE @@ -1297,10 +1289,11 @@ static inline void z_vrfy_k_thread_priority_set(k_tid_t thread, int prio) K_OOPS(K_SYSCALL_OBJ(thread, K_OBJ_THREAD)); K_OOPS(K_SYSCALL_VERIFY_MSG(_is_valid_prio(prio, NULL), "invalid thread priority %d", prio)); +#ifndef CONFIG_USERSPACE_THREAD_MAY_RAISE_PRIORITY K_OOPS(K_SYSCALL_VERIFY_MSG((int8_t)prio >= thread->base.prio, "thread priority may only be downgraded (%d < %d)", prio, thread->base.prio)); - +#endif z_impl_k_thread_priority_set(thread, prio); } #include diff --git a/tests/benchmarks/app_kernel/prj_user.conf b/tests/benchmarks/app_kernel/prj_user.conf index e890dbf3c8..fd222e6fb1 100644 --- a/tests/benchmarks/app_kernel/prj_user.conf +++ b/tests/benchmarks/app_kernel/prj_user.conf @@ -25,3 +25,4 @@ CONFIG_USERSPACE=y # Disable time slicing CONFIG_TIMESLICING=n +CONFIG_USERSPACE_THREAD_MAY_RAISE_PRIORITY=y diff --git a/tests/benchmarks/app_kernel/src/master.c b/tests/benchmarks/app_kernel/src/master.c index 0b5c897691..ba0cf53a4f 100644 --- a/tests/benchmarks/app_kernel/src/master.c +++ b/tests/benchmarks/app_kernel/src/master.c @@ -78,29 +78,6 @@ K_PIPE_DEFINE(PIPE_BIGBUFF, 4096, 4); * Custom syscalls */ -/** - * @brief Change a thread's priority - * - * Unlike the normal k_thread_priority_set(), this custom syscall allows - * a user thread to raise its priority. - */ -void z_impl_test_thread_priority_set(k_tid_t thread, int prio) -{ - extern void z_thread_priority_set(struct k_thread *thread, int prio); - - z_thread_priority_set((struct k_thread *)thread, prio); -} - - -#ifdef CONFIG_USERSPACE -static void z_vrfy_test_thread_priority_set(k_tid_t thread, int prio) -{ - z_impl_test_thread_priority_set(thread, prio); -} - -#include -#endif - /** * @brief Obtain a timestamp * diff --git a/tests/benchmarks/app_kernel/src/pipe_b.c b/tests/benchmarks/app_kernel/src/pipe_b.c index 921f217b63..3589b73806 100644 --- a/tests/benchmarks/app_kernel/src/pipe_b.c +++ b/tests/benchmarks/app_kernel/src/pipe_b.c @@ -115,7 +115,7 @@ void pipe_test(void) PRINT_STRING("| " "non-matching sizes (1_TO_N) to lower priority" " |\n"); - test_thread_priority_set(k_current_get(), TaskPrio - 2); + k_thread_priority_set(k_current_get(), TaskPrio - 2); } PRINT_STRING(dashline); PRINT_1_TO_N_HEADER(); @@ -136,7 +136,7 @@ void pipe_test(void) PRINT_1_TO_N(); } PRINT_STRING(dashline); - test_thread_priority_set(k_current_get(), TaskPrio); + k_thread_priority_set(k_current_get(), TaskPrio); } }