|
ChibiOS/RT
2.5.1 |
00001 /* 00002 ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, 00003 2011,2012 Giovanni Di Sirio. 00004 00005 This file is part of ChibiOS/RT. 00006 00007 ChibiOS/RT is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 ChibiOS/RT is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 */ 00020 00021 /** 00022 * @file chmboxes.h 00023 * @brief Mailboxes macros and structures. 00024 * 00025 * @addtogroup mailboxes 00026 * @{ 00027 */ 00028 00029 #ifndef _CHMBOXES_H_ 00030 #define _CHMBOXES_H_ 00031 00032 #if CH_USE_MAILBOXES || defined(__DOXYGEN__) 00033 00034 /* 00035 * Module dependencies check. 00036 */ 00037 #if !CH_USE_SEMAPHORES 00038 #error "CH_USE_MAILBOXES requires CH_USE_SEMAPHORES" 00039 #endif 00040 00041 /** 00042 * @brief Structure representing a mailbox object. 00043 */ 00044 typedef struct { 00045 msg_t *mb_buffer; /**< @brief Pointer to the mailbox 00046 buffer. */ 00047 msg_t *mb_top; /**< @brief Pointer to the location 00048 after the buffer. */ 00049 msg_t *mb_wrptr; /**< @brief Write pointer. */ 00050 msg_t *mb_rdptr; /**< @brief Read pointer. */ 00051 Semaphore mb_fullsem; /**< @brief Full counter 00052 @p Semaphore. */ 00053 Semaphore mb_emptysem; /**< @brief Empty counter 00054 @p Semaphore. */ 00055 } Mailbox; 00056 00057 #ifdef __cplusplus 00058 extern "C" { 00059 #endif 00060 void chMBInit(Mailbox *mbp, msg_t *buf, cnt_t n); 00061 void chMBReset(Mailbox *mbp); 00062 msg_t chMBPost(Mailbox *mbp, msg_t msg, systime_t timeout); 00063 msg_t chMBPostS(Mailbox *mbp, msg_t msg, systime_t timeout); 00064 msg_t chMBPostI(Mailbox *mbp, msg_t msg); 00065 msg_t chMBPostAhead(Mailbox *mbp, msg_t msg, systime_t timeout); 00066 msg_t chMBPostAheadS(Mailbox *mbp, msg_t msg, systime_t timeout); 00067 msg_t chMBPostAheadI(Mailbox *mbp, msg_t msg); 00068 msg_t chMBFetch(Mailbox *mbp, msg_t *msgp, systime_t timeout); 00069 msg_t chMBFetchS(Mailbox *mbp, msg_t *msgp, systime_t timeout); 00070 msg_t chMBFetchI(Mailbox *mbp, msg_t *msgp); 00071 #ifdef __cplusplus 00072 } 00073 #endif 00074 00075 /** 00076 * @name Macro Functions 00077 * @{ 00078 */ 00079 /** 00080 * @brief Returns the mailbox buffer size. 00081 * 00082 * @param[in] mbp the pointer to an initialized Mailbox object 00083 * 00084 * @iclass 00085 */ 00086 #define chMBSizeI(mbp) \ 00087 ((mbp)->mb_top - (mbp)->mb_buffer) 00088 00089 /** 00090 * @brief Returns the number of free message slots into a mailbox. 00091 * @note Can be invoked in any system state but if invoked out of a locked 00092 * state then the returned value may change after reading. 00093 * @note The returned value can be less than zero when there are waiting 00094 * threads on the internal semaphore. 00095 * 00096 * @param[in] mbp the pointer to an initialized Mailbox object 00097 * @return The number of empty message slots. 00098 * 00099 * @iclass 00100 */ 00101 #define chMBGetFreeCountI(mbp) chSemGetCounterI(&(mbp)->mb_emptysem) 00102 00103 /** 00104 * @brief Returns the number of used message slots into a mailbox. 00105 * @note Can be invoked in any system state but if invoked out of a locked 00106 * state then the returned value may change after reading. 00107 * @note The returned value can be less than zero when there are waiting 00108 * threads on the internal semaphore. 00109 * 00110 * @param[in] mbp the pointer to an initialized Mailbox object 00111 * @return The number of queued messages. 00112 * 00113 * @iclass 00114 */ 00115 #define chMBGetUsedCountI(mbp) chSemGetCounterI(&(mbp)->mb_fullsem) 00116 00117 /** 00118 * @brief Returns the next message in the queue without removing it. 00119 * @pre A message must be waiting in the queue for this function to work 00120 * or it would return garbage. The correct way to use this macro is 00121 * to use @p chMBGetFullCountI() and then use this macro, all within 00122 * a lock state. 00123 * 00124 * @iclass 00125 */ 00126 #define chMBPeekI(mbp) (*(mbp)->mb_rdptr) 00127 /** @} */ 00128 00129 /** 00130 * @brief Data part of a static mailbox initializer. 00131 * @details This macro should be used when statically initializing a 00132 * mailbox that is part of a bigger structure. 00133 * 00134 * @param[in] name the name of the mailbox variable 00135 * @param[in] buffer pointer to the mailbox buffer area 00136 * @param[in] size size of the mailbox buffer area 00137 */ 00138 #define _MAILBOX_DATA(name, buffer, size) { \ 00139 (msg_t *)(buffer), \ 00140 (msg_t *)(buffer) + size, \ 00141 (msg_t *)(buffer), \ 00142 (msg_t *)(buffer), \ 00143 _SEMAPHORE_DATA(name.mb_fullsem, 0), \ 00144 _SEMAPHORE_DATA(name.mb_emptysem, size), \ 00145 } 00146 00147 /** 00148 * @brief Static mailbox initializer. 00149 * @details Statically initialized mailboxes require no explicit 00150 * initialization using @p chMBInit(). 00151 * 00152 * @param[in] name the name of the mailbox variable 00153 * @param[in] buffer pointer to the mailbox buffer area 00154 * @param[in] size size of the mailbox buffer area 00155 */ 00156 #define MAILBOX_DECL(name, buffer, size) \ 00157 Mailbox name = _MAILBOX_DATA(name, buffer, size) 00158 00159 #endif /* CH_USE_MAILBOXES */ 00160 00161 #endif /* _CHMBOXES_H_ */ 00162 00163 /** @} */