2 FreeRTOS.org V4.7.0 - Copyright (C) 2003-2007 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
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com a version that has been certified for use
\r
32 in safety critical systems, plus commercial licensing, development and
\r
34 ***************************************************************************
\r
42 typedef xQueueHandle xSemaphoreHandle;
\r
44 #define semBINARY_SEMAPHORE_QUEUE_LENGTH ( ( unsigned portCHAR ) 1 )
\r
45 #define semSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portCHAR ) 0 )
\r
46 #define semGIVE_BLOCK_TIME ( ( portTickType ) 0 )
\r
51 * <pre>vSemaphoreCreateBinary( xSemaphoreHandle xSemaphore )</pre>
\r
53 * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.
\r
54 * The queue length is 1 as this is a binary semaphore. The data size is 0
\r
55 * as we don't want to actually store any data - we just want to know if the
\r
56 * queue is empty or full.
\r
58 * This type of semaphore can be used for pure synchronisation between tasks or
\r
59 * between an interrupt and a task. The semaphore need not be given back once
\r
60 * obtained, so one task/interrupt can continuously 'give' the semaphore while
\r
61 * another continuously 'takes' the semaphore. For this reason this type of
\r
62 * semaphore does not use a priority inheritance mechanism. For an alternative
\r
63 * that does use priority inheritance see xSemaphoreCreateMutex().
\r
65 * @param xSemaphore Handle to the created semaphore. Should be of type xSemaphoreHandle.
\r
69 xSemaphoreHandle xSemaphore;
\r
71 void vATask( void * pvParameters )
\r
73 // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().
\r
74 // This is a macro so pass the variable in directly.
\r
75 vSemaphoreCreateBinary( xSemaphore );
\r
77 if( xSemaphore != NULL )
\r
79 // The semaphore was created successfully.
\r
80 // The semaphore can now be used.
\r
84 * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary
\r
85 * \ingroup Semaphores
\r
87 #define vSemaphoreCreateBinary( xSemaphore ) { \
\r
88 xSemaphore = xQueueCreate( ( unsigned portBASE_TYPE ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH ); \
\r
89 if( xSemaphore != NULL ) \
\r
91 xSemaphoreGive( xSemaphore ); \
\r
98 * xSemaphoreHandle xSemaphore,
\r
99 * portTickType xBlockTime
\r
102 * <i>Macro</i> to obtain a semaphore. The semaphore must have previously been
\r
103 * created with a call to vSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
\r
104 * xSemaphoreCreateCounting().
\r
106 * @param xSemaphore A handle to the semaphore being taken - obtained when
\r
107 * the semaophore was created.
\r
109 * @param xBlockTime The time in ticks to wait for the semaphore to become
\r
110 * available. The macro portTICK_RATE_MS can be used to convert this to a
\r
111 * real time. A block time of zero can be used to poll the semaphore. A block
\r
112 * time of portMAX_DELAY can be used to block indefinately (provided
\r
113 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h).
\r
115 * @return pdTRUE if the semaphore was obtained. pdFALSE
\r
116 * if xBlockTime expired without the semaphore becoming available.
\r
120 xSemaphoreHandle xSemaphore = NULL;
\r
122 // A task that creates a semaphore.
\r
123 void vATask( void * pvParameters )
\r
125 // Create the semaphore to guard a shared resource.
\r
126 vSemaphoreCreateBinary( xSemaphore );
\r
129 // A task that uses the semaphore.
\r
130 void vAnotherTask( void * pvParameters )
\r
132 // ... Do other things.
\r
134 if( xSemaphore != NULL )
\r
136 // See if we can obtain the semaphore. If the semaphore is not available
\r
137 // wait 10 ticks to see if it becomes free.
\r
138 if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE )
\r
140 // We were able to obtain the semaphore and can now access the
\r
141 // shared resource.
\r
145 // We have finished accessing the shared resource. Release the
\r
147 xSemaphoreGive( xSemaphore );
\r
151 // We could not obtain the semaphore and can therefore not access
\r
152 // the shared resource safely.
\r
157 * \defgroup xSemaphoreTake xSemaphoreTake
\r
158 * \ingroup Semaphores
\r
160 #define xSemaphoreTake( xSemaphore, xBlockTime ) xQueueGenericReceive( ( xQueueHandle ) xSemaphore, NULL, xBlockTime, pdFALSE )
\r
164 * xSemaphoreTakeRecursive(
\r
165 * xSemaphoreHandle xMutex,
\r
166 * portTickType xBlockTime
\r
169 * <i>Macro</i> to recursively obtain, or 'take', a mutex type semaphore.
\r
170 * The mutex must have previously been created using a call to
\r
171 * xSemaphoreCreateRecursiveMutex();
\r
173 * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
\r
174 * macro to be available.
\r
176 * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
\r
178 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
\r
179 * doesn't become available again until the owner has called
\r
180 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
\r
181 * if a task successfully 'takes' the same mutex 5 times then the mutex will
\r
182 * not be avilable to any other task until it has also 'given' the mutex back
\r
183 * exactly five times.
\r
185 * @param xMutex A handle to the mutex being obtained. This is the
\r
186 * handle returned by xSemaphoreCreateMutex();
\r
188 * @param xBlockTime The time in ticks to wait for the semaphore to become
\r
189 * available. The macro portTICK_RATE_MS can be used to convert this to a
\r
190 * real time. A block time of zero can be used to poll the semaphore. If
\r
191 * the task already owns the semaphore then xSemaphoreTakeRecursive() will
\r
192 * return immediately nomatter what the value of xBlockTime.
\r
194 * @return pdTRUE if the semaphore was obtained. pdFALSE if xBlockTime
\r
195 * expired without the semaphore becoming available.
\r
199 xSemaphoreHandle xMutex = NULL;
\r
201 // A task that creates a mutex.
\r
202 void vATask( void * pvParameters )
\r
204 // Create the mutex to guard a shared resource.
\r
205 xMutex = xSemaphoreCreateRecursiveMutex();
\r
208 // A task that uses the mutex.
\r
209 void vAnotherTask( void * pvParameters )
\r
211 // ... Do other things.
\r
213 if( xMutex != NULL )
\r
215 // See if we can obtain the mutex. If the mutex is not available
\r
216 // wait 10 ticks to see if it becomes free.
\r
217 if( xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 ) == pdTRUE )
\r
219 // We were able to obtain the mutex and can now access the
\r
220 // shared resource.
\r
223 // For some reason due to the nature of the code further calls to
\r
224 // xSemaphoreTakeRecursive() are made on the same mutex. In real
\r
225 // code these would not be just sequential calls as this would make
\r
226 // no sense. Instead the calls are likely to be buried inside
\r
227 // a more complex call structure.
\r
228 xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 );
\r
229 xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 );
\r
231 // The mutex has now been 'taken' three times, so will not be
\r
232 // available to another task until it has also been given back
\r
233 // three times. Again it is unlikely that real code would have
\r
234 // these calls sequentially, but instead buried in a more complex
\r
235 // call structure. This is just for illustrative puproses.
\r
236 xSemaphoreGiveRecursive( xSemaphore );
\r
237 xSemaphoreGiveRecursive( xSemaphore );
\r
238 xSemaphoreGiveRecursive( xSemaphore );
\r
240 // Now the mutex can be taken by other tasks.
\r
244 // We could not obtain the mutex and can therefore not access
\r
245 // the shared resource safely.
\r
250 * \defgroup xSemaphoreTakeRecursive xSemaphoreTakeRecursive
\r
251 * \ingroup Semaphores
\r
253 #define xSemaphoreTakeRecursive( xMutex, xBlockTime ) xQueueTakeMutexRecursive( xMutex, xBlockTime )
\r
257 * xSemaphoreAltTake() is an alternative version of xSemaphoreTake().
\r
259 * The source code that implements the alternative (Alt) API is much
\r
260 * simpler because it executes everything from within a critical section.
\r
261 * This is the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
262 * preferred fully featured API too. The fully featured API has more
\r
263 * complex code that takes longer to execute, but makes much less use of
\r
264 * critical sections. Therefore the alternative API sacrifices interrupt
\r
265 * responsiveness to gain execution speed, whereas the fully featured API
\r
266 * sacrifices execution speed to ensure better interrupt responsiveness.
\r
268 #define xSemaphoreAltTake( xSemaphore, xBlockTime ) xQueueAltGenericReceive( ( xQueueHandle ) xSemaphore, NULL, xBlockTime, pdFALSE )
\r
272 * <pre>xSemaphoreGive( xSemaphoreHandle xSemaphore )</pre>
\r
274 * <i>Macro</i> to release a semaphore. The semaphore must have previously been
\r
275 * created with a call to vSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
\r
276 * xSemaphoreCreateCounting(). and obtained using sSemaphoreTake().
\r
278 * This macro must not be used from an ISR. See xSemaphoreGiveFromISR () for
\r
279 * an alternative which can be used from an ISR.
\r
281 * This macro must also not be used on semaphores created using
\r
282 * xSemaphoreCreateRecursiveMutex().
\r
284 * @param xSemaphore A handle to the semaphore being released. This is the
\r
285 * handle returned when the semaphore was created.
\r
287 * @return pdTRUE if the semaphore was released. pdFALSE if an error occurred.
\r
288 * Semaphores are implemented using queues. An error can occur if there is
\r
289 * no space on the queue to post a message - indicating that the
\r
290 * semaphore was not first obtained correctly.
\r
294 xSemaphoreHandle xSemaphore = NULL;
\r
296 void vATask( void * pvParameters )
\r
298 // Create the semaphore to guard a shared resource.
\r
299 vSemaphoreCreateBinary( xSemaphore );
\r
301 if( xSemaphore != NULL )
\r
303 if( xSemaphoreGive( xSemaphore ) != pdTRUE )
\r
305 // We would expect this call to fail because we cannot give
\r
306 // a semaphore without first "taking" it!
\r
309 // Obtain the semaphore - don't block if the semaphore is not
\r
310 // immediately available.
\r
311 if( xSemaphoreTake( xSemaphore, ( portTickType ) 0 ) )
\r
313 // We now have the semaphore and can access the shared resource.
\r
317 // We have finished accessing the shared resource so can free the
\r
319 if( xSemaphoreGive( xSemaphore ) != pdTRUE )
\r
321 // We would not expect this call to fail because we must have
\r
322 // obtained the semaphore to get here.
\r
328 * \defgroup xSemaphoreGive xSemaphoreGive
\r
329 * \ingroup Semaphores
\r
331 #define xSemaphoreGive( xSemaphore ) xQueueGenericSend( ( xQueueHandle ) xSemaphore, NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
\r
335 * <pre>xSemaphoreGiveRecursive( xSemaphoreHandle xSemaphore )</pre>
\r
337 * <i>Macro</i> to recursively release, or 'give', a mutex type semaphore.
\r
338 * The mutex must have previously been created using a call to
\r
339 * xSemaphoreCreateRecursiveMutex();
\r
341 * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
\r
342 * macro to be available.
\r
344 * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
\r
346 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
\r
347 * doesn't become available again until the owner has called
\r
348 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
\r
349 * if a task successfully 'takes' the same mutex 5 times then the mutex will
\r
350 * not be avilable to any other task until it has also 'given' the mutex back
\r
351 * exactly five times.
\r
353 * @param xMutex A handle to the mutex being released, or 'given'. This is the
\r
354 * handle returned by xSemaphoreCreateMutex();
\r
356 * @return pdTRUE if the semaphore was given.
\r
360 xSemaphoreHandle xMutex = NULL;
\r
362 // A task that creates a mutex.
\r
363 void vATask( void * pvParameters )
\r
365 // Create the mutex to guard a shared resource.
\r
366 xMutex = xSemaphoreCreateRecursiveMutex();
\r
369 // A task that uses the mutex.
\r
370 void vAnotherTask( void * pvParameters )
\r
372 // ... Do other things.
\r
374 if( xMutex != NULL )
\r
376 // See if we can obtain the mutex. If the mutex is not available
\r
377 // wait 10 ticks to see if it becomes free.
\r
378 if( xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 ) == pdTRUE )
\r
380 // We were able to obtain the mutex and can now access the
\r
381 // shared resource.
\r
384 // For some reason due to the nature of the code further calls to
\r
385 // xSemaphoreTakeRecursive() are made on the same mutex. In real
\r
386 // code these would not be just sequential calls as this would make
\r
387 // no sense. Instead the calls are likely to be buried inside
\r
388 // a more complex call structure.
\r
389 xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 );
\r
390 xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 );
\r
392 // The mutex has now been 'taken' three times, so will not be
\r
393 // available to another task until it has also been given back
\r
394 // three times. Again it is unlikely that real code would have
\r
395 // these calls sequentially, it would be more likely that the calls
\r
396 // to xSemaphoreGiveRecursive() would be called as a call stack
\r
397 // unwound. This is just for demonstrative purposes.
\r
398 xSemaphoreGiveRecursive( xSemaphore );
\r
399 xSemaphoreGiveRecursive( xSemaphore );
\r
400 xSemaphoreGiveRecursive( xSemaphore );
\r
402 // Now the mutex can be taken by other tasks.
\r
406 // We could not obtain the mutex and can therefore not access
\r
407 // the shared resource safely.
\r
412 * \defgroup xSemaphoreGiveRecursive xSemaphoreGiveRecursive
\r
413 * \ingroup Semaphores
\r
415 #define xSemaphoreGiveRecursive( xMutex ) xQueueGiveMutexRecursive( xMutex )
\r
418 * xSemaphoreAltGive() is an alternative version of xSemaphoreGive().
\r
420 * The source code that implements the alternative (Alt) API is much
\r
421 * simpler because it executes everything from within a critical section.
\r
422 * This is the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
423 * preferred fully featured API too. The fully featured API has more
\r
424 * complex code that takes longer to execute, but makes much less use of
\r
425 * critical sections. Therefore the alternative API sacrifices interrupt
\r
426 * responsiveness to gain execution speed, whereas the fully featured API
\r
427 * sacrifices execution speed to ensure better interrupt responsiveness.
\r
429 #define xSemaphoreAltGive( xSemaphore ) xQueueAltGenericSend( ( xQueueHandle ) xSemaphore, NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
\r
434 xSemaphoreGiveFromISR(
\r
435 xSemaphoreHandle xSemaphore,
\r
436 portSHORT sTaskPreviouslyWoken
\r
439 * <i>Macro</i> to release a semaphore. The semaphore must have previously been
\r
440 * created with a call to vSemaphoreCreateBinary() or xSemaphoreCreateCounting().
\r
442 * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
\r
443 * must not be used with this macro.
\r
445 * This macro can be used from an ISR.
\r
447 * @param xSemaphore A handle to the semaphore being released. This is the
\r
448 * handle returned when the semaphore was created.
\r
450 * @param sTaskPreviouslyWoken This is included so an ISR can make multiple calls
\r
451 * to xSemaphoreGiveFromISR () from a single interrupt. The first call
\r
452 * should always pass in pdFALSE. Subsequent calls should pass in
\r
453 * the value returned from the previous call. See the file serial .c in the
\r
454 * PC port for a good example of using xSemaphoreGiveFromISR ().
\r
456 * @return pdTRUE if a task was woken by releasing the semaphore. This is
\r
457 * used by the ISR to determine if a context switch may be required following
\r
462 #define LONG_TIME 0xffff
\r
463 #define TICKS_TO_WAIT 10
\r
464 xSemaphoreHandle xSemaphore = NULL;
\r
466 // Repetitive task.
\r
467 void vATask( void * pvParameters )
\r
471 // We want this task to run every 10 ticks of a timer. The semaphore
\r
472 // was created before this task was started.
\r
474 // Block waiting for the semaphore to become available.
\r
475 if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )
\r
477 // It is time to execute.
\r
481 // We have finished our task. Return to the top of the loop where
\r
482 // we will block on the semaphore until it is time to execute
\r
483 // again. Note when using the semaphore for synchronisation with an
\r
484 // ISR in this manner there is no need to 'give' the semaphore back.
\r
490 void vTimerISR( void * pvParameters )
\r
492 static unsigned portCHAR ucLocalTickCount = 0;
\r
493 static portBASE_TYPE xTaskWoken;
\r
495 // A timer tick has occurred.
\r
497 // ... Do other time functions.
\r
499 // Is it time for vATask () to run?
\r
500 xTaskWoken = pdFALSE;
\r
501 ucLocalTickCount++;
\r
502 if( ucLocalTickCount >= TICKS_TO_WAIT )
\r
504 // Unblock the task by releasing the semaphore.
\r
505 xTaskWoken = xSemaphoreGiveFromISR( xSemaphore, xTaskWoken );
\r
507 // Reset the count so we release the semaphore again in 10 ticks time.
\r
508 ucLocalTickCount = 0;
\r
511 if( xTaskWoken != pdFALSE )
\r
513 // We can force a context switch here. Context switching from an
\r
514 // ISR uses port specific syntax. Check the demo task for your port
\r
515 // to find the syntax required.
\r
519 * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR
\r
520 * \ingroup Semaphores
\r
522 #define xSemaphoreGiveFromISR( xSemaphore, xTaskPreviouslyWoken ) xQueueGenericSendFromISR( ( xQueueHandle ) xSemaphore, NULL, xTaskPreviouslyWoken, queueSEND_TO_BACK )
\r
526 * <pre>xSemaphoreHandle xSemaphoreCreateMutex( void )</pre>
\r
528 * <i>Macro</i> that implements a mutex semaphore by using the existing queue
\r
531 * Mutexes created using this macro can be accessed using the xSemaphoreTake()
\r
532 * and xSemaphoreGive() macros. The xSemaphoreTakeRecursive() and
\r
533 * xSemaphoreGiveRecursive() macros should not be used.
\r
535 * This type of semaphore uses a priority inheritance mechanism so a task
\r
536 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
\r
537 * semaphore it is no longer required.
\r
539 * Mutex type semaphores cannot be used from within interrupt service routines.
\r
541 * See xSemaphoreCreateBinary() for an alternative implementation that can be
\r
542 * used for pure synchronisation (where one task or interrupt always 'gives' the
\r
543 * semaphore and another always 'takes' the semaphore) and from within interrupt
\r
544 * service routines.
\r
546 * @return xSemaphore Handle to the created mutex semaphore. Should be of type
\r
547 * xSemaphoreHandle.
\r
551 xSemaphoreHandle xSemaphore;
\r
553 void vATask( void * pvParameters )
\r
555 // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
\r
556 // This is a macro so pass the variable in directly.
\r
557 xSemaphore = xSemaphoreCreateMutex();
\r
559 if( xSemaphore != NULL )
\r
561 // The semaphore was created successfully.
\r
562 // The semaphore can now be used.
\r
566 * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex
\r
567 * \ingroup Semaphores
\r
569 #define xSemaphoreCreateMutex() xQueueCreateMutex()
\r
574 * <pre>xSemaphoreHandle xSemaphoreCreateRecursiveMutex( void )</pre>
\r
576 * <i>Macro</i> that implements a recursive mutex by using the existing queue
\r
579 * Mutexes created using this macro can be accessed using the
\r
580 * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros. The
\r
581 * xSemaphoreTake() and xSemaphoreGive() macros should not be used.
\r
583 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
\r
584 * doesn't become available again until the owner has called
\r
585 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
\r
586 * if a task successfully 'takes' the same mutex 5 times then the mutex will
\r
587 * not be avilable to any other task until it has also 'given' the mutex back
\r
588 * exactly five times.
\r
590 * This type of semaphore uses a priority inheritance mechanism so a task
\r
591 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
\r
592 * semaphore it is no longer required.
\r
594 * Mutex type semaphores cannot be used from within interrupt service routines.
\r
596 * See xSemaphoreCreateBinary() for an alternative implementation that can be
\r
597 * used for pure synchronisation (where one task or interrupt always 'gives' the
\r
598 * semaphore and another always 'takes' the semaphore) and from within interrupt
\r
599 * service routines.
\r
601 * @return xSemaphore Handle to the created mutex semaphore. Should be of type
\r
602 * xSemaphoreHandle.
\r
606 xSemaphoreHandle xSemaphore;
\r
608 void vATask( void * pvParameters )
\r
610 // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
\r
611 // This is a macro so pass the variable in directly.
\r
612 xSemaphore = xSemaphoreCreateRecursiveMutex();
\r
614 if( xSemaphore != NULL )
\r
616 // The semaphore was created successfully.
\r
617 // The semaphore can now be used.
\r
621 * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex
\r
622 * \ingroup Semaphores
\r
624 #define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex()
\r
628 * <pre>xSemaphoreHandle xSemaphoreCreateCounting( unsigned portBASE_TYPE uxMaxCount, unsigned portBASE_TYPE uxInitialCount )</pre>
\r
630 * <i>Macro</i> that creates a counting semaphore by using the existing
\r
631 * queue mechanism.
\r
633 * Counting semaphores are typically used for two things:
\r
635 * 1) Counting events.
\r
637 * In this usage scenario an event handler will 'give' a semaphore each time
\r
638 * an event occurs (incrementing the semaphore count value), and a handler
\r
639 * task will 'take' a semaphore each time it processes an event
\r
640 * (decrementing the semaphore count value). The count value is therefore
\r
641 * the difference between the number of events that have occurred and the
\r
642 * number that have been processed. In this case it is desirable for the
\r
643 * initial count value to be zero.
\r
645 * 2) Resource management.
\r
647 * In this usage scenario the count value indicates the number of resources
\r
648 * available. To obtain control of a resource a task must first obtain a
\r
649 * semaphore - decrementing the semaphore count value. When the count value
\r
650 * reaches zero there are no free resources. When a task finishes with the
\r
651 * resource it 'gives' the semaphore back - incrementing the semaphore count
\r
652 * value. In this case it is desirable for the initial count value to be
\r
653 * equal to the maximum count value, indicating that all resources are free.
\r
655 * @param uxMaxCount The maximum count value that can be reached. When the
\r
656 * semaphore reaches this value it can no longer be 'given'.
\r
658 * @param uxInitialCount The count value assigned to the semaphore when it is
\r
661 * @return Handle to the created semaphore. Null if the semaphore could not be
\r
666 xSemaphoreHandle xSemaphore;
\r
668 void vATask( void * pvParameters )
\r
670 xSemaphoreHandle xSemaphore = NULL;
\r
672 // Semaphore cannot be used before a call to xSemaphoreCreateCounting().
\r
673 // The max value to which the semaphore can count should be 10, and the
\r
674 // initial value assigned to the count should be 0.
\r
675 xSemaphore = xSemaphoreCreateCounting( 10, 0 );
\r
677 if( xSemaphore != NULL )
\r
679 // The semaphore was created successfully.
\r
680 // The semaphore can now be used.
\r
684 * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting
\r
685 * \ingroup Semaphores
\r
687 #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( uxMaxCount, uxInitialCount )
\r
690 #endif /* SEMAPHORE_H */
\r