]> git.sur5r.net Git - freertos/blob - Source/include/semphr.h
8ba34d0d170b50dd027b423a87fe8d0655803972
[freertos] / Source / include / semphr.h
1 /*\r
2         FreeRTOS.org V4.2.1 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 #include "queue.h"\r
37 \r
38 #ifndef SEMAPHORE_H\r
39 #define SEMAPHORE_H\r
40 \r
41 typedef xQueueHandle xSemaphoreHandle;\r
42 \r
43 #define semBINARY_SEMAPHORE_QUEUE_LENGTH        ( ( unsigned portCHAR ) 1 )\r
44 #define semSEMAPHORE_QUEUE_ITEM_LENGTH          ( ( unsigned portCHAR ) 0 )\r
45 #define semGIVE_BLOCK_TIME                                      ( ( portTickType ) 0 )\r
46 \r
47 \r
48 /**\r
49  * semphr. h\r
50  * <pre>vSemaphoreCreateBinary( xSemaphoreHandle xSemaphore )</pre>\r
51  *\r
52  * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.\r
53  * The queue length is 1 as this is a binary semaphore.  The data size is 0\r
54  * as we don't want to actually store any data - we just want to know if the\r
55  * queue is empty or full.\r
56  *\r
57  * @param xSemaphore Handle to the created semaphore.  Should be of type xSemaphoreHandle.\r
58  *\r
59  * Example usage:\r
60  <pre>\r
61  xSemaphoreHandle xSemaphore;\r
62 \r
63  void vATask( void * pvParameters )\r
64  {\r
65     // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().\r
66     // This is a macro so pass the variable in directly.\r
67     vSemaphoreCreateBinary( xSemaphore );\r
68 \r
69     if( xSemaphore != NULL )\r
70     {\r
71         // The semaphore was created successfully.\r
72         // The semaphore can now be used.  \r
73     }\r
74  }\r
75  </pre>\r
76  * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary\r
77  * \ingroup Semaphores\r
78  */\r
79 #define vSemaphoreCreateBinary( xSemaphore )            {                                                                                                                                                                                       \\r
80                                                                                                                 xSemaphore = xQueueCreate( ( unsigned portCHAR ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH );   \\r
81                                                                                                                 if( xSemaphore != NULL )                                                                                                                                \\r
82                                                                                                                 {                                                                                                                                                                               \\r
83                                                                                                                         xSemaphoreGive( xSemaphore );                                                                                                           \\r
84                                                                                                                 }                                                                                                                                                                               \\r
85                                                                                                         }\r
86 \r
87 /**\r
88  * semphr. h\r
89  * xSemaphoreTake( \r
90  *                   xSemaphoreHandle xSemaphore, \r
91  *                   portTickType xBlockTime \r
92  *               )</pre>\r
93  *\r
94  * <i>Macro</i> to obtain a semaphore.  The semaphore must of been created using \r
95  * vSemaphoreCreateBinary ().\r
96  *\r
97  * @param xSemaphore A handle to the semaphore being obtained.  This is the\r
98  * handle returned by vSemaphoreCreateBinary ();\r
99  *\r
100  * @param xBlockTime The time in ticks to wait for the semaphore to become\r
101  * available.  The macro portTICK_RATE_MS can be used to convert this to a\r
102  * real time.  A block time of zero can be used to poll the semaphore.\r
103  *\r
104  * @return pdTRUE if the semaphore was obtained.  pdFALSE if xBlockTime\r
105  * expired without the semaphore becoming available.\r
106  *\r
107  * Example usage:\r
108  <pre>\r
109  xSemaphoreHandle xSemaphore = NULL;\r
110 \r
111  // A task that creates a semaphore.\r
112  void vATask( void * pvParameters )\r
113  {\r
114     // Create the semaphore to guard a shared resource.\r
115     vSemaphoreCreateBinary( xSemaphore );\r
116  }\r
117 \r
118  // A task that uses the semaphore.\r
119  void vAnotherTask( void * pvParameters )\r
120  {\r
121     // ... Do other things.\r
122 \r
123     if( xSemaphore != NULL )\r
124     {\r
125         // See if we can obtain the semaphore.  If the semaphore is not available\r
126         // wait 10 ticks to see if it becomes free.     \r
127         if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE )\r
128         {\r
129             // We were able to obtain the semaphore and can now access the\r
130             // shared resource.\r
131 \r
132             // ...\r
133 \r
134             // We have finished accessing the shared resource.  Release the \r
135             // semaphore.\r
136             xSemaphoreGive( xSemaphore );\r
137         }\r
138         else\r
139         {\r
140             // We could not obtain the semaphore and can therefore not access\r
141             // the shared resource safely.\r
142         }\r
143     }\r
144  }\r
145  </pre>\r
146  * \defgroup xSemaphoreTake xSemaphoreTake\r
147  * \ingroup Semaphores\r
148  */\r
149 #define xSemaphoreTake( xSemaphore, xBlockTime )        xQueueReceive( ( xQueueHandle ) xSemaphore, NULL, xBlockTime )\r
150 \r
151 /**\r
152  * semphr. h\r
153  * <pre>xSemaphoreGive( xSemaphoreHandle xSemaphore )</pre>\r
154  *\r
155  * <i>Macro</i> to release a semaphore.  The semaphore must of been created using \r
156  * vSemaphoreCreateBinary (), and obtained using sSemaphoreTake ().\r
157  *\r
158  * This must not be used from an ISR.  See xSemaphoreGiveFromISR () for\r
159  * an alternative which can be used from an ISR.\r
160  *\r
161  * @param xSemaphore A handle to the semaphore being released.  This is the\r
162  * handle returned by vSemaphoreCreateBinary ();\r
163  *\r
164  * @return pdTRUE if the semaphore was released.  pdFALSE if an error occurred.\r
165  * Semaphores are implemented using queues.  An error can occur if there is\r
166  * no space on the queue to post a message - indicating that the \r
167  * semaphore was not first obtained correctly.\r
168  *\r
169  * Example usage:\r
170  <pre>\r
171  xSemaphoreHandle xSemaphore = NULL;\r
172 \r
173  void vATask( void * pvParameters )\r
174  {\r
175     // Create the semaphore to guard a shared resource.\r
176     vSemaphoreCreateBinary( xSemaphore );\r
177 \r
178     if( xSemaphore != NULL )\r
179     {\r
180         if( xSemaphoreGive( xSemaphore ) != pdTRUE )\r
181         {\r
182             // We would expect this call to fail because we cannot give\r
183             // a semaphore without first "taking" it!\r
184         }\r
185 \r
186         // Obtain the semaphore - don't block if the semaphore is not\r
187         // immediately available.\r
188         if( xSemaphoreTake( xSemaphore, ( portTickType ) 0 ) )\r
189         {\r
190             // We now have the semaphore and can access the shared resource.\r
191 \r
192             // ...\r
193 \r
194             // We have finished accessing the shared resource so can free the\r
195             // semaphore.\r
196             if( xSemaphoreGive( xSemaphore ) != pdTRUE )\r
197             {\r
198                 // We would not expect this call to fail because we must have\r
199                 // obtained the semaphore to get here.\r
200             }\r
201         }\r
202     }\r
203  }\r
204  </pre>\r
205  * \defgroup xSemaphoreGive xSemaphoreGive\r
206  * \ingroup Semaphores\r
207  */\r
208 #define xSemaphoreGive( xSemaphore )                            xQueueSend( ( xQueueHandle ) xSemaphore, NULL, semGIVE_BLOCK_TIME )\r
209 \r
210 /**\r
211  * semphr. h\r
212  * <pre>\r
213  xSemaphoreGiveFromISR( \r
214                           xSemaphoreHandle xSemaphore, \r
215                           portSHORT sTaskPreviouslyWoken \r
216                       )</pre>\r
217  *\r
218  * <i>Macro</i> to  release a semaphore.  The semaphore must of been created using \r
219  * vSemaphoreCreateBinary (), and obtained using xSemaphoreTake ().\r
220  *\r
221  * This macro can be used from an ISR.\r
222  *\r
223  * @param xSemaphore A handle to the semaphore being released.  This is the\r
224  * handle returned by vSemaphoreCreateBinary ();\r
225  *\r
226  * @param sTaskPreviouslyWoken This is included so an ISR can make multiple calls\r
227  * to xSemaphoreGiveFromISR () from a single interrupt.  The first call\r
228  * should always pass in pdFALSE.  Subsequent calls should pass in\r
229  * the value returned from the previous call.  See the file serial .c in the\r
230  * PC port for a good example of using xSemaphoreGiveFromISR ().\r
231  *\r
232  * @return pdTRUE if a task was woken by releasing the semaphore.  This is \r
233  * used by the ISR to determine if a context switch may be required following\r
234  * the ISR.\r
235  *\r
236  * Example usage:\r
237  <pre>\r
238  #define LONG_TIME 0xffff\r
239  #define TICKS_TO_WAIT  10\r
240  xSemaphoreHandle xSemaphore = NULL;\r
241 \r
242  // Repetitive task.\r
243  void vATask( void * pvParameters )\r
244  {\r
245     for( ;; )\r
246     {\r
247         // We want this task to run every 10 ticks or a timer.  The semaphore \r
248         // was created before this task was started\r
249 \r
250         // Block waiting for the semaphore to become available.\r
251         if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )\r
252         {\r
253             // It is time to execute.\r
254 \r
255             // ...\r
256 \r
257             // We have finished our task.  Return to the top of the loop where\r
258             // we will block on the semaphore until it is time to execute \r
259             // again.\r
260         }\r
261     }\r
262  }\r
263 \r
264  // Timer ISR\r
265  void vTimerISR( void * pvParameters )\r
266  {\r
267  static unsigned portCHAR ucLocalTickCount = 0;\r
268 \r
269     // A timer tick has occurred.\r
270 \r
271     // ... Do other time functions.\r
272 \r
273     // Is it time for vATask () to run?\r
274     ucLocalTickCount++;\r
275     if( ucLocalTickCount >= TICKS_TO_WAIT )\r
276     {\r
277         // Unblock the task by releasing the semaphore.\r
278         xSemaphoreGive( xSemaphore );\r
279 \r
280         // Reset the count so we release the semaphore again in 10 ticks time.\r
281         ucLocalTickCount = 0;\r
282     }\r
283  }\r
284  </pre>\r
285  * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR\r
286  * \ingroup Semaphores\r
287  */\r
288 #define xSemaphoreGiveFromISR( xSemaphore, xTaskPreviouslyWoken )                       xQueueSendFromISR( ( xQueueHandle ) xSemaphore, NULL, xTaskPreviouslyWoken )\r
289 \r
290 \r
291 #endif\r
292 \r