diff --git a/include/zephyr/posix/pthread.h b/include/zephyr/posix/pthread.h index 7d272fda5e1..2cc05b6df25 100644 --- a/include/zephyr/posix/pthread.h +++ b/include/zephyr/posix/pthread.h @@ -447,6 +447,7 @@ int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *schedparam); int pthread_setschedparam(pthread_t pthread, int policy, const struct sched_param *param); +int pthread_setschedprio(pthread_t thread, int prio); int pthread_rwlock_destroy(pthread_rwlock_t *rwlock); int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr); diff --git a/lib/posix/options/pthread.c b/lib/posix/options/pthread.c index e7fe47fb030..a46cde7614c 100644 --- a/lib/posix/options/pthread.c +++ b/lib/posix/options/pthread.c @@ -869,6 +869,46 @@ int pthread_setschedparam(pthread_t pthread, int policy, const struct sched_para return ret; } +/** + * @brief Set thread scheduling priority. + * + * See IEEE 1003.1 + */ +int pthread_setschedprio(pthread_t thread, int prio) +{ + int ret; + int new_prio = K_LOWEST_APPLICATION_THREAD_PRIO; + struct posix_thread *t = NULL; + int policy; + struct sched_param param; + + ret = pthread_getschedparam(thread, &policy, ¶m); + + if (ret != 0) { + return ret; + } + + if (!is_posix_policy_prio_valid(prio, policy)) { + return EINVAL; + } + + K_SPINLOCK(&pthread_pool_lock) { + t = to_posix_thread(thread); + if (t == NULL) { + ret = ESRCH; + K_SPINLOCK_BREAK; + } + + new_prio = posix_to_zephyr_priority(prio, policy); + } + + if (ret == 0) { + k_thread_priority_set(&t->thread, new_prio); + } + + return ret; +} + /** * @brief Initialise threads attribute object *