SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
sched.h File Reference

POSIX-named scheduling API (<sched.h>) for STK: sched_yield(), sched_get_priority_min/max() and struct sched_param. Implemented by stk_c_pthread.cpp (no dependency on the STK C API). More...

#include <sys/types.h>
Include dependency graph for sched.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  sched_param
 Scheduling parameters. More...

Macros

#define _SYS__PTHREADTYPES_H_
#define STK_C_PTHREADTYPES_GUARD
#define SCHED_OTHER   (0)
#define SCHED_FIFO   (1)
#define SCHED_RR   (2)
#define STK_C_SCHED_PRIORITY_MIN   (0)
#define STK_C_SCHED_PRIORITY_MAX   (31)
#define STK_C_SCHED_PRIORITY_DEFAULT   ((STK_C_SCHED_PRIORITY_MAX + 1) / 2)

Functions

int sched_yield (void)
 Yield the CPU to another ready task.
int sched_get_priority_min (int policy)
 Get the minimum priority of a scheduling policy.
int sched_get_priority_max (int policy)
 Get the maximum priority of a scheduling policy.
int sched_getscheduler (pid_t pid)
 Get the scheduling policy of a thread.
int sched_setscheduler (pid_t pid, int policy, const struct sched_param *param)
 Set the policy and priority of the calling thread (see pthread_setschedparam()).
int sched_getparam (pid_t pid, struct sched_param *param)
 Get the priority of the calling thread.
int sched_setparam (pid_t pid, const struct sched_param *param)
 Set the priority of the calling thread, keeping its policy (see pthread_setschedprio()).
int sched_rr_get_interval (pid_t pid, struct timespec *tp)
 Get the round-robin time slice: one kernel tick (stk::GetTickResolution()).

Detailed Description

POSIX-named scheduling API (<sched.h>) for STK: sched_yield(), sched_get_priority_min/max() and struct sched_param. Implemented by stk_c_pthread.cpp (no dependency on the STK C API).

The kernel must use sched::SwitchStrategyFixedPriority: sched_priority is the fixed priority level of the task (its weight), 0 is the lowest and MAX_PRIORITIES - 1 the highest (sched::SwitchStrategyFP32: 0..31, the default level is PRIORITY_NORMAL = MAX_PRIORITIES / 2). The priority of a running thread can be changed (pthread_setschedparam(), pthread_setschedprio()) through stk::SetWeight(). The policy is always decided by the switch strategy: SCHED_OTHER, SCHED_FIFO and SCHED_RR are accepted and remembered for pthread_attr_setschedpolicy() / pthread_getschedparam() only, they do not change how the kernel schedules (preemptive, round-robin inside one priority level).

Definition in file sched.h.

Macro Definition Documentation

◆ _SYS__PTHREADTYPES_H_

#define _SYS__PTHREADTYPES_H_

Definition at line 28 of file sched.h.

◆ SCHED_FIFO

#define SCHED_FIFO   (1)

Definition at line 44 of file sched.h.

Referenced by stk::pthread::IsValidSchedPolicy().

◆ SCHED_OTHER

#define SCHED_OTHER   (0)

◆ SCHED_RR

#define SCHED_RR   (2)

Definition at line 45 of file sched.h.

Referenced by stk::pthread::IsValidSchedPolicy().

◆ STK_C_PTHREADTYPES_GUARD

#define STK_C_PTHREADTYPES_GUARD

Definition at line 29 of file sched.h.

◆ STK_C_SCHED_PRIORITY_DEFAULT

#define STK_C_SCHED_PRIORITY_DEFAULT   ((STK_C_SCHED_PRIORITY_MAX + 1) / 2)

◆ STK_C_SCHED_PRIORITY_MAX

#define STK_C_SCHED_PRIORITY_MAX   (31)

Definition at line 58 of file sched.h.

Referenced by stk::pthread::IsValidSchedPriority(), and sched_get_priority_max().

