]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/include/semphr.h
f6797038f0e480091c7f54d58b8a0515d2ca2531
[freertos] / FreeRTOS / Source / include / semphr.h
1 /*\r
2     FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 #ifndef SEMAPHORE_H\r
66 #define SEMAPHORE_H\r
67 \r
68 #ifndef INC_FREERTOS_H\r
69         #error "include FreeRTOS.h" must appear in source files before "include semphr.h"\r
70 #endif\r
71 \r
72 #include "queue.h"\r
73 \r
74 typedef xQueueHandle xSemaphoreHandle;\r
75 \r
76 #define semBINARY_SEMAPHORE_QUEUE_LENGTH        ( ( unsigned char ) 1U )\r
77 #define semSEMAPHORE_QUEUE_ITEM_LENGTH          ( ( unsigned char ) 0U )\r
78 #define semGIVE_BLOCK_TIME                                      ( ( portTickType ) 0U )\r
79 \r
80 \r
81 /**\r
82  * semphr. h\r
83  * <pre>vSemaphoreCreateBinary( xSemaphoreHandle xSemaphore )</pre>\r
84  *\r
85  * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.\r
86  * The queue length is 1 as this is a binary semaphore.  The data size is 0\r
87  * as we don't want to actually store any data - we just want to know if the\r
88  * queue is empty or full.\r
89  *\r
90  * This type of semaphore can be used for pure synchronisation between tasks or\r
91  * between an interrupt and a task.  The semaphore need not be given back once\r
92  * obtained, so one task/interrupt can continuously 'give' the semaphore while\r
93  * another continuously 'takes' the semaphore.  For this reason this type of\r
94  * semaphore does not use a priority inheritance mechanism.  For an alternative\r
95  * that does use priority inheritance see xSemaphoreCreateMutex().\r
96  *\r
97  * @param xSemaphore Handle to the created semaphore.  Should be of type xSemaphoreHandle.\r
98  *\r
99  * Example usage:\r
100  <pre>\r
101  xSemaphoreHandle xSemaphore;\r
102 \r
103  void vATask( void * pvParameters )\r
104  {\r
105     // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().\r
106     // This is a macro so pass the variable in directly.\r
107     vSemaphoreCreateBinary( xSemaphore );\r
108 \r
109     if( xSemaphore != NULL )\r
110     {\r
111         // The semaphore was created successfully.\r
112         // The semaphore can now be used.  \r
113     }\r
114  }\r
115  </pre>\r
116  * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary\r
117  * \ingroup Semaphores\r
118  */\r
119 #define vSemaphoreCreateBinary( xSemaphore )                                                                                                                                                                                                    \\r
120         {                                                                                                                                                                                                                                                                                       \\r
121                 ( xSemaphore ) = xQueueGenericCreate( ( unsigned portBASE_TYPE ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE ); \\r
122                 if( ( xSemaphore ) != NULL )                                                                                                                                                                                                                    \\r
123                 {                                                                                                                                                                                                                                                                               \\r
124                         ( void ) xSemaphoreGive( ( xSemaphore ) );                                                                                                                                                                                                      \\r
125                 }                                                                                                                                                                                                                                                                               \\r
126         }\r
127 \r
128 /**\r
129  * semphr. h\r
130  * <pre>xSemaphoreTake( \r
131  *                   xSemaphoreHandle xSemaphore, \r
132  *                   portTickType xBlockTime \r
133  *               )</pre>\r
134  *\r
135  * <i>Macro</i> to obtain a semaphore.  The semaphore must have previously been\r
136  * created with a call to vSemaphoreCreateBinary(), xSemaphoreCreateMutex() or\r
137  * xSemaphoreCreateCounting().\r
138  *\r
139  * @param xSemaphore A handle to the semaphore being taken - obtained when\r
140  * the semaphore was created.\r
141  *\r
142  * @param xBlockTime The time in ticks to wait for the semaphore to become\r
143  * available.  The macro portTICK_RATE_MS can be used to convert this to a\r
144  * real time.  A block time of zero can be used to poll the semaphore.  A block\r
145  * time of portMAX_DELAY can be used to block indefinitely (provided\r
146  * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h).\r
147  *\r
148  * @return pdTRUE if the semaphore was obtained.  pdFALSE\r
149  * if xBlockTime expired without the semaphore becoming available.\r
150  *\r
151  * Example usage:\r
152  <pre>\r
153  xSemaphoreHandle xSemaphore = NULL;\r
154 \r
155  // A task that creates a semaphore.\r
156  void vATask( void * pvParameters )\r
157  {\r
158     // Create the semaphore to guard a shared resource.\r
159     vSemaphoreCreateBinary( xSemaphore );\r
160  }\r
161 \r
162  // A task that uses the semaphore.\r
163  void vAnotherTask( void * pvParameters )\r
164  {\r
165     // ... Do other things.\r
166 \r
167     if( xSemaphore != NULL )\r
168     {\r
169         // See if we can obtain the semaphore.  If the semaphore is not available\r
170         // wait 10 ticks to see if it becomes free.     \r
171         if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE )\r
172         {\r
173             // We were able to obtain the semaphore and can now access the\r
174             // shared resource.\r
175 \r
176             // ...\r
177 \r
178             // We have finished accessing the shared resource.  Release the \r
179             // semaphore.\r
180             xSemaphoreGive( xSemaphore );\r
181         }\r
182         else\r
183         {\r
184             // We could not obtain the semaphore and can therefore not access\r
185             // the shared resource safely.\r
186         }\r
187     }\r
188  }\r
189  </pre>\r
190  * \defgroup xSemaphoreTake xSemaphoreTake\r
191  * \ingroup Semaphores\r
192  */\r
193 #define xSemaphoreTake( xSemaphore, xBlockTime )                xQueueGenericReceive( ( xQueueHandle ) ( xSemaphore ), NULL, ( xBlockTime ), pdFALSE )\r
194 \r
195 /**\r
196  * semphr. h\r
197  * xSemaphoreTakeRecursive( \r
198  *                          xSemaphoreHandle xMutex, \r
199  *                          portTickType xBlockTime \r
200  *                        )\r
201  *\r
202  * <i>Macro</i> to recursively obtain, or 'take', a mutex type semaphore.  \r
203  * The mutex must have previously been created using a call to \r
204  * xSemaphoreCreateRecursiveMutex();\r
205  * \r
206  * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this\r
207  * macro to be available.\r
208  * \r
209  * This macro must not be used on mutexes created using xSemaphoreCreateMutex().\r
210  *\r
211  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex \r
212  * doesn't become available again until the owner has called \r
213  * xSemaphoreGiveRecursive() for each successful 'take' request.  For example, \r
214  * if a task successfully 'takes' the same mutex 5 times then the mutex will \r
215  * not be available to any other task until it has also  'given' the mutex back\r
216  * exactly five times.\r
217  *\r
218  * @param xMutex A handle to the mutex being obtained.  This is the\r
219  * handle returned by xSemaphoreCreateRecursiveMutex();\r
220  *\r
221  * @param xBlockTime The time in ticks to wait for the semaphore to become\r
222  * available.  The macro portTICK_RATE_MS can be used to convert this to a\r
223  * real time.  A block time of zero can be used to poll the semaphore.  If\r
224  * the task already owns the semaphore then xSemaphoreTakeRecursive() will\r
225  * return immediately no matter what the value of xBlockTime. \r
226  *\r
227  * @return pdTRUE if the semaphore was obtained.  pdFALSE if xBlockTime\r
228  * expired without the semaphore becoming available.\r
229  *\r
230  * Example usage:\r
231  <pre>\r
232  xSemaphoreHandle xMutex = NULL;\r
233 \r
234  // A task that creates a mutex.\r
235  void vATask( void * pvParameters )\r
236  {\r
237     // Create the mutex to guard a shared resource.\r
238     xMutex = xSemaphoreCreateRecursiveMutex();\r
239  }\r
240 \r
241  // A task that uses the mutex.\r
242  void vAnotherTask( void * pvParameters )\r
243  {\r
244     // ... Do other things.\r
245 \r
246     if( xMutex != NULL )\r
247     {\r
248         // See if we can obtain the mutex.  If the mutex is not available\r
249         // wait 10 ticks to see if it becomes free.     \r
250         if( xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 ) == pdTRUE )\r
251         {\r
252             // We were able to obtain the mutex and can now access the\r
253             // shared resource.\r
254 \r
255             // ...\r
256             // For some reason due to the nature of the code further calls to \r
257                         // xSemaphoreTakeRecursive() are made on the same mutex.  In real\r
258                         // code these would not be just sequential calls as this would make\r
259                         // no sense.  Instead the calls are likely to be buried inside\r
260                         // a more complex call structure.\r
261             xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 );\r
262             xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 );\r
263 \r
264             // The mutex has now been 'taken' three times, so will not be \r
265                         // available to another task until it has also been given back\r
266                         // three times.  Again it is unlikely that real code would have\r
267                         // these calls sequentially, but instead buried in a more complex\r
268                         // call structure.  This is just for illustrative purposes.\r
269             xSemaphoreGiveRecursive( xMutex );\r
270                         xSemaphoreGiveRecursive( xMutex );\r
271                         xSemaphoreGiveRecursive( xMutex );\r
272 \r
273                         // Now the mutex can be taken by other tasks.\r
274         }\r
275         else\r
276         {\r
277             // We could not obtain the mutex and can therefore not access\r
278             // the shared resource safely.\r
279         }\r
280     }\r
281  }\r
282  </pre>\r
283  * \defgroup xSemaphoreTakeRecursive xSemaphoreTakeRecursive\r
284  * \ingroup Semaphores\r
285  */\r
286 #define xSemaphoreTakeRecursive( xMutex, xBlockTime )   xQueueTakeMutexRecursive( ( xMutex ), ( xBlockTime ) )\r
287 \r
288 \r
289 /* \r
290  * xSemaphoreAltTake() is an alternative version of xSemaphoreTake().\r
291  *\r
292  * The source code that implements the alternative (Alt) API is much \r
293  * simpler      because it executes everything from within a critical section.  \r
294  * This is      the approach taken by many other RTOSes, but FreeRTOS.org has the \r
295  * preferred fully featured API too.  The fully featured API has more \r
296  * complex      code that takes longer to execute, but makes much less use of \r
297  * critical sections.  Therefore the alternative API sacrifices interrupt \r
298  * responsiveness to gain execution speed, whereas the fully featured API\r
299  * sacrifices execution speed to ensure better interrupt responsiveness.\r
300  */\r
301 #define xSemaphoreAltTake( xSemaphore, xBlockTime )             xQueueAltGenericReceive( ( xQueueHandle ) ( xSemaphore ), NULL, ( xBlockTime ), pdFALSE )\r
302 \r
303 /**\r
304  * semphr. h\r
305  * <pre>xSemaphoreGive( xSemaphoreHandle xSemaphore )</pre>\r
306  *\r
307  * <i>Macro</i> to release a semaphore.  The semaphore must have previously been\r
308  * created with a call to vSemaphoreCreateBinary(), xSemaphoreCreateMutex() or\r
309  * xSemaphoreCreateCounting(). and obtained using sSemaphoreTake().\r
310  *\r
311  * This macro must not be used from an ISR.  See xSemaphoreGiveFromISR () for\r
312  * an alternative which can be used from an ISR.\r
313  *\r
314  * This macro must also not be used on semaphores created using \r
315  * xSemaphoreCreateRecursiveMutex().\r
316  *\r
317  * @param xSemaphore A handle to the semaphore being released.  This is the\r
318  * handle returned when the semaphore was created.\r
319  *\r
320  * @return pdTRUE if the semaphore was released.  pdFALSE if an error occurred.\r
321  * Semaphores are implemented using queues.  An error can occur if there is\r
322  * no space on the queue to post a message - indicating that the \r
323  * semaphore was not first obtained correctly.\r
324  *\r
325  * Example usage:\r
326  <pre>\r
327  xSemaphoreHandle xSemaphore = NULL;\r
328 \r
329  void vATask( void * pvParameters )\r
330  {\r
331     // Create the semaphore to guard a shared resource.\r
332     vSemaphoreCreateBinary( xSemaphore );\r
333 \r
334     if( xSemaphore != NULL )\r
335     {\r
336         if( xSemaphoreGive( xSemaphore ) != pdTRUE )\r
337         {\r
338             // We would expect this call to fail because we cannot give\r
339             // a semaphore without first "taking" it!\r
340         }\r
341 \r
342         // Obtain the semaphore - don't block if the semaphore is not\r
343         // immediately available.\r
344         if( xSemaphoreTake( xSemaphore, ( portTickType ) 0 ) )\r
345         {\r
346             // We now have the semaphore and can access the shared resource.\r
347 \r
348             // ...\r
349 \r
350             // We have finished accessing the shared resource so can free the\r
351             // semaphore.\r
352             if( xSemaphoreGive( xSemaphore ) != pdTRUE )\r
353             {\r
354                 // We would not expect this call to fail because we must have\r
355                 // obtained the semaphore to get here.\r
356             }\r
357         }\r
358     }\r
359  }\r
360  </pre>\r
361  * \defgroup xSemaphoreGive xSemaphoreGive\r
362  * \ingroup Semaphores\r
363  */\r
364 #define xSemaphoreGive( xSemaphore )            xQueueGenericSend( ( xQueueHandle ) ( xSemaphore ), NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )\r
365 \r
366 /**\r
367  * semphr. h\r
368  * <pre>xSemaphoreGiveRecursive( xSemaphoreHandle xMutex )</pre>\r
369  *\r
370  * <i>Macro</i> to recursively release, or 'give', a mutex type semaphore.\r
371  * The mutex must have previously been created using a call to \r
372  * xSemaphoreCreateRecursiveMutex();\r
373  * \r
374  * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this\r
375  * macro to be available.\r
376  *\r
377  * This macro must not be used on mutexes created using xSemaphoreCreateMutex().\r
378  * \r
379  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex \r
380  * doesn't become available again until the owner has called \r
381  * xSemaphoreGiveRecursive() for each successful 'take' request.  For example, \r
382  * if a task successfully 'takes' the same mutex 5 times then the mutex will \r
383  * not be available to any other task until it has also  'given' the mutex back\r
384  * exactly five times.\r
385  *\r
386  * @param xMutex A handle to the mutex being released, or 'given'.  This is the\r
387  * handle returned by xSemaphoreCreateMutex();\r
388  *\r
389  * @return pdTRUE if the semaphore was given.\r
390  *\r
391  * Example usage:\r
392  <pre>\r
393  xSemaphoreHandle xMutex = NULL;\r
394 \r
395  // A task that creates a mutex.\r
396  void vATask( void * pvParameters )\r
397  {\r
398     // Create the mutex to guard a shared resource.\r
399     xMutex = xSemaphoreCreateRecursiveMutex();\r
400  }\r
401 \r
402  // A task that uses the mutex.\r
403  void vAnotherTask( void * pvParameters )\r
404  {\r
405     // ... Do other things.\r
406 \r
407     if( xMutex != NULL )\r
408     {\r
409         // See if we can obtain the mutex.  If the mutex is not available\r
410         // wait 10 ticks to see if it becomes free.     \r
411         if( xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 ) == pdTRUE )\r
412         {\r
413             // We were able to obtain the mutex and can now access the\r
414             // shared resource.\r
415 \r
416             // ...\r
417             // For some reason due to the nature of the code further calls to \r
418                         // xSemaphoreTakeRecursive() are made on the same mutex.  In real\r
419                         // code these would not be just sequential calls as this would make\r
420                         // no sense.  Instead the calls are likely to be buried inside\r
421                         // a more complex call structure.\r
422             xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 );\r
423             xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 );\r
424 \r
425             // The mutex has now been 'taken' three times, so will not be \r
426                         // available to another task until it has also been given back\r
427                         // three times.  Again it is unlikely that real code would have\r
428                         // these calls sequentially, it would be more likely that the calls\r
429                         // to xSemaphoreGiveRecursive() would be called as a call stack\r
430                         // unwound.  This is just for demonstrative purposes.\r
431             xSemaphoreGiveRecursive( xMutex );\r
432                         xSemaphoreGiveRecursive( xMutex );\r
433                         xSemaphoreGiveRecursive( xMutex );\r
434 \r
435                         // Now the mutex can be taken by other tasks.\r
436         }\r
437         else\r
438         {\r
439             // We could not obtain the mutex and can therefore not access\r
440             // the shared resource safely.\r
441         }\r
442     }\r
443  }\r
444  </pre>\r
445  * \defgroup xSemaphoreGiveRecursive xSemaphoreGiveRecursive\r
446  * \ingroup Semaphores\r
447  */\r
448 #define xSemaphoreGiveRecursive( xMutex )       xQueueGiveMutexRecursive( ( xMutex ) )\r
449 \r
450 /* \r
451  * xSemaphoreAltGive() is an alternative version of xSemaphoreGive().\r
452  *\r
453  * The source code that implements the alternative (Alt) API is much \r
454  * simpler      because it executes everything from within a critical section.  \r
455  * This is      the approach taken by many other RTOSes, but FreeRTOS.org has the \r
456  * preferred fully featured API too.  The fully featured API has more \r
457  * complex      code that takes longer to execute, but makes much less use of \r
458  * critical sections.  Therefore the alternative API sacrifices interrupt \r
459  * responsiveness to gain execution speed, whereas the fully featured API\r
460  * sacrifices execution speed to ensure better interrupt responsiveness.\r
461  */\r
462 #define xSemaphoreAltGive( xSemaphore )         xQueueAltGenericSend( ( xQueueHandle ) ( xSemaphore ), NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )\r
463 \r
464 /**\r
465  * semphr. h\r
466  * <pre>\r
467  xSemaphoreGiveFromISR( \r
468                           xSemaphoreHandle xSemaphore, \r
469                           signed portBASE_TYPE *pxHigherPriorityTaskWoken\r
470                       )</pre>\r
471  *\r
472  * <i>Macro</i> to  release a semaphore.  The semaphore must have previously been\r
473  * created with a call to vSemaphoreCreateBinary() or xSemaphoreCreateCounting().\r
474  *\r
475  * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())\r
476  * must not be used with this macro.\r
477  *\r
478  * This macro can be used from an ISR.\r
479  *\r
480  * @param xSemaphore A handle to the semaphore being released.  This is the\r
481  * handle returned when the semaphore was created.\r
482  *\r
483  * @param pxHigherPriorityTaskWoken xSemaphoreGiveFromISR() will set\r
484  * *pxHigherPriorityTaskWoken to pdTRUE if giving the semaphore caused a task\r
485  * to unblock, and the unblocked task has a priority higher than the currently\r
486  * running task.  If xSemaphoreGiveFromISR() sets this value to pdTRUE then\r
487  * a context switch should be requested before the interrupt is exited.\r
488  *\r
489  * @return pdTRUE if the semaphore was successfully given, otherwise errQUEUE_FULL.\r
490  *\r
491  * Example usage:\r
492  <pre>\r
493  \#define LONG_TIME 0xffff\r
494  \#define TICKS_TO_WAIT 10\r
495  xSemaphoreHandle xSemaphore = NULL;\r
496 \r
497  // Repetitive task.\r
498  void vATask( void * pvParameters )\r
499  {\r
500     for( ;; )\r
501     {\r
502         // We want this task to run every 10 ticks of a timer.  The semaphore \r
503         // was created before this task was started.\r
504 \r
505         // Block waiting for the semaphore to become available.\r
506         if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )\r
507         {\r
508             // It is time to execute.\r
509 \r
510             // ...\r
511 \r
512             // We have finished our task.  Return to the top of the loop where\r
513             // we will block on the semaphore until it is time to execute \r
514             // again.  Note when using the semaphore for synchronisation with an\r
515                         // ISR in this manner there is no need to 'give' the semaphore back.\r
516         }\r
517     }\r
518  }\r
519 \r
520  // Timer ISR\r
521  void vTimerISR( void * pvParameters )\r
522  {\r
523  static unsigned char ucLocalTickCount = 0;\r
524  static signed portBASE_TYPE xHigherPriorityTaskWoken;\r
525 \r
526     // A timer tick has occurred.\r
527 \r
528     // ... Do other time functions.\r
529 \r
530     // Is it time for vATask () to run?\r
531         xHigherPriorityTaskWoken = pdFALSE;\r
532     ucLocalTickCount++;\r
533     if( ucLocalTickCount >= TICKS_TO_WAIT )\r
534     {\r
535         // Unblock the task by releasing the semaphore.\r
536         xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );\r
537 \r
538         // Reset the count so we release the semaphore again in 10 ticks time.\r
539         ucLocalTickCount = 0;\r
540     }\r
541 \r
542     if( xHigherPriorityTaskWoken != pdFALSE )\r
543     {\r
544         // We can force a context switch here.  Context switching from an\r
545         // ISR uses port specific syntax.  Check the demo task for your port\r
546         // to find the syntax required.\r
547     }\r
548  }\r
549  </pre>\r
550  * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR\r
551  * \ingroup Semaphores\r
552  */\r
553 #define xSemaphoreGiveFromISR( xSemaphore, pxHigherPriorityTaskWoken )                  xQueueGenericSendFromISR( ( xQueueHandle ) ( xSemaphore ), NULL, ( pxHigherPriorityTaskWoken ), queueSEND_TO_BACK )\r
554 \r
555 /**\r
556  * semphr. h\r
557  * <pre>\r
558  xSemaphoreTakeFromISR( \r
559                           xSemaphoreHandle xSemaphore, \r
560                           signed portBASE_TYPE *pxHigherPriorityTaskWoken\r
561                       )</pre>\r
562  *\r
563  * <i>Macro</i> to  take a semaphore from an ISR.  The semaphore must have \r
564  * previously been created with a call to vSemaphoreCreateBinary() or \r
565  * xSemaphoreCreateCounting().\r
566  *\r
567  * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())\r
568  * must not be used with this macro.\r
569  *\r
570  * This macro can be used from an ISR, however taking a semaphore from an ISR\r
571  * is not a common operation.  It is likely to only be useful when taking a\r
572  * counting semaphore when an interrupt is obtaining an object from a resource\r
573  * pool (when the semaphore count indicates the number of resources available).\r
574  *\r
575  * @param xSemaphore A handle to the semaphore being taken.  This is the\r
576  * handle returned when the semaphore was created.\r
577  *\r
578  * @param pxHigherPriorityTaskWoken xSemaphoreTakeFromISR() will set\r
579  * *pxHigherPriorityTaskWoken to pdTRUE if taking the semaphore caused a task\r
580  * to unblock, and the unblocked task has a priority higher than the currently\r
581  * running task.  If xSemaphoreTakeFromISR() sets this value to pdTRUE then\r
582  * a context switch should be requested before the interrupt is exited.\r
583  *\r
584  * @return pdTRUE if the semaphore was successfully taken, otherwise \r
585  * pdFALSE\r
586  */\r
587 #define xSemaphoreTakeFromISR( xSemaphore, pxHigherPriorityTaskWoken )                  xQueueReceiveFromISR( ( xQueueHandle ) ( xSemaphore ), NULL, ( pxHigherPriorityTaskWoken ) )\r
588 \r
589 /**\r
590  * semphr. h\r
591  * <pre>xSemaphoreHandle xSemaphoreCreateMutex( void )</pre>\r
592  *\r
593  * <i>Macro</i> that implements a mutex semaphore by using the existing queue \r
594  * mechanism.\r
595  *\r
596  * Mutexes created using this macro can be accessed using the xSemaphoreTake()\r
597  * and xSemaphoreGive() macros.  The xSemaphoreTakeRecursive() and \r
598  * xSemaphoreGiveRecursive() macros should not be used.\r
599  * \r
600  * This type of semaphore uses a priority inheritance mechanism so a task \r
601  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the \r
602  * semaphore it is no longer required.  \r
603  *\r
604  * Mutex type semaphores cannot be used from within interrupt service routines.  \r
605  *\r
606  * See vSemaphoreCreateBinary() for an alternative implementation that can be \r
607  * used for pure synchronisation (where one task or interrupt always 'gives' the \r
608  * semaphore and another always 'takes' the semaphore) and from within interrupt \r
609  * service routines.\r
610  *\r
611  * @return xSemaphore Handle to the created mutex semaphore.  Should be of type \r
612  *              xSemaphoreHandle.\r
613  *\r
614  * Example usage:\r
615  <pre>\r
616  xSemaphoreHandle xSemaphore;\r
617 \r
618  void vATask( void * pvParameters )\r
619  {\r
620     // Semaphore cannot be used before a call to xSemaphoreCreateMutex().\r
621     // This is a macro so pass the variable in directly.\r
622     xSemaphore = xSemaphoreCreateMutex();\r
623 \r
624     if( xSemaphore != NULL )\r
625     {\r
626         // The semaphore was created successfully.\r
627         // The semaphore can now be used.  \r
628     }\r
629  }\r
630  </pre>\r
631  * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex\r
632  * \ingroup Semaphores\r
633  */\r
634 #define xSemaphoreCreateMutex() xQueueCreateMutex( queueQUEUE_TYPE_MUTEX )\r
635 \r
636 \r
637 /**\r
638  * semphr. h\r
639  * <pre>xSemaphoreHandle xSemaphoreCreateRecursiveMutex( void )</pre>\r
640  *\r
641  * <i>Macro</i> that implements a recursive mutex by using the existing queue \r
642  * mechanism.\r
643  *\r
644  * Mutexes created using this macro can be accessed using the \r
645  * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros.  The \r
646  * xSemaphoreTake() and xSemaphoreGive() macros should not be used.\r
647  *\r
648  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex \r
649  * doesn't become available again until the owner has called \r
650  * xSemaphoreGiveRecursive() for each successful 'take' request.  For example, \r
651  * if a task successfully 'takes' the same mutex 5 times then the mutex will \r
652  * not be available to any other task until it has also  'given' the mutex back\r
653  * exactly five times.\r
654  * \r
655  * This type of semaphore uses a priority inheritance mechanism so a task \r
656  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the \r
657  * semaphore it is no longer required.  \r
658  *\r
659  * Mutex type semaphores cannot be used from within interrupt service routines.  \r
660  *\r
661  * See vSemaphoreCreateBinary() for an alternative implementation that can be \r
662  * used for pure synchronisation (where one task or interrupt always 'gives' the \r
663  * semaphore and another always 'takes' the semaphore) and from within interrupt \r
664  * service routines.\r
665  *\r
666  * @return xSemaphore Handle to the created mutex semaphore.  Should be of type \r
667  *              xSemaphoreHandle.\r
668  *\r
669  * Example usage:\r
670  <pre>\r
671  xSemaphoreHandle xSemaphore;\r
672 \r
673  void vATask( void * pvParameters )\r
674  {\r
675     // Semaphore cannot be used before a call to xSemaphoreCreateMutex().\r
676     // This is a macro so pass the variable in directly.\r
677     xSemaphore = xSemaphoreCreateRecursiveMutex();\r
678 \r
679     if( xSemaphore != NULL )\r
680     {\r
681         // The semaphore was created successfully.\r
682         // The semaphore can now be used.  \r
683     }\r
684  }\r
685  </pre>\r
686  * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex\r
687  * \ingroup Semaphores\r
688  */\r
689 #define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex( queueQUEUE_TYPE_RECURSIVE_MUTEX )\r
690 \r
691 /**\r
692  * semphr. h\r
693  * <pre>xSemaphoreHandle xSemaphoreCreateCounting( unsigned portBASE_TYPE uxMaxCount, unsigned portBASE_TYPE uxInitialCount )</pre>\r
694  *\r
695  * <i>Macro</i> that creates a counting semaphore by using the existing \r
696  * queue mechanism.  \r
697  *\r
698  * Counting semaphores are typically used for two things:\r
699  *\r
700  * 1) Counting events.  \r
701  *\r
702  *    In this usage scenario an event handler will 'give' a semaphore each time\r
703  *    an event occurs (incrementing the semaphore count value), and a handler \r
704  *    task will 'take' a semaphore each time it processes an event \r
705  *    (decrementing the semaphore count value).  The count value is therefore \r
706  *    the difference between the number of events that have occurred and the \r
707  *    number that have been processed.  In this case it is desirable for the \r
708  *    initial count value to be zero.\r
709  *\r
710  * 2) Resource management.\r
711  *\r
712  *    In this usage scenario the count value indicates the number of resources\r
713  *    available.  To obtain control of a resource a task must first obtain a \r
714  *    semaphore - decrementing the semaphore count value.  When the count value\r
715  *    reaches zero there are no free resources.  When a task finishes with the\r
716  *    resource it 'gives' the semaphore back - incrementing the semaphore count\r
717  *    value.  In this case it is desirable for the initial count value to be\r
718  *    equal to the maximum count value, indicating that all resources are free.\r
719  *\r
720  * @param uxMaxCount The maximum count value that can be reached.  When the \r
721  *        semaphore reaches this value it can no longer be 'given'.\r
722  *\r
723  * @param uxInitialCount The count value assigned to the semaphore when it is\r
724  *        created.\r
725  *\r
726  * @return Handle to the created semaphore.  Null if the semaphore could not be\r
727  *         created.\r
728  * \r
729  * Example usage:\r
730  <pre>\r
731  xSemaphoreHandle xSemaphore;\r
732 \r
733  void vATask( void * pvParameters )\r
734  {\r
735  xSemaphoreHandle xSemaphore = NULL;\r
736 \r
737     // Semaphore cannot be used before a call to xSemaphoreCreateCounting().\r
738     // The max value to which the semaphore can count should be 10, and the\r
739     // initial value assigned to the count should be 0.\r
740     xSemaphore = xSemaphoreCreateCounting( 10, 0 );\r
741 \r
742     if( xSemaphore != NULL )\r
743     {\r
744         // The semaphore was created successfully.\r
745         // The semaphore can now be used.  \r
746     }\r
747  }\r
748  </pre>\r
749  * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting\r
750  * \ingroup Semaphores\r
751  */\r
752 #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ) )\r
753 \r
754 /**\r
755  * semphr. h\r
756  * <pre>void vSemaphoreDelete( xSemaphoreHandle xSemaphore );</pre>\r
757  *\r
758  * Delete a semaphore.  This function must be used with care.  For example,\r
759  * do not delete a mutex type semaphore if the mutex is held by a task.\r
760  *\r
761  * @param xSemaphore A handle to the semaphore to be deleted.\r
762  *\r
763  * \defgroup vSemaphoreDelete vSemaphoreDelete\r
764  * \ingroup Semaphores\r
765  */\r
766 #define vSemaphoreDelete( xSemaphore ) vQueueDelete( ( xQueueHandle ) ( xSemaphore ) )\r
767 \r
768 /**\r
769  * semphr.h\r
770  * <pre>xTaskHandle xSemaphoreGetMutexHolder( xSemaphoreHandle xMutex );</pre>\r
771  *\r
772  * If xMutex is indeed a mutex type semaphore, return the current mutex holder.\r
773  * If xMutex is not a mutex type semaphore, or the mutex is available (not held\r
774  * by a task), return NULL.\r
775  *\r
776  * Note: This Is is a good way of determining if the calling task is the mutex \r
777  * holder, but not a good way of determining the identity of the mutex holder as\r
778  * the holder may change between the function exiting and the returned value\r
779  * being tested.\r
780  */\r
781 #define xSemaphoreGetMutexHolder( xSemaphore ) xQueueGetMutexHolder( ( xSemaphore ) )\r
782 \r
783 #endif /* SEMAPHORE_H */\r
784 \r
785 \r