+\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