Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions include/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <errno.h>
#include <phoenix/sched.h>
#include <phoenix/posix-types.h>
#include <phoenix/posix-timespec.h>


#ifdef __cplusplus
Expand All @@ -31,9 +33,6 @@ struct sched_param {
};


int schedInfo(pid_t pid, int policy, sched_info_t *info);


int sched_yield(void);


Expand All @@ -43,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
Expand Down
40 changes: 40 additions & 0 deletions include/sys/sched.h
Original file line number Diff line number Diff line change
@@ -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 <phoenix/sched.h>


#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
180 changes: 158 additions & 22 deletions pthread/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -19,6 +19,7 @@
#include <sys/list.h>
#include <sys/mman.h>
#include <sys/minmax.h>
#include <sys/sched.h>
#include <pthread.h>
#include <unistd.h>

Expand Down Expand Up @@ -218,7 +219,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;

Expand Down Expand Up @@ -616,38 +617,48 @@ 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);
}


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;
}

Expand Down Expand Up @@ -734,16 +745,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;
}
Comment thread
adamgreloch marked this conversation as resolved.


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;
}
Comment thread
adamgreloch marked this conversation as resolved.

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;
}
Comment thread
adamgreloch marked this conversation as resolved.


static int pthread_mutex_lazy_init(pthread_mutex_t *mutex)
Expand Down Expand Up @@ -892,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;
}
Comment thread
adamgreloch marked this conversation as resolved.


int sched_getscheduler(pid_t pid)
{
sched_params_t p;
int err = SET_ERRNO(schedGet(pid, 0, &p));
return err < 0 ? err : p.policy;
}
Comment thread
adamgreloch marked this conversation as resolved.


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;
Expand Down Expand Up @@ -1078,8 +1214,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;

Expand Down
Loading