ChibiOS/RT
2.5.1
Scheduler
Collaboration diagram for Scheduler:

Detailed Description

This module provides the default portable scheduler code, scheduler functions can be individually captured by the port layer in order to provide architecture optimized equivalents. When a function is captured its default code is not built into the OS image, the optimized version is included instead.

Data Structures

struct  ReadyList
 Ready list header. More...

Functions

void _scheduler_init (void)
 Scheduler initialization.
ThreadchSchReadyI (Thread *tp)
 Inserts a thread in the Ready List.
void chSchGoSleepS (tstate_t newstate)
 Puts the current thread to sleep into the specified state.
msg_t chSchGoSleepTimeoutS (tstate_t newstate, systime_t time)
 Puts the current thread to sleep into the specified state with timeout specification.
void chSchWakeupS (Thread *ntp, msg_t msg)
 Wakes up a thread.
void chSchRescheduleS (void)
 Performs a reschedule if a higher priority thread is runnable.
bool_t chSchIsPreemptionRequired (void)
 Evaluates if preemption is required.
void chSchDoRescheduleBehind (void)
 Switches to the first thread on the runnable queue.
void chSchDoRescheduleAhead (void)
 Switches to the first thread on the runnable queue.
void chSchDoReschedule (void)
 Switches to the first thread on the runnable queue.

Variables

ReadyList rlist
 Ready list header.

Wakeup status codes

#define RDY_OK   0
 Normal wakeup message.
#define RDY_TIMEOUT   -1
 Wakeup caused by a timeout condition.
#define RDY_RESET   -2
 Wakeup caused by a reset condition.

Priority constants

#define NOPRIO   0
 Ready list header priority.
#define IDLEPRIO   1
 Idle thread priority.
#define LOWPRIO   2
 Lowest user priority.
#define NORMALPRIO   64
 Normal user priority.
#define HIGHPRIO   127
 Highest user priority.
#define ABSPRIO   255
 Greatest possible priority.

Special time constants

#define TIME_IMMEDIATE   ((systime_t)0)
 Zero time specification for some functions with a timeout specification.
#define TIME_INFINITE   ((systime_t)-1)
 Infinite time specification for all functions with a timeout specification.

Macro Functions

#define chSchIsRescRequiredI()   (firstprio(&rlist.r_queue) > currp->p_prio)
 Determines if the current thread must reschedule.
#define chSchCanYieldS()   (firstprio(&rlist.r_queue) >= currp->p_prio)
 Determines if yielding is possible.
#define chSchDoYieldS()
 Yields the time slot.
#define chSchPreemption()
 Inline-able preemption code.

Defines

#define firstprio(rlp)   ((rlp)->p_next->p_prio)
 Returns the priority of the first thread on the given ready list.
#define currp   rlist.r_current
 Current thread pointer access macro.
#define setcurrp(tp)   (currp = (tp))
 Current thread pointer change macro.
#define chSchIsPreemptionRequired()
 Inline-able version of this kernel function.

Function Documentation

void _scheduler_init ( void  )

Scheduler initialization.

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

Definition at line 48 of file chschd.c.

References NOPRIO, queue_init, ReadyList::r_newer, ReadyList::r_older, ReadyList::r_prio, and ReadyList::r_queue.

Referenced by chSysInit().

Thread * chSchReadyI ( Thread tp)

Inserts a thread in the Ready List.

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 p_next and p_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 74 of file chschd.c.

References chDbgAssert, chDbgCheckClassI(), Thread::p_next, Thread::p_state, ReadyList::r_queue, THD_STATE_FINAL, and THD_STATE_READY.

Referenced by chCondBroadcastI(), chCondSignalI(), chEvtSignalI(), chIQPutI(), chIQResetI(), chMsgSend(), chMtxLockS(), chMtxUnlockAll(), chMtxUnlockS(), chOQGetI(), chOQResetI(), chSchDoRescheduleBehind(), chSchWakeupS(), chSemAddCounterI(), chSemResetI(), chSemSignalI(), chSemSignalWait(), and chThdExitS().

