2 FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
10 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
17 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
19 A special exception to the GPL is included to allow you to distribute a
\r
20 combined work that includes FreeRTOS.org without being obliged to provide
\r
21 the source code for any proprietary components. See the licensing section
\r
22 of http://www.FreeRTOS.org for full details.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
52 #ifndef INC_FREERTOS_H
\r
53 #error "#include FreeRTOS.h" must appear in source files before "#include semphr.h"
\r
61 typedef xQueueHandle xSemaphoreHandle;
\r
63 #define semBINARY_SEMAPHORE_QUEUE_LENGTH ( ( unsigned portCHAR ) 1 )
\r
64 #define semSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portCHAR ) 0 )
\r
65 #define semGIVE_BLOCK_TIME ( ( portTickType ) 0 )
\r
70 * <pre>vSemaphoreCreateBinary( xSemaphoreHandle xSemaphore )</pre>
\r
72 * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.
\r
73 * The queue length is 1 as this is a binary semaphore. The data size is 0
\r
74 * as we don't want to actually store any data - we just want to know if the
\r
75 * queue is empty or full.
\r
77 * This type of semaphore can be used for pure synchronisation between tasks or
\r
78 * between an interrupt and a task. The semaphore need not be given back once
\r
79 * obtained, so one task/interrupt can continuously 'give' the semaphore while
\r
80 * another continuously 'takes' the semaphore. For this reason this type of
\r
81 * semaphore does not use a priority inheritance mechanism. For an alternative
\r
82 * that does use priority inheritance see xSemaphoreCreateMutex().
\r
84 * @param xSemaphore Handle to the created semaphore. Should be of type xSemaphoreHandle.
\r
88 xSemaphoreHandle xSemaphore;
\r
90 void vATask( void * pvParameters )
\r
92 // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().
\r
93 // This is a macro so pass the variable in directly.
\r
94 vSemaphoreCreateBinary( xSemaphore );
\r
96 if( xSemaphore != NULL )
\r
98 // The semaphore was created successfully.
\r
99 // The semaphore can now be used.
\r
103 * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary
\r
104 * \ingroup Semaphores
\r
106 #define vSemaphoreCreateBinary( xSemaphore ) { \
\r
107 xSemaphore = xQueueCreate( ( unsigned portBASE_TYPE ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH ); \
\r
108 if( xSemaphore != NULL ) \
\r
110 xSemaphoreGive( xSemaphore ); \
\r
117 * xSemaphoreHandle xSemaphore,
\r
118 * portTickType xBlockTime
\r
121 * <i>Macro</i> to obtain a semaphore. The semaphore must have previously been
\r
122 * created with a call to vSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
\r
123 * xSemaphoreCreateCounting().
\r
125 * @param xSemaphore A handle to the semaphore being taken - obtained when
\r
126 * the semaphore was created.
\r
128 * @param xBlockTime The time in ticks to wait for the semaphore to become
\r
129 * available. The macro portTICK_RATE_MS can be used to convert this to a
\r
130 * real time. A block time of zero can be used to poll the semaphore. A block
\r
131 * time of portMAX_DELAY can be used to block indefinitely (provided
\r
132 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h).
\r
134 * @return pdTRUE if the semaphore was obtained. pdFALSE
\r
135 * if xBlockTime expired without the semaphore becoming available.
\r
139 xSemaphoreHandle xSemaphore = NULL;
\r
141 // A task that creates a semaphore.
\r
142 void vATask( void * pvParameters )
\r
144 // Create the semaphore to guard a shared resource.
\r
145 vSemaphoreCreateBinary( xSemaphore );
\r
148 // A task that uses the semaphore.
\r
149 void vAnotherTask( void * pvParameters )
\r
151 // ... Do other things.
\r
153 if( xSemaphore != NULL )
\r
155 // See if we can obtain the semaphore. If the semaphore is not available
\r
156 // wait 10 ticks to see if it becomes free.
\r
157 if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE )
\r
159 // We were able to obtain the semaphore and can now access the
\r
160 // shared resource.
\r
164 // We have finished accessing the shared resource. Release the
\r
166 xSemaphoreGive( xSemaphore );
\r
170 // We could not obtain the semaphore and can therefore not access
\r
171 // the shared resource safely.
\r
176 * \defgroup xSemaphoreTake xSemaphoreTake
\r
177 * \ingroup Semaphores
\r
179 #define xSemaphoreTake( xSemaphore, xBlockTime ) xQueueGenericReceive( ( xQueueHandle ) xSemaphore, NULL, xBlockTime, pdFALSE )
\r
183 * xSemaphoreTakeRecursive(
\r
184 * xSemaphoreHandle xMutex,
\r
185 * portTickType xBlockTime
\r
188 * <i>Macro</i> to recursively obtain, or 'take', a mutex type semaphore.
\r
189 * The mutex must have previously been created using a call to
\r
190 * xSemaphoreCreateRecursiveMutex();
\r
192 * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
\r
193 * macro to be available.
\r
195 * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
\r
197 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
\r
198 * doesn't become available again until the owner has called
\r
199 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
\r
200 * if a task successfully 'takes' the same mutex 5 times then the mutex will
\r
201 * not be available to any other task until it has also 'given' the mutex back
\r
202 * exactly five times.
\r
204 * @param xMutex A handle to the mutex being obtained. This is the
\r
205 * handle returned by xSemaphoreCreateRecursiveMutex();
\r
207 * @param xBlockTime The time in ticks to wait for the semaphore to become
\r
208 * available. The macro portTICK_RATE_MS can be used to convert this to a
\r
209 * real time. A block time of zero can be used to poll the semaphore. If
\r
210 * the task already owns the semaphore then xSemaphoreTakeRecursive() will
\r
211 * return immediately no matter what the value of xBlockTime.
\r
213 * @return pdTRUE if the semaphore was obtained. pdFALSE if xBlockTime
\r
214 * expired without the semaphore becoming available.
\r
218 xSemaphoreHandle xMutex = NULL;
\r
220 // A task that creates a mutex.
\r
221 void vATask( void * pvParameters )
\r
223 // Create the mutex to guard a shared resource.
\r
224 xMutex = xSemaphoreCreateRecursiveMutex();
\r
227 // A task that uses the mutex.
\r
228 void vAnotherTask( void * pvParameters )
\r
230 // ... Do other things.
\r
232 if( xMutex != NULL )
\r
234 // See if we can obtain the mutex. If the mutex is not available
\r
235 // wait 10 ticks to see if it becomes free.
\r
236 if( xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 ) == pdTRUE )
\r
238 // We were able to obtain the mutex and can now access the
\r
239 // shared resource.
\r
242 // For some reason due to the nature of the code further calls to
\r
243 // xSemaphoreTakeRecursive() are made on the same mutex. In real
\r
244 // code these would not be just sequential calls as this would make
\r
245 // no sense. Instead the calls are likely to be buried inside
\r
246 // a more complex call structure.
\r
247 xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 );
\r
248 xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 );
\r
250 // The mutex has now been 'taken' three times, so will not be
\r
251 // available to another task until it has also been given back
\r
252 // three times. Again it is unlikely that real code would have
\r
253 // these calls sequentially, but instead buried in a more complex
\r
254 // call structure. This is just for illustrative purposes.
\r
255 xSemaphoreGiveRecursive( xMutex );
\r
256 xSemaphoreGiveRecursive( xMutex );
\r
257 xSemaphoreGiveRecursive( xMutex );
\r
259 // Now the mutex can be taken by other tasks.
\r
263 // We could not obtain the mutex and can therefore not access
\r
264 // the shared resource safely.
\r
269 * \defgroup xSemaphoreTakeRecursive xSemaphoreTakeRecursive
\r
270 * \ingroup Semaphores
\r
272 #define xSemaphoreTakeRecursive( xMutex, xBlockTime ) xQueueTakeMutexRecursive( xMutex, xBlockTime )
\r
276 * xSemaphoreAltTake() is an alternative version of xSemaphoreTake().
\r
278 * The source code that implements the alternative (Alt) API is much
\r
279 * simpler because it executes everything from within a critical section.
\r
280 * This is the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
281 * preferred fully featured API too. The fully featured API has more
\r
282 * complex code that takes longer to execute, but makes much less use of
\r
283 * critical sections. Therefore the alternative API sacrifices interrupt
\r
284 * responsiveness to gain execution speed, whereas the fully featured API
\r
285 * sacrifices execution speed to ensure better interrupt responsiveness.
\r
287 #define xSemaphoreAltTake( xSemaphore, xBlockTime ) xQueueAltGenericReceive( ( xQueueHandle ) xSemaphore, NULL, xBlockTime, pdFALSE )
\r
291 * <pre>xSemaphoreGive( xSemaphoreHandle xSemaphore )</pre>
\r
293 * <i>Macro</i> to release a semaphore. The semaphore must have previously been
\r
294 * created with a call to vSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
\r
295 * xSemaphoreCreateCounting(). and obtained using sSemaphoreTake().
\r
297 * This macro must not be used from an ISR. See xSemaphoreGiveFromISR () for
\r
298 * an alternative which can be used from an ISR.
\r
300 * This macro must also not be used on semaphores created using
\r
301 * xSemaphoreCreateRecursiveMutex().
\r
303 * @param xSemaphore A handle to the semaphore being released. This is the
\r
304 * handle returned when the semaphore was created.
\r
306 * @return pdTRUE if the semaphore was released. pdFALSE if an error occurred.
\r
307 * Semaphores are implemented using queues. An error can occur if there is
\r
308 * no space on the queue to post a message - indicating that the
\r
309 * semaphore was not first obtained correctly.
\r
313 xSemaphoreHandle xSemaphore = NULL;
\r
315 void vATask( void * pvParameters )
\r
317 // Create the semaphore to guard a shared resource.
\r
318 vSemaphoreCreateBinary( xSemaphore );
\r
320 if( xSemaphore != NULL )
\r
322 if( xSemaphoreGive( xSemaphore ) != pdTRUE )
\r
324 // We would expect this call to fail because we cannot give
\r
325 // a semaphore without first "taking" it!
\r
328 // Obtain the semaphore - don't block if the semaphore is not
\r
329 // immediately available.
\r
330 if( xSemaphoreTake( xSemaphore, ( portTickType ) 0 ) )
\r
332 // We now have the semaphore and can access the shared resource.
\r
336 // We have finished accessing the shared resource so can free the
\r
338 if( xSemaphoreGive( xSemaphore ) != pdTRUE )
\r
340 // We would not expect this call to fail because we must have
\r
341 // obtained the semaphore to get here.
\r
347 * \defgroup xSemaphoreGive xSemaphoreGive
\r
348 * \ingroup Semaphores
\r
350 #define xSemaphoreGive( xSemaphore ) xQueueGenericSend( ( xQueueHandle ) xSemaphore, NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
\r
354 * <pre>xSemaphoreGiveRecursive( xSemaphoreHandle xMutex )</pre>
\r
356 * <i>Macro</i> to recursively release, or 'give', a mutex type semaphore.
\r
357 * The mutex must have previously been created using a call to
\r
358 * xSemaphoreCreateRecursiveMutex();
\r
360 * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
\r
361 * macro to be available.
\r
363 * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
\r
365 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
\r
366 * doesn't become available again until the owner has called
\r
367 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
\r
368 * if a task successfully 'takes' the same mutex 5 times then the mutex will
\r
369 * not be available to any other task until it has also 'given' the mutex back
\r
370 * exactly five times.
\r
372 * @param xMutex A handle to the mutex being released, or 'given'. This is the
\r
373 * handle returned by xSemaphoreCreateMutex();
\r
375 * @return pdTRUE if the semaphore was given.
\r
379 xSemaphoreHandle xMutex = NULL;
\r
381 // A task that creates a mutex.
\r
382 void vATask( void * pvParameters )
\r
384 // Create the mutex to guard a shared resource.
\r
385 xMutex = xSemaphoreCreateRecursiveMutex();
\r
388 // A task that uses the mutex.
\r
389 void vAnotherTask( void * pvParameters )
\r
391 // ... Do other things.
\r
393 if( xMutex != NULL )
\r
395 // See if we can obtain the mutex. If the mutex is not available
\r
396 // wait 10 ticks to see if it becomes free.
\r
397 if( xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 ) == pdTRUE )
\r
399 // We were able to obtain the mutex and can now access the
\r
400 // shared resource.
\r
403 // For some reason due to the nature of the code further calls to
\r
404 // xSemaphoreTakeRecursive() are made on the same mutex. In real
\r
405 // code these would not be just sequential calls as this would make
\r
406 // no sense. Instead the calls are likely to be buried inside
\r
407 // a more complex call structure.
\r
408 xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 );
\r
409 xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 );
\r
411 // The mutex has now been 'taken' three times, so will not be
\r
412 // available to another task until it has also been given back
\r
413 // three times. Again it is unlikely that real code would have
\r
414 // these calls sequentially, it would be more likely that the calls
\r
415 // to xSemaphoreGiveRecursive() would be called as a call stack
\r
416 // unwound. This is just for demonstrative purposes.
\r
417 xSemaphoreGiveRecursive( xMutex );
\r
418 xSemaphoreGiveRecursive( xMutex );
\r
419 xSemaphoreGiveRecursive( xMutex );
\r
421 // Now the mutex can be taken by other tasks.
\r
425 // We could not obtain the mutex and can therefore not access
\r
426 // the shared resource safely.
\r
431 * \defgroup xSemaphoreGiveRecursive xSemaphoreGiveRecursive
\r
432 * \ingroup Semaphores
\r
434 #define xSemaphoreGiveRecursive( xMutex ) xQueueGiveMutexRecursive( xMutex )
\r
437 * xSemaphoreAltGive() is an alternative version of xSemaphoreGive().
\r
439 * The source code that implements the alternative (Alt) API is much
\r
440 * simpler because it executes everything from within a critical section.
\r
441 * This is the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
442 * preferred fully featured API too. The fully featured API has more
\r
443 * complex code that takes longer to execute, but makes much less use of
\r
444 * critical sections. Therefore the alternative API sacrifices interrupt
\r
445 * responsiveness to gain execution speed, whereas the fully featured API
\r
446 * sacrifices execution speed to ensure better interrupt responsiveness.
\r
448 #define xSemaphoreAltGive( xSemaphore ) xQueueAltGenericSend( ( xQueueHandle ) xSemaphore, NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
\r
453 xSemaphoreGiveFromISR(
\r
454 xSemaphoreHandle xSemaphore,
\r
455 portBASE_TYPE *pxHigherPriorityTaskWoken
\r
458 * <i>Macro</i> to release a semaphore. The semaphore must have previously been
\r
459 * created with a call to vSemaphoreCreateBinary() or xSemaphoreCreateCounting().
\r
461 * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
\r
462 * must not be used with this macro.
\r
464 * This macro can be used from an ISR.
\r
466 * @param xSemaphore A handle to the semaphore being released. This is the
\r
467 * handle returned when the semaphore was created.
\r
469 * @param pxHigherPriorityTaskWoken xSemaphoreGiveFromISR() will set
\r
470 * *pxHigherPriorityTaskWoken to pdTRUE if giving the semaphore caused a task
\r
471 * to unblock, and the unblocked task has a priority higher than the currently
\r
472 * running task. If xSemaphoreGiveFromISR() sets this value to pdTRUE then
\r
473 * a context switch should be requested before the interrupt is exited.
\r
475 * @return pdTRUE if the semaphore was successfully given, otherwise errQUEUE_FULL.
\r
479 #define LONG_TIME 0xffff
\r
480 #define TICKS_TO_WAIT 10
\r
481 xSemaphoreHandle xSemaphore = NULL;
\r
483 // Repetitive task.
\r
484 void vATask( void * pvParameters )
\r
488 // We want this task to run every 10 ticks of a timer. The semaphore
\r
489 // was created before this task was started.
\r
491 // Block waiting for the semaphore to become available.
\r
492 if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )
\r
494 // It is time to execute.
\r
498 // We have finished our task. Return to the top of the loop where
\r
499 // we will block on the semaphore until it is time to execute
\r
500 // again. Note when using the semaphore for synchronisation with an
\r
501 // ISR in this manner there is no need to 'give' the semaphore back.
\r
507 void vTimerISR( void * pvParameters )
\r
509 static unsigned portCHAR ucLocalTickCount = 0;
\r
510 static portBASE_TYPE xHigherPriorityTaskWoken;
\r
512 // A timer tick has occurred.
\r
514 // ... Do other time functions.
\r
516 // Is it time for vATask () to run?
\r
517 xHigherPriorityTaskWoken = pdFALSE;
\r
518 ucLocalTickCount++;
\r
519 if( ucLocalTickCount >= TICKS_TO_WAIT )
\r
521 // Unblock the task by releasing the semaphore.
\r
522 xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
\r
524 // Reset the count so we release the semaphore again in 10 ticks time.
\r
525 ucLocalTickCount = 0;
\r
528 if( xHigherPriorityTaskWoken != pdFALSE )
\r
530 // We can force a context switch here. Context switching from an
\r
531 // ISR uses port specific syntax. Check the demo task for your port
\r
532 // to find the syntax required.
\r
536 * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR
\r
537 * \ingroup Semaphores
\r
539 #define xSemaphoreGiveFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueGenericSendFromISR( ( xQueueHandle ) xSemaphore, NULL, pxHigherPriorityTaskWoken, queueSEND_TO_BACK )
\r
543 * <pre>xSemaphoreHandle xSemaphoreCreateMutex( void )</pre>
\r
545 * <i>Macro</i> that implements a mutex semaphore by using the existing queue
\r
548 * Mutexes created using this macro can be accessed using the xSemaphoreTake()
\r
549 * and xSemaphoreGive() macros. The xSemaphoreTakeRecursive() and
\r
550 * xSemaphoreGiveRecursive() macros should not be used.
\r
552 * This type of semaphore uses a priority inheritance mechanism so a task
\r
553 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
\r
554 * semaphore it is no longer required.
\r
556 * Mutex type semaphores cannot be used from within interrupt service routines.
\r
558 * See vSemaphoreCreateBinary() for an alternative implementation that can be
\r
559 * used for pure synchronisation (where one task or interrupt always 'gives' the
\r
560 * semaphore and another always 'takes' the semaphore) and from within interrupt
\r
561 * service routines.
\r
563 * @return xSemaphore Handle to the created mutex semaphore. Should be of type
\r
564 * xSemaphoreHandle.
\r
568 xSemaphoreHandle xSemaphore;
\r
570 void vATask( void * pvParameters )
\r
572 // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
\r
573 // This is a macro so pass the variable in directly.
\r
574 xSemaphore = xSemaphoreCreateMutex();
\r
576 if( xSemaphore != NULL )
\r
578 // The semaphore was created successfully.
\r
579 // The semaphore can now be used.
\r
583 * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex
\r
584 * \ingroup Semaphores
\r
586 #define xSemaphoreCreateMutex() xQueueCreateMutex()
\r
591 * <pre>xSemaphoreHandle xSemaphoreCreateRecursiveMutex( void )</pre>
\r
593 * <i>Macro</i> that implements a recursive mutex by using the existing queue
\r
596 * Mutexes created using this macro can be accessed using the
\r
597 * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros. The
\r
598 * xSemaphoreTake() and xSemaphoreGive() macros should not be used.
\r
600 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
\r
601 * doesn't become available again until the owner has called
\r
602 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
\r
603 * if a task successfully 'takes' the same mutex 5 times then the mutex will
\r
604 * not be available to any other task until it has also 'given' the mutex back
\r
605 * exactly five times.
\r
607 * This type of semaphore uses a priority inheritance mechanism so a task
\r
608 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
\r
609 * semaphore it is no longer required.
\r
611 * Mutex type semaphores cannot be used from within interrupt service routines.
\r
613 * See vSemaphoreCreateBinary() for an alternative implementation that can be
\r
614 * used for pure synchronisation (where one task or interrupt always 'gives' the
\r
615 * semaphore and another always 'takes' the semaphore) and from within interrupt
\r
616 * service routines.
\r
618 * @return xSemaphore Handle to the created mutex semaphore. Should be of type
\r
619 * xSemaphoreHandle.
\r
623 xSemaphoreHandle xSemaphore;
\r
625 void vATask( void * pvParameters )
\r
627 // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
\r
628 // This is a macro so pass the variable in directly.
\r
629 xSemaphore = xSemaphoreCreateRecursiveMutex();
\r
631 if( xSemaphore != NULL )
\r
633 // The semaphore was created successfully.
\r
634 // The semaphore can now be used.
\r
638 * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex
\r
639 * \ingroup Semaphores
\r
641 #define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex()
\r
645 * <pre>xSemaphoreHandle xSemaphoreCreateCounting( unsigned portBASE_TYPE uxMaxCount, unsigned portBASE_TYPE uxInitialCount )</pre>
\r
647 * <i>Macro</i> that creates a counting semaphore by using the existing
\r
648 * queue mechanism.
\r
650 * Counting semaphores are typically used for two things:
\r
652 * 1) Counting events.
\r
654 * In this usage scenario an event handler will 'give' a semaphore each time
\r
655 * an event occurs (incrementing the semaphore count value), and a handler
\r
656 * task will 'take' a semaphore each time it processes an event
\r
657 * (decrementing the semaphore count value). The count value is therefore
\r
658 * the difference between the number of events that have occurred and the
\r
659 * number that have been processed. In this case it is desirable for the
\r
660 * initial count value to be zero.
\r
662 * 2) Resource management.
\r
664 * In this usage scenario the count value indicates the number of resources
\r
665 * available. To obtain control of a resource a task must first obtain a
\r
666 * semaphore - decrementing the semaphore count value. When the count value
\r
667 * reaches zero there are no free resources. When a task finishes with the
\r
668 * resource it 'gives' the semaphore back - incrementing the semaphore count
\r
669 * value. In this case it is desirable for the initial count value to be
\r
670 * equal to the maximum count value, indicating that all resources are free.
\r
672 * @param uxMaxCount The maximum count value that can be reached. When the
\r
673 * semaphore reaches this value it can no longer be 'given'.
\r
675 * @param uxInitialCount The count value assigned to the semaphore when it is
\r
678 * @return Handle to the created semaphore. Null if the semaphore could not be
\r
683 xSemaphoreHandle xSemaphore;
\r
685 void vATask( void * pvParameters )
\r
687 xSemaphoreHandle xSemaphore = NULL;
\r
689 // Semaphore cannot be used before a call to xSemaphoreCreateCounting().
\r
690 // The max value to which the semaphore can count should be 10, and the
\r
691 // initial value assigned to the count should be 0.
\r
692 xSemaphore = xSemaphoreCreateCounting( 10, 0 );
\r
694 if( xSemaphore != NULL )
\r
696 // The semaphore was created successfully.
\r
697 // The semaphore can now be used.
\r
701 * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting
\r
702 * \ingroup Semaphores
\r
704 #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( uxMaxCount, uxInitialCount )
\r
707 #endif /* SEMAPHORE_H */
\r