ChibiOS  21.6.0
Scheduler
Collaboration diagram for Scheduler:

Detailed Description

This module provides the default portable scheduler code.

Macros

#define firstprio(rlp)   ((rlp)->next->prio)
 Returns the priority of the first thread on the given ready list. More...
 
#define __sch_get_currthread()   __instance_get_currthread(currcore)
 Current thread pointer get macro. More...
 

Wakeup status codes

#define MSG_OK   (msg_t)0
 Normal wakeup message.
More...
 
#define MSG_TIMEOUT   (msg_t)-1
 Wakeup caused by a timeout condition.
More...
 
#define MSG_RESET   (msg_t)-2
 Wakeup caused by a reset condition.
More...
 

Priority constants

#define NOPRIO   (tprio_t)0
 Ready list header priority.
More...
 
#define IDLEPRIO   (tprio_t)1
 Idle priority.
More...
 
#define LOWPRIO   (tprio_t)2
 Lowest priority.
More...
 
#define NORMALPRIO   (tprio_t)128
 Normal priority.
More...
 
#define HIGHPRIO   (tprio_t)255
 Highest priority.
More...
 

Thread states

#define CH_STATE_READY   (tstate_t)0
 Waiting on the ready list.
More...
 
#define CH_STATE_CURRENT   (tstate_t)1
 Currently running. More...
 
#define CH_STATE_WTSTART   (tstate_t)2
 Just created.
More...
 
#define CH_STATE_SUSPENDED   (tstate_t)3
 Suspended state.
More...
 
#define CH_STATE_QUEUED   (tstate_t)4
 On a queue.
More...
 
#define CH_STATE_WTSEM   (tstate_t)5
 On a semaphore.
More...
 
#define CH_STATE_WTMTX   (tstate_t)6
 On a mutex.
More...
 
#define CH_STATE_WTCOND   (tstate_t)7
 On a cond.variable. More...
 
#define CH_STATE_SLEEPING   (tstate_t)8
 Sleeping.
More...
 
#define CH_STATE_WTEXIT   (tstate_t)9
 Waiting a thread.
More...
 
#define CH_STATE_WTOREVT   (tstate_t)10
 One event.
More...
 
#define CH_STATE_WTANDEVT   (tstate_t)11
 Several events.
More...
 
#define CH_STATE_SNDMSGQ   (tstate_t)12
 Sending a message, in queue.
More...
 
#define CH_STATE_SNDMSG   (tstate_t)13
 Sent a message, waiting answer.
More...
 
#define CH_STATE_WTMSG   (tstate_t)14
 Waiting for a message.
More...
 
#define CH_STATE_FINAL   (tstate_t)15
 Thread terminated. More...
 
#define CH_STATE_NAMES
 Thread states as array of strings. More...
 

Thread flags and attributes

#define CH_FLAG_MODE_MASK   (tmode_t)3U
 Thread memory mode mask.
More...
 
#define CH_FLAG_MODE_STATIC   (tmode_t)0U
 Static thread.
More...
 
#define CH_FLAG_MODE_HEAP   (tmode_t)1U
 Thread allocated from a Memory Heap.
More...
 
#define CH_FLAG_MODE_MPOOL   (tmode_t)2U
 Thread allocated from a Memory Pool.
More...
 
#define CH_FLAG_TERMINATE   (tmode_t)4U
 Termination requested flag.
More...
 

Functions

static thread_t__sch_ready_behind (thread_t *tp)
 Inserts a thread in the Ready List placing it behind its peers. More...
 
static thread_t__sch_ready_ahead (thread_t *tp)
 Inserts a thread in the Ready List placing it ahead its peers. More...
 
static void __sch_reschedule_behind (void)
 Switches to the first thread on the runnable queue. More...
 
static void __sch_reschedule_ahead (void)
 Switches to the first thread on the runnable queue. More...
 
