]> git.sur5r.net Git - freertos/blob - Source/include/semphr.h
Correct spelling.
[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 have previously been\r
103  * created with a call to vSemaphoreCreateBinary(), xSemaphoreCreateMutex() or\r
104  * xSemaphoreCreateCounting().\r
105  *\r
106  * @param xSemaphore A handle to the semaphore being taken - obtained when\r
107  * the semaphore was created.\r
108  *\r
109  * @param xBlockTime The time in ticks to wait for the semaphore to become\r
110  * available.  The macro portTICK_RATE_MS can be used to convert this to a\r
111  * real time.  A block time of zero can be used to poll the semaphore.  A block\r
112  * time of portMAX_DELAY can be used to block indefinitely (provided\r
113  * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h).\r
114  *\r
115  * @return pdTRUE if the semaphore was obtained.  pdFALSE\r
116  * if xBlockTime expired without the semaphore becoming available.\r
117  *\r
118  * Example usage:\r
119  <pre>\r
120  xSemaphoreHandle xSemaphore = NULL;\r
121 \r
122  // A task that creates a semaphore.\r
123  void vATask( void * pvParameters )\r
124  {\r
125     // Create the semaphore to guard a shared resource.\r
126     vSemaphoreCreateBinary( xSemaphore );\r
127  }\r
128 \r
129  // A task that uses the semaphore.\r
130  void vAnotherTask( void * pvParameters )\r
131  {\r
132     // ... Do other things.\r
133 \r
134     if( xSemaphore != NULL )\r
135     {\r
136         // See if we can obtain the semaphore.  If the semaphore is not available\r
137         // wait 10 ticks to see if it becomes free.     \r
138         if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE )\r
139         {\r
140             // We were able to obtain the semaphore and can now access the\r
141             // shared resource.\r
142 \r
143             // ...\r
144 \r
145             // We have finished accessing the shared resource.  Release the \r
146             // semaphore.\r
147             xSemaphoreGive( xSemaphore );\r
148         }\r
149         else\r
150         {\r
151             // We could not obtain the semaphore and can therefore not access\r
152             // the shared resource safely.\r
153         }\r
154     }\r
155  }\r
156  </pre>\r
157  * \defgroup xSemaphoreTake xSemaphoreTake\r
158  * \ingroup Semaphores\r
159  */\r
160 #define xSemaphoreTake( xSemaphore, xBlockTime )                xQueueGenericReceive( ( xQueueHandle ) xSemaphore, NULL, xBlockTime, pdFALSE )\r
161 \r
162 /**\r
163  * semphr. h\r
164  * xSemaphoreTakeRecursive( \r
165  *                          xSemaphoreHandle xMutex, \r
166  *                          portTickType xBlockTime \r
167  *                        )\r
168  *\r
169  * <i>Macro</i> to recursively obtain, or 'take', a mutex type semaphore.  \r
170  * The mutex must have previously been created using a call to \r
171  * xSemaphoreCreateRecursiveMutex();\r
172  * \r
173  * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this\r
174  * macro to be available.\r
175  * \r
176  * This macro must not be used on mutexes created using xSemaphoreCreateMutex().\r
177  *\r
178  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex \r
179  * doesn't become available again until the owner has called \r
180  * xSemaphoreGiveRecursive() for each successful 'take' request.  For example, \r
181  * if a task successfully 'takes' the same mutex 5 times then the mutex will \r
182  * not be available to any other task until it has also  'given' the mutex back\r
183  * exactly five times.\r
184  *\r
185  * @param xMutex A handle to the mutex being obtained.  This is the\r
186  * handle returned by xSemaphoreCreateMutex();\r
187  *\r
188  * @param xBlockTime The time in ticks to wait for the semaphore to become\r
189  * available.  The macro portTICK_RATE_MS can be used to convert this to a\r
190  * real time.  A block time of zero can be used to poll the semaphore.  If\r
191  * the task already owns the semaphore then xSemaphoreTakeRecursive() will\r
192  * return immediately no matter what the value of xBlockTime. \r
193  *\r
194  * @return pdTRUE if the semaphore was obtained.  pdFALSE if xBlockTime\r
195  * expired without the semaphore becoming available.\r
196  *\r
197  * Example usage:\r
198  <pre>\r
199  xSemaphoreHandle xMutex = NULL;\r
200 \r
201  // A task that creates a mutex.\r
202  void vATask( void * pvParameters )\r
203  {\r
204     // Create the mutex to guard a shared resource.\r
205     xMutex = xSemaphoreCreateRecursiveMutex();\r
206  }\r
207 \r
208  // A task that uses the mutex.\r
209  void vAnotherTask( void * pvParameters )\r
210  {\r
211     // ... Do other things.\r
212 \r
213     if( xMutex != NULL )\r
214     {\r
215         // See if we can obtain the mutex.  If the mutex is not available\r
216         // wait 10 ticks to see if it becomes free.     \r
217         if( xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 ) == pdTRUE )\r
218         {\r
219             // We were able to obtain the mutex and can now access the\r
220             // shared resource.\r
221 \r
222             // ...\r
223             // For some reason due to the nature of the code further calls to \r
224                         // xSemaphoreTakeRecursive() are made on the same mutex.  In real\r
225                         // code these would not be just sequential calls as this would make\r
226                         // no sense.  Instead the calls are likely to be buried inside\r
227                         // a more complex call structure.\r
228             xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 );\r
229             xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 );\r
230 \r
231             // The mutex has now been 'taken' three times, so will not be \r
232                         // available to another task until it has also been given back\r
233                         // three times.  Again it is unlikely that real code would have\r
234                         // these calls sequentially, but instead buried in a more complex\r
235                         // call structure.  This is just for illustrative purposes.\r
236             xSemaphoreGiveRecursive( xSemaphore );\r
237                         xSemaphoreGiveRecursive( xSemaphore );\r
238                         xSemaphoreGiveRecursive( xSemaphore );\r
239 \r
240                         // Now the mutex can be taken by other tasks.\r
241         }\r
242         else\r
243         {\r
244             // We could not obtain the mutex and can therefore not access\r
245             // the shared resource safely.\r
246         }\r
247     }\r
248  }\r
249  </pre>\r
250  * \defgroup xSemaphoreTakeRecursive xSemaphoreTakeRecursive\r
251  * \ingroup Semaphores\r
252  */\r
253 #define xSemaphoreTakeRecursive( xMutex, xBlockTime )   xQueueTakeMutexRecursive( xMutex, xBlockTime )\r
254 \r
255 \r
256 /* \r
257  * xSemaphoreAltTake() is an alternative version of xSemaphoreTake().\r
258  *\r
259  * The source code that implements the alternative (Alt) API is much \r
260  * simpler      because it executes everything from within a critical section.  \r
261  * This is      the approach taken by many other RTOSes, but FreeRTOS.org has the \r
262  * preferred fully featured API too.  The fully featured API has more \r
263  * complex      code that takes longer to execute, but makes much less use of \r
264  * critical sections.  Therefore the alternative API sacrifices interrupt \r
265  * responsiveness to gain execution speed, whereas the fully featured API\r
266  * sacrifices execution speed to ensure better interrupt responsiveness.\r
267  */\r
268 #define xSemaphoreAltTake( xSemaphore, xBlockTime )             xQueueAltGenericReceive( ( xQueueHandle ) xSemaphore, NULL, xBlockTime, pdFALSE )\r
269 \r
270 /**\r
271  * semphr. h\r
272  * <pre>xSemaphoreGive( xSemaphoreHandle xSemaphore )</pre>\r
273  *\r
274  * <i>Macro</i> to release a semaphore.  The semaphore must have previously been\r
275  * created with a call to vSemaphoreCreateBinary(), xSemaphoreCreateMutex() or\r
276  * xSemaphoreCreateCounting(). and obtained using sSemaphoreTake().\r
277  *\r
278  * This macro must not be used from an ISR.  See xSemaphoreGiveFromISR () for\r
279  * an alternative which can be used from an ISR.\r
280  *\r
281  * This macro must also not be used on semaphores created using \r
282  * xSemaphoreCreateRecursiveMutex().\r
283  *\r
284  * @param xSemaphore A handle to the semaphore being released.  This is the\r
285  * handle returned when the semaphore was created.\r
286  *\r
287  * @return pdTRUE if the semaphore was released.  pdFALSE if an error occurred.\r
288  * Semaphores are implemented using queues.  An error can occur if there is\r
289  * no space on the queue to post a message - indicating that the \r
290  * semaphore was not first obtained correctly.\r
291  *\r
292  * Example usage:\r
293  <pre>\r
294  xSemaphoreHandle xSemaphore = NULL;\r
295 \r
296  void vATask( void * pvParameters )\r
297  {\r
298     // Create the semaphore to guard a shared resource.\r
299     vSemaphoreCreateBinary( xSemaphore );\r
300 \r
301     if( xSemaphore != NULL )\r
302     {\r
303         if( xSemaphoreGive( xSemaphore ) != pdTRUE )\r
304         {\r
305             // We would expect this call to fail because we cannot give\r
306             // a semaphore without first "taking" it!\r
307         }\r
308 \r
309         // Obtain the semaphore - don't block if the semaphore is not\r
310         // immediately available.\r
311         if( xSemaphoreTake( xSemaphore, ( portTickType ) 0 ) )\r
312         {\r
313             // We now have the semaphore and can access the shared resource.\r
314 \r
315             // ...\r
316 \r
317             // We have finished accessing the shared resource so can free the\r
318             // semaphore.\r
319             if( xSemaphoreGive( xSemaphore ) != pdTRUE )\r
320             {\r
321                 // We would not expect this call to fail because we must have\r
322                 // obtained the semaphore to get here.\r
323             }\r
324         }\r
325     }\r
326  }\r
327  </pre>\r
328  * \defgroup xSemaphoreGive xSemaphoreGive\r
329  * \ingroup Semaphores\r
330  */\r
331 #define xSemaphoreGive( xSemaphore )            xQueueGenericSend( ( xQueueHandle ) xSemaphore, NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )\r
332 \r
333 /**\r
334  * semphr. h\r
335  * <pre>xSemaphoreGiveRecursive( xSemaphoreHandle xSemaphore )</pre>\r
336  *\r
337  * <i>Macro</i> to recursively release, or 'give', a mutex type semaphore.\r
338  * The mutex must have previously been created using a call to \r
339  * xSemaphoreCreateRecursiveMutex();\r
340  * \r
341  * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this\r
342  * macro to be available.\r
343  *\r
344  * This macro must not be used on mutexes created using xSemaphoreCreateMutex().\r
345  * \r
346  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex \r
347  * doesn't become available again until the owner has called \r
348  * xSemaphoreGiveRecursive() for each successful 'take' request.  For example, \r
349  * if a task successfully 'takes' the same mutex 5 times then the mutex will \r
350  * not be available to any other task until it has also  'given' the mutex back\r
351  * exactly five times.\r
352  *\r
353  * @param xMutex A handle to the mutex being released, or 'given'.  This is the\r
354  * handle returned by xSemaphoreCreateMutex();\r
355  *\r
356  * @return pdTRUE if the semaphore was given.\r
357  *\r
358  * Example usage:\r
359  <pre>\r
360  xSemaphoreHandle xMutex = NULL;\r
361 \r
362  // A task that creates a mutex.\r
363  void vATask( void * pvParameters )\r
364  {\r
365     // Create the mutex to guard a shared resource.\r
366     xMutex = xSemaphoreCreateRecursiveMutex();\r
367  }\r
368 \r
369  // A task that uses the mutex.\r
370  void vAnotherTask( void * pvParameters )\r
371  {\r
372     // ... Do other things.\r
373 \r
374     if( xMutex != NULL )\r
375     {\r
376         // See if we can obtain the mutex.  If the mutex is not available\r
377         // wait 10 ticks to see if it becomes free.     \r
378         if( xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 ) == pdTRUE )\r
379         {\r
380             // We were able to obtain the mutex and can now access the\r
381             // shared resource.\r
382 \r
383             // ...\r
384             // For some reason due to the nature of the code further calls to \r
385                         // xSemaphoreTakeRecursive() are made on the same mutex.  In real\r
386                         // code these would not be just sequential calls as this would make\r
387                         // no sense.  Instead the calls are likely to be buried inside\r
388                         // a more complex call structure.\r
389             xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 );\r
390             xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 );\r
391 \r
392             // The mutex has now been 'taken' three times, so will not be \r
393                         // available to another task until it has also been given back\r
394                         // three times.  Again it is unlikely that real code would have\r
395                         // these calls sequentially, it would be more likely that the calls\r
396                         // to xSemaphoreGiveRecursive() would be called as a call stack\r
397                         // unwound.  This is just for demonstrative purposes.\r
398             xSemaphoreGiveRecursive( xSemaphore );\r
399                         xSemaphoreGiveRecursive( xSemaphore );\r
400                         xSemaphoreGiveRecursive( xSemaphore );\r
401 \r
402                         // Now the mutex can be taken by other tasks.\r
403         }\r
404         else\r
405         {\r
406             // We could not obtain the mutex and can therefore not access\r
407             // the shared resource safely.\r
408         }\r
409     }\r
410  }\r
411  </pre>\r
412  * \defgroup xSemaphoreGiveRecursive xSemaphoreGiveRecursive\r
413  * \ingroup Semaphores\r
414  */\r
415 #define xSemaphoreGiveRecursive( xMutex )       xQueueGiveMutexRecursive( xMutex )\r
416 \r
417 /* \r
418  * xSemaphoreAltGive() is an alternative version of xSemaphoreGive().\r
419  *\r
420  * The source code that implements the alternative (Alt) API is much \r
421  * simpler      because it executes everything from within a critical section.  \r
422  * This is      the approach taken by many other RTOSes, but FreeRTOS.org has the \r
423  * preferred fully featured API too.  The fully featured API has more \r
424  * complex      code that takes longer to execute, but makes much less use of \r
425  * critical sections.  Therefore the alternative API sacrifices interrupt \r
426  * responsiveness to gain execution speed, whereas the fully featured API\r
427  * sacrifices execution speed to ensure better interrupt responsiveness.\r
428  */\r
429 #define xSemaphoreAltGive( xSemaphore )         xQueueAltGenericSend( ( xQueueHandle ) xSemaphore, NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )\r
430 \r
431 /**\r
432  * semphr. h\r
433  * <pre>\r
434  xSemaphoreGiveFromISR( \r
435                           xSemaphoreHandle xSemaphore, \r
436                           portSHORT sTaskPreviouslyWoken \r
437                       )</pre>\r
438  *\r
439  * <i>Macro</i> to  release a semaphore.  The semaphore must have previously been\r
440  * created with a call to vSemaphoreCreateBinary() or xSemaphoreCreateCounting().\r
441  *\r
442  * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())\r
443  * must not be used with this macro.\r
444  *\r
445  * This macro can be used from an ISR.\r
446  *\r
447  * @param xSemaphore A handle to the semaphore being released.  This is the\r
448  * handle returned when the semaphore was created.\r
449  *\r
450  * @param sTaskPreviouslyWoken This is included so an ISR can make multiple calls\r
451  * to xSemaphoreGiveFromISR () from a single interrupt.  The first call\r
452  * should always pass in pdFALSE.  Subsequent calls should pass in\r
453  * the value returned from the previous call.  See the file serial .c in the\r
454  * PC port for a good example of using xSemaphoreGiveFromISR ().\r
455  *\r
456  * @return pdTRUE if a task was woken by releasing the semaphore.  This is \r
457  * used by the ISR to determine if a context switch may be required following\r
458  * the ISR.\r
459  *\r
460  * Example usage:\r
461  <pre>\r
462  #define LONG_TIME 0xffff\r
463  #define TICKS_TO_WAIT  10\r
464  xSemaphoreHandle xSemaphore = NULL;\r
465 \r
466  // Repetitive task.\r
467  void vATask( void * pvParameters )\r
468  {\r
469     for( ;; )\r
470     {\r
471         // We want this task to run every 10 ticks of a timer.  The semaphore \r
472         // was created before this task was started.\r
473 \r
474         // Block waiting for the semaphore to become available.\r
475         if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )\r
476         {\r
477             // It is time to execute.\r
478 \r
479             // ...\r
480 \r
481             // We have finished our task.  Return to the top of the loop where\r
482             // we will block on the semaphore until it is time to execute \r
483             // again.  Note when using the semaphore for synchronisation with an\r
484                         // ISR in this manner there is no need to 'give' the semaphore back.\r
485         }\r
486     }\r
487  }\r
488 \r
489  // Timer ISR\r
490  void vTimerISR( void * pvParameters )\r
491  {\r
492  static unsigned portCHAR ucLocalTickCount = 0;\r
493  static portBASE_TYPE xTaskWoken;\r
494 \r
495     // A timer tick has occurred.\r
496 \r
497     // ... Do other time functions.\r
498 \r
499     // Is it time for vATask () to run?\r
500         xTaskWoken = pdFALSE;\r
501     ucLocalTickCount++;\r
502     if( ucLocalTickCount >= TICKS_TO_WAIT )\r
503     {\r
504         // Unblock the task by releasing the semaphore.\r
505         xTaskWoken = xSemaphoreGiveFromISR( xSemaphore, xTaskWoken );\r
506 \r
507         // Reset the count so we release the semaphore again in 10 ticks time.\r
508         ucLocalTickCount = 0;\r
509     }\r
510 \r
511     if( xTaskWoken != pdFALSE )\r
512     {\r
513         // We can force a context switch here.  Context switching from an\r
514         // ISR uses port specific syntax.  Check the demo task for your port\r
515         // to find the syntax required.\r
516     }\r
517  }\r
518  </pre>\r
519  * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR\r
520  * \ingroup Semaphores\r
521  */\r
522 #define xSemaphoreGiveFromISR( xSemaphore, xTaskPreviouslyWoken )                       xQueueGenericSendFromISR( ( xQueueHandle ) xSemaphore, NULL, xTaskPreviouslyWoken, queueSEND_TO_BACK )\r
523 \r
524 /**\r
525  * semphr. h\r
526  * <pre>xSemaphoreHandle xSemaphoreCreateMutex( void )</pre>\r
527  *\r
528  * <i>Macro</i> that implements a mutex semaphore by using the existing queue \r
529  * mechanism.\r
530  *\r
531  * Mutexes created using this macro can be accessed using the xSemaphoreTake()\r
532  * and xSemaphoreGive() macros.  The xSemaphoreTakeRecursive() and \r
533  * xSemaphoreGiveRecursive() macros should not be used.\r
534  * \r
535  * This type of semaphore uses a priority inheritance mechanism so a task \r
536  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the \r
537  * semaphore it is no longer required.  \r
538  *\r
539  * Mutex type semaphores cannot be used from within interrupt service routines.  \r
540  *\r
541  * See xSemaphoreCreateBinary() for an alternative implementation that can be \r
542  * used for pure synchronisation (where one task or interrupt always 'gives' the \r
543  * semaphore and another always 'takes' the semaphore) and from within interrupt \r
544  * service routines.\r
545  *\r
546  * @return xSemaphore Handle to the created mutex semaphore.  Should be of type \r
547  *              xSemaphoreHandle.\r
548  *\r
549  * Example usage:\r
550  <pre>\r
551  xSemaphoreHandle xSemaphore;\r
552 \r
553  void vATask( void * pvParameters )\r
554  {\r
555     // Semaphore cannot be used before a call to xSemaphoreCreateMutex().\r
556     // This is a macro so pass the variable in directly.\r
557     xSemaphore = xSemaphoreCreateMutex();\r
558 \r
559     if( xSemaphore != NULL )\r
560     {\r
561         // The semaphore was created successfully.\r
562         // The semaphore can now be used.  \r
563     }\r
564  }\r
565  </pre>\r
566  * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex\r
567  * \ingroup Semaphores\r
568  */\r
569 #define xSemaphoreCreateMutex() xQueueCreateMutex()\r
570 \r
571 \r
572 /**\r
573  * semphr. h\r
574  * <pre>xSemaphoreHandle xSemaphoreCreateRecursiveMutex( void )</pre>\r
575  *\r
576  * <i>Macro</i> that implements a recursive mutex by using the existing queue \r
577  * mechanism.\r
578  *\r
579  * Mutexes created using this macro can be accessed using the \r
580  * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros.  The \r
581  * xSemaphoreTake() and xSemaphoreGive() macros should not be used.\r
582  *\r
583  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex \r
584  * doesn't become available again until the owner has called \r
585  * xSemaphoreGiveRecursive() for each successful 'take' request.  For example, \r
586  * if a task successfully 'takes' the same mutex 5 times then the mutex will \r
587  * not be available to any other task until it has also  'given' the mutex back\r
588  * exactly five times.\r
589  * \r
590  * This type of semaphore uses a priority inheritance mechanism so a task \r
591  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the \r
592  * semaphore it is no longer required.  \r
593  *\r
594  * Mutex type semaphores cannot be used from within interrupt service routines.  \r
595  *\r
596  * See xSemaphoreCreateBinary() for an alternative implementation that can be \r
597  * used for pure synchronisation (where one task or interrupt always 'gives' the \r
598  * semaphore and another always 'takes' the semaphore) and from within interrupt \r
599  * service routines.\r
600  *\r
601  * @return xSemaphore Handle to the created mutex semaphore.  Should be of type \r
602  *              xSemaphoreHandle.\r
603  *\r
604  * Example usage:\r
605  <pre>\r
606  xSemaphoreHandle xSemaphore;\r
607 \r
608  void vATask( void * pvParameters )\r
609  {\r
610     // Semaphore cannot be used before a call to xSemaphoreCreateMutex().\r
611     // This is a macro so pass the variable in directly.\r
612     xSemaphore = xSemaphoreCreateRecursiveMutex();\r
613 \r
614     if( xSemaphore != NULL )\r
615     {\r
616         // The semaphore was created successfully.\r
617         // The semaphore can now be used.  \r
618     }\r
619  }\r
620  </pre>\r
621  * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex\r
622  * \ingroup Semaphores\r
623  */\r
624 #define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex()\r
625 \r
626 /**\r
627  * semphr. h\r
628  * <pre>xSemaphoreHandle xSemaphoreCreateCounting( unsigned portBASE_TYPE uxMaxCount, unsigned portBASE_TYPE uxInitialCount )</pre>\r
629  *\r
630  * <i>Macro</i> that creates a counting semaphore by using the existing \r
631  * queue mechanism.  \r
632  *\r
633  * Counting semaphores are typically used for two things:\r
634  *\r
635  * 1) Counting events.  \r
636  *\r
637  *    In this usage scenario an event handler will 'give' a semaphore each time\r
638  *    an event occurs (incrementing the semaphore count value), and a handler \r
639  *    task will 'take' a semaphore each time it processes an event \r
640  *    (decrementing the semaphore count value).  The count value is therefore \r
641  *    the difference between the number of events that have occurred and the \r
642  *    number that have been processed.  In this case it is desirable for the \r
643  *    initial count value to be zero.\r
644  *\r
645  * 2) Resource management.\r
646  *\r
647  *    In this usage scenario the count value indicates the number of resources\r
648  *    available.  To obtain control of a resource a task must first obtain a \r
649  *    semaphore - decrementing the semaphore count value.  When the count value\r
650  *    reaches zero there are no free resources.  When a task finishes with the\r
651  *    resource it 'gives' the semaphore back - incrementing the semaphore count\r
652  *    value.  In this case it is desirable for the initial count value to be\r
653  *    equal to the maximum count value, indicating that all resources are free.\r
654  *\r
655  * @param uxMaxCount The maximum count value that can be reached.  When the \r
656  *        semaphore reaches this value it can no longer be 'given'.\r
657  *\r
658  * @param uxInitialCount The count value assigned to the semaphore when it is\r
659  *        created.\r
660  *\r
661  * @return Handle to the created semaphore.  Null if the semaphore could not be\r
662  *         created.\r
663  * \r
664  * Example usage:\r
665  <pre>\r
666  xSemaphoreHandle xSemaphore;\r
667 \r
668  void vATask( void * pvParameters )\r
669  {\r
670  xSemaphoreHandle xSemaphore = NULL;\r
671 \r
672     // Semaphore cannot be used before a call to xSemaphoreCreateCounting().\r
673     // The max value to which the semaphore can count should be 10, and the\r
674     // initial value assigned to the count should be 0.\r
675     xSemaphore = xSemaphoreCreateCounting( 10, 0 );\r
676 \r
677     if( xSemaphore != NULL )\r
678     {\r
679         // The semaphore was created successfully.\r
680         // The semaphore can now be used.  \r
681     }\r
682  }\r
683  </pre>\r
684  * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting\r
685  * \ingroup Semaphores\r
686  */\r
687 #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( uxMaxCount, uxInitialCount )\r
688 \r
689 \r
690 #endif /* SEMAPHORE_H */\r
691 \r
692 \r