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 }
static void prvCreateAndDeleteStaticallyAllocatedBinarySemaphores( void ) { SemaphoreHandle_t xSemaphore; /* 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 xSemaphoreCreateBinaryStatic() 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 binary semaphore. http://www.freertos.org/RTOS-task-notifications.html */ StaticSemaphore_t xSemaphoreBuffer; /* Create the semaphore. xSemaphoreCreateBinaryStatic() has one more parameter than the usual xSemaphoreCreateBinary() 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 xSemaphoreCreateBinary() is called. */ xSemaphore = xSemaphoreCreateBinaryStatic( &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, staticBINARY_SEMAPHORE_MAX_COUNT ); /* Delete the semaphore again so the buffers can be reused. */ vSemaphoreDelete( xSemaphore ); /* Now do the same using a dynamically allocated semaphore to check the delete function is working correctly in both the static and dynamic allocation cases. */ #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) { xSemaphore = xSemaphoreCreateBinary(); configASSERT( xSemaphore != NULL ); prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT ); vSemaphoreDelete( xSemaphore ); } #endif /* There isn't a static version of the old and deprecated vSemaphoreCreateBinary() macro (because its deprecated!), but check it is still functioning correctly. */ #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) { vSemaphoreCreateBinary( xSemaphore ); /* The macro starts with the binary semaphore available, but the test function expects it to be unavailable. */ if( xSemaphoreTake( xSemaphore, staticDONT_BLOCK ) == pdFAIL ) { xErrorOccurred = pdTRUE; } prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT ); vSemaphoreDelete( xSemaphore ); } #endif }
static void prvCreateAndDeleteStaticallyAllocatedMutexes( void ) { SemaphoreHandle_t xSemaphore; BaseType_t xReturned; /* 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 xSemaphoreCreateMutexStatic() function calls within this function. */ StaticSemaphore_t xSemaphoreBuffer; /* Create the semaphore. xSemaphoreCreateMutexStatic() has one more parameter than the usual xSemaphoreCreateMutex() 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 xSemaphoreCreateMutex() is called. */ xSemaphore = xSemaphoreCreateMutexStatic( &xSemaphoreBuffer ); /* The semaphore handle should equal the static semaphore structure passed into the xSemaphoreCreateMutexStatic() function. */ configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer ); /* Take the mutex so the mutex is in the state expected by the prvSanityCheckCreatedSemaphore() function. */ xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK ); if( xReturned != pdPASS ) { xErrorOccurred = pdTRUE; } /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */ prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT ); /* Delete the semaphore again so the buffers can be reused. */ vSemaphoreDelete( xSemaphore ); /* Now do the same using a dynamically allocated mutex to ensure the delete function is working correctly in both the static and dynamic allocation cases. */ #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) { xSemaphore = xSemaphoreCreateMutex(); /* The semaphore handle should equal the static semaphore structure passed into the xSemaphoreCreateMutexStatic() function. */ configASSERT( xSemaphore != NULL ); /* Take the mutex so the mutex is in the state expected by the prvSanityCheckCreatedSemaphore() function. */ xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK ); if( xReturned != pdPASS ) { xErrorOccurred = pdTRUE; } /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */ prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT ); /* Delete the semaphore again so the buffers can be reused. */ vSemaphoreDelete( xSemaphore ); } #endif }
static void prvCreateAndDeleteStaticallyAllocatedMutexes( void ) { SemaphoreHandle_t xSemaphore; BaseType_t xReturned; /* 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 xSemaphoreCreateMutexStatic() function calls within this function. */ StaticSemaphore_t xSemaphoreBuffer; /* Create the semaphore. xSemaphoreCreateMutexStatic() has one more parameter than the usual xSemaphoreCreateMutex() 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 xSemaphoreCreateMutex() is called. */ xSemaphore = xSemaphoreCreateMutexStatic( &xSemaphoreBuffer ); /* The semaphore handle should equal the static semaphore structure passed into the xSemaphoreCreateMutexStatic() function. */ configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer ); /* Take the mutex so the mutex is in the state expected by the prvSanityCheckCreatedSemaphore() function. */ xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK ); if( xReturned != pdPASS ) { xErrorOccurred = pdTRUE; } /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */ prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT ); /* 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 xSemaphoreCreateMutexStatic() parameter so the semaphore structure is instead allocated dynamically. */ xSemaphore = xSemaphoreCreateMutexStatic( NULL ); /* Take the mutex so the mutex is in the state expected by the prvSanityCheckCreatedSemaphore() function. */ xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK ); if( xReturned != pdPASS ) { xErrorOccurred = pdTRUE; } /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */ prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT ); /* 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++; }