ChibiOS/RT Architecture - Reference Manual - Guides |
00001 /* 00002 ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio. 00003 00004 This file is part of ChibiOS/RT. 00005 00006 ChibiOS/RT is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 ChibiOS/RT is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 00019 --- 00020 00021 A special exception to the GPL can be applied should you wish to distribute 00022 a combined work that includes ChibiOS/RT, without being obliged to provide 00023 the source code for any proprietary components. See the file exception.txt 00024 for full details of how and when the exception can be applied. 00025 */ 00026 00027 /** 00028 * @file chlists.c 00029 * @brief Thread queues/lists code. 00030 * 00031 * @addtogroup internals 00032 * @details All the functions present in this module, while public, are not 00033 * an OS API and should not be directly used in the user applications 00034 * code. 00035 * @{ 00036 */ 00037 #include "ch.h" 00038 00039 #if !CH_OPTIMIZE_SPEED || defined(__DOXYGEN__) 00040 /** 00041 * @brief Inserts a thread into a priority ordered queue. 00042 * @note The insertion is done by scanning the list from the highest priority 00043 * toward the lowest. 00044 * @note This function is @b not an API. 00045 * 00046 * @param[in] tp the pointer to the thread to be inserted in the list 00047 * @param[in] tqp the pointer to the threads list header 00048 */ 00049 void prio_insert(Thread *tp, ThreadsQueue *tqp) { 00050 00051 /* cp iterates over the queue.*/ 00052 Thread *cp = (Thread *)tqp; 00053 do { 00054 /* Iterate to next thread in queue.*/ 00055 cp = cp->p_next; 00056 /* Not end of queue? and cp has equal or higher priority than tp?.*/ 00057 } while ((cp != (Thread *)tqp) && (cp->p_prio >= tp->p_prio)); 00058 /* Insertion on p_prev.*/ 00059 tp->p_next = cp; 00060 tp->p_prev = cp->p_prev; 00061 tp->p_prev->p_next = cp->p_prev = tp; 00062 } 00063 00064 /** 00065 * @brief Inserts a Thread into a queue. 00066 * @note This function is @b not an API. 00067 * 00068 * @param[in] tp the pointer to the thread to be inserted in the list 00069 * @param[in] tqp the pointer to the threads list header 00070 */ 00071 void queue_insert(Thread *tp, ThreadsQueue *tqp) { 00072 00073 tp->p_next = (Thread *)tqp; 00074 tp->p_prev = tqp->p_prev; 00075 tp->p_prev->p_next = tqp->p_prev = tp; 00076 } 00077 00078 /** 00079 * @brief Removes the first-out Thread from a queue and returns it. 00080 * @note If the queue is priority ordered then this function returns the 00081 * thread with the highest priority. 00082 * @note This function is @b not an API. 00083 * 00084 * @param[in] tqp the pointer to the threads list header 00085 * @return The removed thread pointer. 00086 */ 00087 Thread *fifo_remove(ThreadsQueue *tqp) { 00088 Thread *tp = tqp->p_next; 00089 00090 (tqp->p_next = tp->p_next)->p_prev = (Thread *)tqp; 00091 return tp; 00092 } 00093 00094 /** 00095 * @brief Removes the last-out Thread from a queue and returns it. 00096 * @note If the queue is priority ordered then this function returns the 00097 * thread with the lowest priority. 00098 * @note This function is @b not an API. 00099 * 00100 * @param[in] tqp the pointer to the threads list header 00101 * @return The removed thread pointer. 00102 */ 00103 Thread *lifo_remove(ThreadsQueue *tqp) { 00104 Thread *tp = tqp->p_prev; 00105 00106 (tqp->p_prev = tp->p_prev)->p_next = (Thread *)tqp; 00107 return tp; 00108 } 00109 00110 /** 00111 * @brief Removes a Thread from a queue and returns it. 00112 * @details The thread is removed from the queue regardless of its relative 00113 * position and regardless the used insertion method. 00114 * @note This function is @b not an API. 00115 * 00116 * @param[in] tp the pointer to the thread to be removed from the queue 00117 * @return The removed thread pointer. 00118 */ 00119 Thread *dequeue(Thread *tp) { 00120 00121 tp->p_prev->p_next = tp->p_next; 00122 tp->p_next->p_prev = tp->p_prev; 00123 return tp; 00124 } 00125 00126 /** 00127 * @brief Pushes a Thread on top of a stack list. 00128 * @note This function is @b not an API. 00129 * 00130 * @param[in] tp the pointer to the thread to be inserted in the list 00131 * @param[in] tlp the pointer to the threads list header 00132 */ 00133 void list_insert(Thread *tp, ThreadsList *tlp) { 00134 00135 tp->p_next = tlp->p_next; 00136 tlp->p_next = tp; 00137 } 00138 00139 /** 00140 * @brief Pops a Thread from the top of a stack list and returns it. 00141 * @note The list must be non-empty before calling this function. 00142 * @note This function is @b not an API. 00143 * 00144 * @param[in] tlp the pointer to the threads list header 00145 * @return The removed thread pointer. 00146 */ 00147 Thread *list_remove(ThreadsList *tlp) { 00148 00149 Thread *tp = tlp->p_next; 00150 tlp->p_next = tp->p_next; 00151 return tp; 00152 } 00153 #endif /* CH_OPTIMIZE_SPEED */ 00154 00155 /** @} */