void ch_sch_prio_insert (ch_queue_t *qp, ch_queue_t *tp)
 Inserts a thread into a priority ordered queue. More...
 
thread_tchSchReadyI (thread_t *tp)
 Inserts a thread in the Ready List placing it behind its peers. More...
 
void chSchGoSleepS (tstate_t newstate)
 Puts the current thread to sleep into the specified state. More...
 
msg_t chSchGoSleepTimeoutS (tstate_t newstate, sysinterval_t timeout)
 Puts the current thread to sleep into the specified state with timeout specification. More...
 
void chSchWakeupS (thread_t *ntp, msg_t msg)
 Wakes up a thread. More...
 
void chSchRescheduleS (void)
 Performs a reschedule if a higher priority thread is runnable. More...
 
bool chSchIsPreemptionRequired (void)
 Evaluates if preemption is required. More...
 
void chSchDoPreemption (void)
 Switches to the first thread on the runnable queue. More...
 
void chSchPreemption (void)
 All-in-one preemption code. More...
 
void chSchDoYieldS (void)
 Yields the time slot. More...
 
thread_tchSchSelectFirstI (void)
 Makes runnable the fist thread in the ready list, does not reschedule internally. More...
 

Macro Definition Documentation

◆ MSG_OK

#define MSG_OK   (msg_t)0

Normal wakeup message.

Definition at line 39 of file chschd.h.

◆ MSG_TIMEOUT

#define MSG_TIMEOUT   (msg_t)-1

Wakeup caused by a timeout condition.

Definition at line 40 of file chschd.h.

◆ MSG_RESET

#define MSG_RESET   (msg_t)-2

Wakeup caused by a reset condition.

Definition at line 42 of file chschd.h.

◆ NOPRIO

#define NOPRIO   (tprio_t)0

Ready list header priority.

Definition at line 50 of file chschd.h.

◆ IDLEPRIO

#define IDLEPRIO   (tprio_t)1

Idle priority.

Definition at line 52 of file chschd.h.

◆ LOWPRIO

#define LOWPRIO   (tprio_t)2

Lowest priority.

Definition at line 53 of file chschd.h.

◆ NORMALPRIO

#define NORMALPRIO   (tprio_t)128

Normal priority.

Definition at line 54 of file chschd.h.

◆ HIGHPRIO

#define HIGHPRIO   (tprio_t)255

Highest priority.

Definition at line 55 of file chschd.h.

◆ CH_STATE_READY

#define CH_STATE_READY   (tstate_t)0

Waiting on the ready list.

Definition at line 62 of file chschd.h.

◆ CH_STATE_CURRENT

#define CH_STATE_CURRENT   (tstate_t)1

Currently running.

Definition at line 64 of file chschd.h.

◆ CH_STATE_WTSTART

#define CH_STATE_WTSTART   (tstate_t)2

Just created.

Definition at line 65 of file chschd.h.

◆ CH_STATE_SUSPENDED

#define CH_STATE_SUSPENDED   (tstate_t)3

Suspended state.

Definition at line 66 of file chschd.h.

◆ CH_STATE_QUEUED

#define CH_STATE_QUEUED   (tstate_t)4

On a queue.

Definition at line 67 of file chschd.h.

◆ CH_STATE_WTSEM

#define CH_STATE_WTSEM   (tstate_t)5

On a semaphore.

Definition at line 68 of file chschd.h.

◆ CH_STATE_WTMTX

#define CH_STATE_WTMTX   (tstate_t)6

On a mutex.

Definition at line 69 of file chschd.h.

◆ CH_STATE_WTCOND

#define CH_STATE_WTCOND   (tstate_t)7

On a cond.variable.

Definition at line 70 of file chschd.h.

◆ CH_STATE_SLEEPING

#define CH_STATE_SLEEPING   (tstate_t)8

Sleeping.

Definition at line 71 of file chschd.h.

◆ CH_STATE_WTEXIT

