From d79bf6106d2c93224fee4db13fac182d2dea09cd Mon Sep 17 00:00:00 2001 From: Adam Greloch Date: Sun, 5 Jul 2026 13:53:05 +0200 Subject: [PATCH 1/4] pthread: implement setschedprio and {get,set}schedparam TASK: RTOS-1364 --- include/sched.h | 8 ++++- pthread/pthread.c | 87 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 81 insertions(+), 14 deletions(-) diff --git a/include/sched.h b/include/sched.h index 6e06400b..46f88c70 100644 --- a/include/sched.h +++ b/include/sched.h @@ -31,7 +31,13 @@ struct sched_param { }; -int schedInfo(pid_t pid, int policy, sched_info_t *info); +int schedInfo(int pid, int policy, sched_info_t *info); + + +int schedGet(int pid, int tid, sched_params_t *params); + + +int schedSet(int pid, int tid, int policy, sched_params_t *params); int sched_yield(void); diff --git a/pthread/pthread.c b/pthread/pthread.c index 9fd464d7..b100050d 100644 --- a/pthread/pthread.c +++ b/pthread/pthread.c @@ -631,23 +631,33 @@ int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, } -int pthread_attr_setschedparam(pthread_attr_t *attr, - const struct sched_param *param) +static int check_rr_prio(int prio) { - if (attr == NULL || param == NULL) { + sched_info_t info; + + if (schedInfo(0, SCHED_RR, &info) < 0) { return EINVAL; } - if (attr->schedpolicy != SCHED_RR) { - return ENOTSUP; + if (prio > info.maxPriority || prio < info.minPriority) { + return EINVAL; } - sched_info_t info; - if (schedInfo(getpid(), attr->schedpolicy, &info) < 0) { + return EOK; +} + + +int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param) +{ + if (attr == NULL || param == NULL) { return EINVAL; } - if (param->sched_priority > info.maxPriority || param->sched_priority < info.minPriority) { + if (attr->schedpolicy != SCHED_RR) { + return ENOTSUP; + } + + if (check_rr_prio(param->sched_priority) != 0) { return EINVAL; } @@ -734,16 +744,67 @@ int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched) return 0; } +int pthread_setschedprio(pthread_t thread, int prio) +{ + pthread_ctx *ctx = (pthread_ctx *)thread; + int err = EOK; -int pthread_setschedprio(pthread_t thread, int prio); + if (ctx == NULL) { + err = EINVAL; + } + else { + if (check_rr_prio(prio) != 0) { + return EINVAL; + } + sched_params_t p = { 0 }; + p.priorityBase = prio; + err = -schedSet(0, ctx->id, SCHED_RR, &p); + } + + return err; +} -int pthread_getschedparam(pthread_t thread, int *policy, - struct sched_param *__restrict param); +int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *__restrict param) +{ + pthread_ctx *ctx = (pthread_ctx *)thread; + int err = EOK; + + if (ctx == NULL || policy == NULL || param == NULL) { + err = EINVAL; + } + else { + sched_params_t p; + err = -schedGet(0, ctx->id, &p); + if (err == EOK) { + *policy = SCHED_RR; /* Nothing else supported for now */ + param->sched_priority = p.priorityBase; + } + } + + return err; +} -int pthread_setschedparam(pthread_t thread, int policy, - const struct sched_param *param); +int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param) +{ + pthread_ctx *ctx = (pthread_ctx *)thread; + int err; + + if (ctx == NULL || param == NULL || (policy != SCHED_FIFO && policy != SCHED_RR && policy != SCHED_OTHER)) { + err = EINVAL; + } + else if (policy != SCHED_RR) { + err = ENOTSUP; + } + else { + sched_params_t p = { 0 }; + p.priorityBase = param->sched_priority; + err = -schedSet(0, ctx->id, SCHED_RR, &p); + } + + return err; +} static int pthread_mutex_lazy_init(pthread_mutex_t *mutex) From c105bfad4d5a5f9f8b4ef6db87c40eb8255c6d12 Mon Sep 17 00:00:00 2001 From: Adam Greloch Date: Sun, 5 Jul 2026 16:20:28 +0200 Subject: [PATCH 2/4] pthread: reformat TASK: RTOS-1364 --- pthread/pthread.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pthread/pthread.c b/pthread/pthread.c index b100050d..7fd00cc9 100644 --- a/pthread/pthread.c +++ b/pthread/pthread.c @@ -218,7 +218,7 @@ static int pthread_create_main(void) int pthread_create(pthread_t *thread, const pthread_attr_t *attr, - void *(*start_routine)(void *), void *arg) + void *(*start_routine)(void *), void *arg) { const pthread_attr_t *attrs = &pthread_attr_default; @@ -616,18 +616,18 @@ int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, - size_t stacksize) + size_t stacksize) { return pthread_attr_setstackaddr(attr, stackaddr) | - pthread_attr_setstacksize(attr, stacksize); + pthread_attr_setstacksize(attr, stacksize); } int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, - size_t *stacksize) + size_t *stacksize) { return pthread_attr_getstackaddr(attr, stackaddr) | - pthread_attr_getstacksize(attr, stacksize); + pthread_attr_getstacksize(attr, stacksize); } @@ -1139,8 +1139,8 @@ static time_t timespec_to_us(const struct timespec *__restrict time) int pthread_cond_timedwait(pthread_cond_t *__restrict cond, - pthread_mutex_t *__restrict mutex, - const struct timespec *__restrict abstime) + pthread_mutex_t *__restrict mutex, + const struct timespec *__restrict abstime) { int err = 0; From 9d3cf81c3a989ca1014456c621c8b8b9db51ac3b Mon Sep 17 00:00:00 2001 From: Adam Greloch Date: Sun, 5 Jul 2026 16:34:37 +0200 Subject: [PATCH 3/4] sys/sched.h: move native sched syscalls to sys header TASK: RTOS-1364 --- include/sched.h | 9 --------- include/sys/sched.h | 40 ++++++++++++++++++++++++++++++++++++++++ pthread/pthread.c | 1 + 3 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 include/sys/sched.h diff --git a/include/sched.h b/include/sched.h index 46f88c70..f07ab932 100644 --- a/include/sched.h +++ b/include/sched.h @@ -31,15 +31,6 @@ struct sched_param { }; -int schedInfo(int pid, int policy, sched_info_t *info); - - -int schedGet(int pid, int tid, sched_params_t *params); - - -int schedSet(int pid, int tid, int policy, sched_params_t *params); - - int sched_yield(void); diff --git a/include/sys/sched.h b/include/sys/sched.h new file mode 100644 index 00000000..5f9e49d7 --- /dev/null +++ b/include/sys/sched.h @@ -0,0 +1,40 @@ +/* + * Phoenix-RTOS + * + * libphoenix + * + * sys/sched + * + * Copyright 2026 Phoenix Systems + * Author: Adam Greloch + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef _SYS_SCHED_H_ +#define _SYS_SCHED_H_ + + +#include + + +#ifdef __cplusplus +extern "C" { +#endif + + +int schedInfo(int pid, int policy, sched_info_t *info); + + +int schedGet(int pid, int tid, sched_params_t *params); + + +int schedSet(int pid, int tid, int policy, sched_params_t *params); + + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/pthread/pthread.c b/pthread/pthread.c index 7fd00cc9..328f30d5 100644 --- a/pthread/pthread.c +++ b/pthread/pthread.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include From 2ca5da820b18b35d8002ab7668356064f1d866a4 Mon Sep 17 00:00:00 2001 From: Adam Greloch Date: Sun, 5 Jul 2026 17:07:38 +0200 Subject: [PATCH 4/4] sched: implement rest of sched_ POSIX functions TASK: RTOS-1364 --- include/sched.h | 17 +++++++++++ pthread/pthread.c | 78 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/include/sched.h b/include/sched.h index f07ab932..9ca81165 100644 --- a/include/sched.h +++ b/include/sched.h @@ -19,6 +19,8 @@ #include #include +#include +#include #ifdef __cplusplus @@ -40,6 +42,21 @@ int sched_get_priority_max(int policy); int sched_get_priority_min(int policy); +int sched_setparam(pid_t pid, const struct sched_param *param); + + +int sched_getparam(pid_t pid, struct sched_param *param); + + +int sched_setscheduler(pid_t pid, int policy, const struct sched_param *param); + + +int sched_getscheduler(pid_t pid); + + +int sched_rr_get_interval(pid_t pid, struct timespec *tp); + + #ifdef __cplusplus } #endif diff --git a/pthread/pthread.c b/pthread/pthread.c index 328f30d5..3f4c0d0d 100644 --- a/pthread/pthread.c +++ b/pthread/pthread.c @@ -5,8 +5,8 @@ * * pthread * - * Copyright 2017, 2019, 2023 Phoenix Systems - * Author: Pawel Pisarczyk, Marcin Baran, Hubert Badocha + * Copyright 2017, 2019, 2023, 2026 Phoenix Systems + * Author: Pawel Pisarczyk, Marcin Baran, Hubert Badocha, Adam Greloch * * This file is part of Phoenix-RTOS. * @@ -954,6 +954,80 @@ int sched_get_priority_min(int policy) } +int sched_setparam(pid_t pid, const struct sched_param *param) +{ + if (param == NULL || pid < 0) { + return SET_ERRNO(-EINVAL); + } + sched_params_t p = { 0 }; + p.priorityBase = param->sched_priority; + int err = SET_ERRNO(schedSet(pid, 0, SCHED_RR, &p)); + return err < 0 ? err : EOK; +} + + +int sched_getparam(pid_t pid, struct sched_param *param) +{ + if (pid < 0 || param == NULL) { + return SET_ERRNO(-EINVAL); + } + sched_params_t p; + int err = SET_ERRNO(schedGet(pid, 0, &p)); + if (err < 0) { + return err; + } + param->sched_priority = p.priorityBase; + return EOK; +} + + +int sched_setscheduler(pid_t pid, int policy, const struct sched_param *param) +{ + if (policy != SCHED_FIFO && policy != SCHED_RR && policy != SCHED_OTHER) { + return SET_ERRNO(-EINVAL); + } + + if (policy != SCHED_RR) { + return SET_ERRNO(-ENOTSUP); + } + + int err = sched_setparam(pid, param); + return err < 0 ? err : SCHED_RR; +} + + +int sched_getscheduler(pid_t pid) +{ + sched_params_t p; + int err = SET_ERRNO(schedGet(pid, 0, &p)); + return err < 0 ? err : p.policy; +} + + +static void us_to_timespec(time_t abstime_us, struct timespec *__restrict time) +{ + time->tv_sec = abstime_us / (1000 * 1000); + time->tv_nsec = (abstime_us % (1000 * 1000)) * 1000; +} + + +int sched_rr_get_interval(pid_t pid, struct timespec *tp) +{ + if (pid < 0 || tp == NULL) { + return SET_ERRNO(-EINVAL); + } + + sched_info_t info; + int err = SET_ERRNO(schedInfo(pid, SCHED_RR, &info)); + if (err < 0) { + return err; + } + + us_to_timespec(info.interval, tp); + return EOK; +} + + int pthread_condattr_init(pthread_condattr_t *attr) { attr->pshared = PTHREAD_PROCESS_PRIVATE;