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