#define CH_STATE_WTEXIT   (tstate_t)9

Waiting a thread.

Definition at line 72 of file chschd.h.

◆ CH_STATE_WTOREVT

#define CH_STATE_WTOREVT   (tstate_t)10

One event.

Definition at line 73 of file chschd.h.

◆ CH_STATE_WTANDEVT

#define CH_STATE_WTANDEVT   (tstate_t)11

Several events.

Definition at line 74 of file chschd.h.

◆ CH_STATE_SNDMSGQ

#define CH_STATE_SNDMSGQ   (tstate_t)12

Sending a message, in queue.

Definition at line 75 of file chschd.h.

◆ CH_STATE_SNDMSG

#define CH_STATE_SNDMSG   (tstate_t)13

Sent a message, waiting answer.

Definition at line 77 of file chschd.h.

◆ CH_STATE_WTMSG

#define CH_STATE_WTMSG   (tstate_t)14

Waiting for a message.

Definition at line 79 of file chschd.h.

◆ CH_STATE_FINAL

#define CH_STATE_FINAL   (tstate_t)15

Thread terminated.

Definition at line 81 of file chschd.h.

◆ CH_STATE_NAMES

#define CH_STATE_NAMES
Value:
"READY", "CURRENT", "WTSTART", "SUSPENDED", "QUEUED", "WTSEM", "WTMTX", \
"WTCOND", "SLEEPING", "WTEXIT", "WTOREVT", "WTANDEVT", "SNDMSGQ", \
"SNDMSG", "WTMSG", "FINAL"

Thread states as array of strings.

Each element in an array initialized with this macro can be indexed using the numeric thread state values.

Definition at line 88 of file chschd.h.

◆ CH_FLAG_MODE_MASK

#define CH_FLAG_MODE_MASK   (tmode_t)3U

Thread memory mode mask.

Definition at line 98 of file chschd.h.

◆ CH_FLAG_MODE_STATIC

#define CH_FLAG_MODE_STATIC   (tmode_t)0U

Static thread.

Definition at line 100 of file chschd.h.

◆ CH_FLAG_MODE_HEAP

#define CH_FLAG_MODE_HEAP   (tmode_t)1U

Thread allocated from a Memory Heap.

Definition at line 101 of file chschd.h.

◆ CH_FLAG_MODE_MPOOL

#define CH_FLAG_MODE_MPOOL   (tmode_t)2U

Thread allocated from a Memory Pool.

Definition at line 103 of file chschd.h.

◆ CH_FLAG_TERMINATE

#define CH_FLAG_TERMINATE   (tmode_t)4U

Termination requested flag.

Definition at line 105 of file chschd.h.

◆ firstprio

#define firstprio (   rlp)    ((rlp)->next->prio)

Returns the priority of the first thread on the given ready list.

Function Class:
Not an API, this function is for internal use only.

Definition at line 130 of file chschd.h.

◆ __sch_get_currthread

#define __sch_get_currthread ( )    __instance_get_currthread(currcore)

Current thread pointer get macro.

Note
This macro is not meant to be used in the application code but only from within the kernel, use chThdGetSelfX() instead.

Definition at line 137 of file chschd.h.

Function Documentation

◆ __sch_ready_behind()

static thread_t* __sch_ready_behind ( thread_t tp)
static

Inserts a thread in the Ready List placing it behind its peers.

The thread is positioned behind all threads with higher or equal priority.

Precondition
The thread must not be already inserted in any list through its next and prev or list corruption would occur.
Postcondition
This function does not reschedule so a call to a rescheduling function must be performed before unlocking the kernel. Note that interrupt handlers always reschedule on exit so an explicit reschedule must not be performed in ISRs.
Parameters
[in]tpthe thread to be made ready
Returns
The thread pointer.
Function Class:
Not an API, this function is for internal use only.

Definition at line 67 of file chschd.c.

