2010-02-21 07:24:53 +00:00
|
|
|
/*
|
2011-03-18 18:38:08 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
2013-02-02 10:58:09 +00:00
|
|
|
2011,2012,2013 Giovanni Di Sirio.
|
2010-02-21 07:24:53 +00:00
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-02-27 08:54:22 +00:00
|
|
|
* @file chschd.h
|
2010-02-21 07:24:53 +00:00
|
|
|
* @brief Scheduler macros and structures.
|
|
|
|
*
|
|
|
|
* @addtogroup scheduler
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#ifndef _CHSCHD_H_
|
|
|
|
#define _CHSCHD_H_
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2013-07-19 11:48:43 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module constants. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2011-08-22 17:18:52 +00:00
|
|
|
/**
|
|
|
|
* @name Wakeup status codes
|
|
|
|
* @{
|
|
|
|
*/
|
2013-08-17 11:52:50 +00:00
|
|
|
#define MSG_OK 0 /**< @brief Normal wakeup message. */
|
|
|
|
#define MSG_TIMEOUT -1 /**< @brief Wakeup caused by a timeout
|
2011-08-22 17:18:52 +00:00
|
|
|
condition. */
|
2013-08-17 11:52:50 +00:00
|
|
|
#define MSG_RESET -2 /**< @brief Wakeup caused by a reset
|
2011-08-22 17:18:52 +00:00
|
|
|
condition. */
|
|
|
|
/** @} */
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2011-08-22 17:18:52 +00:00
|
|
|
/**
|
|
|
|
* @name Priority constants
|
|
|
|
* @{
|
|
|
|
*/
|
2010-02-21 07:24:53 +00:00
|
|
|
#define NOPRIO 0 /**< @brief Ready list header priority. */
|
|
|
|
#define IDLEPRIO 1 /**< @brief Idle thread priority. */
|
|
|
|
#define LOWPRIO 2 /**< @brief Lowest user priority. */
|
|
|
|
#define NORMALPRIO 64 /**< @brief Normal user priority. */
|
|
|
|
#define HIGHPRIO 127 /**< @brief Highest user priority. */
|
|
|
|
#define ABSPRIO 255 /**< @brief Greatest possible priority. */
|
2011-08-22 17:18:52 +00:00
|
|
|
/** @} */
|
2010-02-21 07:24:53 +00:00
|
|
|
|
|
|
|
/**
|
2011-08-22 17:18:52 +00:00
|
|
|
* @name Special time constants
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @brief Zero time specification for some functions with a timeout
|
2010-02-21 07:24:53 +00:00
|
|
|
* specification.
|
|
|
|
* @note Not all functions accept @p TIME_IMMEDIATE as timeout parameter,
|
|
|
|
* see the specific function documentation.
|
|
|
|
*/
|
2011-02-21 19:06:46 +00:00
|
|
|
#define TIME_IMMEDIATE ((systime_t)0)
|
2010-02-21 07:24:53 +00:00
|
|
|
|
|
|
|
/**
|
2011-08-22 17:18:52 +00:00
|
|
|
* @brief Infinite time specification for all functions with a timeout
|
2010-02-21 07:24:53 +00:00
|
|
|
* specification.
|
|
|
|
*/
|
2011-02-21 19:06:46 +00:00
|
|
|
#define TIME_INFINITE ((systime_t)-1)
|
2011-08-22 17:18:52 +00:00
|
|
|
/** @} */
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2013-07-19 11:48:43 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module pre-compile time settings. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Derived constants and error checks. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module data structures and types. */
|
|
|
|
/*===========================================================================*/
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2013-07-19 11:48:43 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module macros. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the priority of the first thread on the given ready list.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
#define firstprio(rlp) ((rlp)->p_next->p_prio)
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2010-03-19 12:48:54 +00:00
|
|
|
/**
|
2010-06-19 11:24:30 +00:00
|
|
|
* @brief Current thread pointer access macro.
|
2010-03-19 12:48:54 +00:00
|
|
|
* @note This macro is not meant to be used in the application code but
|
|
|
|
* only from within the kernel, use the @p chThdSelf() API instead.
|
|
|
|
* @note It is forbidden to use this macro in order to change the pointer
|
|
|
|
* (currp = something), use @p setcurrp() instead.
|
|
|
|
*/
|
2013-08-10 08:07:43 +00:00
|
|
|
#define currp ch.rlist.r_current
|
2010-03-19 12:48:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Current thread pointer change macro.
|
|
|
|
* @note This macro is not meant to be used in the application code but
|
|
|
|
* only from within the kernel.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2010-03-19 12:48:54 +00:00
|
|
|
*/
|
2010-04-03 15:46:41 +00:00
|
|
|
#define setcurrp(tp) (currp = (tp))
|
2013-07-19 11:48:43 +00:00
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* External declarations. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2010-02-21 07:24:53 +00:00
|
|
|
/*
|
|
|
|
* Scheduler APIs.
|
|
|
|
*/
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2011-03-29 14:51:08 +00:00
|
|
|
void _scheduler_init(void);
|
2013-07-19 12:22:31 +00:00
|
|
|
thread_t *chSchReadyI(thread_t *tp);
|
2010-02-21 07:24:53 +00:00
|
|
|
void chSchGoSleepS(tstate_t newstate);
|
|
|
|
msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t time);
|
2013-07-19 12:22:31 +00:00
|
|
|
void chSchWakeupS(thread_t *tp, msg_t msg);
|
2010-02-21 07:24:53 +00:00
|
|
|
void chSchRescheduleS(void);
|
2013-07-19 11:48:43 +00:00
|
|
|
bool chSchIsPreemptionRequired(void);
|
2012-02-06 19:45:47 +00:00
|
|
|
void chSchDoRescheduleBehind(void);
|
|
|
|
void chSchDoRescheduleAhead(void);
|
2011-08-13 07:06:02 +00:00
|
|
|
void chSchDoReschedule(void);
|
2010-02-21 07:24:53 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-07-19 11:48:43 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module inline functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2010-03-16 15:43:23 +00:00
|
|
|
/**
|
|
|
|
* @brief Determines if the current thread must reschedule.
|
2013-07-20 07:24:12 +00:00
|
|
|
* @details This function returns @p true if there is a ready thread with
|
2010-03-16 15:43:23 +00:00
|
|
|
* higher priority.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2010-03-16 15:43:23 +00:00
|
|
|
*/
|
2013-07-19 11:48:43 +00:00
|
|
|
static inline bool chSchIsRescRequiredI(void) {
|
|
|
|
|
|
|
|
chDbgCheckClassI();
|
|
|
|
|
2013-08-10 08:07:43 +00:00
|
|
|
return firstprio(&ch.rlist.r_queue) > currp->p_prio;
|
2013-07-19 11:48:43 +00:00
|
|
|
}
|
2010-03-16 15:43:23 +00:00
|
|
|
|
2010-02-21 07:24:53 +00:00
|
|
|
/**
|
|
|
|
* @brief Determines if yielding is possible.
|
2013-07-20 07:24:12 +00:00
|
|
|
* @details This function returns @p true if there is a ready thread with
|
2010-02-21 07:24:53 +00:00
|
|
|
* equal or higher priority.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @sclass
|
2010-02-21 07:24:53 +00:00
|
|
|
*/
|
2013-07-19 11:48:43 +00:00
|
|
|
static inline bool chSchCanYieldS(void) {
|
|
|
|
|
|
|
|
chDbgCheckClassI();
|
|
|
|
|
2013-08-10 08:07:43 +00:00
|
|
|
return firstprio(&ch.rlist.r_queue) >= currp->p_prio;
|
2013-07-19 11:48:43 +00:00
|
|
|
}
|
2010-02-21 07:24:53 +00:00
|
|
|
|
|
|
|
/**
|
2010-03-16 15:43:23 +00:00
|
|
|
* @brief Yields the time slot.
|
|
|
|
* @details Yields the CPU control to the next thread in the ready list with
|
|
|
|
* equal or higher priority, if any.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @sclass
|
2010-02-21 07:24:53 +00:00
|
|
|
*/
|
2013-07-19 11:48:43 +00:00
|
|
|
static inline void chSchDoYieldS(void) {
|
|
|
|
|
|
|
|
chDbgCheckClassS();
|
|
|
|
|
|
|
|
if (chSchCanYieldS())
|
|
|
|
chSchDoRescheduleBehind();
|
2010-03-16 15:43:23 +00:00
|
|
|
}
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2011-08-13 07:06:02 +00:00
|
|
|
/**
|
2012-03-26 09:13:37 +00:00
|
|
|
* @brief Inline-able preemption code.
|
2011-08-13 07:06:02 +00:00
|
|
|
* @details This is the common preemption code, this function must be invoked
|
|
|
|
* exclusively from the port layer.
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2013-07-19 11:48:43 +00:00
|
|
|
static inline void chSchPreemption(void) {
|
2013-08-10 08:07:43 +00:00
|
|
|
tprio_t p1 = firstprio(&ch.rlist.r_queue);
|
2013-07-19 11:48:43 +00:00
|
|
|
tprio_t p2 = currp->p_prio;
|
|
|
|
|
2013-07-20 10:12:44 +00:00
|
|
|
#if CH_CFG_TIME_QUANTUM > 0
|
2013-07-19 11:48:43 +00:00
|
|
|
if (currp->p_preempt) {
|
|
|
|
if (p1 > p2)
|
|
|
|
chSchDoRescheduleAhead();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (p1 >= p2)
|
|
|
|
chSchDoRescheduleBehind();
|
|
|
|
}
|
2013-07-20 10:12:44 +00:00
|
|
|
#else /* CH_CFG_TIME_QUANTUM == 0 */
|
2013-07-19 11:48:43 +00:00
|
|
|
if (p1 >= p2)
|
|
|
|
chSchDoRescheduleAhead();
|
2013-07-20 10:12:44 +00:00
|
|
|
#endif /* CH_CFG_TIME_QUANTUM == 0 */
|
2013-07-19 11:48:43 +00:00
|
|
|
}
|
2011-08-13 07:06:02 +00:00
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#endif /* _CHSCHD_H_ */
|
2010-02-21 07:24:53 +00:00
|
|
|
|
|
|
|
/** @} */
|