2 * FreeRTOS SMP Kernel V202110.00
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
30 #ifndef INC_FREERTOS_H
31 #error "include FreeRTOS.h" must appear in source files before "include semphr.h"
36 typedef QueueHandle_t SemaphoreHandle_t;
38 #define semBINARY_SEMAPHORE_QUEUE_LENGTH ( ( uint8_t ) 1U )
39 #define semSEMAPHORE_QUEUE_ITEM_LENGTH ( ( uint8_t ) 0U )
40 #define semGIVE_BLOCK_TIME ( ( TickType_t ) 0U )
46 * vSemaphoreCreateBinary( SemaphoreHandle_t xSemaphore );
49 * In many usage scenarios it is faster and more memory efficient to use a
50 * direct to task notification in place of a binary semaphore!
51 * https://www.FreeRTOS.org/RTOS-task-notifications.html
53 * This old vSemaphoreCreateBinary() macro is now deprecated in favour of the
54 * xSemaphoreCreateBinary() function. Note that binary semaphores created using
55 * the vSemaphoreCreateBinary() macro are created in a state such that the
56 * first call to 'take' the semaphore would pass, whereas binary semaphores
57 * created using xSemaphoreCreateBinary() are created in a state such that the
58 * the semaphore must first be 'given' before it can be 'taken'.
60 * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.
61 * The queue length is 1 as this is a binary semaphore. The data size is 0
62 * as we don't want to actually store any data - we just want to know if the
63 * queue is empty or full.
65 * This type of semaphore can be used for pure synchronisation between tasks or
66 * between an interrupt and a task. The semaphore need not be given back once
67 * obtained, so one task/interrupt can continuously 'give' the semaphore while
68 * another continuously 'takes' the semaphore. For this reason this type of
69 * semaphore does not use a priority inheritance mechanism. For an alternative
70 * that does use priority inheritance see xSemaphoreCreateMutex().
72 * @param xSemaphore Handle to the created semaphore. Should be of type SemaphoreHandle_t.
76 * SemaphoreHandle_t xSemaphore = NULL;
78 * void vATask( void * pvParameters )
80 * // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().
81 * // This is a macro so pass the variable in directly.
82 * vSemaphoreCreateBinary( xSemaphore );
84 * if( xSemaphore != NULL )
86 * // The semaphore was created successfully.
87 * // The semaphore can now be used.
91 * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary
94 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
95 #define vSemaphoreCreateBinary( xSemaphore ) \
97 ( xSemaphore ) = xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE ); \
98 if( ( xSemaphore ) != NULL ) \
100 ( void ) xSemaphoreGive( ( xSemaphore ) ); \
108 * SemaphoreHandle_t xSemaphoreCreateBinary( void );
111 * Creates a new binary semaphore instance, and returns a handle by which the
112 * new semaphore can be referenced.
114 * In many usage scenarios it is faster and more memory efficient to use a
115 * direct to task notification in place of a binary semaphore!
116 * https://www.FreeRTOS.org/RTOS-task-notifications.html
118 * Internally, within the FreeRTOS implementation, binary semaphores use a block
119 * of memory, in which the semaphore structure is stored. If a binary semaphore
120 * is created using xSemaphoreCreateBinary() then the required memory is
121 * automatically dynamically allocated inside the xSemaphoreCreateBinary()
122 * function. (see https://www.FreeRTOS.org/a00111.html). If a binary semaphore
123 * is created using xSemaphoreCreateBinaryStatic() then the application writer
124 * must provide the memory. xSemaphoreCreateBinaryStatic() therefore allows a
125 * binary semaphore to be created without using any dynamic memory allocation.
127 * The old vSemaphoreCreateBinary() macro is now deprecated in favour of this
128 * xSemaphoreCreateBinary() function. Note that binary semaphores created using
129 * the vSemaphoreCreateBinary() macro are created in a state such that the
130 * first call to 'take' the semaphore would pass, whereas binary semaphores
131 * created using xSemaphoreCreateBinary() are created in a state such that the
132 * the semaphore must first be 'given' before it can be 'taken'.
134 * This type of semaphore can be used for pure synchronisation between tasks or
135 * between an interrupt and a task. The semaphore need not be given back once
136 * obtained, so one task/interrupt can continuously 'give' the semaphore while
137 * another continuously 'takes' the semaphore. For this reason this type of
138 * semaphore does not use a priority inheritance mechanism. For an alternative
139 * that does use priority inheritance see xSemaphoreCreateMutex().
141 * @return Handle to the created semaphore, or NULL if the memory required to
142 * hold the semaphore's data structures could not be allocated.
146 * SemaphoreHandle_t xSemaphore = NULL;
148 * void vATask( void * pvParameters )
150 * // Semaphore cannot be used before a call to xSemaphoreCreateBinary().
151 * // This is a macro so pass the variable in directly.
152 * xSemaphore = xSemaphoreCreateBinary();
154 * if( xSemaphore != NULL )
156 * // The semaphore was created successfully.
157 * // The semaphore can now be used.
161 * \defgroup xSemaphoreCreateBinary xSemaphoreCreateBinary
162 * \ingroup Semaphores
164 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
165 #define xSemaphoreCreateBinary() xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE )
171 * SemaphoreHandle_t xSemaphoreCreateBinaryStatic( StaticSemaphore_t *pxSemaphoreBuffer );
174 * Creates a new binary semaphore instance, and returns a handle by which the
175 * new semaphore can be referenced.
177 * NOTE: In many usage scenarios it is faster and more memory efficient to use a
178 * direct to task notification in place of a binary semaphore!
179 * https://www.FreeRTOS.org/RTOS-task-notifications.html
181 * Internally, within the FreeRTOS implementation, binary semaphores use a block
182 * of memory, in which the semaphore structure is stored. If a binary semaphore
183 * is created using xSemaphoreCreateBinary() then the required memory is
184 * automatically dynamically allocated inside the xSemaphoreCreateBinary()
185 * function. (see https://www.FreeRTOS.org/a00111.html). If a binary semaphore
186 * is created using xSemaphoreCreateBinaryStatic() then the application writer
187 * must provide the memory. xSemaphoreCreateBinaryStatic() therefore allows a
188 * binary semaphore to be created without using any dynamic memory allocation.
190 * This type of semaphore can be used for pure synchronisation between tasks or
191 * between an interrupt and a task. The semaphore need not be given back once
192 * obtained, so one task/interrupt can continuously 'give' the semaphore while
193 * another continuously 'takes' the semaphore. For this reason this type of
194 * semaphore does not use a priority inheritance mechanism. For an alternative
195 * that does use priority inheritance see xSemaphoreCreateMutex().
197 * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,
198 * which will then be used to hold the semaphore's data structure, removing the
199 * need for the memory to be allocated dynamically.
201 * @return If the semaphore is created then a handle to the created semaphore is
202 * returned. If pxSemaphoreBuffer is NULL then NULL is returned.
206 * SemaphoreHandle_t xSemaphore = NULL;
207 * StaticSemaphore_t xSemaphoreBuffer;
209 * void vATask( void * pvParameters )
211 * // Semaphore cannot be used before a call to xSemaphoreCreateBinary().
212 * // The semaphore's data structures will be placed in the xSemaphoreBuffer
213 * // variable, the address of which is passed into the function. The
214 * // function's parameter is not NULL, so the function will not attempt any
215 * // dynamic memory allocation, and therefore the function will not return
217 * xSemaphore = xSemaphoreCreateBinary( &xSemaphoreBuffer );
219 * // Rest of task code goes here.
222 * \defgroup xSemaphoreCreateBinaryStatic xSemaphoreCreateBinaryStatic
223 * \ingroup Semaphores
225 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
226 #define xSemaphoreCreateBinaryStatic( pxStaticSemaphore ) xQueueGenericCreateStatic( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticSemaphore, queueQUEUE_TYPE_BINARY_SEMAPHORE )
227 #endif /* configSUPPORT_STATIC_ALLOCATION */
233 * SemaphoreHandle_t xSemaphore,
234 * TickType_t xBlockTime
238 * <i>Macro</i> to obtain a semaphore. The semaphore must have previously been
239 * created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
240 * xSemaphoreCreateCounting().
242 * @param xSemaphore A handle to the semaphore being taken - obtained when
243 * the semaphore was created.
245 * @param xBlockTime The time in ticks to wait for the semaphore to become
246 * available. The macro portTICK_PERIOD_MS can be used to convert this to a
247 * real time. A block time of zero can be used to poll the semaphore. A block
248 * time of portMAX_DELAY can be used to block indefinitely (provided
249 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h).
251 * @return pdTRUE if the semaphore was obtained. pdFALSE
252 * if xBlockTime expired without the semaphore becoming available.
256 * SemaphoreHandle_t xSemaphore = NULL;
258 * // A task that creates a semaphore.
259 * void vATask( void * pvParameters )
261 * // Create the semaphore to guard a shared resource.
262 * xSemaphore = xSemaphoreCreateBinary();
265 * // A task that uses the semaphore.
266 * void vAnotherTask( void * pvParameters )
268 * // ... Do other things.
270 * if( xSemaphore != NULL )
272 * // See if we can obtain the semaphore. If the semaphore is not available
273 * // wait 10 ticks to see if it becomes free.
274 * if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
276 * // We were able to obtain the semaphore and can now access the
277 * // shared resource.
281 * // We have finished accessing the shared resource. Release the
283 * xSemaphoreGive( xSemaphore );
287 * // We could not obtain the semaphore and can therefore not access
288 * // the shared resource safely.
293 * \defgroup xSemaphoreTake xSemaphoreTake
294 * \ingroup Semaphores
296 #define xSemaphoreTake( xSemaphore, xBlockTime ) xQueueSemaphoreTake( ( xSemaphore ), ( xBlockTime ) )
301 * xSemaphoreTakeRecursive(
302 * SemaphoreHandle_t xMutex,
303 * TickType_t xBlockTime
307 * <i>Macro</i> to recursively obtain, or 'take', a mutex type semaphore.
308 * The mutex must have previously been created using a call to
309 * xSemaphoreCreateRecursiveMutex();
311 * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
312 * macro to be available.
314 * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
316 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
317 * doesn't become available again until the owner has called
318 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
319 * if a task successfully 'takes' the same mutex 5 times then the mutex will
320 * not be available to any other task until it has also 'given' the mutex back
321 * exactly five times.
323 * @param xMutex A handle to the mutex being obtained. This is the
324 * handle returned by xSemaphoreCreateRecursiveMutex();
326 * @param xBlockTime The time in ticks to wait for the semaphore to become
327 * available. The macro portTICK_PERIOD_MS can be used to convert this to a
328 * real time. A block time of zero can be used to poll the semaphore. If
329 * the task already owns the semaphore then xSemaphoreTakeRecursive() will
330 * return immediately no matter what the value of xBlockTime.
332 * @return pdTRUE if the semaphore was obtained. pdFALSE if xBlockTime
333 * expired without the semaphore becoming available.
337 * SemaphoreHandle_t xMutex = NULL;
339 * // A task that creates a mutex.
340 * void vATask( void * pvParameters )
342 * // Create the mutex to guard a shared resource.
343 * xMutex = xSemaphoreCreateRecursiveMutex();
346 * // A task that uses the mutex.
347 * void vAnotherTask( void * pvParameters )
349 * // ... Do other things.
351 * if( xMutex != NULL )
353 * // See if we can obtain the mutex. If the mutex is not available
354 * // wait 10 ticks to see if it becomes free.
355 * if( xSemaphoreTakeRecursive( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
357 * // We were able to obtain the mutex and can now access the
358 * // shared resource.
361 * // For some reason due to the nature of the code further calls to
362 * // xSemaphoreTakeRecursive() are made on the same mutex. In real
363 * // code these would not be just sequential calls as this would make
364 * // no sense. Instead the calls are likely to be buried inside
365 * // a more complex call structure.
366 * xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
367 * xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
369 * // The mutex has now been 'taken' three times, so will not be
370 * // available to another task until it has also been given back
371 * // three times. Again it is unlikely that real code would have
372 * // these calls sequentially, but instead buried in a more complex
373 * // call structure. This is just for illustrative purposes.
374 * xSemaphoreGiveRecursive( xMutex );
375 * xSemaphoreGiveRecursive( xMutex );
376 * xSemaphoreGiveRecursive( xMutex );
378 * // Now the mutex can be taken by other tasks.
382 * // We could not obtain the mutex and can therefore not access
383 * // the shared resource safely.
388 * \defgroup xSemaphoreTakeRecursive xSemaphoreTakeRecursive
389 * \ingroup Semaphores
391 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
392 #define xSemaphoreTakeRecursive( xMutex, xBlockTime ) xQueueTakeMutexRecursive( ( xMutex ), ( xBlockTime ) )
398 * xSemaphoreGive( SemaphoreHandle_t xSemaphore );
401 * <i>Macro</i> to release a semaphore. The semaphore must have previously been
402 * created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
403 * xSemaphoreCreateCounting(). and obtained using sSemaphoreTake().
405 * This macro must not be used from an ISR. See xSemaphoreGiveFromISR () for
406 * an alternative which can be used from an ISR.
408 * This macro must also not be used on semaphores created using
409 * xSemaphoreCreateRecursiveMutex().
411 * @param xSemaphore A handle to the semaphore being released. This is the
412 * handle returned when the semaphore was created.
414 * @return pdTRUE if the semaphore was released. pdFALSE if an error occurred.
415 * Semaphores are implemented using queues. An error can occur if there is
416 * no space on the queue to post a message - indicating that the
417 * semaphore was not first obtained correctly.
421 * SemaphoreHandle_t xSemaphore = NULL;
423 * void vATask( void * pvParameters )
425 * // Create the semaphore to guard a shared resource.
426 * xSemaphore = vSemaphoreCreateBinary();
428 * if( xSemaphore != NULL )
430 * if( xSemaphoreGive( xSemaphore ) != pdTRUE )
432 * // We would expect this call to fail because we cannot give
433 * // a semaphore without first "taking" it!
436 * // Obtain the semaphore - don't block if the semaphore is not
437 * // immediately available.
438 * if( xSemaphoreTake( xSemaphore, ( TickType_t ) 0 ) )
440 * // We now have the semaphore and can access the shared resource.
444 * // We have finished accessing the shared resource so can free the
446 * if( xSemaphoreGive( xSemaphore ) != pdTRUE )
448 * // We would not expect this call to fail because we must have
449 * // obtained the semaphore to get here.
455 * \defgroup xSemaphoreGive xSemaphoreGive
456 * \ingroup Semaphores
458 #define xSemaphoreGive( xSemaphore ) xQueueGenericSend( ( QueueHandle_t ) ( xSemaphore ), NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
463 * xSemaphoreGiveRecursive( SemaphoreHandle_t xMutex );
466 * <i>Macro</i> to recursively release, or 'give', a mutex type semaphore.
467 * The mutex must have previously been created using a call to
468 * xSemaphoreCreateRecursiveMutex();
470 * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
471 * macro to be available.
473 * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
475 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
476 * doesn't become available again until the owner has called
477 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
478 * if a task successfully 'takes' the same mutex 5 times then the mutex will
479 * not be available to any other task until it has also 'given' the mutex back
480 * exactly five times.
482 * @param xMutex A handle to the mutex being released, or 'given'. This is the
483 * handle returned by xSemaphoreCreateMutex();
485 * @return pdTRUE if the semaphore was given.
489 * SemaphoreHandle_t xMutex = NULL;
491 * // A task that creates a mutex.
492 * void vATask( void * pvParameters )
494 * // Create the mutex to guard a shared resource.
495 * xMutex = xSemaphoreCreateRecursiveMutex();
498 * // A task that uses the mutex.
499 * void vAnotherTask( void * pvParameters )
501 * // ... Do other things.
503 * if( xMutex != NULL )
505 * // See if we can obtain the mutex. If the mutex is not available
506 * // wait 10 ticks to see if it becomes free.
507 * if( xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 ) == pdTRUE )
509 * // We were able to obtain the mutex and can now access the
510 * // shared resource.
513 * // For some reason due to the nature of the code further calls to
514 * // xSemaphoreTakeRecursive() are made on the same mutex. In real
515 * // code these would not be just sequential calls as this would make
516 * // no sense. Instead the calls are likely to be buried inside
517 * // a more complex call structure.
518 * xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
519 * xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
521 * // The mutex has now been 'taken' three times, so will not be
522 * // available to another task until it has also been given back
523 * // three times. Again it is unlikely that real code would have
524 * // these calls sequentially, it would be more likely that the calls
525 * // to xSemaphoreGiveRecursive() would be called as a call stack
526 * // unwound. This is just for demonstrative purposes.
527 * xSemaphoreGiveRecursive( xMutex );
528 * xSemaphoreGiveRecursive( xMutex );
529 * xSemaphoreGiveRecursive( xMutex );
531 * // Now the mutex can be taken by other tasks.
535 * // We could not obtain the mutex and can therefore not access
536 * // the shared resource safely.
541 * \defgroup xSemaphoreGiveRecursive xSemaphoreGiveRecursive
542 * \ingroup Semaphores
544 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
545 #define xSemaphoreGiveRecursive( xMutex ) xQueueGiveMutexRecursive( ( xMutex ) )
551 * xSemaphoreGiveFromISR(
552 * SemaphoreHandle_t xSemaphore,
553 * BaseType_t *pxHigherPriorityTaskWoken
557 * <i>Macro</i> to release a semaphore. The semaphore must have previously been
558 * created with a call to xSemaphoreCreateBinary() or xSemaphoreCreateCounting().
560 * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
561 * must not be used with this macro.
563 * This macro can be used from an ISR.
565 * @param xSemaphore A handle to the semaphore being released. This is the
566 * handle returned when the semaphore was created.
568 * @param pxHigherPriorityTaskWoken xSemaphoreGiveFromISR() will set
569 * *pxHigherPriorityTaskWoken to pdTRUE if giving the semaphore caused a task
570 * to unblock, and the unblocked task has a priority higher than the currently
571 * running task. If xSemaphoreGiveFromISR() sets this value to pdTRUE then
572 * a context switch should be requested before the interrupt is exited.
574 * @return pdTRUE if the semaphore was successfully given, otherwise errQUEUE_FULL.
578 \#define LONG_TIME 0xffff
579 \#define TICKS_TO_WAIT 10
580 * SemaphoreHandle_t xSemaphore = NULL;
582 * // Repetitive task.
583 * void vATask( void * pvParameters )
587 * // We want this task to run every 10 ticks of a timer. The semaphore
588 * // was created before this task was started.
590 * // Block waiting for the semaphore to become available.
591 * if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )
593 * // It is time to execute.
597 * // We have finished our task. Return to the top of the loop where
598 * // we will block on the semaphore until it is time to execute
599 * // again. Note when using the semaphore for synchronisation with an
600 * // ISR in this manner there is no need to 'give' the semaphore back.
606 * void vTimerISR( void * pvParameters )
608 * static uint8_t ucLocalTickCount = 0;
609 * static BaseType_t xHigherPriorityTaskWoken;
611 * // A timer tick has occurred.
613 * // ... Do other time functions.
615 * // Is it time for vATask () to run?
616 * xHigherPriorityTaskWoken = pdFALSE;
617 * ucLocalTickCount++;
618 * if( ucLocalTickCount >= TICKS_TO_WAIT )
620 * // Unblock the task by releasing the semaphore.
621 * xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
623 * // Reset the count so we release the semaphore again in 10 ticks time.
624 * ucLocalTickCount = 0;
627 * if( xHigherPriorityTaskWoken != pdFALSE )
629 * // We can force a context switch here. Context switching from an
630 * // ISR uses port specific syntax. Check the demo task for your port
631 * // to find the syntax required.
635 * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR
636 * \ingroup Semaphores
638 #define xSemaphoreGiveFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueGiveFromISR( ( QueueHandle_t ) ( xSemaphore ), ( pxHigherPriorityTaskWoken ) )
643 * xSemaphoreTakeFromISR(
644 * SemaphoreHandle_t xSemaphore,
645 * BaseType_t *pxHigherPriorityTaskWoken
649 * <i>Macro</i> to take a semaphore from an ISR. The semaphore must have
650 * previously been created with a call to xSemaphoreCreateBinary() or
651 * xSemaphoreCreateCounting().
653 * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
654 * must not be used with this macro.
656 * This macro can be used from an ISR, however taking a semaphore from an ISR
657 * is not a common operation. It is likely to only be useful when taking a
658 * counting semaphore when an interrupt is obtaining an object from a resource
659 * pool (when the semaphore count indicates the number of resources available).
661 * @param xSemaphore A handle to the semaphore being taken. This is the
662 * handle returned when the semaphore was created.
664 * @param pxHigherPriorityTaskWoken xSemaphoreTakeFromISR() will set
665 * *pxHigherPriorityTaskWoken to pdTRUE if taking the semaphore caused a task
666 * to unblock, and the unblocked task has a priority higher than the currently
667 * running task. If xSemaphoreTakeFromISR() sets this value to pdTRUE then
668 * a context switch should be requested before the interrupt is exited.
670 * @return pdTRUE if the semaphore was successfully taken, otherwise
673 #define xSemaphoreTakeFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueReceiveFromISR( ( QueueHandle_t ) ( xSemaphore ), NULL, ( pxHigherPriorityTaskWoken ) )
678 * SemaphoreHandle_t xSemaphoreCreateMutex( void );
681 * Creates a new mutex type semaphore instance, and returns a handle by which
682 * the new mutex can be referenced.
684 * Internally, within the FreeRTOS implementation, mutex semaphores use a block
685 * of memory, in which the mutex structure is stored. If a mutex is created
686 * using xSemaphoreCreateMutex() then the required memory is automatically
687 * dynamically allocated inside the xSemaphoreCreateMutex() function. (see
688 * https://www.FreeRTOS.org/a00111.html). If a mutex is created using
689 * xSemaphoreCreateMutexStatic() then the application writer must provided the
690 * memory. xSemaphoreCreateMutexStatic() therefore allows a mutex to be created
691 * without using any dynamic memory allocation.
693 * Mutexes created using this function can be accessed using the xSemaphoreTake()
694 * and xSemaphoreGive() macros. The xSemaphoreTakeRecursive() and
695 * xSemaphoreGiveRecursive() macros must not be used.
697 * This type of semaphore uses a priority inheritance mechanism so a task
698 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
699 * semaphore it is no longer required.
701 * Mutex type semaphores cannot be used from within interrupt service routines.
703 * See xSemaphoreCreateBinary() for an alternative implementation that can be
704 * used for pure synchronisation (where one task or interrupt always 'gives' the
705 * semaphore and another always 'takes' the semaphore) and from within interrupt
708 * @return If the mutex was successfully created then a handle to the created
709 * semaphore is returned. If there was not enough heap to allocate the mutex
710 * data structures then NULL is returned.
714 * SemaphoreHandle_t xSemaphore;
716 * void vATask( void * pvParameters )
718 * // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
719 * // This is a macro so pass the variable in directly.
720 * xSemaphore = xSemaphoreCreateMutex();
722 * if( xSemaphore != NULL )
724 * // The semaphore was created successfully.
725 * // The semaphore can now be used.
729 * \defgroup xSemaphoreCreateMutex xSemaphoreCreateMutex
730 * \ingroup Semaphores
732 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
733 #define xSemaphoreCreateMutex() xQueueCreateMutex( queueQUEUE_TYPE_MUTEX )
739 * SemaphoreHandle_t xSemaphoreCreateMutexStatic( StaticSemaphore_t *pxMutexBuffer );
742 * Creates a new mutex type semaphore instance, and returns a handle by which
743 * the new mutex can be referenced.
745 * Internally, within the FreeRTOS implementation, mutex semaphores use a block
746 * of memory, in which the mutex structure is stored. If a mutex is created
747 * using xSemaphoreCreateMutex() then the required memory is automatically
748 * dynamically allocated inside the xSemaphoreCreateMutex() function. (see
749 * https://www.FreeRTOS.org/a00111.html). If a mutex is created using
750 * xSemaphoreCreateMutexStatic() then the application writer must provided the
751 * memory. xSemaphoreCreateMutexStatic() therefore allows a mutex to be created
752 * without using any dynamic memory allocation.
754 * Mutexes created using this function can be accessed using the xSemaphoreTake()
755 * and xSemaphoreGive() macros. The xSemaphoreTakeRecursive() and
756 * xSemaphoreGiveRecursive() macros must not be used.
758 * This type of semaphore uses a priority inheritance mechanism so a task
759 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
760 * semaphore it is no longer required.
762 * Mutex type semaphores cannot be used from within interrupt service routines.
764 * See xSemaphoreCreateBinary() for an alternative implementation that can be
765 * used for pure synchronisation (where one task or interrupt always 'gives' the
766 * semaphore and another always 'takes' the semaphore) and from within interrupt
769 * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,
770 * which will be used to hold the mutex's data structure, removing the need for
771 * the memory to be allocated dynamically.
773 * @return If the mutex was successfully created then a handle to the created
774 * mutex is returned. If pxMutexBuffer was NULL then NULL is returned.
778 * SemaphoreHandle_t xSemaphore;
779 * StaticSemaphore_t xMutexBuffer;
781 * void vATask( void * pvParameters )
783 * // A mutex cannot be used before it has been created. xMutexBuffer is
784 * // into xSemaphoreCreateMutexStatic() so no dynamic memory allocation is
786 * xSemaphore = xSemaphoreCreateMutexStatic( &xMutexBuffer );
788 * // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,
789 * // so there is no need to check it.
792 * \defgroup xSemaphoreCreateMutexStatic xSemaphoreCreateMutexStatic
793 * \ingroup Semaphores
795 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
796 #define xSemaphoreCreateMutexStatic( pxMutexBuffer ) xQueueCreateMutexStatic( queueQUEUE_TYPE_MUTEX, ( pxMutexBuffer ) )
797 #endif /* configSUPPORT_STATIC_ALLOCATION */
803 * SemaphoreHandle_t xSemaphoreCreateRecursiveMutex( void );
806 * Creates a new recursive mutex type semaphore instance, and returns a handle
807 * by which the new recursive mutex can be referenced.
809 * Internally, within the FreeRTOS implementation, recursive mutexs use a block
810 * of memory, in which the mutex structure is stored. If a recursive mutex is
811 * created using xSemaphoreCreateRecursiveMutex() then the required memory is
812 * automatically dynamically allocated inside the
813 * xSemaphoreCreateRecursiveMutex() function. (see
814 * https://www.FreeRTOS.org/a00111.html). If a recursive mutex is created using
815 * xSemaphoreCreateRecursiveMutexStatic() then the application writer must
816 * provide the memory that will get used by the mutex.
817 * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to
818 * be created without using any dynamic memory allocation.
820 * Mutexes created using this macro can be accessed using the
821 * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros. The
822 * xSemaphoreTake() and xSemaphoreGive() macros must not be used.
824 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
825 * doesn't become available again until the owner has called
826 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
827 * if a task successfully 'takes' the same mutex 5 times then the mutex will
828 * not be available to any other task until it has also 'given' the mutex back
829 * exactly five times.
831 * This type of semaphore uses a priority inheritance mechanism so a task
832 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
833 * semaphore it is no longer required.
835 * Mutex type semaphores cannot be used from within interrupt service routines.
837 * See xSemaphoreCreateBinary() for an alternative implementation that can be
838 * used for pure synchronisation (where one task or interrupt always 'gives' the
839 * semaphore and another always 'takes' the semaphore) and from within interrupt
842 * @return xSemaphore Handle to the created mutex semaphore. Should be of type
847 * SemaphoreHandle_t xSemaphore;
849 * void vATask( void * pvParameters )
851 * // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
852 * // This is a macro so pass the variable in directly.
853 * xSemaphore = xSemaphoreCreateRecursiveMutex();
855 * if( xSemaphore != NULL )
857 * // The semaphore was created successfully.
858 * // The semaphore can now be used.
862 * \defgroup xSemaphoreCreateRecursiveMutex xSemaphoreCreateRecursiveMutex
863 * \ingroup Semaphores
865 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )
866 #define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex( queueQUEUE_TYPE_RECURSIVE_MUTEX )
872 * SemaphoreHandle_t xSemaphoreCreateRecursiveMutexStatic( StaticSemaphore_t *pxMutexBuffer );
875 * Creates a new recursive mutex type semaphore instance, and returns a handle
876 * by which the new recursive mutex can be referenced.
878 * Internally, within the FreeRTOS implementation, recursive mutexs use a block
879 * of memory, in which the mutex structure is stored. If a recursive mutex is
880 * created using xSemaphoreCreateRecursiveMutex() then the required memory is
881 * automatically dynamically allocated inside the
882 * xSemaphoreCreateRecursiveMutex() function. (see
883 * https://www.FreeRTOS.org/a00111.html). If a recursive mutex is created using
884 * xSemaphoreCreateRecursiveMutexStatic() then the application writer must
885 * provide the memory that will get used by the mutex.
886 * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to
887 * be created without using any dynamic memory allocation.
889 * Mutexes created using this macro can be accessed using the
890 * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros. The
891 * xSemaphoreTake() and xSemaphoreGive() macros must not be used.
893 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
894 * doesn't become available again until the owner has called
895 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
896 * if a task successfully 'takes' the same mutex 5 times then the mutex will
897 * not be available to any other task until it has also 'given' the mutex back
898 * exactly five times.
900 * This type of semaphore uses a priority inheritance mechanism so a task
901 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
902 * semaphore it is no longer required.
904 * Mutex type semaphores cannot be used from within interrupt service routines.
906 * See xSemaphoreCreateBinary() for an alternative implementation that can be
907 * used for pure synchronisation (where one task or interrupt always 'gives' the
908 * semaphore and another always 'takes' the semaphore) and from within interrupt
911 * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,
912 * which will then be used to hold the recursive mutex's data structure,
913 * removing the need for the memory to be allocated dynamically.
915 * @return If the recursive mutex was successfully created then a handle to the
916 * created recursive mutex is returned. If pxMutexBuffer was NULL then NULL is
921 * SemaphoreHandle_t xSemaphore;
922 * StaticSemaphore_t xMutexBuffer;
924 * void vATask( void * pvParameters )
926 * // A recursive semaphore cannot be used before it is created. Here a
927 * // recursive mutex is created using xSemaphoreCreateRecursiveMutexStatic().
928 * // The address of xMutexBuffer is passed into the function, and will hold
929 * // the mutexes data structures - so no dynamic memory allocation will be
931 * xSemaphore = xSemaphoreCreateRecursiveMutexStatic( &xMutexBuffer );
933 * // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,
934 * // so there is no need to check it.
937 * \defgroup xSemaphoreCreateRecursiveMutexStatic xSemaphoreCreateRecursiveMutexStatic
938 * \ingroup Semaphores
940 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )
941 #define xSemaphoreCreateRecursiveMutexStatic( pxStaticSemaphore ) xQueueCreateMutexStatic( queueQUEUE_TYPE_RECURSIVE_MUTEX, pxStaticSemaphore )
942 #endif /* configSUPPORT_STATIC_ALLOCATION */
947 * SemaphoreHandle_t xSemaphoreCreateCounting( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount );
950 * Creates a new counting semaphore instance, and returns a handle by which the
951 * new counting semaphore can be referenced.
953 * In many usage scenarios it is faster and more memory efficient to use a
954 * direct to task notification in place of a counting semaphore!
955 * https://www.FreeRTOS.org/RTOS-task-notifications.html
957 * Internally, within the FreeRTOS implementation, counting semaphores use a
958 * block of memory, in which the counting semaphore structure is stored. If a
959 * counting semaphore is created using xSemaphoreCreateCounting() then the
960 * required memory is automatically dynamically allocated inside the
961 * xSemaphoreCreateCounting() function. (see
962 * https://www.FreeRTOS.org/a00111.html). If a counting semaphore is created
963 * using xSemaphoreCreateCountingStatic() then the application writer can
964 * instead optionally provide the memory that will get used by the counting
965 * semaphore. xSemaphoreCreateCountingStatic() therefore allows a counting
966 * semaphore to be created without using any dynamic memory allocation.
968 * Counting semaphores are typically used for two things:
970 * 1) Counting events.
972 * In this usage scenario an event handler will 'give' a semaphore each time
973 * an event occurs (incrementing the semaphore count value), and a handler
974 * task will 'take' a semaphore each time it processes an event
975 * (decrementing the semaphore count value). The count value is therefore
976 * the difference between the number of events that have occurred and the
977 * number that have been processed. In this case it is desirable for the
978 * initial count value to be zero.
980 * 2) Resource management.
982 * In this usage scenario the count value indicates the number of resources
983 * available. To obtain control of a resource a task must first obtain a
984 * semaphore - decrementing the semaphore count value. When the count value
985 * reaches zero there are no free resources. When a task finishes with the
986 * resource it 'gives' the semaphore back - incrementing the semaphore count
987 * value. In this case it is desirable for the initial count value to be
988 * equal to the maximum count value, indicating that all resources are free.
990 * @param uxMaxCount The maximum count value that can be reached. When the
991 * semaphore reaches this value it can no longer be 'given'.
993 * @param uxInitialCount The count value assigned to the semaphore when it is
996 * @return Handle to the created semaphore. Null if the semaphore could not be
1001 * SemaphoreHandle_t xSemaphore;
1003 * void vATask( void * pvParameters )
1005 * SemaphoreHandle_t xSemaphore = NULL;
1007 * // Semaphore cannot be used before a call to xSemaphoreCreateCounting().
1008 * // The max value to which the semaphore can count should be 10, and the
1009 * // initial value assigned to the count should be 0.
1010 * xSemaphore = xSemaphoreCreateCounting( 10, 0 );
1012 * if( xSemaphore != NULL )
1014 * // The semaphore was created successfully.
1015 * // The semaphore can now be used.
1019 * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting
1020 * \ingroup Semaphores
1022 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1023 #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ) )
1029 * SemaphoreHandle_t xSemaphoreCreateCountingStatic( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount, StaticSemaphore_t *pxSemaphoreBuffer );
1032 * Creates a new counting semaphore instance, and returns a handle by which the
1033 * new counting semaphore can be referenced.
1035 * In many usage scenarios it is faster and more memory efficient to use a
1036 * direct to task notification in place of a counting semaphore!
1037 * https://www.FreeRTOS.org/RTOS-task-notifications.html
1039 * Internally, within the FreeRTOS implementation, counting semaphores use a
1040 * block of memory, in which the counting semaphore structure is stored. If a
1041 * counting semaphore is created using xSemaphoreCreateCounting() then the
1042 * required memory is automatically dynamically allocated inside the
1043 * xSemaphoreCreateCounting() function. (see
1044 * https://www.FreeRTOS.org/a00111.html). If a counting semaphore is created
1045 * using xSemaphoreCreateCountingStatic() then the application writer must
1046 * provide the memory. xSemaphoreCreateCountingStatic() therefore allows a
1047 * counting semaphore to be created without using any dynamic memory allocation.
1049 * Counting semaphores are typically used for two things:
1051 * 1) Counting events.
1053 * In this usage scenario an event handler will 'give' a semaphore each time
1054 * an event occurs (incrementing the semaphore count value), and a handler
1055 * task will 'take' a semaphore each time it processes an event
1056 * (decrementing the semaphore count value). The count value is therefore
1057 * the difference between the number of events that have occurred and the
1058 * number that have been processed. In this case it is desirable for the
1059 * initial count value to be zero.
1061 * 2) Resource management.
1063 * In this usage scenario the count value indicates the number of resources
1064 * available. To obtain control of a resource a task must first obtain a
1065 * semaphore - decrementing the semaphore count value. When the count value
1066 * reaches zero there are no free resources. When a task finishes with the
1067 * resource it 'gives' the semaphore back - incrementing the semaphore count
1068 * value. In this case it is desirable for the initial count value to be
1069 * equal to the maximum count value, indicating that all resources are free.
1071 * @param uxMaxCount The maximum count value that can be reached. When the
1072 * semaphore reaches this value it can no longer be 'given'.
1074 * @param uxInitialCount The count value assigned to the semaphore when it is
1077 * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,
1078 * which will then be used to hold the semaphore's data structure, removing the
1079 * need for the memory to be allocated dynamically.
1081 * @return If the counting semaphore was successfully created then a handle to
1082 * the created counting semaphore is returned. If pxSemaphoreBuffer was NULL
1083 * then NULL is returned.
1087 * SemaphoreHandle_t xSemaphore;
1088 * StaticSemaphore_t xSemaphoreBuffer;
1090 * void vATask( void * pvParameters )
1092 * SemaphoreHandle_t xSemaphore = NULL;
1094 * // Counting semaphore cannot be used before they have been created. Create
1095 * // a counting semaphore using xSemaphoreCreateCountingStatic(). The max
1096 * // value to which the semaphore can count is 10, and the initial value
1097 * // assigned to the count will be 0. The address of xSemaphoreBuffer is
1098 * // passed in and will be used to hold the semaphore structure, so no dynamic
1099 * // memory allocation will be used.
1100 * xSemaphore = xSemaphoreCreateCounting( 10, 0, &xSemaphoreBuffer );
1102 * // No memory allocation was attempted so xSemaphore cannot be NULL, so there
1103 * // is no need to check its value.
1106 * \defgroup xSemaphoreCreateCountingStatic xSemaphoreCreateCountingStatic
1107 * \ingroup Semaphores
1109 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1110 #define xSemaphoreCreateCountingStatic( uxMaxCount, uxInitialCount, pxSemaphoreBuffer ) xQueueCreateCountingSemaphoreStatic( ( uxMaxCount ), ( uxInitialCount ), ( pxSemaphoreBuffer ) )
1111 #endif /* configSUPPORT_STATIC_ALLOCATION */
1116 * void vSemaphoreDelete( SemaphoreHandle_t xSemaphore );
1119 * Delete a semaphore. This function must be used with care. For example,
1120 * do not delete a mutex type semaphore if the mutex is held by a task.
1122 * @param xSemaphore A handle to the semaphore to be deleted.
1124 * \defgroup vSemaphoreDelete vSemaphoreDelete
1125 * \ingroup Semaphores
1127 #define vSemaphoreDelete( xSemaphore ) vQueueDelete( ( QueueHandle_t ) ( xSemaphore ) )
1132 * TaskHandle_t xSemaphoreGetMutexHolder( SemaphoreHandle_t xMutex );
1135 * If xMutex is indeed a mutex type semaphore, return the current mutex holder.
1136 * If xMutex is not a mutex type semaphore, or the mutex is available (not held
1137 * by a task), return NULL.
1139 * Note: This is a good way of determining if the calling task is the mutex
1140 * holder, but not a good way of determining the identity of the mutex holder as
1141 * the holder may change between the function exiting and the returned value
1144 #define xSemaphoreGetMutexHolder( xSemaphore ) xQueueGetMutexHolder( ( xSemaphore ) )
1149 * TaskHandle_t xSemaphoreGetMutexHolderFromISR( SemaphoreHandle_t xMutex );
1152 * If xMutex is indeed a mutex type semaphore, return the current mutex holder.
1153 * If xMutex is not a mutex type semaphore, or the mutex is available (not held
1154 * by a task), return NULL.
1157 #define xSemaphoreGetMutexHolderFromISR( xSemaphore ) xQueueGetMutexHolderFromISR( ( xSemaphore ) )
1162 * UBaseType_t uxSemaphoreGetCount( SemaphoreHandle_t xSemaphore );
1165 * If the semaphore is a counting semaphore then uxSemaphoreGetCount() returns
1166 * its current count value. If the semaphore is a binary semaphore then
1167 * uxSemaphoreGetCount() returns 1 if the semaphore is available, and 0 if the
1168 * semaphore is not available.
1171 #define uxSemaphoreGetCount( xSemaphore ) uxQueueMessagesWaiting( ( QueueHandle_t ) ( xSemaphore ) )
1173 #endif /* SEMAPHORE_H */