References __trace_ready(), ch_pqueue_insert_behind(), CH_STATE_FINAL, CH_STATE_READY, chDbgAssert, ch_thread::hdr, ch_thread::owner, ch_thread::pqueue, ch_ready_list::pqueue, ch_thread::rdymsg, ch_os_instance::rlist, ch_thread::state, and ch_thread::u.

Referenced by __sch_reschedule_behind(), and chSchDoPreemption().

Here is the call graph for this function:

◆ __sch_ready_ahead()

static thread_t* __sch_ready_ahead ( thread_t tp)
static

Inserts a thread in the Ready List placing it ahead its peers.

The thread is positioned ahead all threads with higher or equal priority.

Precondition
The thread must not be already inserted in any list through its next and prev or list corruption would occur.
Postcondition
This function does not reschedule so a call to a rescheduling function must be performed before unlocking the kernel. Note that interrupt handlers always reschedule on exit so an explicit reschedule must not be performed in ISRs.
Parameters
[in]tpthe thread to be made ready
Returns
The thread pointer.
Function Class:
Not an API, this function is for internal use only.

Definition at line 100 of file chschd.c.

References __trace_ready(), ch_pqueue_insert_ahead(), CH_STATE_FINAL, CH_STATE_READY, chDbgAssert, ch_thread::hdr, ch_thread::owner, ch_thread::pqueue, ch_ready_list::pqueue, ch_thread::rdymsg, ch_os_instance::rlist, ch_thread::state, and ch_thread::u.

Referenced by __sch_reschedule_ahead(), chSchDoPreemption(), and chSchSelectFirstI().

Here is the call graph for this function:

◆ __sch_reschedule_behind()

static void __sch_reschedule_behind ( void  )
static

Switches to the first thread on the runnable queue.

The current thread is positioned in the ready list behind all threads having the same priority. The thread regains its time quantum.

Note
Not a user function, it is meant to be invoked by the scheduler itself.
Function Class:
Not an API, this function is for internal use only.

Definition at line 127 of file chschd.c.

References __instance_get_currthread, __instance_set_currthread, __sch_ready_behind(), CH_CFG_IDLE_LEAVE_HOOK, CH_CFG_TIME_QUANTUM, ch_pqueue_remove_highest(), CH_STATE_CURRENT, chSysSwitch, currcore, ch_thread::hdr, IDLEPRIO, ch_thread::pqueue, ch_ready_list::pqueue, ch_priority_queue::prio, ch_os_instance::rlist, ch_thread::state, and ch_thread::ticks.

Referenced by chSchPreemption().

Here is the call graph for this function:

◆ __sch_reschedule_ahead()

static void __sch_reschedule_ahead ( void  )
static

Switches to the first thread on the runnable queue.

The current thread is positioned in the ready list ahead of all threads having the same priority.

Note
Not a user function, it is meant to be invoked by the scheduler itself.
Function Class:
Not an API, this function is for internal use only.

Definition at line 163 of file chschd.c.

References __instance_get_currthread, __instance_set_currthread, __sch_ready_ahead(), CH_CFG_IDLE_LEAVE_HOOK, ch_pqueue_remove_highest(), CH_STATE_CURRENT, chSysSwitch, currcore, ch_thread::hdr, IDLEPRIO, ch_thread::pqueue, ch_ready_list::pqueue, ch_priority_queue::prio, ch_os_instance::rlist, and ch_thread::state.

Referenced by chSchPreemption().

Here is the call graph for this function:

◆ ch_sch_prio_insert()

static void ch_sch_prio_insert ( ch_queue_t qp,
ch_queue_t tp 
)
inline

Inserts a thread into a priority ordered queue.

Note
The insertion is done by scanning the list from the highest priority toward the lowest.
Parameters
[in]qpthe pointer to the threads list header
[in]tpthe pointer to the thread to be inserted in the list
Function Class:
Not an API, this function is for internal use only.

Definition at line 246 of file chschd.c.

References ch_queue::next.

