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 chregistry.c 00029 * @brief Threads registry code. 00030 * 00031 * @addtogroup registry 00032 * @details Threads Registry related APIs and services. 00033 * 00034 * <h2>Operation mode</h2> 00035 * The Threads Registry is a double linked list that holds all the 00036 * active threads in the system.<br> 00037 * Operations defined for the registry: 00038 * - <b>First</b>, returns the first, in creation order, active thread 00039 * in the system. 00040 * - <b>Next</b>, returns the next, in creation order, active thread 00041 * in the system. 00042 * . 00043 * The registry is meant to be mainly a debug feature, as example, 00044 * using the registry a debugger can enumerate the active threads 00045 * in any given moment or the shell can print the active threads 00046 * and their state.<br> 00047 * Another possible use is for centralized threads memory management, 00048 * terminating threads can pulse an event source and an event handler 00049 * can perform a scansion of the registry in order to recover the 00050 * memory.<br> 00051 * In order to use the threads registry the @p CH_USE_REGISTRY option 00052 * must be enabled in @p chconf.h. 00053 * @{ 00054 */ 00055 #include "ch.h" 00056 00057 #if CH_USE_REGISTRY 00058 00059 /** 00060 * @brief Returns the first thread in the system. 00061 * @details Returns the most ancient thread in the system, usually this is 00062 * the main thread unless it terminated. 00063 * @note A reference is added to the returned thread in order to make sure 00064 * it status is not lost. 00065 * @note This function cannot return @p NULL because there is always at 00066 * least one thread in the system. 00067 * 00068 * @return A reference to the first thread. 00069 */ 00070 Thread *chRegFirstThread(void) { 00071 Thread *tp; 00072 00073 chSysLock(); 00074 tp = rlist.r_newer; 00075 #if CH_USE_DYNAMIC 00076 tp->p_refs++; 00077 #endif 00078 chSysUnlock(); 00079 return tp; 00080 } 00081 00082 /** 00083 * @brief Returns the thread next to the specified one. 00084 * @details The reference counter of the specified thread is decremented and 00085 * the reference counter of the returned thread is incremented. 00086 * 00087 * @param[in] tp pointer to the thread 00088 * @return A reference to the next thread. 00089 * @retval NULL if there is no next thread. 00090 */ 00091 Thread *chRegNextThread(Thread *tp) { 00092 Thread *ntp; 00093 00094 chSysLock(); 00095 ntp = tp->p_newer; 00096 if (ntp == (Thread *)&rlist) 00097 ntp = NULL; 00098 #if CH_USE_DYNAMIC 00099 else { 00100 chDbgAssert(ntp->p_refs < 255, "chRegNextThread(), #1", 00101 "too many references"); 00102 ntp->p_refs++; 00103 } 00104 #endif 00105 chSysUnlock(); 00106 #if CH_USE_DYNAMIC 00107 chThdRelease(tp); 00108 #endif 00109 return ntp; 00110 } 00111 00112 #endif /* CH_USE_REGISTRY */ 00113 00114 /** @} */