◆ STK_C_SCHED_PRIORITY_MIN

#define STK_C_SCHED_PRIORITY_MIN   (0)

Definition at line 55 of file sched.h.

Referenced by stk::pthread::IsValidSchedPriority(), and sched_get_priority_min().

Function Documentation

◆ sched_get_priority_max()

int sched_get_priority_max ( int policy)

Get the maximum priority of a scheduling policy.

Parameters
[in]policySCHED_OTHER, SCHED_FIFO or SCHED_RR.
Returns
STK_C_SCHED_PRIORITY_MAX, or -1 with errno = EINVAL for an unknown policy.

Definition at line 2353 of file stk_c_pthread.cpp.

2354{
2355 int result = -1;
2356
2357 if (IsValidSchedPolicy(policy))
2358 {
2359 result = STK_C_SCHED_PRIORITY_MAX;
2360 }
2361 else
2362 {
2363 errno = EINVAL;
2364 }
2365
2366 return result;
2367}
#define STK_C_SCHED_PRIORITY_MAX
Definition sched.h:58
static bool IsValidSchedPolicy(int policy)

References stk::pthread::IsValidSchedPolicy(), and STK_C_SCHED_PRIORITY_MAX.

Here is the call graph for this function:

◆ sched_get_priority_min()

int sched_get_priority_min ( int policy)

Get the minimum priority of a scheduling policy.

Parameters
[in]policySCHED_OTHER, SCHED_FIFO or SCHED_RR.
Returns
STK_C_SCHED_PRIORITY_MIN, or -1 with errno = EINVAL for an unknown policy.

Definition at line 2337 of file stk_c_pthread.cpp.

2338{
2339 int result = -1;
2340
2341 if (IsValidSchedPolicy(policy))
2342 {
2343 result = STK_C_SCHED_PRIORITY_MIN;
2344 }
2345 else
2346 {
2347 errno = EINVAL;
2348 }
2349
2350 return result;
2351}
#define STK_C_SCHED_PRIORITY_MIN
Definition sched.h:55

References stk::pthread::IsValidSchedPolicy(), and STK_C_SCHED_PRIORITY_MIN.

Here is the call graph for this function:

◆ sched_getparam()

int sched_getparam ( pid_t pid,
struct sched_param * param )

Get the priority of the calling thread.

Returns
0, or -1 with errno = EINVAL or ESRCH.

Definition at line 4495 of file stk_c_pthread.cpp.

4496{
4497 int result = -1;
4498 const pthread_stk_ctrl_t *const ctrl = SchedTarget(pid);
4499
4500 if (ctrl != nullptr)
4501 {
4502 if (param == nullptr)
4503 {
4504 errno = EINVAL;
4505 }
4506 else
4507 {
4508 param->sched_priority = static_cast<int>(ctrl->task.GetWeight());
4509 result = 0;
4510 }
4511 }
4512
4513 return result;
4514}
static pthread_stk_ctrl_t * SchedTarget(pid_t pid)
int sched_priority
Priority level (task weight), STK_C_SCHED_PRIORITY_MIN..STK_C_SCHED_PRIORITY_MAX, higher = more urgen...
Definition sched.h:68
int32_t GetWeight() const override
Get static base weight of the task.
stk::pthread::PthreadTask task

References stk::pthread::PthreadTask::GetWeight(), sched_param::sched_priority, SchedTarget(), and pthread_stk_ctrl_t::task.

Here is the call graph for this function:

◆ sched_getscheduler()

int sched_getscheduler ( pid_t pid)

Get the scheduling policy of a thread.

Parameters
[in]pid0 or the value of getpid() select the CALLING thread (STK has one process, its threads have no pids); any other value fails with ESRCH.
Returns
The policy (SCHED_OTHER / SCHED_FIFO / SCHED_RR), or -1 with errno = ESRCH.

Definition at line 4488 of file stk_c_pthread.cpp.