◆ chSchReadyI()

thread_t * chSchReadyI ( thread_t tp)

Inserts a thread in the Ready List placing it behind its peers.

The thread is positioned behind all threads with higher or equal priority.

Precondition
The thread must not be already inserted in any list through its next and prev or list corruption would occur.
Postcondition
This function does not reschedule so a call to a rescheduling function must be performed before unlocking the kernel. Note that interrupt handlers always reschedule on exit so an explicit reschedule must not be performed in ISRs.
Parameters
[in]tpthe thread to be made ready
Returns
The thread pointer.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 276 of file chschd.c.

Referenced by chMtxUnlock(), chMtxUnlockAllS(), chSemSignalWait(), chThdCreateI(), chThdDoDequeueNextI(), chThdExitS(), chThdResumeI(), and chThdStartI().

◆ chSchGoSleepS()

void chSchGoSleepS ( tstate_t  newstate)

Puts the current thread to sleep into the specified state.

The thread goes into a sleeping state. The possible Thread States are defined into threads.h.

Parameters
[in]newstatethe new thread state
Function Class:
This is an S-Class API, this function can be invoked from within a system lock zone by threads only.

Definition at line 301 of file chschd.c.

References __instance_get_currthread, and currcore.

Referenced by chEvtWaitAll(), chEvtWaitAny(), chEvtWaitOne(), chThdExitS(), chThdSuspendS(), and chThdWait().

◆ chSchGoSleepTimeoutS()

msg_t chSchGoSleepTimeoutS ( tstate_t  newstate,
sysinterval_t  timeout 
)

Puts the current thread to sleep into the specified state with timeout specification.

The thread goes into a sleeping state, if it is not awakened explicitly within the specified timeout then it is forcibly awakened with a MSG_TIMEOUT low level message. The possible Thread States are defined into threads.h.

Parameters
[in]newstatethe new thread state
[in]timeoutthe number of ticks before the operation timeouts, the special values are handled as follow:
  • TIME_INFINITE the thread enters an infinite sleep state, this is equivalent to invoking chSchGoSleepS() but, of course, less efficient.
  • TIME_IMMEDIATE this value is not allowed.
Returns
The wakeup message.
Return values
MSG_TIMEOUTif a timeout occurs.
Function Class:
This is an S-Class API, this function can be invoked from within a system lock zone by threads only.

Definition at line 355 of file chschd.c.

References __instance_get_currthread, and currcore.

Referenced by chEvtWaitAllTimeout(), chEvtWaitAnyTimeout(), chEvtWaitOneTimeout(), chThdEnqueueTimeoutS(), chThdSleepS(), and chThdSuspendTimeoutS().

◆ chSchWakeupS()

void chSchWakeupS ( thread_t ntp,
msg_t  msg 
)

Wakes up a thread.

The thread is inserted into the ready list or immediately made running depending on its relative priority compared to the current thread.

Precondition
The thread must not be already inserted in any list through its next and prev or list corruption would occur.
Note
It is equivalent to a chSchReadyI() followed by a chSchRescheduleS() but much more efficient.
The function assumes that the current thread has the highest priority.
Parameters
[in]ntpthe thread to be made ready
[in]msgthe wakeup message
Function Class:
This is an S-Class API, this function can be invoked from within a system lock zone by threads only.

Definition at line 393 of file chschd.c.

References __instance_get_currthread, and currcore.

Referenced by chCondSignal(), chSemSignal(), chThdCreate(), chThdCreateFromHeap(), chThdCreateFromMemoryPool(), chThdCreateStatic(), chThdResumeS(), and chThdStart().

◆ chSchRescheduleS()

void chSchRescheduleS ( void  )

Performs a reschedule if a higher priority thread is runnable.

If a thread with a higher priority than the current thread is in the ready list then make the higher priority thread running.

