]> git.sur5r.net Git - freertos/blob - Source/include/semphr.h
Prepare for V4.7.1 release.
[freertos] / Source / include / semphr.h
1 /*\r
2         FreeRTOS.org V4.7.0 - 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 a version that has been certified for use\r
32         in safety critical systems, plus commercial licensing, development and\r
33         support options.\r
34         ***************************************************************************\r
35 */\r
36 \r
37 #ifndef SEMAPHORE_H\r
38 #define SEMAPHORE_H\r
39 \r
40 #include "queue.h"\r
41 \r
42 typedef xQueueHandle xSemaphoreHandle;\r
43 \r
44 #define semBINARY_SEMAPHORE_QUEUE_LENGTH        ( ( unsigned portCHAR ) 1 )\r
45 #define semSEMAPHORE_QUEUE_ITEM_LENGTH          ( ( unsigned portCHAR ) 0 )\r
46 #define semGIVE_BLOCK_TIME                                      ( ( portTickType ) 0 )\r
47 \r
48 \r
49 /**\r
50  * semphr. h\r
51  * <pre>vSemaphoreCreateBinary( xSemaphoreHandle xSemaphore )</pre>\r
52  *\r
53  * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.\r
54  * The queue length is 1 as this is a binary semaphore.  The data size is 0\r
55  * as we don't want to actually store any data - we just want to know if the\r
56  * queue is empty or full.\r
57  *\r
58  * This type of semaphore can be used for pure synchronisation between tasks or\r
59  * between an interrupt and a task.  The semaphore need not be given back once\r
60  * obtained, so one task/interrupt can continuously 'give' the semaphore while\r
61  * another continuously 'takes' the semaphore.  For this reason this type of\r
62  * semaphore does not use a priority inheritance mechanism.  For an alternative\r
63  * that does use priority inheritance see xSemaphoreCreateMutex().\r
64  *\r
65  * @param xSemaphore Handle to the created semaphore.  Should be of type xSemaphoreHandle.\r
66  *\r
67  * Example usage:\r
68  <pre>\r
69  xSemaphoreHandle xSemaphore;\r
70 \r
71  void vATask( void * pvParameters )\r
72  {\r
73     // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().\r
74     // This is a macro so pass the variable in directly.\r
75     vSemaphoreCreateBinary( xSemaphore );\r
76 \r
77     if( xSemaphore != NULL )\r
78     {\r
79         // The semaphore was created successfully.\r
80         // The semaphore can now be used.  \r
81     }\r
82  }\r
83  </pre>\r
84  * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary\r
85  * \ingroup Semaphores\r
86  */\r
87 #define vSemaphoreCreateBinary( xSemaphore )            {                                                                                                                                                                                               \\r
88                                                                                                                 xSemaphore = xQueueCreate( ( unsigned portBASE_TYPE ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH );      \\r
89                                                                                                                 if( xSemaphore != NULL )                                                                                                                                        \\r
90                                                                                                                 {                                                                                                                                                                                       \\r
91                                                                                                                         xSemaphoreGive( xSemaphore );                                                                                                                   \\r
92                                                                                                                 }                                                                                                                                                                                       \\r
93                                                                                                         }\r
94 \r
95 /**\r
96  * semphr. h\r
97  * xSemaphoreTake( \r
98  *                   xSemaphoreHandle xSemaphore, \r
99  *                   portTickType xBlockTime \r
100  *               )</pre>\r
101  *\r
102  * <i>Macro</i> to obtain a semaphore.  The semaphore must of been created using \r
103  * vSemaphoreCreateBinary ().\r
104  *\r
105  * @param xSemaphore A handle to the semaphore being obtained.  This is the\r
106  * handle returned by vSemaphoreCreateBinary ();\r
107  *\r
108  * @param xBlockTime The time in ticks to wait for the semaphore to become\r
109  * available.  The macro portTICK_RATE_MS can be used to convert this to a\r
110  * real time.  A block time of zero can be used to poll the semaphore.\r
111  *\r
112  * @return pdTRUE if the semaphore was obtained.  pdFALSE if xBlockTime\r
113  * expired without the semaphore becoming available.\r
114  *\r
115  * Example usage:\r
116  <pre>\r
117  xSemaphoreHandle xSemaphore = NULL;\r
118 \r
119  // A task that creates a semaphore.\r
120  void vATask( void * pvParameters )\r
121  {\r
122     // Create the semaphore to guard a shared resource.\r
123     vSemaphoreCreateBinary( xSemaphore );\r
124  }\r
125 \r
126  // A task that uses the semaphore.\r
127  void vAnotherTask( void * pvParameters )\r
128  {\r
129     // ... Do other things.\r
130 \r
131     if( xSemaphore != NULL )\r
132     {\r
133         // See if we can obtain the semaphore.  If the semaphore is not available\r
134         // wait 10 ticks to see if it becomes free.     \r
135         if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE )\r
136         {\r
137             // We were able to obtain the semaphore and can now access the\r
138             // shared resource.\r
139 \r
140             // ...\r
141 \r
142             // We have finished accessing the shared resource.  Release the \r
143             // semaphore.\r
144             xSemaphoreGive( xSemaphore );\r
145         }\r
146         else\r
147         {\r
148             // We could not obtain the semaphore and can therefore not access\r
149             // the shared resource safely.\r
150         }\r
151     }\r
152  }\r
153  </pre>\r
154  * \defgroup xSemaphoreTake xSemaphoreTake\r
155  * \ingroup Semaphores\r
156  */\r
157 #define xSemaphoreTake( xSemaphore, xBlockTime )                xQueueGenericReceive( ( xQueueHandle ) xSemaphore, NULL, xBlockTime, pdFALSE )\r
158 \r
159 /* \r
160  * xSemaphoreAltTake() is an alternative version of xSemaphoreTake().\r
161  *\r
162  * The source code that implements the alternative (Alt) API is much \r
163  * simpler      because it executes everything from within a critical section.  \r
164  * This is      the approach taken by many other RTOSes, but FreeRTOS.org has the \r
165  * preferred fully featured API too.  The fully featured API has more \r
166  * complex      code that takes longer to execute, but makes much less use of \r
167  * critical sections.  Therefore the alternative API sacrifices interrupt \r
168  * responsiveness to gain execution speed, whereas the fully featured API\r
169  * sacrifices execution speed to ensure better interrupt responsiveness.\r
170  */\r
171 #define xSemaphoreAltTake( xSemaphore, xBlockTime )             xQueueAltGenericReceive( ( xQueueHandle ) xSemaphore, NULL, xBlockTime, pdFALSE )\r
172 \r
173 /**\r
174  * semphr. h\r
175  * <pre>xSemaphoreGive( xSemaphoreHandle xSemaphore )</pre>\r
176  *\r
177  * <i>Macro</i> to release a semaphore.  The semaphore must of been created using \r
178  * vSemaphoreCreateBinary (), and obtained using sSemaphoreTake ().\r
179  *\r
180  * This must not be used from an ISR.  See xSemaphoreGiveFromISR () for\r
181  * an alternative which can be used from an ISR.\r
182  *\r
183  * @param xSemaphore A handle to the semaphore being released.  This is the\r
184  * handle returned by vSemaphoreCreateBinary ();\r
185  *\r
186  * @return pdTRUE if the semaphore was released.  pdFALSE if an error occurred.\r
187  * Semaphores are implemented using queues.  An error can occur if there is\r
188  * no space on the queue to post a message - indicating that the \r
189  * semaphore was not first obtained correctly.\r
190  *\r
191  * Example usage:\r
192  <pre>\r
193  xSemaphoreHandle xSemaphore = NULL;\r
194 \r
195  void vATask( void * pvParameters )\r
196  {\r
197     // Create the semaphore to guard a shared resource.\r
198     vSemaphoreCreateBinary( xSemaphore );\r
199 \r
200     if( xSemaphore != NULL )\r
201     {\r
202         if( xSemaphoreGive( xSemaphore ) != pdTRUE )\r
203         {\r
204             // We would expect this call to fail because we cannot give\r
205             // a semaphore without first "taking" it!\r
206         }\r
207 \r
208         // Obtain the semaphore - don't block if the semaphore is not\r
209         // immediately available.\r
210         if( xSemaphoreTake( xSemaphore, ( portTickType ) 0 ) )\r
211         {\r
212             // We now have the semaphore and can access the shared resource.\r
213 \r
214             // ...\r
215 \r
216             // We have finished accessing the shared resource so can free the\r
217             // semaphore.\r
218             if( xSemaphoreGive( xSemaphore ) != pdTRUE )\r
219             {\r
220                 // We would not expect this call to fail because we must have\r
221                 // obtained the semaphore to get here.\r
222             }\r
223         }\r
224     }\r
225  }\r
226  </pre>\r
227  * \defgroup xSemaphoreGive xSemaphoreGive\r
228  * \ingroup Semaphores\r
229  */\r
230 #define xSemaphoreGive( xSemaphore )            xQueueGenericSend( ( xQueueHandle ) xSemaphore, NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )\r
231 \r
232 /* \r
233  * xSemaphoreAltGive() is an alternative version of xSemaphoreGive().\r
234  *\r
235  * The source code that implements the alternative (Alt) API is much \r
236  * simpler      because it executes everything from within a critical section.  \r
237  * This is      the approach taken by many other RTOSes, but FreeRTOS.org has the \r
238  * preferred fully featured API too.  The fully featured API has more \r
239  * complex      code that takes longer to execute, but makes much less use of \r
240  * critical sections.  Therefore the alternative API sacrifices interrupt \r
241  * responsiveness to gain execution speed, whereas the fully featured API\r
242  * sacrifices execution speed to ensure better interrupt responsiveness.\r
243  */\r
244 #define xSemaphoreAltGive( xSemaphore )         xQueueAltGenericSend( ( xQueueHandle ) xSemaphore, NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )\r
245 \r
246 /**\r
247  * semphr. h\r
248  * <pre>\r
249  xSemaphoreGiveFromISR( \r
250                           xSemaphoreHandle xSemaphore, \r
251                           portSHORT sTaskPreviouslyWoken \r
252                       )</pre>\r
253  *\r
254  * <i>Macro</i> to  release a semaphore.  The semaphore must of been created using \r
255  * vSemaphoreCreateBinary (), and obtained using xSemaphoreTake ().\r
256  *\r
257  * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())\r
258  * must not be used with this macro.\r
259  *\r
260  * This macro can be used from an ISR.\r
261  *\r
262  * @param xSemaphore A handle to the semaphore being released.  This is the\r
263  * handle returned by vSemaphoreCreateBinary ();\r
264  *\r
265  * @param sTaskPreviouslyWoken This is included so an ISR can make multiple calls\r
266  * to xSemaphoreGiveFromISR () from a single interrupt.  The first call\r
267  * should always pass in pdFALSE.  Subsequent calls should pass in\r
268  * the value returned from the previous call.  See the file serial .c in the\r
269  * PC port for a good example of using xSemaphoreGiveFromISR ().\r
270  *\r
271  * @return pdTRUE if a task was woken by releasing the semaphore.  This is \r
272  * used by the ISR to determine if a context switch may be required following\r
273  * the ISR.\r
274  *\r
275  * Example usage:\r
276  <pre>\r
277  #define LONG_TIME 0xffff\r
278  #define TICKS_TO_WAIT  10\r
279  xSemaphoreHandle xSemaphore = NULL;\r
280 \r
281  // Repetitive task.\r
282  void vATask( void * pvParameters )\r
283  {\r
284     for( ;; )\r
285     {\r
286         // We want this task to run every 10 ticks or a timer.  The semaphore \r
287         // was created before this task was started\r
288 \r
289         // Block waiting for the semaphore to become available.\r
290         if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )\r
291         {\r
292             // It is time to execute.\r
293 \r
294             // ...\r
295 \r
296             // We have finished our task.  Return to the top of the loop where\r
297             // we will block on the semaphore until it is time to execute \r
298             // again.\r
299         }\r
300     }\r
301  }\r
302 \r
303  // Timer ISR\r
304  void vTimerISR( void * pvParameters )\r
305  {\r
306  static unsigned portCHAR ucLocalTickCount = 0;\r
307  static portBASE_TYPE xTaskWoken;\r
308 \r
309     // A timer tick has occurred.\r
310 \r
311     // ... Do other time functions.\r
312 \r
313     // Is it time for vATask () to run?\r
314         xTaskWoken = pdFALSE;\r
315     ucLocalTickCount++;\r
316     if( ucLocalTickCount >= TICKS_TO_WAIT )\r
317     {\r
318         // Unblock the task by releasing the semaphore.\r
319         xSemaphoreGiveFromISR( xSemaphore, xTaskWoken );\r
320 \r
321         // Reset the count so we release the semaphore again in 10 ticks time.\r
322         ucLocalTickCount = 0;\r
323     }\r
324  }\r
325  </pre>\r
326  * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR\r
327  * \ingroup Semaphores\r
328  */\r
329 #define xSemaphoreGiveFromISR( xSemaphore, xTaskPreviouslyWoken )                       xQueueGenericSendFromISR( ( xQueueHandle ) xSemaphore, NULL, xTaskPreviouslyWoken, queueSEND_TO_BACK )\r
330 \r
331 /**\r
332  * semphr. h\r
333  * <pre>xSemaphoreHandle xSemaphoreCreateMutex( void )</pre>\r
334  *\r
335  * <i>Macro</i> that implements a mutex semaphore by using the existing queue \r
336  * mechanism.\r
337  *\r
338  * This type of semaphore uses a priority inheritance mechanism so a task \r
339  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the \r
340  * semaphore it is no longer required.  \r
341  *\r
342  * Mutex type semaphores cannot be used from within interrupt service routines.  \r
343  *\r
344  * See xSemaphoreCreateBinary() for an alternative implementation that can be \r
345  * used for pure synchronisation (where one task or interrupt always 'gives' the \r
346  * semaphore and another always 'takes' the semaphore) and from within interrupt \r
347  * service routines.\r
348  *\r
349  * @return xSemaphore Handle to the created mutex semaphore.  Should be of type \r
350  *              xSemaphoreHandle.\r
351  *\r
352  * Example usage:\r
353  <pre>\r
354  xSemaphoreHandle xSemaphore;\r
355 \r
356  void vATask( void * pvParameters )\r
357  {\r
358     // Semaphore cannot be used before a call to xSemaphoreCreateMutex().\r
359     // This is a macro so pass the variable in directly.\r
360     xSemaphore = xSemaphoreCreateMutex();\r
361 \r
362     if( xSemaphore != NULL )\r
363     {\r
364         // The semaphore was created successfully.\r
365         // The semaphore can now be used.  \r
366     }\r
367  }\r
368  </pre>\r
369  * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex\r
370  * \ingroup Semaphores\r
371  */\r
372 #define xSemaphoreCreateMutex() xQueueCreateMutex()\r
373 #define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex()\r
374 \r
375 /**\r
376  * semphr. h\r
377  * <pre>xSemaphoreHandle xSemaphoreCreateCounting( unsigned portBASE_TYPE uxMaxCount, unsigned portBASE_TYPE uxInitialCount )</pre>\r
378  *\r
379  * <i>Macro</i> that creates a counting semaphore by using the existing \r
380  * queue mechanism.  \r
381  *\r
382  * Counting semaphores are typically used for two things:\r
383  *\r
384  * 1) Counting events.  \r
385  *\r
386  *    In this usage scenario an event handler will 'give' a semaphore each time\r
387  *    an event occurs (incrementing the semaphore count value), and a handler \r
388  *    task will 'take' a semaphore each time it processes an event \r
389  *    (decrementing the semaphore count value).  The count value is therefore \r
390  *    the difference between the number of events that have occurred and the \r
391  *    number that have been processed.  In this case it is desirable for the \r
392  *    initial count value to be zero.\r
393  *\r
394  * 2) Resource management.\r
395  *\r
396  *    In this usage scenario the count value indicates the number of resources\r
397  *    available.  To obtain control of a resource a task must first obtain a \r
398  *    semaphore - decrementing the semaphore count value.  When the count value\r
399  *    reaches zero there are no free resources.  When a task finishes with the\r
400  *    resource it 'gives' the semaphore back - incrementing the semaphore count\r
401  *    value.  In this case it is desirable for the initial count value to be\r
402  *    equal to the maximum count value, indicating that all resources are free.\r
403  *\r
404  * @param uxMaxCount The maximum count value that can be reached.  When the \r
405  *        semaphore reaches this value it can no longer be 'given'.\r
406  *\r
407  * @param uxInitialCount The count value assigned to the semaphore when it is\r
408  *        created.\r
409  *\r
410  * @return Handle to the created semaphore.  Null if the semaphore could not be\r
411  *         created.\r
412  * \r
413  * Example usage:\r
414  <pre>\r
415  xSemaphoreHandle xSemaphore;\r
416 \r
417  void vATask( void * pvParameters )\r
418  {\r
419  xSemaphoreHandle xSemaphore = NULL;\r
420 \r
421     // Semaphore cannot be used before a call to xSemaphoreCreateCounting().\r
422     // The max value to which the semaphore can count should be 10, and the\r
423     // initial value assigned to the count should be 0.\r
424     xSemaphore = xSemaphoreCreateCounting( 10, 0 );\r
425 \r
426     if( xSemaphore != NULL )\r
427     {\r
428         // The semaphore was created successfully.\r
429         // The semaphore can now be used.  \r
430     }\r
431  }\r
432  </pre>\r
433  * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting\r
434  * \ingroup Semaphores\r
435  */\r
436 #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( uxMaxCount, uxInitialCount )\r
437 \r
438 #define xSemaphoreTakeRecursive( xMutex, xBlockTime )   xQueueTakeMutexRecursive( xMutex, xBlockTime )\r
439 #define xSemaphoreGiveRecursive( xMutex )                               xQueueGiveMutexRecursive( xMutex )\r
440 \r
441 #endif /* SEMAPHORE_H */\r
442 \r
443 \r