4489{
4490 const pthread_stk_ctrl_t *const ctrl = SchedTarget(pid);
4491
4492 return (ctrl != nullptr) ? ctrl->task.GetPolicy() : -1;
4493}

References stk::pthread::PthreadTask::GetPolicy(), SchedTarget(), and pthread_stk_ctrl_t::task.

Here is the call graph for this function:

◆ sched_rr_get_interval()

int sched_rr_get_interval ( pid_t pid,
struct timespec * tp )

Get the round-robin time slice: one kernel tick (stk::GetTickResolution()).

Parameters
[out]tpReceives the interval.
Returns
0, or -1 with errno = EINVAL or ESRCH.

Definition at line 4561 of file stk_c_pthread.cpp.

4562{
4563 int result = -1;
4564
4565 if (SchedTarget(pid) != nullptr)
4566 {
4567 if (tp == nullptr)
4568 {
4569 errno = EINVAL;
4570 }
4571 else
4572 {
4573 TicksToTimespec(1, tp); // time slice of the kernel: one tick (GetTickResolution())
4574 result = 0;
4575 }
4576 }
4577
4578 return result;
4579}
static void TicksToTimespec(int64_t ticks, struct timespec *ts)

References SchedTarget(), and stk::pthread::TicksToTimespec().

Here is the call graph for this function:

◆ sched_setparam()

int sched_setparam ( pid_t pid,
const struct sched_param * param )

Set the priority of the calling thread, keeping its policy (see pthread_setschedprio()).

Returns
0, or -1 with errno = EINVAL, ESRCH or ENOTSUP.

Definition at line 4539 of file stk_c_pthread.cpp.

4540{
4541 int result = -1;
4542 pthread_stk_ctrl_t *const ctrl = SchedTarget(pid);
4543
4544 if (ctrl != nullptr)
4545 {
4546 const int err = pthread_setschedparam(ctrl, ctrl->task.GetPolicy(), param);
4547
4548 if (err == 0)
4549 {
4550 result = 0;
4551 }
4552 else
4553 {
4554 errno = err;
4555 }
4556 }
4557
4558 return result;
4559}
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
Change the policy/priority of a running thread.

References stk::pthread::PthreadTask::GetPolicy(), pthread_setschedparam(), SchedTarget(), and pthread_stk_ctrl_t::task.

Here is the call graph for this function:

◆ sched_setscheduler()

int sched_setscheduler ( pid_t pid,
int policy,
const struct sched_param * param )

Set the policy and priority of the calling thread (see pthread_setschedparam()).

Returns
The previous policy, or -1 with errno = EINVAL, ESRCH or ENOTSUP.

Definition at line 4516 of file stk_c_pthread.cpp.

4517{
4518 int result = -1;
4519 pthread_stk_ctrl_t *const ctrl = SchedTarget(pid);
4520
4521 if (ctrl != nullptr)
4522 {
4523 const int old_policy = ctrl->task.GetPolicy();
4524 const int err = pthread_setschedparam(ctrl, policy, param);
4525
4526 if (err == 0)
4527 {
4528 result = old_policy; // POSIX: the previous policy
4529 }
4530 else
4531 {
4532 errno = err;
4533 }
4534 }
4535
4536 return result;
4537}

References stk::pthread::PthreadTask::GetPolicy(), pthread_setschedparam(), SchedTarget(), and pthread_stk_ctrl_t::task.

Here is the call graph for this function:

◆ sched_yield()

int sched_yield ( void )

Yield the CPU to another ready task.

Returns
Always 0.
Warning
ISR-unsafe.

Definition at line 2331 of file stk_c_pthread.cpp.

2332{
2333 stk::Yield();
2334 return 0;
2335}
static void Yield()
Notify scheduler to switch to the next runnable task.
Definition stk_helper.h:562

References stk::Yield().

Referenced by thrd_yield().

Here is the call graph for this function:
Here is the caller graph for this function: