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,
|
|
|
|
2011 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
|
|
|
|
2011-08-22 17:18:52 +00:00
|
|
|
/**
|
|
|
|
* @name Wakeup status codes
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
#define RDY_OK 0 /**< @brief Normal wakeup message. */
|
|
|
|
#define RDY_TIMEOUT -1 /**< @brief Wakeup caused by a timeout
|
|
|
|
condition. */
|
|
|
|
#define RDY_RESET -2 /**< @brief Wakeup caused by a reset
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the priority of the first thread on the given ready list.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2010-02-21 07:24:53 +00:00
|
|
|
*/
|
|
|
|
#define firstprio(rlp) ((rlp)->p_next->p_prio)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @extends ThreadsQueue
|
|
|
|
*
|
|
|
|
* @brief Ready list header.
|
|
|
|
*/
|
2010-03-16 15:43:23 +00:00
|
|
|
#if !defined(PORT_OPTIMIZED_READYLIST_STRUCT) || defined(__DOXYGEN__)
|
2010-02-21 07:24:53 +00:00
|
|
|
typedef struct {
|
|
|
|
ThreadsQueue r_queue; /**< @brief Threads queue. */
|
|
|
|
tprio_t r_prio; /**< @brief This field must be
|
|
|
|
initialized to zero. */
|
2010-03-19 15:45:25 +00:00
|
|
|
struct context r_ctx; /**< @brief Not used, present because
|
2010-02-21 07:24:53 +00:00
|
|
|
offsets. */
|
2011-08-07 09:00:12 +00:00
|
|
|
#if CH_USE_REGISTRY || defined(__DOXYGEN__)
|
2010-03-19 15:45:25 +00:00
|
|
|
Thread *r_newer; /**< @brief Newer registry element. */
|
|
|
|
Thread *r_older; /**< @brief Older registry element. */
|
2010-02-21 07:24:53 +00:00
|
|
|
#endif
|
|
|
|
/* End of the fields shared with the Thread structure.*/
|
2011-08-07 09:00:12 +00:00
|
|
|
#if (CH_TIME_QUANTUM > 0) || defined(__DOXYGEN__)
|
2010-02-21 07:24:53 +00:00
|
|
|
cnt_t r_preempt; /**< @brief Round robin counter. */
|
|
|
|
#endif
|
|
|
|
Thread *r_current; /**< @brief The currently running
|
|
|
|
thread. */
|
|
|
|
} ReadyList;
|
2010-03-16 15:43:23 +00:00
|
|
|
#endif /* !defined(PORT_OPTIMIZED_READYLIST_STRUCT) */
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2010-03-16 15:43:23 +00:00
|
|
|
#if !defined(PORT_OPTIMIZED_RLIST_EXT) && !defined(__DOXYGEN__)
|
2010-02-21 07:24:53 +00:00
|
|
|
extern ReadyList rlist;
|
2010-03-16 15:43:23 +00:00
|
|
|
#endif /* !defined(PORT_OPTIMIZED_RLIST_EXT) */
|
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.
|
|
|
|
*/
|
|
|
|
#if !defined(PORT_OPTIMIZED_CURRP) || defined(__DOXYGEN__)
|
2010-02-21 07:24:53 +00:00
|
|
|
#define currp rlist.r_current
|
2010-03-19 12:48:54 +00:00
|
|
|
#endif /* !defined(PORT_OPTIMIZED_CURRP) */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
#if !defined(PORT_OPTIMIZED_SETCURRP) || defined(__DOXYGEN__)
|
2010-04-03 15:46:41 +00:00
|
|
|
#define setcurrp(tp) (currp = (tp))
|
2010-03-19 12:48:54 +00:00
|
|
|
#endif /* !defined(PORT_OPTIMIZED_SETCURRP) */
|
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);
|
2010-03-18 16:01:11 +00:00
|
|
|
#if !defined(PORT_OPTIMIZED_READYI)
|
2010-02-21 07:24:53 +00:00
|
|
|
Thread *chSchReadyI(Thread *tp);
|
2010-03-18 16:01:11 +00:00
|
|
|
#endif
|
|
|
|
#if !defined(PORT_OPTIMIZED_GOSLEEPS)
|
2010-02-21 07:24:53 +00:00
|
|
|
void chSchGoSleepS(tstate_t newstate);
|
2010-03-18 16:01:11 +00:00
|
|
|
#endif
|
|
|
|
#if !defined(PORT_OPTIMIZED_GOSLEEPTIMEOUTS)
|
2010-02-21 07:24:53 +00:00
|
|
|
msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t time);
|
2010-03-18 16:01:11 +00:00
|
|
|
#endif
|
|
|
|
#if !defined(PORT_OPTIMIZED_WAKEUPS)
|
2010-02-21 07:24:53 +00:00
|
|
|
void chSchWakeupS(Thread *tp, msg_t msg);
|
2010-03-18 16:01:11 +00:00
|
|
|
#endif
|
|
|
|
#if !defined(PORT_OPTIMIZED_RESCHEDULES)
|
2010-02-21 07:24:53 +00:00
|
|
|
void chSchRescheduleS(void);
|
2010-03-18 16:01:11 +00:00
|
|
|
#endif
|
2011-08-13 07:06:02 +00:00
|
|
|
#if !defined(PORT_OPTIMIZED_ISPREEMPTIONREQUIRED)
|
|
|
|
bool_t chSchIsPreemptionRequired(void);
|
|
|
|
#endif
|
|
|
|
#if !defined(PORT_OPTIMIZED_DORESCHEDULE)
|
|
|
|
void chSchDoReschedule(void);
|
2010-03-18 16:01:11 +00:00
|
|
|
#endif
|
2010-02-21 07:24:53 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-08-23 10:09:08 +00:00
|
|
|
/**
|
|
|
|
* @name Macro Functions
|
|
|
|
* @{
|
|
|
|
*/
|
2010-03-16 15:43:23 +00:00
|
|
|
/**
|
|
|
|
* @brief Determines if the current thread must reschedule.
|
|
|
|
* @details This function returns @p TRUE if there is a ready thread with
|
|
|
|
* higher priority.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2010-03-16 15:43:23 +00:00
|
|
|
*/
|
2010-03-19 12:48:54 +00:00
|
|
|
#if !defined(PORT_OPTIMIZED_ISRESCHREQUIREDI) || defined(__DOXYGEN__)
|
2010-03-16 15:43:23 +00:00
|
|
|
#define chSchIsRescRequiredI() (firstprio(&rlist.r_queue) > currp->p_prio)
|
|
|
|
#endif /* !defined(PORT_OPTIMIZED_ISRESCHREQUIREDI) */
|
|
|
|
|
2010-02-21 07:24:53 +00:00
|
|
|
/**
|
|
|
|
* @brief Determines if yielding is possible.
|
|
|
|
* @details This function returns @p TRUE if there is a ready thread with
|
|
|
|
* equal or higher priority.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @sclass
|
2010-02-21 07:24:53 +00:00
|
|
|
*/
|
2010-03-19 12:48:54 +00:00
|
|
|
#if !defined(PORT_OPTIMIZED_CANYIELDS) || defined(__DOXYGEN__)
|
2010-02-21 07:24:53 +00:00
|
|
|
#define chSchCanYieldS() (firstprio(&rlist.r_queue) >= currp->p_prio)
|
2010-03-16 15:43:23 +00:00
|
|
|
#endif /* !defined(PORT_OPTIMIZED_CANYIELDS) */
|
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
|
|
|
*/
|
2010-03-16 15:43:23 +00:00
|
|
|
#if !defined(PORT_OPTIMIZED_DOYIELDS) || defined(__DOXYGEN__)
|
2010-04-08 12:39:05 +00:00
|
|
|
#define chSchDoYieldS() { \
|
2010-03-16 15:43:23 +00:00
|
|
|
if (chSchCanYieldS()) \
|
2011-08-13 07:06:02 +00:00
|
|
|
chSchDoReschedule(); \
|
2010-03-16 15:43:23 +00:00
|
|
|
}
|
|
|
|
#endif /* !defined(PORT_OPTIMIZED_DOYIELDS) */
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2011-08-13 07:06:02 +00:00
|
|
|
/**
|
|
|
|
* @brief Inlineable preemption code.
|
|
|
|
* @details This is the common preemption code, this function must be invoked
|
|
|
|
* exclusively from the port layer.
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
|
|
|
#if (CH_TIME_QUANTUM > 0) || defined(__DOXYGEN__)
|
|
|
|
#define chSchPreemption() { \
|
|
|
|
tprio_t p1 = firstprio(&rlist.r_queue); \
|
|
|
|
tprio_t p2 = currp->p_prio; \
|
|
|
|
if (rlist.r_preempt) { \
|
|
|
|
if (p1 > p2) \
|
|
|
|
chSchDoReschedule(); \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
if (p1 >= p2) \
|
|
|
|
chSchDoReschedule(); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
#else /* CH_TIME_QUANTUM == 0 */
|
|
|
|
#define chSchPreemption() { \
|
|
|
|
if (p1 >= p2) \
|
|
|
|
chSchDoReschedule(); \
|
|
|
|
}
|
|
|
|
#endif /* CH_TIME_QUANTUM == 0 */
|
2011-08-23 10:09:08 +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
|
|
|
|
|
|
|
/** @} */
|