Note
Only local threads are considered, other cores are signaled and perform a reschedule locally.
Function Class:
This is an S-Class API, this function can be invoked from within a system lock zone by threads only.

Definition at line 454 of file chschd.c.

References __instance_get_currthread, and currcore.

Referenced by chBSemSignal(), chCacheReleaseObject(), chCondBroadcast(), chEvtBroadcastFlags(), chEvtSignal(), chGuardedPoolFree(), chGuardedPoolFreeS(), chMBReset(), chMtxUnlock(), chMtxUnlockAllS(), chSemResetWithMessage(), chSemSignal(), chSysRestoreStatusX(), and chThdSetPriority().

◆ chSchIsPreemptionRequired()

bool chSchIsPreemptionRequired ( void  )

Evaluates if preemption is required.

The decision is taken by comparing the relative priorities and depending on the state of the round robin timeout counter.

Note
Not a user function, it is meant to be invoked from within the port layer in the IRQ-related preemption code.
Return values
trueif there is a thread that must go in running state immediately.
falseif preemption is not required.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 481 of file chschd.c.

References __instance_get_currthread, currcore, firstprio, ch_thread::hdr, ch_thread::pqueue, ch_ready_list::pqueue, ch_priority_queue::prio, ch_os_instance::rlist, and ch_thread::ticks.

◆ chSchDoPreemption()

void chSchDoPreemption ( void  )

Switches to the first thread on the runnable queue.

The current thread is positioned in the ready list behind or ahead of all threads having the same priority depending on if it used its whole time slice.

Note
Not a user function, it is meant to be invoked from within the port layer in the IRQ-related preemption code.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 513 of file chschd.c.

References __instance_get_currthread, __instance_set_currthread, __sch_ready_ahead(), __sch_ready_behind(), CH_CFG_IDLE_LEAVE_HOOK, CH_CFG_TIME_QUANTUM, ch_pqueue_remove_highest(), CH_STATE_CURRENT, chSysSwitch, currcore, ch_thread::hdr, IDLEPRIO, ch_thread::pqueue, ch_ready_list::pqueue, ch_priority_queue::prio, ch_os_instance::rlist, ch_thread::state, and ch_thread::ticks.

Here is the call graph for this function:

◆ chSchPreemption()

void chSchPreemption ( void  )

All-in-one preemption code.

Note
Not a user function, it is meant to be invoked from within the port layer in the IRQ-related preemption code.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 564 of file chschd.c.

References __instance_get_currthread, __sch_reschedule_ahead(), __sch_reschedule_behind(), currcore, firstprio, ch_thread::hdr, likely, ch_thread::pqueue, ch_ready_list::pqueue, ch_priority_queue::prio, ch_os_instance::rlist, and ch_thread::ticks.

Here is the call graph for this function:

◆ chSchDoYieldS()

void chSchDoYieldS ( void  )

Yields the time slot.

Yields the CPU control to the next thread in the ready list with equal or higher priority, if any.

Function Class:
This is an S-Class API, this function can be invoked from within a system lock zone by threads only.

Definition at line 598 of file chschd.c.

References __instance_get_currthread, and currcore.

Referenced by chThdYield().

◆ chSchSelectFirstI()

thread_t * chSchSelectFirstI ( void  )

Makes runnable the fist thread in the ready list, does not reschedule internally.

The current thread is positioned in the ready list ahead of all threads having the same priority.

Note
Not a user function, it is meant to be invoked by the scheduler itself.
Returns
The pointer to the thread being switched in.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 623 of file chschd.c.

References __instance_get_currthread, __instance_set_currthread, __sch_ready_ahead(), CH_CFG_IDLE_LEAVE_HOOK, ch_pqueue_remove_highest(), CH_STATE_CURRENT, currcore, ch_thread::hdr, IDLEPRIO, ch_thread::pqueue, ch_ready_list::pqueue, ch_priority_queue::prio, ch_os_instance::rlist, and ch_thread::state.

Here is the call graph for this function: