-/*\r
- * FreeRTOS Kernel V10.2.1\r
- * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.\r
- *\r
- * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
- * this software and associated documentation files (the "Software"), to deal in\r
- * the Software without restriction, including without limitation the rights to\r
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
- * the Software, and to permit persons to whom the Software is furnished to do so,\r
- * subject to the following conditions:\r
- *\r
- * The above copyright notice and this permission notice shall be included in all\r
- * copies or substantial portions of the Software.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
- *\r
- * http://www.FreeRTOS.org\r
- * http://aws.amazon.com/freertos\r
- *\r
- * 1 tab == 4 spaces!\r
- */\r
-\r
-/* Kernel includes. */\r
-#include "FreeRTOS.h"\r
-#include "task.h"\r
-\r
-/* Standard includes. */\r
-#include <stdio.h>\r
-\r
-/* IoT SDK includes. */\r
-#include "iot_taskpool.h"\r
-\r
-/* The priority at which that tasks in the task pool (the worker tasks) get\r
-created. */\r
-#define tpTASK_POOL_WORKER_PRIORITY 1\r
-\r
-/*\r
- * Prototypes for the functions that demonstrate the task pool API.\r
- */\r
-static void prvExample_BasicSingleJob( void );\r
-static void prvExample_DeferredSingleJob( void );\r
-static void prvExample_BasicRecyclableJob( void );\r
-static void prvExample_ReuseRecyclableJobFromLowPriorityTask( void );\r
-static void prvExample_ReuseRecyclableJobFromHighPriorityTask( void );\r
-\r
-/* Prototypes of the callback functions used in the examples. */\r
-static void prvSimpleTaskNotifyCallback( IotTaskPool_t pTaskPool, IotTaskPoolJob_t pJob, void *pUserContext );\r
-\r
-/*\r
- * Prototypes for the standard FreeRTOS application hook (callback) functions\r
- * implemented within this file. See http://www.freertos.org/a00016.html .\r
- */\r
-void vApplicationMallocFailedHook( void );\r
-void vApplicationIdleHook( void );\r
-void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
-void vApplicationTickHook( void );\r
-void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );\r
-void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize );\r
-\r
-/*\r
- * The task used to demonstrate the task pool API.\r
- */\r
-static void prvTaskPoolDemoTask( void *pvParameters );\r
-\r
-static const IotTaskPoolInfo_t xTaskPoolParameters = {\r
- /* Minimum number of threads in a task pool. */\r
- 2,\r
- /* Maximum number of threads in a task pool. */\r
- 2,\r
- /* Stack size for every task pool thread - in words, not bytes. */\r
- configMINIMAL_STACK_SIZE,\r
- /* Priority for every task pool thread. */\r
- tpTASK_POOL_WORKER_PRIORITY,\r
- };\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-int main( void )\r
-{\r
- /* This example uses a single application task, which in turn is used to\r
- create and send jobs to task pool tasks. */\r
- xTaskCreate( prvTaskPoolDemoTask, /* Function that implements the task. */\r
- "PoolDemo", /* Text name for the task - only used for debugging. */\r
- configMINIMAL_STACK_SIZE, /* Size of stack (in words, not bytes) to allocate for the task. */\r
- NULL, /* Task parameter - not used in this case. */\r
- tskIDLE_PRIORITY, /* Task priority, must be between 0 and configMAX_PRIORITIES - 1. */\r
- NULL ); /* Used to pass out a handle to the created tsak - not used in this case. */\r
-\r
- vTaskStartScheduler();\r
-\r
- /* Should not reach here as vTaskStartScheduler() will only return if there\r
- was insufficient FreeRTOS heap memory to create the Idle or Timer\r
- Daemon task. */\r
- return 0;\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvTaskPoolDemoTask( void *pvParameters )\r
-{\r
-IotTaskPoolError_t xResult;\r
-uint32_t ulLoops;\r
-\r
- /* Remove compiler warnings about unused parameters. */\r
- ( void ) pvParameters;\r
-\r
- /* The task pool must be created before it can be used. */\r
- xResult = IotTaskPool_CreateSystemTaskPool( &xTaskPoolParameters );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- /* Attempting to create the task pool again should then appear to succeed\r
- (in case it is initialised by more than one library), but have no effect. */\r
- xResult = IotTaskPool_CreateSystemTaskPool( &xTaskPoolParameters );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- for( ;; )\r
- {\r
- /* Demonstrate the most basic use case where a non persistent job is\r
- created and scheduled to run immediately. The task pool worker tasks\r
- (in which the job callback function executes) have a priority above the\r
- priority of this task so the job's callback executes as soon as it is\r
- scheduled. */\r
- prvExample_BasicSingleJob();\r
-\r
- /* Demonstrate a job being scheduled to run at some time in the\r
- future, and how a job scheduled to run in the future can be cancelled if\r
- it has not yet started executing. */\r
- prvExample_DeferredSingleJob();\r
-\r
- /* Demonstrate the most basic use of a recyclable job. This is similar\r
- to prvExample_BasicSingleJob() but using a recyclable job. Creating a\r
- recyclable job will re-use a previously created and now spare job from\r
- the task pool's job cache if one is available, or otherwise dynamically\r
- create a new job if a spare job is not available in the cache but space\r
- remains in the cache. */\r
- prvExample_BasicRecyclableJob();\r
-\r
- /* Demonstrate multiple recyclable jobs being created, used, and then\r
- re-used. In this the task pool worker tasks (in which the job callback\r
- functions execute) have a priority above the priority of this task so\r
- the job's callback functions execute as soon as they are scheduled. */\r
- prvExample_ReuseRecyclableJobFromLowPriorityTask();\r
-\r
- /* Again demonstrate multiple recyclable jobs being used, but this time\r
- the priority of the task pool worker tasks (in which the job callback\r
- functions execute) are lower than the priority of this task so the job's\r
- callback functions don't execute until this task enteres the blocked\r
- state. */\r
- prvExample_ReuseRecyclableJobFromHighPriorityTask();\r
-\r
- ulLoops++;\r
- if( ( ulLoops % 10UL ) == 0 )\r
- {\r
- printf( "Performed %u successful iterations.\r\n", ulLoops );\r
- fflush( stdout );\r
- }\r
- }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvSimpleTaskNotifyCallback( IotTaskPool_t pTaskPool, IotTaskPoolJob_t pJob, void *pUserContext )\r
-{\r
-TaskHandle_t xTaskToNotify = ( TaskHandle_t ) pUserContext;\r
-\r
- /* Remove warnings about unused parameters. */\r
- ( void ) pTaskPool;\r
- ( void ) pJob;\r
-\r
- /* Notify the task that created this job. */\r
- xTaskNotifyGive( xTaskToNotify );\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvExample_BasicSingleJob( void )\r
-{\r
-IotTaskPoolJobStorage_t xJobStorage;\r
-IotTaskPoolJob_t xJob;\r
-IotTaskPoolError_t xResult;\r
-uint32_t ulReturn;\r
-const uint32_t ulNoFlags = 0UL;\r
-const TickType_t xNoDelay = ( TickType_t ) 0;\r
-size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize();\r
-IotTaskPoolJobStatus_t xJobStatus;\r
-\r
- /* Don't expect any notifications to be pending yet. */\r
- configASSERT( ulTaskNotifyTake( pdTRUE, 0 ) == 0 );\r
-\r
- /* Create and schedule a job using the handle of this task as the job's\r
- context and the function that sends a notification to the task handle as\r
- the jobs callback function. The job is created using storage allocated on\r
- the stack of this function - so no memory is allocated. */\r
- xResult = IotTaskPool_CreateJob( prvSimpleTaskNotifyCallback, /* Callback function. */\r
- ( void * ) xTaskGetCurrentTaskHandle(), /* Job context. */\r
- &xJobStorage,\r
- &xJob );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- /* The job has been created but not scheduled so is now ready. */\r
- IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );\r
- configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_READY );\r
-\r
- /* This is not a persistent (recyclable) job and its storage is on the\r
- stack of this function, so the amount of heap space available should not\r
- have chanced since entering this function. */\r
- configASSERT( xFreeHeapBeforeCreatingJob == xPortGetFreeHeapSize() );\r
-\r
- /* In the full task pool implementation the first parameter is used to\r
- pass the handle of the task pool to schedule. The lean task pool\r
- implementation used in this demo only supports a single task pool, which\r
- is created internally within the library, so the first parameter is NULL. */\r
- xResult = IotTaskPool_Schedule( NULL, xJob, ulNoFlags );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- /* Look for the notification coming from the job's callback function. The\r
- priority of the task pool worker task that executes the callback is higher\r
- than the priority of this task so a block time is not needed - the task pool\r
- worker task pre-empts this task and sends the notification (from the job's\r
- callback) as soon as the job is scheduled. */\r
- ulReturn = ulTaskNotifyTake( pdTRUE, xNoDelay );\r
- configASSERT( ulReturn );\r
-\r
- /* The job's callback has executed so the job has now completed. */\r
- IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );\r
- configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_COMPLETED );\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvExample_DeferredSingleJob( void )\r
-{\r
-IotTaskPoolJobStorage_t xJobStorage;\r
-IotTaskPoolJob_t xJob;\r
-IotTaskPoolError_t xResult;\r
-uint32_t ulReturn;\r
-const uint32_t ulShortDelay_ms = 100UL;\r
-const TickType_t xNoDelay = ( TickType_t ) 0, xAllowableMargin = ( TickType_t ) 5; /* Large margin for Windows port, which is not real time. */\r
-TickType_t xTimeBefore, xElapsedTime, xShortDelay_ticks;\r
-size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize();\r
-IotTaskPoolJobStatus_t xJobStatus;\r
-\r
- /* Don't expect any notifications to be pending yet. */\r
- configASSERT( ulTaskNotifyTake( pdTRUE, 0 ) == 0 );\r
-\r
- /* Create a job using the handle of this task as the job's context and the\r
- function that sends a notification to the task handle as the jobs callback\r
- function. The job is created using storage allocated on the stack of this\r
- function - so no memory is allocated. */\r
- xResult = IotTaskPool_CreateJob( prvSimpleTaskNotifyCallback, /* Callback function. */\r
- ( void * ) xTaskGetCurrentTaskHandle(), /* Job context. */\r
- &xJobStorage,\r
- &xJob );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- /* The job has been created but not scheduled so is now ready. */\r
- IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );\r
- configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_READY );\r
-\r
- /* This is not a persistent (recyclable) job and its storage is on the\r
- stack of this function, so the amount of heap space available should not\r
- have chanced since entering this function. */\r
- configASSERT( xFreeHeapBeforeCreatingJob == xPortGetFreeHeapSize() );\r
-\r
- /* Schedule the job to run its callback in xShortDelay_ms milliseconds time.\r
- In the full task pool implementation the first parameter is used to pass the\r
- handle of the task pool to schedule. The lean task pool implementation used\r
- in this demo only supports a single task pool, which is created internally\r
- within the library, so the first parameter is NULL. */\r
- xResult = IotTaskPool_ScheduleDeferred( NULL, xJob, ulShortDelay_ms );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- /* The scheduled job should not have executed yet, so don't expect any\r
- notifications and expect the job's status to be 'deferred'. */\r
- ulReturn = ulTaskNotifyTake( pdTRUE, xNoDelay );\r
- configASSERT( ulReturn == 0 );\r
- IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );\r
- configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_DEFERRED );\r
-\r
- /* As the job has not yet been executed it can be stopped. */\r
- xResult = IotTaskPool_TryCancel( NULL, xJob, &xJobStatus );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
- IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );\r
- configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_CANCELED );\r
-\r
- /* Schedule the job again, and this time wait until its callback is\r
- executed (the callback function sends a notification to this task) to see\r
- that it executes at the right time. */\r
- xTimeBefore = xTaskGetTickCount();\r
- xResult = IotTaskPool_ScheduleDeferred( NULL, xJob, ulShortDelay_ms );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- /* Wait twice the deferred execution time to ensure the callback is executed\r
- before the call below times out. */\r
- ulReturn = ulTaskNotifyTake( pdTRUE, pdMS_TO_TICKS( ulShortDelay_ms * 2UL ) );\r
- xElapsedTime = xTaskGetTickCount() - xTimeBefore;\r
-\r
- /* A single notification should not have been received... */\r
- configASSERT( ulReturn == 1 );\r
-\r
- /* ...and the time since scheduling the job should be greater than or\r
- equal to the deferred execution time - which is converted to ticks for\r
- comparison. */\r
- xShortDelay_ticks = pdMS_TO_TICKS( ulShortDelay_ms );\r
- configASSERT( ( xElapsedTime >= xShortDelay_ticks ) && ( xElapsedTime < ( xShortDelay_ticks + xAllowableMargin ) ) );\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvExample_BasicRecyclableJob( void )\r
-{\r
-IotTaskPoolJob_t xJob;\r
-IotTaskPoolError_t xResult;\r
-uint32_t ulReturn;\r
-const uint32_t ulNoFlags = 0UL;\r
-const TickType_t xNoDelay = ( TickType_t ) 0;\r
-size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize();\r
-\r
- /* Don't expect any notifications to be pending yet. */\r
- configASSERT( ulTaskNotifyTake( pdTRUE, 0 ) == 0 );\r
-\r
- /* Create and schedule a job using the handle of this task as the job's\r
- context and the function that sends a notification to the task handle as\r
- the jobs callback function. The job is created as a recyclable job and in\r
- this case the memory used to hold the job status is allocated inside the\r
- create function. As the job is persistent it can be used multiple times,\r
- as demonstrated in other examples within this demo. In the full task pool\r
- implementation the first parameter is used to pass the handle of the task\r
- pool this recyclable job is to be associated with. In the lean\r
- implementation of the task pool used by this demo there is only one task\r
- pool (the system task pool created within the task pool library) so the\r
- first parameter is NULL. */\r
- xResult = IotTaskPool_CreateRecyclableJob( NULL,\r
- prvSimpleTaskNotifyCallback,\r
- (void * ) xTaskGetCurrentTaskHandle(),\r
- &xJob );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- /* This recyclable job is persistent, and in this case created dynamically,\r
- so expect there to be less heap space then when entering the function. */\r
- configASSERT( xPortGetFreeHeapSize() < xFreeHeapBeforeCreatingJob );\r
-\r
- /* In the full task pool implementation the first parameter is used to\r
- pass the handle of the task pool to schedule. The lean task pool\r
- implementation used in this demo only supports a single task pool, which\r
- is created internally within the library, so the first parameter is NULL. */\r
- xResult = IotTaskPool_Schedule( NULL, xJob, ulNoFlags );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- /* Look for the notification coming from the job's callback function. The\r
- priority of the task pool worker task that executes the callback is higher\r
- than the priority of this task so a block time is not needed - the task pool\r
- worker task pre-empts this task and sends the notification (from the job's\r
- callback) as soon as the job is scheduled. */\r
- ulReturn = ulTaskNotifyTake( pdTRUE, xNoDelay );\r
- configASSERT( ulReturn );\r
-\r
- /* Clean up recyclable job. In the full implementation of the task pool\r
- the first parameter is used to pass a handle to the task pool the job is\r
- associated with. In the lean implementation of the task pool used by this\r
- demo there is only one task pool (the system task pool created in the\r
- task pool library itself) so the first parameter is NULL. */\r
- IotTaskPool_DestroyRecyclableJob( NULL, xJob );\r
-\r
- /* Once the job has been deleted the memory used to hold the job is\r
- returned, so the available heap should be exactly as when entering this\r
- function. */\r
- configASSERT( xPortGetFreeHeapSize() == xFreeHeapBeforeCreatingJob );\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvExample_ReuseRecyclableJobFromLowPriorityTask( void )\r
-{\r
-IotTaskPoolError_t xResult;\r
-uint32_t x, xIndex, ulNotificationValue;\r
-const uint32_t ulJobsToCreate = 5UL, ulNoFlags = 0UL;\r
-IotTaskPoolJob_t xJobs[ ulJobsToCreate ];\r
-size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize();\r
-IotTaskPoolJobStatus_t xJobStatus;\r
-\r
- /* Don't expect any notifications to be pending yet. */\r
- configASSERT( ulTaskNotifyTake( pdTRUE, 0 ) == 0 );\r
-\r
- /* Create ulJobsToCreate jobs using the handle of this task as the job's\r
- context and the function that sends a notification to the task handle as\r
- the jobs callback function. The jobs are created as a recyclable job and\r
- in this case the memory to store the job information is allocated within\r
- the create function as at this time there are no recyclable jobs in the\r
- task pool jobs cache. As the jobs are persistent they can be used multiple\r
- times. In the full task pool implementation the first parameter is used to\r
- pass the handle of the task pool this recyclable job is to be associated\r
- with. In the lean implementation of the task pool used by this demo there\r
- is only one task pool (the system task pool created within the task pool\r
- library) so the first parameter is NULL. */\r
- for( x = 0; x < ulJobsToCreate; x++ )\r
- {\r
- xResult = IotTaskPool_CreateRecyclableJob( NULL,\r
- prvSimpleTaskNotifyCallback,\r
- (void * ) xTaskGetCurrentTaskHandle(),\r
- &( xJobs[ x ] ) );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- /* The job has been created but not scheduled so is now ready. */\r
- IotTaskPool_GetStatus( NULL, xJobs[ x ], &xJobStatus );\r
- configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_READY );\r
- }\r
-\r
- /* Demonstrate that the jobs can be recycled by performing twice the number\r
- of iterations of scheduling jobs than there actually are created jobs. This\r
- works because the task pool task priorities are above the priority of this\r
- task, so the tasks that run the jobs pre-empt this task as soon as a job is\r
- ready. */\r
- for( x = 0; x < ( ulJobsToCreate * 2UL ); x++ )\r
- {\r
- /* Make sure array index does not go out of bounds. */\r
- xIndex = x % ulJobsToCreate;\r
-\r
- xResult = IotTaskPool_Schedule( NULL, xJobs[ xIndex ], ulNoFlags );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- /* The priority of the task pool task(s) is higher than the priority\r
- of this task, so the job's callback function should have already\r
- executed, sending a notification to this task, and incrementing this\r
- task's notification value. */\r
- xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */\r
- 0UL, /* Don't clear any bits on exit. */\r
- &ulNotificationValue, /* Obtain the notification value. */\r
- 0UL ); /* No block time, return immediately. */\r
- configASSERT( ulNotificationValue == ( x + 1 ) );\r
-\r
- /* The job's callback has executed so the job is now completed. */\r
- IotTaskPool_GetStatus( NULL, xJobs[ xIndex ], &xJobStatus );\r
- configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_COMPLETED );\r
-\r
- /* To leave the list of jobs empty we can stop re-creating jobs half\r
- way through iterations of this loop. */\r
- if( x < ulJobsToCreate )\r
- {\r
- /* Recycle the job so it can be used again. In the full task pool\r
- implementation the first parameter is used to pass the handle of the\r
- task pool this job will be associated with. In this lean task pool\r
- implementation only the system task pool exists (the task pool created\r
- internally to the task pool library) so the first parameter is just\r
- passed as NULL. *//*_RB_ Why not recycle it automatically? */\r
- IotTaskPool_RecycleJob( NULL, xJobs[ xIndex ] );\r
- xResult = IotTaskPool_CreateRecyclableJob( NULL,\r
- prvSimpleTaskNotifyCallback,\r
- (void * ) xTaskGetCurrentTaskHandle(),\r
- &( xJobs[ xIndex ] ) );\r
- }\r
- }\r
-\r
- /* Clear all the notification value bits again. */\r
- xTaskNotifyWait( portMAX_DELAY, /* Clear all bits on entry - portMAX_DELAY is used as it is a portable way of having all bits set. */\r
- 0UL, /* Don't clear any bits on exit. */\r
- NULL, /* Don't need the notification value this time. */\r
- 0UL ); /* No block time, return immediately. */\r
- configASSERT( ulTaskNotifyTake( pdTRUE, 0 ) == 0 );\r
-\r
- /* Clean up all the recyclable job. In the full implementation of the task\r
- pool the first parameter is used to pass a handle to the task pool the job\r
- is associated with. In the lean implementation of the task pool used by\r
- this demo there is only one task pool (the system task pool created in the\r
- task pool library itself) so the first parameter is NULL. */\r
- for( x = 0; x < ulJobsToCreate; x++ )\r
- {\r
- xResult = IotTaskPool_DestroyRecyclableJob( NULL, xJobs[ x ] );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- /* Attempting to destroy the same job twice will fail. */\r
-//_RB_ vPortFree() asserts because it attempts to free memory again. xResult = IotTaskPool_DestroyRecyclableJob( NULL, xJobs[ x ] );\r
-// configASSERT( xResult != IOT_TASKPOOL_SUCCESS );\r
- }\r
-\r
- /* Once the job has been deleted the memory used to hold the job is\r
- returned, so the available heap should be exactly as when entering this\r
- function. */\r
- configASSERT( xPortGetFreeHeapSize() == xFreeHeapBeforeCreatingJob );\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvExample_ReuseRecyclableJobFromHighPriorityTask( void )\r
-{\r
-IotTaskPoolError_t xResult;\r
-uint32_t x, ulNotificationValue;\r
-const uint32_t ulJobsToCreate = 5UL;\r
-const uint32_t ulNoFlags = 0UL;\r
-IotTaskPoolJob_t xJobs[ ulJobsToCreate ];\r
-IotTaskPoolJobStorage_t xJobStorage[ ulJobsToCreate ];\r
-size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize();\r
-TickType_t xShortDelay = pdMS_TO_TICKS( 150 );\r
-IotTaskPoolJobStatus_t xJobStatus;\r
-\r
- /* Don't expect any notifications to be pending yet. */\r
- configASSERT( ulTaskNotifyTake( pdTRUE, 0 ) == 0 );\r
-\r
- /* prvExample_ReuseRecyclableJobFromLowPriorityTask() executes in a task\r
- that has a lower [task] priority than the task pool's worker tasks.\r
- Therefore a talk pool worker preempts the task that calls\r
- prvExample_ReuseRecyclableJobFromHighPriorityTask() as soon as the job is\r
- scheduled. prvExample_ReuseRecyclableJobFromHighPriorityTask() reverses the\r
- priorities - prvExample_ReuseRecyclableJobFromHighPriorityTask() raises its\r
- priority to above the task pool's worker tasks, so the worker tasks do not\r
- execute until the calling task enters the blocked state. First raise the\r
- priority - passing NULL means raise the priority of the calling task. */\r
- vTaskPrioritySet( NULL, tpTASK_POOL_WORKER_PRIORITY + 1 );\r
-\r
- /* Create ulJobsToCreate jobs using the handle of this task as the job's\r
- context and the function that sends a notification to the task handle as\r
- the jobs callback function. */\r
- for( x = 0; x < ulJobsToCreate; x++ )\r
- {\r
- xResult = IotTaskPool_CreateJob( prvSimpleTaskNotifyCallback, /* Callback function. */\r
- ( void * ) xTaskGetCurrentTaskHandle(), /* Job context. */\r
- &( xJobStorage[ x ] ),\r
- &( xJobs[ x ] ) );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- /* This is not a persistent (recyclable) job and its storage is on the\r
- stack of this function, so the amount of heap space available should not\r
- have chanced since entering this function. */\r
- configASSERT( xFreeHeapBeforeCreatingJob == xPortGetFreeHeapSize() );\r
- }\r
-\r
- for( x = 0; x < ulJobsToCreate; x++ )\r
- {\r
- /* Schedule the next job. */\r
- xResult = IotTaskPool_Schedule( NULL, xJobs[ x ], ulNoFlags );\r
- configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
-\r
- /* Although scheduled, the job's callback has not executed, so the job\r
- reports itself as scheduled. */\r
- IotTaskPool_GetStatus( NULL, xJobs[ x ], &xJobStatus );\r
- configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_SCHEDULED );\r
-\r
- /* The priority of the task pool task(s) is lower than the priority\r
- of this task, so the job's callback function should not have executed\r
- yes, so don't expect the notification value for this task to have\r
- changed. */\r
- xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */\r
- 0UL, /* Don't clear any bits on exit. */\r
- &ulNotificationValue, /* Obtain the notification value. */\r
- 0UL ); /* No block time, return immediately. */\r
- configASSERT( ulNotificationValue == 0 );\r
- }\r
-\r
- /* At this point there are ulJobsToCreate scheduled, but none have executed\r
- their callbacks because the priority of this task is higher than the\r
- priority of the task pool worker threads. When this task blocks to wait for\r
- a notification a worker thread will be able to executes - but as soon as its\r
- callback function sends a notification to this task this task will\r
- preempt it (because it has a higher priority) so this task only expects to\r
- receive one notification at a time. */\r
- for( x = 0; x < ulJobsToCreate; x++ )\r
- {\r
- xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */\r
- 0UL, /* Don't clear any bits on exit. */\r
- &ulNotificationValue, /* Obtain the notification value. */\r
- xShortDelay ); /* Short delay to allow a task pool worker to execute. */\r
- configASSERT( ulNotificationValue == ( x + 1 ) );\r
- }\r
-\r
- /* All the scheduled jobs have now executed, so waiting for another\r
- notification should timeout without the notification value changing. */\r
- xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */\r
- 0UL, /* Don't clear any bits on exit. */\r
- &ulNotificationValue, /* Obtain the notification value. */\r
- xShortDelay ); /* Short delay to allow a task pool worker to execute. */\r
- configASSERT( ulNotificationValue == x );\r
-\r
- /* Reset the priority of this task and clear the notifications ready for the\r
- next example. */\r
- vTaskPrioritySet( NULL, tskIDLE_PRIORITY );\r
- xTaskNotifyWait( portMAX_DELAY, /* Clear all bits on entry - portMAX_DELAY is used as it is a portable way of having all bits set. */\r
- 0UL, /* Don't clear any bits on exit. */\r
- NULL, /* Don't need the notification value this time. */\r
- 0UL ); /* No block time, return immediately. */\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-void vApplicationMallocFailedHook( void )\r
-{\r
- /* vApplicationMallocFailedHook() will only be called if\r
- configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook\r
- function that will get called if a call to pvPortMalloc() fails.\r
- pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
- timer or semaphore is created. It is also called by various parts of the\r
- demo application. If heap_1.c, heap_2.c or heap_4.c is being used, then the\r
- size of the heap available to pvPortMalloc() is defined by\r
- configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()\r
- API function can be used to query the size of free heap space that remains\r
- (although it does not provide information on how the remaining heap might be\r
- fragmented). See http://www.freertos.org/a00111.html for more\r
- information. */\r
- vAssertCalled( __LINE__, __FILE__ );\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-void vApplicationIdleHook( void )\r
-{\r
- /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
- to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle\r
- task. It is essential that code added to this hook function never attempts\r
- to block in any way (for example, call xQueueReceive() with a block time\r
- specified, or call vTaskDelay()). If application tasks make use of the\r
- vTaskDelete() API function to delete themselves then it is also important\r
- that vApplicationIdleHook() is permitted to return to its calling function,\r
- because it is the responsibility of the idle task to clean up memory\r
- allocated by the kernel to any task that has since deleted itself. */\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
-{\r
- ( void ) pcTaskName;\r
- ( void ) pxTask;\r
-\r
- /* Run time stack overflow checking is performed if\r
- configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook\r
- function is called if a stack overflow is detected. This function is\r
- provided as an example only as stack overflow checking does not function\r
- when running the FreeRTOS Windows port. */\r
- vAssertCalled( __LINE__, __FILE__ );\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-void vApplicationTickHook( void )\r
-{\r
- /* This function will be called by each tick interrupt if\r
- configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be\r
- added here, but the tick hook is called from an interrupt context, so\r
- code must not attempt to block, and only the interrupt safe FreeRTOS API\r
- functions can be used (those that end in FromISR()). */\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-void vApplicationDaemonTaskStartupHook( void )\r
-{\r
- /* This function will be called once only, when the daemon task starts to\r
- execute (sometimes called the timer task). This is useful if the\r
- application includes initialisation code that would benefit from executing\r
- after the scheduler has been started. */\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-void vAssertCalled( unsigned long ulLine, const char * const pcFileName )\r
-{\r
-volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;\r
-\r
- /* Called if an assertion passed to configASSERT() fails. See\r
- http://www.freertos.org/a00110.html#configASSERT for more information. */\r
-\r
- /* Parameters are not used. */\r
- ( void ) ulLine;\r
- ( void ) pcFileName;\r
-\r
-\r
- taskENTER_CRITICAL();\r
- {\r
- printf( "Assert hit on line %lu of %s\r\n", ulLine, pcFileName );\r
- fflush( stdout );\r
-\r
- /* You can step out of this function to debug the assertion by using\r
- the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero\r
- value. */\r
- while( ulSetToNonZeroInDebuggerToContinue == 0 )\r
- {\r
- __asm volatile( "NOP" );\r
- __asm volatile( "NOP" );\r
- }\r
- }\r
- taskEXIT_CRITICAL();\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-/* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an\r
-implementation of vApplicationGetIdleTaskMemory() to provide the memory that is\r
-used by the Idle task. */\r
-void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )\r
-{\r
-/* If the buffers to be provided to the Idle task are declared inside this\r
-function then they must be declared static - otherwise they will be allocated on\r
-the stack and so not exists after this function exits. */\r
-static StaticTask_t xIdleTaskTCB;\r
-static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];\r
-\r
- /* Pass out a pointer to the StaticTask_t structure in which the Idle task's\r
- state will be stored. */\r
- *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;\r
-\r
- /* Pass out the array that will be used as the Idle task's stack. */\r
- *ppxIdleTaskStackBuffer = uxIdleTaskStack;\r
-\r
- /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.\r
- Note that, as the array is necessarily of type StackType_t,\r
- configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
- *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-/* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the\r
-application must provide an implementation of vApplicationGetTimerTaskMemory()\r
-to provide the memory that is used by the Timer service task. */\r
-void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )\r
-{\r
-/* If the buffers to be provided to the Timer task are declared inside this\r
-function then they must be declared static - otherwise they will be allocated on\r
-the stack and so not exists after this function exits. */\r
-static StaticTask_t xTimerTaskTCB;\r
-static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];\r
-\r
- /* Pass out a pointer to the StaticTask_t structure in which the Timer\r
- task's state will be stored. */\r
- *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;\r
-\r
- /* Pass out the array that will be used as the Timer task's stack. */\r
- *ppxTimerTaskStackBuffer = uxTimerTaskStack;\r
-\r
- /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.\r
- Note that, as the array is necessarily of type StackType_t,\r
- configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
- *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;\r
-}\r
-\r