Here is the call graph for this function:

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 108 of file chschd.c.

References CH_TIME_QUANTUM, chDbgCheckClassS(), chSysSwitch, currp, fifo_remove(), Thread::p_preempt, ReadyList::r_queue, setcurrp, and THD_STATE_CURRENT.

Referenced by chCondWaitS(), chEvtWaitAll(), chEvtWaitAny(), chEvtWaitOne(), chMsgSend(), chMsgWait(), chMtxLockS(), chSchGoSleepTimeoutS(), chSemSignalWait(), chSemWaitS(), chThdExitS(), and chThdWait().

Here is the call graph for this function:

msg_t chSchGoSleepTimeoutS ( tstate_t  newstate,
systime_t  time 
)

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 RDY_TIMEOUT low level message. The possible Thread States are defined into threads.h.

Parameters:
[in]newstatethe new thread state
[in]timethe 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:
RDY_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 182 of file chschd.c.

References chDbgCheckClassS(), chSchGoSleepS(), chVTIsArmedI, chVTResetI(), chVTSetI(), currp, and TIME_INFINITE.

Referenced by chCondWaitTimeoutS(), chEvtWaitAllTimeout(), chEvtWaitAnyTimeout(), chEvtWaitOneTimeout(), and chSemWaitTimeoutS().

Here is the call graph for this function:

void chSchWakeupS ( Thread 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 p_next and p_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]msgmessage to the awakened thread
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 218 of file chschd.c.

References chDbgCheckClassS(), chSchReadyI(), chSysSwitch, currp, Thread::p_prio, Thread::p_state, Thread::rdymsg, setcurrp, and THD_STATE_CURRENT.

Referenced by chCondSignal(), chMtxUnlock(), chSemSignal(), chThdCreateFromHeap(), chThdCreateFromMemoryPool(), chThdCreateStatic(), and chThdResume().

Here is the call graph for this function:

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.

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 246 of file chschd.c.

References chDbgCheckClassS(), chSchDoRescheduleAhead(), and chSchIsRescRequiredI.

Referenced by chCondBroadcast(), chEvtBroadcastFlags(), chEvtSignal(), chMBFetchS(), chMBPostAheadS(), chMBPostS(), chMBReset(), chMtxUnlockAll(), chSemReset(), chSemSignalWait(), and chThdSetPriority().

Here is the call graph for this function:

bool_t 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 by the scheduler itself or from within the port layer.
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.

Referenced by _port_irq_epilogue().

void chSchDoRescheduleBehind ( void  )

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 or from within the port layer.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 297 of file chschd.c.

References CH_TIME_QUANTUM, chSchReadyI(), chSysSwitch, currp, fifo_remove(), Thread::p_preempt, ReadyList::r_queue, setcurrp, and THD_STATE_CURRENT.

Referenced by chSchDoReschedule().

Here is the call graph for this function:

void chSchDoRescheduleAhead ( void  )

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 or from within the port layer.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 322 of file chschd.c.

References currp, fifo_remove(), Thread::p_next, Thread::p_state, ReadyList::r_queue, setcurrp, THD_STATE_CURRENT, and THD_STATE_READY.

Referenced by chSchDoReschedule(), and chSchRescheduleS().

Here is the call graph for this function:

void chSchDoReschedule ( 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 by the scheduler itself or from within the port layer.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 355 of file chschd.c.

References chSchDoRescheduleAhead(), chSchDoRescheduleBehind(), and currp.

Referenced by _port_switch_from_isr().

Here is the call graph for this function:


Variable Documentation

Ready list header.

Definition at line 40 of file chschd.c.

Referenced by chRegFirstThread(), and chRegNextThread().


Define Documentation

#define RDY_TIMEOUT   -1

Wakeup caused by a timeout condition.

Definition at line 37 of file chschd.h.

Referenced by chCondWaitTimeoutS(), chMBFetchI(), chMBPostAheadI(), chMBPostI(), and chSemWaitTimeoutS().

#define RDY_RESET   -2

Wakeup caused by a reset condition.

Definition at line 39 of file chschd.h.

Referenced by chCondBroadcastI(), and chSemResetI().

#define NOPRIO   0

Ready list header priority.

Definition at line 47 of file chschd.h.

Referenced by _scheduler_init().

#define IDLEPRIO   1

Idle thread priority.

Definition at line 48 of file chschd.h.

Referenced by chSysInit().

#define LOWPRIO   2

Lowest user priority.

Definition at line 49 of file chschd.h.

#define NORMALPRIO   64

Normal user priority.

Definition at line 50 of file chschd.h.

Referenced by chSysInit().

#define HIGHPRIO   127

Highest user priority.

Definition at line 51 of file chschd.h.

Referenced by chThdCreateI(), and chThdSetPriority().

#define ABSPRIO   255

Greatest possible priority.

Definition at line 52 of file chschd.h.

#define TIME_IMMEDIATE   ((systime_t)0)

Zero time specification for some functions with a timeout specification.

Note:
Not all functions accept TIME_IMMEDIATE as timeout parameter, see the specific function documentation.

Definition at line 65 of file chschd.h.

Referenced by chCondWaitTimeoutS(), chEvtWaitAllTimeout(), chEvtWaitAnyTimeout(), chEvtWaitOneTimeout(), chSemWaitTimeoutS(), chThdSleep(), and chVTSetI().

#define TIME_INFINITE   ((systime_t)-1)

Infinite time specification for all functions with a timeout specification.

Definition at line 71 of file chschd.h.

Referenced by chSchGoSleepTimeoutS().

#define firstprio (   rlp)    ((rlp)->p_next->p_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 79 of file chschd.h.

#define currp   rlist.r_current
#define setcurrp (   tp)    (currp = (tp))

Current thread pointer change macro.

Note:
This macro is not meant to be used in the application code but only from within the kernel.
Function Class:
Not an API, this function is for internal use only.

Definition at line 126 of file chschd.h.

Referenced by chSchDoRescheduleAhead(), chSchDoRescheduleBehind(), chSchGoSleepS(), chSchWakeupS(), and chSysInit().

#define chSchIsRescRequiredI ( )    (firstprio(&rlist.r_queue) > currp->p_prio)

Determines if the current thread must reschedule.

This function returns TRUE if there is a ready thread with higher priority.

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 179 of file chschd.h.

Referenced by chSchRescheduleS().

#define chSchCanYieldS ( )    (firstprio(&rlist.r_queue) >= currp->p_prio)

Determines if yielding is possible.

This function returns TRUE if there is a ready thread with equal or higher priority.

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 190 of file chschd.h.

#define chSchDoYieldS ( )
Value:

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 201 of file chschd.h.

Referenced by chThdYield().

#define chSchPreemption ( )
Value:
{                                                 \
  tprio_t p1 = firstprio(&rlist.r_queue);                                   \
  tprio_t p2 = currp->p_prio;                                               \
  if (currp->p_preempt) {                                                   \
    if (p1 > p2)                                                            \
      chSchDoRescheduleAhead();                                             \
  }                                                                         \
  else {                                                                    \
    if (p1 >= p2)                                                           \
      chSchDoRescheduleBehind();                                            \
  }                                                                         \
}

Inline-able preemption code.

This is the common preemption code, this function must be invoked exclusively from the port layer.

Function Class:
Special function, this function has special requirements see the notes.

Definition at line 215 of file chschd.h.

bool_t chSchIsPreemptionRequired (   void)
Value:
(currp->p_preempt ? firstprio(&rlist.r_queue) > currp->p_prio :           \
                      firstprio(&rlist.r_queue) >= currp->p_prio)

Inline-able version of this kernel function.

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 by the scheduler itself or from within the port layer.
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 176 of file chcore.h.