]> begriffs open source - freertos/blob - include/semphr.h
Moving the function prototypes to headers (#128)
[freertos] / include / semphr.h
1 /*\r
2  * FreeRTOS Kernel V10.3.1\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  */\r
26 \r
27 #ifndef SEMAPHORE_H\r
28 #define SEMAPHORE_H\r
29 \r
30 #ifndef INC_FREERTOS_H\r
31     #error "include FreeRTOS.h" must appear in source files before "include semphr.h"\r
32 #endif\r
33 \r
34 #include "queue.h"\r
35 \r
36 typedef QueueHandle_t SemaphoreHandle_t;\r
37 \r
38 #define semBINARY_SEMAPHORE_QUEUE_LENGTH    ( ( uint8_t ) 1U )\r
39 #define semSEMAPHORE_QUEUE_ITEM_LENGTH      ( ( uint8_t ) 0U )\r
40 #define semGIVE_BLOCK_TIME                  ( ( TickType_t ) 0U )\r
41 \r
42 \r
43 /**\r
44  * semphr. h\r
45  * <pre>vSemaphoreCreateBinary( SemaphoreHandle_t xSemaphore )</pre>\r
46  *\r
47  * In many usage scenarios it is faster and more memory efficient to use a\r
48  * direct to task notification in place of a binary semaphore!\r
49  * http://www.freertos.org/RTOS-task-notifications.html\r
50  *\r
51  * This old vSemaphoreCreateBinary() macro is now deprecated in favour of the\r
52  * xSemaphoreCreateBinary() function.  Note that binary semaphores created using\r
53  * the vSemaphoreCreateBinary() macro are created in a state such that the\r
54  * first call to 'take' the semaphore would pass, whereas binary semaphores\r
55  * created using xSemaphoreCreateBinary() are created in a state such that the\r
56  * the semaphore must first be 'given' before it can be 'taken'.\r
57  *\r
58  * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.\r
59  * The queue length is 1 as this is a binary semaphore.  The data size is 0\r
60  * as we don't want to actually store any data - we just want to know if the\r
61  * queue is empty or full.\r
62  *\r
63  * This type of semaphore can be used for pure synchronisation between tasks or\r
64  * between an interrupt and a task.  The semaphore need not be given back once\r
65  * obtained, so one task/interrupt can continuously 'give' the semaphore while\r
66  * another continuously 'takes' the semaphore.  For this reason this type of\r
67  * semaphore does not use a priority inheritance mechanism.  For an alternative\r
68  * that does use priority inheritance see xSemaphoreCreateMutex().\r
69  *\r
70  * @param xSemaphore Handle to the created semaphore.  Should be of type SemaphoreHandle_t.\r
71  *\r
72  * Example usage:\r
73  * <pre>\r
74  * SemaphoreHandle_t xSemaphore = NULL;\r
75  *\r
76  * void vATask( void * pvParameters )\r
77  * {\r
78  *  // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().\r
79  *  // This is a macro so pass the variable in directly.\r
80  *  vSemaphoreCreateBinary( xSemaphore );\r
81  *\r
82  *  if( xSemaphore != NULL )\r
83  *  {\r
84  *      // The semaphore was created successfully.\r
85  *      // The semaphore can now be used.\r
86  *  }\r
87  * }\r
88  * </pre>\r
89  * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary\r
90  * \ingroup Semaphores\r
91  */\r
92 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
93     #define vSemaphoreCreateBinary( xSemaphore )                                                                                     \\r
94     {                                                                                                                                \\r
95         ( xSemaphore ) = xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE ); \\r
96         if( ( xSemaphore ) != NULL )                                                                                                 \\r
97         {                                                                                                                            \\r
98             ( void ) xSemaphoreGive( ( xSemaphore ) );                                                                               \\r
99         }                                                                                                                            \\r
100     }\r
101 #endif\r
102 \r
103 /**\r
104  * semphr. h\r
105  * <pre>SemaphoreHandle_t xSemaphoreCreateBinary( void )</pre>\r
106  *\r
107  * Creates a new binary semaphore instance, and returns a handle by which the\r
108  * new semaphore can be referenced.\r
109  *\r
110  * In many usage scenarios it is faster and more memory efficient to use a\r
111  * direct to task notification in place of a binary semaphore!\r
112  * http://www.freertos.org/RTOS-task-notifications.html\r
113  *\r
114  * Internally, within the FreeRTOS implementation, binary semaphores use a block\r
115  * of memory, in which the semaphore structure is stored.  If a binary semaphore\r
116  * is created using xSemaphoreCreateBinary() then the required memory is\r
117  * automatically dynamically allocated inside the xSemaphoreCreateBinary()\r
118  * function.  (see http://www.freertos.org/a00111.html).  If a binary semaphore\r
119  * is created using xSemaphoreCreateBinaryStatic() then the application writer\r
120  * must provide the memory.  xSemaphoreCreateBinaryStatic() therefore allows a\r
121  * binary semaphore to be created without using any dynamic memory allocation.\r
122  *\r
123  * The old vSemaphoreCreateBinary() macro is now deprecated in favour of this\r
124  * xSemaphoreCreateBinary() function.  Note that binary semaphores created using\r
125  * the vSemaphoreCreateBinary() macro are created in a state such that the\r
126  * first call to 'take' the semaphore would pass, whereas binary semaphores\r
127  * created using xSemaphoreCreateBinary() are created in a state such that the\r
128  * the semaphore must first be 'given' before it can be 'taken'.\r
129  *\r
130  * This type of semaphore can be used for pure synchronisation between tasks or\r
131  * between an interrupt and a task.  The semaphore need not be given back once\r
132  * obtained, so one task/interrupt can continuously 'give' the semaphore while\r
133  * another continuously 'takes' the semaphore.  For this reason this type of\r
134  * semaphore does not use a priority inheritance mechanism.  For an alternative\r
135  * that does use priority inheritance see xSemaphoreCreateMutex().\r
136  *\r
137  * @return Handle to the created semaphore, or NULL if the memory required to\r
138  * hold the semaphore's data structures could not be allocated.\r
139  *\r
140  * Example usage:\r
141  * <pre>\r
142  * SemaphoreHandle_t xSemaphore = NULL;\r
143  *\r
144  * void vATask( void * pvParameters )\r
145  * {\r
146  *  // Semaphore cannot be used before a call to xSemaphoreCreateBinary().\r
147  *  // This is a macro so pass the variable in directly.\r
148  *  xSemaphore = xSemaphoreCreateBinary();\r
149  *\r
150  *  if( xSemaphore != NULL )\r
151  *  {\r
152  *      // The semaphore was created successfully.\r
153  *      // The semaphore can now be used.\r
154  *  }\r
155  * }\r
156  * </pre>\r
157  * \defgroup xSemaphoreCreateBinary xSemaphoreCreateBinary\r
158  * \ingroup Semaphores\r
159  */\r
160 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
161     #define xSemaphoreCreateBinary()    xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE )\r
162 #endif\r
163 \r
164 /**\r
165  * semphr. h\r
166  * <pre>SemaphoreHandle_t xSemaphoreCreateBinaryStatic( StaticSemaphore_t *pxSemaphoreBuffer )</pre>\r
167  *\r
168  * Creates a new binary semaphore instance, and returns a handle by which the\r
169  * new semaphore can be referenced.\r
170  *\r
171  * NOTE: In many usage scenarios it is faster and more memory efficient to use a\r
172  * direct to task notification in place of a binary semaphore!\r
173  * http://www.freertos.org/RTOS-task-notifications.html\r
174  *\r
175  * Internally, within the FreeRTOS implementation, binary semaphores use a block\r
176  * of memory, in which the semaphore structure is stored.  If a binary semaphore\r
177  * is created using xSemaphoreCreateBinary() then the required memory is\r
178  * automatically dynamically allocated inside the xSemaphoreCreateBinary()\r
179  * function.  (see http://www.freertos.org/a00111.html).  If a binary semaphore\r
180  * is created using xSemaphoreCreateBinaryStatic() then the application writer\r
181  * must provide the memory.  xSemaphoreCreateBinaryStatic() therefore allows a\r
182  * binary semaphore to be created without using any dynamic memory allocation.\r
183  *\r
184  * This type of semaphore can be used for pure synchronisation between tasks or\r
185  * between an interrupt and a task.  The semaphore need not be given back once\r
186  * obtained, so one task/interrupt can continuously 'give' the semaphore while\r
187  * another continuously 'takes' the semaphore.  For this reason this type of\r
188  * semaphore does not use a priority inheritance mechanism.  For an alternative\r
189  * that does use priority inheritance see xSemaphoreCreateMutex().\r
190  *\r
191  * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,\r
192  * which will then be used to hold the semaphore's data structure, removing the\r
193  * need for the memory to be allocated dynamically.\r
194  *\r
195  * @return If the semaphore is created then a handle to the created semaphore is\r
196  * returned.  If pxSemaphoreBuffer is NULL then NULL is returned.\r
197  *\r
198  * Example usage:\r
199  * <pre>\r
200  * SemaphoreHandle_t xSemaphore = NULL;\r
201  * StaticSemaphore_t xSemaphoreBuffer;\r
202  *\r
203  * void vATask( void * pvParameters )\r
204  * {\r
205  *  // Semaphore cannot be used before a call to xSemaphoreCreateBinary().\r
206  *  // The semaphore's data structures will be placed in the xSemaphoreBuffer\r
207  *  // variable, the address of which is passed into the function.  The\r
208  *  // function's parameter is not NULL, so the function will not attempt any\r
209  *  // dynamic memory allocation, and therefore the function will not return\r
210  *  // return NULL.\r
211  *  xSemaphore = xSemaphoreCreateBinary( &xSemaphoreBuffer );\r
212  *\r
213  *  // Rest of task code goes here.\r
214  * }\r
215  * </pre>\r
216  * \defgroup xSemaphoreCreateBinaryStatic xSemaphoreCreateBinaryStatic\r
217  * \ingroup Semaphores\r
218  */\r
219 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )\r
220     #define xSemaphoreCreateBinaryStatic( pxStaticSemaphore )    xQueueGenericCreateStatic( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticSemaphore, queueQUEUE_TYPE_BINARY_SEMAPHORE )\r
221 #endif /* configSUPPORT_STATIC_ALLOCATION */\r
222 \r
223 /**\r
224  * semphr. h\r
225  * <pre>xSemaphoreTake(\r
226  *                   SemaphoreHandle_t xSemaphore,\r
227  *                   TickType_t xBlockTime\r
228  *               )</pre>\r
229  *\r
230  * <i>Macro</i> to obtain a semaphore.  The semaphore must have previously been\r
231  * created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or\r
232  * xSemaphoreCreateCounting().\r
233  *\r
234  * @param xSemaphore A handle to the semaphore being taken - obtained when\r
235  * the semaphore was created.\r
236  *\r
237  * @param xBlockTime The time in ticks to wait for the semaphore to become\r
238  * available.  The macro portTICK_PERIOD_MS can be used to convert this to a\r
239  * real time.  A block time of zero can be used to poll the semaphore.  A block\r
240  * time of portMAX_DELAY can be used to block indefinitely (provided\r
241  * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h).\r
242  *\r
243  * @return pdTRUE if the semaphore was obtained.  pdFALSE\r
244  * if xBlockTime expired without the semaphore becoming available.\r
245  *\r
246  * Example usage:\r
247  * <pre>\r
248  * SemaphoreHandle_t xSemaphore = NULL;\r
249  *\r
250  * // A task that creates a semaphore.\r
251  * void vATask( void * pvParameters )\r
252  * {\r
253  *  // Create the semaphore to guard a shared resource.\r
254  *  xSemaphore = xSemaphoreCreateBinary();\r
255  * }\r
256  *\r
257  * // A task that uses the semaphore.\r
258  * void vAnotherTask( void * pvParameters )\r
259  * {\r
260  *  // ... Do other things.\r
261  *\r
262  *  if( xSemaphore != NULL )\r
263  *  {\r
264  *      // See if we can obtain the semaphore.  If the semaphore is not available\r
265  *      // wait 10 ticks to see if it becomes free.\r
266  *      if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )\r
267  *      {\r
268  *          // We were able to obtain the semaphore and can now access the\r
269  *          // shared resource.\r
270  *\r
271  *          // ...\r
272  *\r
273  *          // We have finished accessing the shared resource.  Release the\r
274  *          // semaphore.\r
275  *          xSemaphoreGive( xSemaphore );\r
276  *      }\r
277  *      else\r
278  *      {\r
279  *          // We could not obtain the semaphore and can therefore not access\r
280  *          // the shared resource safely.\r
281  *      }\r
282  *  }\r
283  * }\r
284  * </pre>\r
285  * \defgroup xSemaphoreTake xSemaphoreTake\r
286  * \ingroup Semaphores\r
287  */\r
288 #define xSemaphoreTake( xSemaphore, xBlockTime )    xQueueSemaphoreTake( ( xSemaphore ), ( xBlockTime ) )\r
289 \r
290 /**\r
291  * semphr. h\r
292  * xSemaphoreTakeRecursive(\r
293  *                          SemaphoreHandle_t xMutex,\r
294  *                          TickType_t xBlockTime\r
295  *                        )\r
296  *\r
297  * <i>Macro</i> to recursively obtain, or 'take', a mutex type semaphore.\r
298  * The mutex must have previously been created using a call to\r
299  * xSemaphoreCreateRecursiveMutex();\r
300  *\r
301  * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this\r
302  * macro to be available.\r
303  *\r
304  * This macro must not be used on mutexes created using xSemaphoreCreateMutex().\r
305  *\r
306  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex\r
307  * doesn't become available again until the owner has called\r
308  * xSemaphoreGiveRecursive() for each successful 'take' request.  For example,\r
309  * if a task successfully 'takes' the same mutex 5 times then the mutex will\r
310  * not be available to any other task until it has also  'given' the mutex back\r
311  * exactly five times.\r
312  *\r
313  * @param xMutex A handle to the mutex being obtained.  This is the\r
314  * handle returned by xSemaphoreCreateRecursiveMutex();\r
315  *\r
316  * @param xBlockTime The time in ticks to wait for the semaphore to become\r
317  * available.  The macro portTICK_PERIOD_MS can be used to convert this to a\r
318  * real time.  A block time of zero can be used to poll the semaphore.  If\r
319  * the task already owns the semaphore then xSemaphoreTakeRecursive() will\r
320  * return immediately no matter what the value of xBlockTime.\r
321  *\r
322  * @return pdTRUE if the semaphore was obtained.  pdFALSE if xBlockTime\r
323  * expired without the semaphore becoming available.\r
324  *\r
325  * Example usage:\r
326  * <pre>\r
327  * SemaphoreHandle_t xMutex = NULL;\r
328  *\r
329  * // A task that creates a mutex.\r
330  * void vATask( void * pvParameters )\r
331  * {\r
332  *  // Create the mutex to guard a shared resource.\r
333  *  xMutex = xSemaphoreCreateRecursiveMutex();\r
334  * }\r
335  *\r
336  * // A task that uses the mutex.\r
337  * void vAnotherTask( void * pvParameters )\r
338  * {\r
339  *  // ... Do other things.\r
340  *\r
341  *  if( xMutex != NULL )\r
342  *  {\r
343  *      // See if we can obtain the mutex.  If the mutex is not available\r
344  *      // wait 10 ticks to see if it becomes free.\r
345  *      if( xSemaphoreTakeRecursive( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )\r
346  *      {\r
347  *          // We were able to obtain the mutex and can now access the\r
348  *          // shared resource.\r
349  *\r
350  *          // ...\r
351  *          // For some reason due to the nature of the code further calls to\r
352  *          // xSemaphoreTakeRecursive() are made on the same mutex.  In real\r
353  *          // code these would not be just sequential calls as this would make\r
354  *          // no sense.  Instead the calls are likely to be buried inside\r
355  *          // a more complex call structure.\r
356  *          xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );\r
357  *          xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );\r
358  *\r
359  *          // The mutex has now been 'taken' three times, so will not be\r
360  *          // available to another task until it has also been given back\r
361  *          // three times.  Again it is unlikely that real code would have\r
362  *          // these calls sequentially, but instead buried in a more complex\r
363  *          // call structure.  This is just for illustrative purposes.\r
364  *          xSemaphoreGiveRecursive( xMutex );\r
365  *          xSemaphoreGiveRecursive( xMutex );\r
366  *          xSemaphoreGiveRecursive( xMutex );\r
367  *\r
368  *          // Now the mutex can be taken by other tasks.\r
369  *      }\r
370  *      else\r
371  *      {\r
372  *          // We could not obtain the mutex and can therefore not access\r
373  *          // the shared resource safely.\r
374  *      }\r
375  *  }\r
376  * }\r
377  * </pre>\r
378  * \defgroup xSemaphoreTakeRecursive xSemaphoreTakeRecursive\r
379  * \ingroup Semaphores\r
380  */\r
381 #if ( configUSE_RECURSIVE_MUTEXES == 1 )\r
382     #define xSemaphoreTakeRecursive( xMutex, xBlockTime )    xQueueTakeMutexRecursive( ( xMutex ), ( xBlockTime ) )\r
383 #endif\r
384 \r
385 /**\r
386  * semphr. h\r
387  * <pre>xSemaphoreGive( SemaphoreHandle_t xSemaphore )</pre>\r
388  *\r
389  * <i>Macro</i> to release a semaphore.  The semaphore must have previously been\r
390  * created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or\r
391  * xSemaphoreCreateCounting(). and obtained using sSemaphoreTake().\r
392  *\r
393  * This macro must not be used from an ISR.  See xSemaphoreGiveFromISR () for\r
394  * an alternative which can be used from an ISR.\r
395  *\r
396  * This macro must also not be used on semaphores created using\r
397  * xSemaphoreCreateRecursiveMutex().\r
398  *\r
399  * @param xSemaphore A handle to the semaphore being released.  This is the\r
400  * handle returned when the semaphore was created.\r
401  *\r
402  * @return pdTRUE if the semaphore was released.  pdFALSE if an error occurred.\r
403  * Semaphores are implemented using queues.  An error can occur if there is\r
404  * no space on the queue to post a message - indicating that the\r
405  * semaphore was not first obtained correctly.\r
406  *\r
407  * Example usage:\r
408  * <pre>\r
409  * SemaphoreHandle_t xSemaphore = NULL;\r
410  *\r
411  * void vATask( void * pvParameters )\r
412  * {\r
413  *  // Create the semaphore to guard a shared resource.\r
414  *  xSemaphore = vSemaphoreCreateBinary();\r
415  *\r
416  *  if( xSemaphore != NULL )\r
417  *  {\r
418  *      if( xSemaphoreGive( xSemaphore ) != pdTRUE )\r
419  *      {\r
420  *          // We would expect this call to fail because we cannot give\r
421  *          // a semaphore without first "taking" it!\r
422  *      }\r
423  *\r
424  *      // Obtain the semaphore - don't block if the semaphore is not\r
425  *      // immediately available.\r
426  *      if( xSemaphoreTake( xSemaphore, ( TickType_t ) 0 ) )\r
427  *      {\r
428  *          // We now have the semaphore and can access the shared resource.\r
429  *\r
430  *          // ...\r
431  *\r
432  *          // We have finished accessing the shared resource so can free the\r
433  *          // semaphore.\r
434  *          if( xSemaphoreGive( xSemaphore ) != pdTRUE )\r
435  *          {\r
436  *              // We would not expect this call to fail because we must have\r
437  *              // obtained the semaphore to get here.\r
438  *          }\r
439  *      }\r
440  *  }\r
441  * }\r
442  * </pre>\r
443  * \defgroup xSemaphoreGive xSemaphoreGive\r
444  * \ingroup Semaphores\r
445  */\r
446 #define xSemaphoreGive( xSemaphore )    xQueueGenericSend( ( QueueHandle_t ) ( xSemaphore ), NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )\r
447 \r
448 /**\r
449  * semphr. h\r
450  * <pre>xSemaphoreGiveRecursive( SemaphoreHandle_t xMutex )</pre>\r
451  *\r
452  * <i>Macro</i> to recursively release, or 'give', a mutex type semaphore.\r
453  * The mutex must have previously been created using a call to\r
454  * xSemaphoreCreateRecursiveMutex();\r
455  *\r
456  * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this\r
457  * macro to be available.\r
458  *\r
459  * This macro must not be used on mutexes created using xSemaphoreCreateMutex().\r
460  *\r
461  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex\r
462  * doesn't become available again until the owner has called\r
463  * xSemaphoreGiveRecursive() for each successful 'take' request.  For example,\r
464  * if a task successfully 'takes' the same mutex 5 times then the mutex will\r
465  * not be available to any other task until it has also  'given' the mutex back\r
466  * exactly five times.\r
467  *\r
468  * @param xMutex A handle to the mutex being released, or 'given'.  This is the\r
469  * handle returned by xSemaphoreCreateMutex();\r
470  *\r
471  * @return pdTRUE if the semaphore was given.\r
472  *\r
473  * Example usage:\r
474  * <pre>\r
475  * SemaphoreHandle_t xMutex = NULL;\r
476  *\r
477  * // A task that creates a mutex.\r
478  * void vATask( void * pvParameters )\r
479  * {\r
480  *  // Create the mutex to guard a shared resource.\r
481  *  xMutex = xSemaphoreCreateRecursiveMutex();\r
482  * }\r
483  *\r
484  * // A task that uses the mutex.\r
485  * void vAnotherTask( void * pvParameters )\r
486  * {\r
487  *  // ... Do other things.\r
488  *\r
489  *  if( xMutex != NULL )\r
490  *  {\r
491  *      // See if we can obtain the mutex.  If the mutex is not available\r
492  *      // wait 10 ticks to see if it becomes free.\r
493  *      if( xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 ) == pdTRUE )\r
494  *      {\r
495  *          // We were able to obtain the mutex and can now access the\r
496  *          // shared resource.\r
497  *\r
498  *          // ...\r
499  *          // For some reason due to the nature of the code further calls to\r
500  *          // xSemaphoreTakeRecursive() are made on the same mutex.  In real\r
501  *          // code these would not be just sequential calls as this would make\r
502  *          // no sense.  Instead the calls are likely to be buried inside\r
503  *          // a more complex call structure.\r
504  *          xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );\r
505  *          xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );\r
506  *\r
507  *          // The mutex has now been 'taken' three times, so will not be\r
508  *          // available to another task until it has also been given back\r
509  *          // three times.  Again it is unlikely that real code would have\r
510  *          // these calls sequentially, it would be more likely that the calls\r
511  *          // to xSemaphoreGiveRecursive() would be called as a call stack\r
512  *          // unwound.  This is just for demonstrative purposes.\r
513  *          xSemaphoreGiveRecursive( xMutex );\r
514  *          xSemaphoreGiveRecursive( xMutex );\r
515  *          xSemaphoreGiveRecursive( xMutex );\r
516  *\r
517  *          // Now the mutex can be taken by other tasks.\r
518  *      }\r
519  *      else\r
520  *      {\r
521  *          // We could not obtain the mutex and can therefore not access\r
522  *          // the shared resource safely.\r
523  *      }\r
524  *  }\r
525  * }\r
526  * </pre>\r
527  * \defgroup xSemaphoreGiveRecursive xSemaphoreGiveRecursive\r
528  * \ingroup Semaphores\r
529  */\r
530 #if ( configUSE_RECURSIVE_MUTEXES == 1 )\r
531     #define xSemaphoreGiveRecursive( xMutex )    xQueueGiveMutexRecursive( ( xMutex ) )\r
532 #endif\r
533 \r
534 /**\r
535  * semphr. h\r
536  * <pre>\r
537  * xSemaphoreGiveFromISR(\r
538  *                        SemaphoreHandle_t xSemaphore,\r
539  *                        BaseType_t *pxHigherPriorityTaskWoken\r
540  *                    )</pre>\r
541  *\r
542  * <i>Macro</i> to  release a semaphore.  The semaphore must have previously been\r
543  * created with a call to xSemaphoreCreateBinary() or xSemaphoreCreateCounting().\r
544  *\r
545  * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())\r
546  * must not be used with this macro.\r
547  *\r
548  * This macro can be used from an ISR.\r
549  *\r
550  * @param xSemaphore A handle to the semaphore being released.  This is the\r
551  * handle returned when the semaphore was created.\r
552  *\r
553  * @param pxHigherPriorityTaskWoken xSemaphoreGiveFromISR() will set\r
554  * *pxHigherPriorityTaskWoken to pdTRUE if giving the semaphore caused a task\r
555  * to unblock, and the unblocked task has a priority higher than the currently\r
556  * running task.  If xSemaphoreGiveFromISR() sets this value to pdTRUE then\r
557  * a context switch should be requested before the interrupt is exited.\r
558  *\r
559  * @return pdTRUE if the semaphore was successfully given, otherwise errQUEUE_FULL.\r
560  *\r
561  * Example usage:\r
562  * <pre>\r
563  \#define LONG_TIME 0xffff\r
564  \#define TICKS_TO_WAIT 10\r
565  * SemaphoreHandle_t xSemaphore = NULL;\r
566  *\r
567  * // Repetitive task.\r
568  * void vATask( void * pvParameters )\r
569  * {\r
570  *  for( ;; )\r
571  *  {\r
572  *      // We want this task to run every 10 ticks of a timer.  The semaphore\r
573  *      // was created before this task was started.\r
574  *\r
575  *      // Block waiting for the semaphore to become available.\r
576  *      if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )\r
577  *      {\r
578  *          // It is time to execute.\r
579  *\r
580  *          // ...\r
581  *\r
582  *          // We have finished our task.  Return to the top of the loop where\r
583  *          // we will block on the semaphore until it is time to execute\r
584  *          // again.  Note when using the semaphore for synchronisation with an\r
585  *          // ISR in this manner there is no need to 'give' the semaphore back.\r
586  *      }\r
587  *  }\r
588  * }\r
589  *\r
590  * // Timer ISR\r
591  * void vTimerISR( void * pvParameters )\r
592  * {\r
593  * static uint8_t ucLocalTickCount = 0;\r
594  * static BaseType_t xHigherPriorityTaskWoken;\r
595  *\r
596  *  // A timer tick has occurred.\r
597  *\r
598  *  // ... Do other time functions.\r
599  *\r
600  *  // Is it time for vATask () to run?\r
601  *  xHigherPriorityTaskWoken = pdFALSE;\r
602  *  ucLocalTickCount++;\r
603  *  if( ucLocalTickCount >= TICKS_TO_WAIT )\r
604  *  {\r
605  *      // Unblock the task by releasing the semaphore.\r
606  *      xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );\r
607  *\r
608  *      // Reset the count so we release the semaphore again in 10 ticks time.\r
609  *      ucLocalTickCount = 0;\r
610  *  }\r
611  *\r
612  *  if( xHigherPriorityTaskWoken != pdFALSE )\r
613  *  {\r
614  *      // We can force a context switch here.  Context switching from an\r
615  *      // ISR uses port specific syntax.  Check the demo task for your port\r
616  *      // to find the syntax required.\r
617  *  }\r
618  * }\r
619  * </pre>\r
620  * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR\r
621  * \ingroup Semaphores\r
622  */\r
623 #define xSemaphoreGiveFromISR( xSemaphore, pxHigherPriorityTaskWoken )    xQueueGiveFromISR( ( QueueHandle_t ) ( xSemaphore ), ( pxHigherPriorityTaskWoken ) )\r
624 \r
625 /**\r
626  * semphr. h\r
627  * <pre>\r
628  * xSemaphoreTakeFromISR(\r
629  *                        SemaphoreHandle_t xSemaphore,\r
630  *                        BaseType_t *pxHigherPriorityTaskWoken\r
631  *                    )</pre>\r
632  *\r
633  * <i>Macro</i> to  take a semaphore from an ISR.  The semaphore must have\r
634  * previously been created with a call to xSemaphoreCreateBinary() or\r
635  * xSemaphoreCreateCounting().\r
636  *\r
637  * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())\r
638  * must not be used with this macro.\r
639  *\r
640  * This macro can be used from an ISR, however taking a semaphore from an ISR\r
641  * is not a common operation.  It is likely to only be useful when taking a\r
642  * counting semaphore when an interrupt is obtaining an object from a resource\r
643  * pool (when the semaphore count indicates the number of resources available).\r
644  *\r
645  * @param xSemaphore A handle to the semaphore being taken.  This is the\r
646  * handle returned when the semaphore was created.\r
647  *\r
648  * @param pxHigherPriorityTaskWoken xSemaphoreTakeFromISR() will set\r
649  * *pxHigherPriorityTaskWoken to pdTRUE if taking the semaphore caused a task\r
650  * to unblock, and the unblocked task has a priority higher than the currently\r
651  * running task.  If xSemaphoreTakeFromISR() sets this value to pdTRUE then\r
652  * a context switch should be requested before the interrupt is exited.\r
653  *\r
654  * @return pdTRUE if the semaphore was successfully taken, otherwise\r
655  * pdFALSE\r
656  */\r
657 #define xSemaphoreTakeFromISR( xSemaphore, pxHigherPriorityTaskWoken )    xQueueReceiveFromISR( ( QueueHandle_t ) ( xSemaphore ), NULL, ( pxHigherPriorityTaskWoken ) )\r
658 \r
659 /**\r
660  * semphr. h\r
661  * <pre>SemaphoreHandle_t xSemaphoreCreateMutex( void )</pre>\r
662  *\r
663  * Creates a new mutex type semaphore instance, and returns a handle by which\r
664  * the new mutex can be referenced.\r
665  *\r
666  * Internally, within the FreeRTOS implementation, mutex semaphores use a block\r
667  * of memory, in which the mutex structure is stored.  If a mutex is created\r
668  * using xSemaphoreCreateMutex() then the required memory is automatically\r
669  * dynamically allocated inside the xSemaphoreCreateMutex() function.  (see\r
670  * http://www.freertos.org/a00111.html).  If a mutex is created using\r
671  * xSemaphoreCreateMutexStatic() then the application writer must provided the\r
672  * memory.  xSemaphoreCreateMutexStatic() therefore allows a mutex to be created\r
673  * without using any dynamic memory allocation.\r
674  *\r
675  * Mutexes created using this function can be accessed using the xSemaphoreTake()\r
676  * and xSemaphoreGive() macros.  The xSemaphoreTakeRecursive() and\r
677  * xSemaphoreGiveRecursive() macros must not be used.\r
678  *\r
679  * This type of semaphore uses a priority inheritance mechanism so a task\r
680  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the\r
681  * semaphore it is no longer required.\r
682  *\r
683  * Mutex type semaphores cannot be used from within interrupt service routines.\r
684  *\r
685  * See xSemaphoreCreateBinary() for an alternative implementation that can be\r
686  * used for pure synchronisation (where one task or interrupt always 'gives' the\r
687  * semaphore and another always 'takes' the semaphore) and from within interrupt\r
688  * service routines.\r
689  *\r
690  * @return If the mutex was successfully created then a handle to the created\r
691  * semaphore is returned.  If there was not enough heap to allocate the mutex\r
692  * data structures then NULL is returned.\r
693  *\r
694  * Example usage:\r
695  * <pre>\r
696  * SemaphoreHandle_t xSemaphore;\r
697  *\r
698  * void vATask( void * pvParameters )\r
699  * {\r
700  *  // Semaphore cannot be used before a call to xSemaphoreCreateMutex().\r
701  *  // This is a macro so pass the variable in directly.\r
702  *  xSemaphore = xSemaphoreCreateMutex();\r
703  *\r
704  *  if( xSemaphore != NULL )\r
705  *  {\r
706  *      // The semaphore was created successfully.\r
707  *      // The semaphore can now be used.\r
708  *  }\r
709  * }\r
710  * </pre>\r
711  * \defgroup xSemaphoreCreateMutex xSemaphoreCreateMutex\r
712  * \ingroup Semaphores\r
713  */\r
714 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
715     #define xSemaphoreCreateMutex()    xQueueCreateMutex( queueQUEUE_TYPE_MUTEX )\r
716 #endif\r
717 \r
718 /**\r
719  * semphr. h\r
720  * <pre>SemaphoreHandle_t xSemaphoreCreateMutexStatic( StaticSemaphore_t *pxMutexBuffer )</pre>\r
721  *\r
722  * Creates a new mutex type semaphore instance, and returns a handle by which\r
723  * the new mutex can be referenced.\r
724  *\r
725  * Internally, within the FreeRTOS implementation, mutex semaphores use a block\r
726  * of memory, in which the mutex structure is stored.  If a mutex is created\r
727  * using xSemaphoreCreateMutex() then the required memory is automatically\r
728  * dynamically allocated inside the xSemaphoreCreateMutex() function.  (see\r
729  * http://www.freertos.org/a00111.html).  If a mutex is created using\r
730  * xSemaphoreCreateMutexStatic() then the application writer must provided the\r
731  * memory.  xSemaphoreCreateMutexStatic() therefore allows a mutex to be created\r
732  * without using any dynamic memory allocation.\r
733  *\r
734  * Mutexes created using this function can be accessed using the xSemaphoreTake()\r
735  * and xSemaphoreGive() macros.  The xSemaphoreTakeRecursive() and\r
736  * xSemaphoreGiveRecursive() macros must not be used.\r
737  *\r
738  * This type of semaphore uses a priority inheritance mechanism so a task\r
739  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the\r
740  * semaphore it is no longer required.\r
741  *\r
742  * Mutex type semaphores cannot be used from within interrupt service routines.\r
743  *\r
744  * See xSemaphoreCreateBinary() for an alternative implementation that can be\r
745  * used for pure synchronisation (where one task or interrupt always 'gives' the\r
746  * semaphore and another always 'takes' the semaphore) and from within interrupt\r
747  * service routines.\r
748  *\r
749  * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,\r
750  * which will be used to hold the mutex's data structure, removing the need for\r
751  * the memory to be allocated dynamically.\r
752  *\r
753  * @return If the mutex was successfully created then a handle to the created\r
754  * mutex is returned.  If pxMutexBuffer was NULL then NULL is returned.\r
755  *\r
756  * Example usage:\r
757  * <pre>\r
758  * SemaphoreHandle_t xSemaphore;\r
759  * StaticSemaphore_t xMutexBuffer;\r
760  *\r
761  * void vATask( void * pvParameters )\r
762  * {\r
763  *  // A mutex cannot be used before it has been created.  xMutexBuffer is\r
764  *  // into xSemaphoreCreateMutexStatic() so no dynamic memory allocation is\r
765  *  // attempted.\r
766  *  xSemaphore = xSemaphoreCreateMutexStatic( &xMutexBuffer );\r
767  *\r
768  *  // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,\r
769  *  // so there is no need to check it.\r
770  * }\r
771  * </pre>\r
772  * \defgroup xSemaphoreCreateMutexStatic xSemaphoreCreateMutexStatic\r
773  * \ingroup Semaphores\r
774  */\r
775 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )\r
776     #define xSemaphoreCreateMutexStatic( pxMutexBuffer )    xQueueCreateMutexStatic( queueQUEUE_TYPE_MUTEX, ( pxMutexBuffer ) )\r
777 #endif /* configSUPPORT_STATIC_ALLOCATION */\r
778 \r
779 \r
780 /**\r
781  * semphr. h\r
782  * <pre>SemaphoreHandle_t xSemaphoreCreateRecursiveMutex( void )</pre>\r
783  *\r
784  * Creates a new recursive mutex type semaphore instance, and returns a handle\r
785  * by which the new recursive mutex can be referenced.\r
786  *\r
787  * Internally, within the FreeRTOS implementation, recursive mutexs use a block\r
788  * of memory, in which the mutex structure is stored.  If a recursive mutex is\r
789  * created using xSemaphoreCreateRecursiveMutex() then the required memory is\r
790  * automatically dynamically allocated inside the\r
791  * xSemaphoreCreateRecursiveMutex() function.  (see\r
792  * http://www.freertos.org/a00111.html).  If a recursive mutex is created using\r
793  * xSemaphoreCreateRecursiveMutexStatic() then the application writer must\r
794  * provide the memory that will get used by the mutex.\r
795  * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to\r
796  * be created without using any dynamic memory allocation.\r
797  *\r
798  * Mutexes created using this macro can be accessed using the\r
799  * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros.  The\r
800  * xSemaphoreTake() and xSemaphoreGive() macros must not be used.\r
801  *\r
802  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex\r
803  * doesn't become available again until the owner has called\r
804  * xSemaphoreGiveRecursive() for each successful 'take' request.  For example,\r
805  * if a task successfully 'takes' the same mutex 5 times then the mutex will\r
806  * not be available to any other task until it has also  'given' the mutex back\r
807  * exactly five times.\r
808  *\r
809  * This type of semaphore uses a priority inheritance mechanism so a task\r
810  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the\r
811  * semaphore it is no longer required.\r
812  *\r
813  * Mutex type semaphores cannot be used from within interrupt service routines.\r
814  *\r
815  * See xSemaphoreCreateBinary() for an alternative implementation that can be\r
816  * used for pure synchronisation (where one task or interrupt always 'gives' the\r
817  * semaphore and another always 'takes' the semaphore) and from within interrupt\r
818  * service routines.\r
819  *\r
820  * @return xSemaphore Handle to the created mutex semaphore.  Should be of type\r
821  * SemaphoreHandle_t.\r
822  *\r
823  * Example usage:\r
824  * <pre>\r
825  * SemaphoreHandle_t xSemaphore;\r
826  *\r
827  * void vATask( void * pvParameters )\r
828  * {\r
829  *  // Semaphore cannot be used before a call to xSemaphoreCreateMutex().\r
830  *  // This is a macro so pass the variable in directly.\r
831  *  xSemaphore = xSemaphoreCreateRecursiveMutex();\r
832  *\r
833  *  if( xSemaphore != NULL )\r
834  *  {\r
835  *      // The semaphore was created successfully.\r
836  *      // The semaphore can now be used.\r
837  *  }\r
838  * }\r
839  * </pre>\r
840  * \defgroup xSemaphoreCreateRecursiveMutex xSemaphoreCreateRecursiveMutex\r
841  * \ingroup Semaphores\r
842  */\r
843 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )\r
844     #define xSemaphoreCreateRecursiveMutex()    xQueueCreateMutex( queueQUEUE_TYPE_RECURSIVE_MUTEX )\r
845 #endif\r
846 \r
847 /**\r
848  * semphr. h\r
849  * <pre>SemaphoreHandle_t xSemaphoreCreateRecursiveMutexStatic( StaticSemaphore_t *pxMutexBuffer )</pre>\r
850  *\r
851  * Creates a new recursive mutex type semaphore instance, and returns a handle\r
852  * by which the new recursive mutex can be referenced.\r
853  *\r
854  * Internally, within the FreeRTOS implementation, recursive mutexs use a block\r
855  * of memory, in which the mutex structure is stored.  If a recursive mutex is\r
856  * created using xSemaphoreCreateRecursiveMutex() then the required memory is\r
857  * automatically dynamically allocated inside the\r
858  * xSemaphoreCreateRecursiveMutex() function.  (see\r
859  * http://www.freertos.org/a00111.html).  If a recursive mutex is created using\r
860  * xSemaphoreCreateRecursiveMutexStatic() then the application writer must\r
861  * provide the memory that will get used by the mutex.\r
862  * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to\r
863  * be created without using any dynamic memory allocation.\r
864  *\r
865  * Mutexes created using this macro can be accessed using the\r
866  * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros.  The\r
867  * xSemaphoreTake() and xSemaphoreGive() macros must not be used.\r
868  *\r
869  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex\r
870  * doesn't become available again until the owner has called\r
871  * xSemaphoreGiveRecursive() for each successful 'take' request.  For example,\r
872  * if a task successfully 'takes' the same mutex 5 times then the mutex will\r
873  * not be available to any other task until it has also  'given' the mutex back\r
874  * exactly five times.\r
875  *\r
876  * This type of semaphore uses a priority inheritance mechanism so a task\r
877  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the\r
878  * semaphore it is no longer required.\r
879  *\r
880  * Mutex type semaphores cannot be used from within interrupt service routines.\r
881  *\r
882  * See xSemaphoreCreateBinary() for an alternative implementation that can be\r
883  * used for pure synchronisation (where one task or interrupt always 'gives' the\r
884  * semaphore and another always 'takes' the semaphore) and from within interrupt\r
885  * service routines.\r
886  *\r
887  * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,\r
888  * which will then be used to hold the recursive mutex's data structure,\r
889  * removing the need for the memory to be allocated dynamically.\r
890  *\r
891  * @return If the recursive mutex was successfully created then a handle to the\r
892  * created recursive mutex is returned.  If pxMutexBuffer was NULL then NULL is\r
893  * returned.\r
894  *\r
895  * Example usage:\r
896  * <pre>\r
897  * SemaphoreHandle_t xSemaphore;\r
898  * StaticSemaphore_t xMutexBuffer;\r
899  *\r
900  * void vATask( void * pvParameters )\r
901  * {\r
902  *  // A recursive semaphore cannot be used before it is created.  Here a\r
903  *  // recursive mutex is created using xSemaphoreCreateRecursiveMutexStatic().\r
904  *  // The address of xMutexBuffer is passed into the function, and will hold\r
905  *  // the mutexes data structures - so no dynamic memory allocation will be\r
906  *  // attempted.\r
907  *  xSemaphore = xSemaphoreCreateRecursiveMutexStatic( &xMutexBuffer );\r
908  *\r
909  *  // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,\r
910  *  // so there is no need to check it.\r
911  * }\r
912  * </pre>\r
913  * \defgroup xSemaphoreCreateRecursiveMutexStatic xSemaphoreCreateRecursiveMutexStatic\r
914  * \ingroup Semaphores\r
915  */\r
916 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )\r
917     #define xSemaphoreCreateRecursiveMutexStatic( pxStaticSemaphore )    xQueueCreateMutexStatic( queueQUEUE_TYPE_RECURSIVE_MUTEX, pxStaticSemaphore )\r
918 #endif /* configSUPPORT_STATIC_ALLOCATION */\r
919 \r
920 /**\r
921  * semphr. h\r
922  * <pre>SemaphoreHandle_t xSemaphoreCreateCounting( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount )</pre>\r
923  *\r
924  * Creates a new counting semaphore instance, and returns a handle by which the\r
925  * new counting semaphore can be referenced.\r
926  *\r
927  * In many usage scenarios it is faster and more memory efficient to use a\r
928  * direct to task notification in place of a counting semaphore!\r
929  * http://www.freertos.org/RTOS-task-notifications.html\r
930  *\r
931  * Internally, within the FreeRTOS implementation, counting semaphores use a\r
932  * block of memory, in which the counting semaphore structure is stored.  If a\r
933  * counting semaphore is created using xSemaphoreCreateCounting() then the\r
934  * required memory is automatically dynamically allocated inside the\r
935  * xSemaphoreCreateCounting() function.  (see\r
936  * http://www.freertos.org/a00111.html).  If a counting semaphore is created\r
937  * using xSemaphoreCreateCountingStatic() then the application writer can\r
938  * instead optionally provide the memory that will get used by the counting\r
939  * semaphore.  xSemaphoreCreateCountingStatic() therefore allows a counting\r
940  * semaphore to be created without using any dynamic memory allocation.\r
941  *\r
942  * Counting semaphores are typically used for two things:\r
943  *\r
944  * 1) Counting events.\r
945  *\r
946  *    In this usage scenario an event handler will 'give' a semaphore each time\r
947  *    an event occurs (incrementing the semaphore count value), and a handler\r
948  *    task will 'take' a semaphore each time it processes an event\r
949  *    (decrementing the semaphore count value).  The count value is therefore\r
950  *    the difference between the number of events that have occurred and the\r
951  *    number that have been processed.  In this case it is desirable for the\r
952  *    initial count value to be zero.\r
953  *\r
954  * 2) Resource management.\r
955  *\r
956  *    In this usage scenario the count value indicates the number of resources\r
957  *    available.  To obtain control of a resource a task must first obtain a\r
958  *    semaphore - decrementing the semaphore count value.  When the count value\r
959  *    reaches zero there are no free resources.  When a task finishes with the\r
960  *    resource it 'gives' the semaphore back - incrementing the semaphore count\r
961  *    value.  In this case it is desirable for the initial count value to be\r
962  *    equal to the maximum count value, indicating that all resources are free.\r
963  *\r
964  * @param uxMaxCount The maximum count value that can be reached.  When the\r
965  *        semaphore reaches this value it can no longer be 'given'.\r
966  *\r
967  * @param uxInitialCount The count value assigned to the semaphore when it is\r
968  *        created.\r
969  *\r
970  * @return Handle to the created semaphore.  Null if the semaphore could not be\r
971  *         created.\r
972  *\r
973  * Example usage:\r
974  * <pre>\r
975  * SemaphoreHandle_t xSemaphore;\r
976  *\r
977  * void vATask( void * pvParameters )\r
978  * {\r
979  * SemaphoreHandle_t xSemaphore = NULL;\r
980  *\r
981  *  // Semaphore cannot be used before a call to xSemaphoreCreateCounting().\r
982  *  // The max value to which the semaphore can count should be 10, and the\r
983  *  // initial value assigned to the count should be 0.\r
984  *  xSemaphore = xSemaphoreCreateCounting( 10, 0 );\r
985  *\r
986  *  if( xSemaphore != NULL )\r
987  *  {\r
988  *      // The semaphore was created successfully.\r
989  *      // The semaphore can now be used.\r
990  *  }\r
991  * }\r
992  * </pre>\r
993  * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting\r
994  * \ingroup Semaphores\r
995  */\r
996 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
997     #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount )    xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ) )\r
998 #endif\r
999 \r
1000 /**\r
1001  * semphr. h\r
1002  * <pre>SemaphoreHandle_t xSemaphoreCreateCountingStatic( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount, StaticSemaphore_t *pxSemaphoreBuffer )</pre>\r
1003  *\r
1004  * Creates a new counting semaphore instance, and returns a handle by which the\r
1005  * new counting semaphore can be referenced.\r
1006  *\r
1007  * In many usage scenarios it is faster and more memory efficient to use a\r
1008  * direct to task notification in place of a counting semaphore!\r
1009  * http://www.freertos.org/RTOS-task-notifications.html\r
1010  *\r
1011  * Internally, within the FreeRTOS implementation, counting semaphores use a\r
1012  * block of memory, in which the counting semaphore structure is stored.  If a\r
1013  * counting semaphore is created using xSemaphoreCreateCounting() then the\r
1014  * required memory is automatically dynamically allocated inside the\r
1015  * xSemaphoreCreateCounting() function.  (see\r
1016  * http://www.freertos.org/a00111.html).  If a counting semaphore is created\r
1017  * using xSemaphoreCreateCountingStatic() then the application writer must\r
1018  * provide the memory.  xSemaphoreCreateCountingStatic() therefore allows a\r
1019  * counting semaphore to be created without using any dynamic memory allocation.\r
1020  *\r
1021  * Counting semaphores are typically used for two things:\r
1022  *\r
1023  * 1) Counting events.\r
1024  *\r
1025  *    In this usage scenario an event handler will 'give' a semaphore each time\r
1026  *    an event occurs (incrementing the semaphore count value), and a handler\r
1027  *    task will 'take' a semaphore each time it processes an event\r
1028  *    (decrementing the semaphore count value).  The count value is therefore\r
1029  *    the difference between the number of events that have occurred and the\r
1030  *    number that have been processed.  In this case it is desirable for the\r
1031  *    initial count value to be zero.\r
1032  *\r
1033  * 2) Resource management.\r
1034  *\r
1035  *    In this usage scenario the count value indicates the number of resources\r
1036  *    available.  To obtain control of a resource a task must first obtain a\r
1037  *    semaphore - decrementing the semaphore count value.  When the count value\r
1038  *    reaches zero there are no free resources.  When a task finishes with the\r
1039  *    resource it 'gives' the semaphore back - incrementing the semaphore count\r
1040  *    value.  In this case it is desirable for the initial count value to be\r
1041  *    equal to the maximum count value, indicating that all resources are free.\r
1042  *\r
1043  * @param uxMaxCount The maximum count value that can be reached.  When the\r
1044  *        semaphore reaches this value it can no longer be 'given'.\r
1045  *\r
1046  * @param uxInitialCount The count value assigned to the semaphore when it is\r
1047  *        created.\r
1048  *\r
1049  * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,\r
1050  * which will then be used to hold the semaphore's data structure, removing the\r
1051  * need for the memory to be allocated dynamically.\r
1052  *\r
1053  * @return If the counting semaphore was successfully created then a handle to\r
1054  * the created counting semaphore is returned.  If pxSemaphoreBuffer was NULL\r
1055  * then NULL is returned.\r
1056  *\r
1057  * Example usage:\r
1058  * <pre>\r
1059  * SemaphoreHandle_t xSemaphore;\r
1060  * StaticSemaphore_t xSemaphoreBuffer;\r
1061  *\r
1062  * void vATask( void * pvParameters )\r
1063  * {\r
1064  * SemaphoreHandle_t xSemaphore = NULL;\r
1065  *\r
1066  *  // Counting semaphore cannot be used before they have been created.  Create\r
1067  *  // a counting semaphore using xSemaphoreCreateCountingStatic().  The max\r
1068  *  // value to which the semaphore can count is 10, and the initial value\r
1069  *  // assigned to the count will be 0.  The address of xSemaphoreBuffer is\r
1070  *  // passed in and will be used to hold the semaphore structure, so no dynamic\r
1071  *  // memory allocation will be used.\r
1072  *  xSemaphore = xSemaphoreCreateCounting( 10, 0, &xSemaphoreBuffer );\r
1073  *\r
1074  *  // No memory allocation was attempted so xSemaphore cannot be NULL, so there\r
1075  *  // is no need to check its value.\r
1076  * }\r
1077  * </pre>\r
1078  * \defgroup xSemaphoreCreateCountingStatic xSemaphoreCreateCountingStatic\r
1079  * \ingroup Semaphores\r
1080  */\r
1081 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )\r
1082     #define xSemaphoreCreateCountingStatic( uxMaxCount, uxInitialCount, pxSemaphoreBuffer )    xQueueCreateCountingSemaphoreStatic( ( uxMaxCount ), ( uxInitialCount ), ( pxSemaphoreBuffer ) )\r
1083 #endif /* configSUPPORT_STATIC_ALLOCATION */\r
1084 \r
1085 /**\r
1086  * semphr. h\r
1087  * <pre>void vSemaphoreDelete( SemaphoreHandle_t xSemaphore );</pre>\r
1088  *\r
1089  * Delete a semaphore.  This function must be used with care.  For example,\r
1090  * do not delete a mutex type semaphore if the mutex is held by a task.\r
1091  *\r
1092  * @param xSemaphore A handle to the semaphore to be deleted.\r
1093  *\r
1094  * \defgroup vSemaphoreDelete vSemaphoreDelete\r
1095  * \ingroup Semaphores\r
1096  */\r
1097 #define vSemaphoreDelete( xSemaphore )                   vQueueDelete( ( QueueHandle_t ) ( xSemaphore ) )\r
1098 \r
1099 /**\r
1100  * semphr.h\r
1101  * <pre>TaskHandle_t xSemaphoreGetMutexHolder( SemaphoreHandle_t xMutex );</pre>\r
1102  *\r
1103  * If xMutex is indeed a mutex type semaphore, return the current mutex holder.\r
1104  * If xMutex is not a mutex type semaphore, or the mutex is available (not held\r
1105  * by a task), return NULL.\r
1106  *\r
1107  * Note: This is a good way of determining if the calling task is the mutex\r
1108  * holder, but not a good way of determining the identity of the mutex holder as\r
1109  * the holder may change between the function exiting and the returned value\r
1110  * being tested.\r
1111  */\r
1112 #define xSemaphoreGetMutexHolder( xSemaphore )           xQueueGetMutexHolder( ( xSemaphore ) )\r
1113 \r
1114 /**\r
1115  * semphr.h\r
1116  * <pre>TaskHandle_t xSemaphoreGetMutexHolderFromISR( SemaphoreHandle_t xMutex );</pre>\r
1117  *\r
1118  * If xMutex is indeed a mutex type semaphore, return the current mutex holder.\r
1119  * If xMutex is not a mutex type semaphore, or the mutex is available (not held\r
1120  * by a task), return NULL.\r
1121  *\r
1122  */\r
1123 #define xSemaphoreGetMutexHolderFromISR( xSemaphore )    xQueueGetMutexHolderFromISR( ( xSemaphore ) )\r
1124 \r
1125 /**\r
1126  * semphr.h\r
1127  * <pre>UBaseType_t uxSemaphoreGetCount( SemaphoreHandle_t xSemaphore );</pre>\r
1128  *\r
1129  * If the semaphore is a counting semaphore then uxSemaphoreGetCount() returns\r
1130  * its current count value.  If the semaphore is a binary semaphore then\r
1131  * uxSemaphoreGetCount() returns 1 if the semaphore is available, and 0 if the\r
1132  * semaphore is not available.\r
1133  *\r
1134  */\r
1135 #define uxSemaphoreGetCount( xSemaphore )                uxQueueMessagesWaiting( ( QueueHandle_t ) ( xSemaphore ) )\r
1136 \r
1137 #endif /* SEMAPHORE_H */\r