static void prvCreateAndDeleteStaticallyAllocatedCountingSemaphores( void ) { SemaphoreHandle_t xSemaphore; const UBaseType_t uxMaxCount = ( UBaseType_t ) 10; /* StaticSemaphore_t is a publicly accessible structure that has the same size and alignment requirements as the real semaphore structure. It is provided as a mechanism for applications to know the size of the semaphore (which is dependent on the architecture and configuration file settings) without breaking the strict data hiding policy by exposing the real semaphore internals. This StaticSemaphore_t variable is passed into the xSemaphoreCreateCountingStatic() function calls within this function. NOTE: In most usage scenarios now it is faster and more memory efficient to use a direct to task notification instead of a counting semaphore. http://www.freertos.org/RTOS-task-notifications.html */ StaticSemaphore_t xSemaphoreBuffer; /* Create the semaphore. xSemaphoreCreateCountingStatic() has one more parameter than the usual xSemaphoreCreateCounting() function. The paraemter is a pointer to the pre-allocated StaticSemaphore_t structure, which will hold information on the semaphore in an anonymous way. If the pointer is passed as NULL then the structure will be allocated dynamically, just as when xSemaphoreCreateCounting() is called. */ xSemaphore = xSemaphoreCreateCountingStatic( uxMaxCount, 0, &xSemaphoreBuffer ); /* The semaphore handle should equal the static semaphore structure passed into the xSemaphoreCreateBinaryStatic() function. */ configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer ); /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */ prvSanityCheckCreatedSemaphore( xSemaphore, uxMaxCount ); /* Delete the semaphore again so the buffers can be reused. */ vSemaphoreDelete( xSemaphore ); /* The semaphore created above had a statically allocated semaphore structure. Repeat the above using NULL as the third xSemaphoreCreateCountingStatic() parameter so the semaphore structure is instead allocated dynamically. */ xSemaphore = xSemaphoreCreateCountingStatic( uxMaxCount, 0, NULL ); /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */ prvSanityCheckCreatedSemaphore( xSemaphore, uxMaxCount ); /* Delete the semaphore again so the buffers can be reused. */ vSemaphoreDelete( xSemaphore ); /* Ensure lower priority tasks get CPU time. */ vTaskDelay( prvGetNextDelayTime() ); /* Just to show the check task that this task is still executing. */ uxCycleCounter++; }
static void prvCreateAndDeleteStaticallyAllocatedCountingSemaphores( void ) { SemaphoreHandle_t xSemaphore; const UBaseType_t uxMaxCount = ( UBaseType_t ) 10; /* StaticSemaphore_t is a publicly accessible structure that has the same size and alignment requirements as the real semaphore structure. It is provided as a mechanism for applications to know the size of the semaphore (which is dependent on the architecture and configuration file settings) without breaking the strict data hiding policy by exposing the real semaphore internals. This StaticSemaphore_t variable is passed into the xSemaphoreCreateCountingStatic() function calls within this function. NOTE: In most usage scenarios now it is faster and more memory efficient to use a direct to task notification instead of a counting semaphore. http://www.freertos.org/RTOS-task-notifications.html */ StaticSemaphore_t xSemaphoreBuffer; /* Create the semaphore. xSemaphoreCreateCountingStatic() has one more parameter than the usual xSemaphoreCreateCounting() function. The parameter is a pointer to the pre-allocated StaticSemaphore_t structure, which will hold information on the semaphore in an anonymous way. If the pointer is passed as NULL then the structure will be allocated dynamically, just as when xSemaphoreCreateCounting() is called. */ xSemaphore = xSemaphoreCreateCountingStatic( uxMaxCount, 0, &xSemaphoreBuffer ); /* The semaphore handle should equal the static semaphore structure passed into the xSemaphoreCreateBinaryStatic() function. */ configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer ); /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */ prvSanityCheckCreatedSemaphore( xSemaphore, uxMaxCount ); /* Delete the semaphore again so the buffers can be reused. */ vSemaphoreDelete( xSemaphore ); #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) { /* Now do the same but using dynamically allocated buffers to ensure the delete functions are working correctly in both the static and dynamic allocation cases. */ xSemaphore = xSemaphoreCreateCounting( uxMaxCount, 0 ); configASSERT( xSemaphore != NULL ); prvSanityCheckCreatedSemaphore( xSemaphore, uxMaxCount ); vSemaphoreDelete( xSemaphore ); } #endif }