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 chsem.h 00029 * @brief Semaphores macros and structures. 00030 * 00031 * @addtogroup semaphores 00032 * @{ 00033 */ 00034 00035 #ifndef _CHSEM_H_ 00036 #define _CHSEM_H_ 00037 00038 #if CH_USE_SEMAPHORES 00039 00040 /** 00041 * @brief Semaphore structure. 00042 */ 00043 typedef struct Semaphore { 00044 ThreadsQueue s_queue; /**< @brief Queue of the threads sleeping 00045 on this semaphore. */ 00046 cnt_t s_cnt; /**< @brief The semaphore counter. */ 00047 } Semaphore; 00048 00049 #ifdef __cplusplus 00050 extern "C" { 00051 #endif 00052 void chSemInit(Semaphore *sp, cnt_t n); 00053 void chSemReset(Semaphore *sp, cnt_t n); 00054 void chSemResetI(Semaphore *sp, cnt_t n); 00055 msg_t chSemWait(Semaphore *sp); 00056 msg_t chSemWaitS(Semaphore *sp); 00057 msg_t chSemWaitTimeout(Semaphore *sp, systime_t time); 00058 msg_t chSemWaitTimeoutS(Semaphore *sp, systime_t time); 00059 void chSemSignal(Semaphore *sp); 00060 void chSemSignalI(Semaphore *sp); 00061 #if CH_USE_SEMSW 00062 msg_t chSemSignalWait(Semaphore *sps, Semaphore *spw); 00063 #endif 00064 #ifdef __cplusplus 00065 } 00066 #endif 00067 00068 /** 00069 * @brief Data part of a static semaphore initializer. 00070 * @details This macro should be used when statically initializing a semaphore 00071 * that is part of a bigger structure. 00072 * 00073 * @param[in] name the name of the semaphore variable 00074 * @param[in] n the counter initial value, this value must be 00075 * non-negative 00076 */ 00077 #define _SEMAPHORE_DATA(name, n) {_THREADSQUEUE_DATA(name.s_queue), n} 00078 00079 /** 00080 * @brief Static semaphore initializer. 00081 * @details Statically initialized semaphores require no explicit 00082 * initialization using @p chSemInit(). 00083 * 00084 * @param[in] name the name of the semaphore variable 00085 * @param[in] n the counter initial value, this value must be 00086 * non-negative 00087 */ 00088 #define SEMAPHORE_DECL(name, n) Semaphore name = _SEMAPHORE_DATA(name, n) 00089 00090 /** 00091 * @brief Decreases the semaphore counter. 00092 * @details This macro can be used when the counter is known to be positive. 00093 */ 00094 #define chSemFastWaitI(sp) ((sp)->s_cnt--) 00095 00096 /** 00097 * @brief Increases the semaphore counter. 00098 * @details This macro can be used when the counter is known to be not negative. 00099 */ 00100 #define chSemFastSignalI(sp) ((sp)->s_cnt++) 00101 00102 /** 00103 * @brief Returns the semaphore counter current value. 00104 */ 00105 #define chSemGetCounterI(sp) ((sp)->s_cnt) 00106 00107 #endif /* CH_USE_SEMAPHORES */ 00108 00109 #endif /* _CHSEM_H_ */ 00110 00111 /** @} */