summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Demo/FreeRTOS_IoT_Libraries/task_pool/DemoTasks/SimpleTaskPoolExamples.c
blob: e7fb1e070cca3fadb4b3c0c791c76d6ef1f9be0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*
 * FreeRTOS Kernel V10.2.1
 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://www.FreeRTOS.org
 * http://aws.amazon.com/freertos
 *
 * 1 tab == 4 spaces!
 */

//_RB_ Add link to docs here.

/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"

/* Standard includes. */
#include <stdio.h>

/* IoT SDK includes. */
#include "iot_taskpool.h"

/* The priority at which that tasks in the task pool (the worker tasks) get
created. */
#define tpTASK_POOL_WORKER_PRIORITY		1

/*
 * Prototypes for the functions that demonstrate the task pool API.
 * See the implementation of the prvTaskPoolDemoTask() function within this file
 * for a description of the individual functions.  A configASSERT() is hit if
 * any of the demos encounter any unexpected behaviour.
 */
static void prvExample_BasicSingleJob( void );
static void prvExample_DeferredJobAndCancellingJobs( void );
static void prvExample_BasicRecyclableJob( void );
static void prvExample_ReuseRecyclableJobFromLowPriorityTask( void );
static void prvExample_ReuseRecyclableJobFromHighPriorityTask( void );

/*
 * Prototypes of the callback functions used in the examples.  The callback
 * simply sends a signal (in the form of a direct task notification) to the
 * prvTaskPoolDemoTask() task to let the task know that the callback execute.
 * The handle of the prvTaskPoolDemoTask() task is not accessed directly, but
 * instead passed into the task pool job as the job's context.
 */
static void prvSimpleTaskNotifyCallback( IotTaskPool_t pTaskPool, IotTaskPoolJob_t pJob, void *pUserContext );

/*
 * The task used to demonstrate the task pool API.  This task just loops through
 * each demo in turn.
 */
static void prvTaskPoolDemoTask( void *pvParameters );

/*-----------------------------------------------------------*/

/* Parameters used to create the system task pool - see TBD for more information
 * as the task pool used in this example is a slimmed down version of the full
 * library - the slimmed down version being intended specifically for FreeRTOS
 * kernel use cases. */
static const IotTaskPoolInfo_t xTaskPoolParameters = {
														/* Minimum number of threads in a task pool.
														 * Note the slimmed down version of the task
														 * pool used by this library does not autoscale
														 * the number of tasks in the pool so in this
														 * case this sets the number of tasks in the
														 * pool. */
														2,
														/* Maximum number of threads in a task pool.
														 * Note the slimmed down version of the task
														 * pool used by this library does not autoscale
														 * the number of tasks in the pool so in this
														 * case this parameter is just ignored. */
														2,
														/* Stack size for every task pool thread - in
														 * bytes, hence multiplying by the number of bytes
														 * in a word as configMINIMAL_STACK_SIZE is
														 * specified in words. */
														configMINIMAL_STACK_SIZE * sizeof( portSTACK_TYPE ),
														/* Priority for every task pool thread. */
														tpTASK_POOL_WORKER_PRIORITY,
													 };

/*-----------------------------------------------------------*/

void vStartSimpleTaskPoolDemo( void )
{
	/* This example uses a single application task, which in turn is used to
	 * create and send jobs to task pool tasks. */
	xTaskCreate( prvTaskPoolDemoTask,		/* Function that implements the task. */
				 "PoolDemo",				/* Text name for the task - only used for debugging. */
				 configMINIMAL_STACK_SIZE,	/* Size of stack (in words, not bytes) to allocate for the task. */
				 NULL,						/* Task parameter - not used in this case. */
				 tskIDLE_PRIORITY,			/* Task priority, must be between 0 and configMAX_PRIORITIES - 1. */
				 NULL );					/* Used to pass out a handle to the created task - not used in this case. */
}
/*-----------------------------------------------------------*/

static void prvTaskPoolDemoTask( void *pvParameters )
{
IotTaskPoolError_t xResult;
uint32_t ulLoops = 0;

	/* Remove compiler warnings about unused parameters. */
	( void ) pvParameters;

	/* The task pool must be created before it can be used.  The system task
	 * pool is the task pool managed by the task pool library itself - the storage
	 * used by the task pool is provided by the library. */
	xResult = IotTaskPool_CreateSystemTaskPool( &xTaskPoolParameters );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* Attempting to create the task pool again should then appear to succeed
	 * (in case it is initialised by more than one library), but have no effect. */
	xResult = IotTaskPool_CreateSystemTaskPool( &xTaskPoolParameters );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	for( ;; )
	{
		/* Demonstrate the most basic use case where a non persistent job is
		 * created and scheduled to run immediately.  The task pool worker tasks
		 * (in which the job callback function executes) have a priority above the
		 * priority of this task so the job's callback executes as soon as it is
		 * scheduled. */
		prvExample_BasicSingleJob();

		/* Demonstrate a job being scheduled to run at some time in the
		 * future, and how a job scheduled to run in the future can be cancelled
		 * if it has not yet started executing.  */
		prvExample_DeferredJobAndCancellingJobs();

		/* Demonstrate the most basic use of a recyclable job.  This is similar
		 * to prvExample_BasicSingleJob() but using a recyclable job.  Creating a
		 * recyclable job will re-use a previously created and now spare job from
		 * the task pool's job cache if one is available, or otherwise dynamically
		 * create a new job if a spare job is not available in the cache but space
		 * remains in the cache. */
		prvExample_BasicRecyclableJob();

		/* Demonstrate a recyclable job being created, used, and then re-used.
		 * In this the task pool worker tasks (in which the job callback
		 * functions execute) have a priority above the priority of this task so
		 * the job's callback functions execute as soon as they are scheduled. */
		prvExample_ReuseRecyclableJobFromLowPriorityTask();

		/* Again demonstrate a recyclable job being created, used, and then
		 * re-usedbut this time the priority of the task pool worker tasks (in
		 * which the job callback functions execute) are lower than the priority
		 * of this task so the job's callback functions don't execute until this
		 * task enters the blocked state. */
		prvExample_ReuseRecyclableJobFromHighPriorityTask();

		ulLoops++;
		if( ( ulLoops % 10UL ) == 0 )
		{
			printf( "prvTaskPoolDemoTask() performed %u iterations without hitting an assert.\r\n", ulLoops );
			fflush( stdout );
		}
	}
}
/*-----------------------------------------------------------*/

static void prvSimpleTaskNotifyCallback( IotTaskPool_t pTaskPool, IotTaskPoolJob_t pJob, void *pUserContext )
{
/* The jobs context is the handle of the task to which a notification should
 * be sent. */
TaskHandle_t xTaskToNotify = ( TaskHandle_t ) pUserContext;

	/* Remove warnings about unused parameters. */
	( void ) pTaskPool;
	( void ) pJob;

	/* Notify the task that created this job. */
	xTaskNotifyGive( xTaskToNotify );
}
/*-----------------------------------------------------------*/

static void prvExample_BasicSingleJob( void )
{
IotTaskPoolJobStorage_t xJobStorage;
IotTaskPoolJob_t xJob;
IotTaskPoolError_t xResult;
uint32_t ulReturn;
const uint32_t ulNoFlags = 0UL;
const TickType_t xNoDelay = ( TickType_t ) 0;
size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize();
IotTaskPoolJobStatus_t xJobStatus;

	/* Don't expect any notifications to be pending yet. */
	configASSERT( ulTaskNotifyTake( pdTRUE, xNoDelay ) == 0 );

	/* Create and schedule a job using the handle of this task as the job's
	 * context and the function that sends a notification to the task handle as
	 * the job's callback function.  This is not a recyclable job so the storage
	 * required to hold information about the job is provided by this task - in
	 * this case the storage is on the stack of this task so no memory is allocated
	 * dynamically but the stack frame must remain in scope for the lifetime of
	 * the job. */
	xResult = IotTaskPool_CreateJob(  prvSimpleTaskNotifyCallback, /* Callback function. */
									  ( void * ) xTaskGetCurrentTaskHandle(), /* Job context. */
									  &xJobStorage,
									  &xJob );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* The job has been created but not scheduled so is now ready. */
	IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
	configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_READY );

	/* This is not a persistent (recyclable) job and its storage is on the
	 * stack of this function, so the amount of heap space available should not
	 * have changed since entering this function. */
	configASSERT( xFreeHeapBeforeCreatingJob == xPortGetFreeHeapSize() );

	/* In the full task pool implementation the first parameter is used to
	 * pass the handle of the task pool to schedule.  The lean task pool
	 * implementation used in this demo only supports a single task pool, which
	 * is created internally within the library, so the first parameter is NULL. */
	xResult = IotTaskPool_Schedule( NULL, xJob, ulNoFlags );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* Look for the notification coming from the job's callback function.  The
	 * priority of the task pool worker task that executes the callback is higher
	 * than the priority of this task so a block time is not needed - the task pool
	 * worker task preempts this task and sends the notification (from the job's
	 * callback) as soon as the job is scheduled. */
	ulReturn = ulTaskNotifyTake( pdTRUE, xNoDelay );
	configASSERT( ulReturn );

	/* The job's callback has executed so the job has now completed. */
	IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
	configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_COMPLETED );
}
/*-----------------------------------------------------------*/

static void prvExample_DeferredJobAndCancellingJobs( void )
{
IotTaskPoolJobStorage_t xJobStorage;
IotTaskPoolJob_t xJob;
IotTaskPoolError_t xResult;
uint32_t ulReturn;
const uint32_t ulShortDelay_ms = 100UL;
const TickType_t xNoDelay = ( TickType_t ) 0, xAllowableMargin = ( TickType_t ) 5; /* Large margin for Windows port, which is not real time. */
TickType_t xTimeBefore, xElapsedTime, xShortDelay_ticks;
size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize();
IotTaskPoolJobStatus_t xJobStatus;

	/* Don't expect any notifications to be pending yet. */
	configASSERT( ulTaskNotifyTake( pdTRUE, xNoDelay ) == 0 );

	/* Create a job using the handle of this task as the job's context and the
	 * function that sends a notification to the task handle as the job's callback
	 * function.  The job is created using storage allocated on the stack of this
	 * function - so no memory is allocated. */
	xResult = IotTaskPool_CreateJob(  prvSimpleTaskNotifyCallback, /* Callback function. */
									  ( void * ) xTaskGetCurrentTaskHandle(), /* Job context. */
									  &xJobStorage,
									  &xJob );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* The job has been created but not scheduled so is now ready. */
	IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
	configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_READY );

	/* This is not a persistent (recyclable) job and its storage is on the
	 * stack of this function, so the amount of heap space available should not
	 * have changed since entering this function. */
	configASSERT( xFreeHeapBeforeCreatingJob == xPortGetFreeHeapSize() );

	/* Schedule the job to run its callback in ulShortDelay_ms milliseconds time.
	 * In the full task pool implementation the first parameter is used to	pass the
	 * handle of the task pool to schedule.  The lean task pool implementation used
	 * in this demo only supports a single task pool, which is created internally
	 * within the library, so the first parameter is NULL. */
	xResult = IotTaskPool_ScheduleDeferred( NULL, xJob, ulShortDelay_ms );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* The scheduled job should not have executed yet, so don't expect any
	 * notifications and expect the job's status to be 'deferred'. */
	ulReturn = ulTaskNotifyTake( pdTRUE, xNoDelay );
	configASSERT( ulReturn == 0 );
	IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
	configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_DEFERRED );

	/* As the job has not yet been executed it can be cancelled. */
	xResult = IotTaskPool_TryCancel( NULL, xJob, &xJobStatus );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );
	IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
	configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_CANCELED );

	/* Schedule the job again, and this time wait until its callback is
	 * executed (the callback function sends a notification to this task) to see
	 * that it executes at the right time. */
	xTimeBefore = xTaskGetTickCount();
	xResult = IotTaskPool_ScheduleDeferred( NULL, xJob, ulShortDelay_ms );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* Wait twice the deferred execution time to ensure the callback is executed
	 * before the call below times out. */
	ulReturn = ulTaskNotifyTake( pdTRUE, pdMS_TO_TICKS( ulShortDelay_ms * 2UL ) );
	xElapsedTime = xTaskGetTickCount() - xTimeBefore;

	/* A single notification should have been received... */
	configASSERT( ulReturn == 1 );

	/* ...and the time since scheduling the job should be greater than or
	 * equal to the deferred execution time - which is converted to ticks for
	 * comparison. */
	xShortDelay_ticks = pdMS_TO_TICKS( ulShortDelay_ms );
	configASSERT( ( xElapsedTime >= xShortDelay_ticks ) && ( xElapsedTime  < ( xShortDelay_ticks + xAllowableMargin ) ) );
}
/*-----------------------------------------------------------*/

static void prvExample_BasicRecyclableJob( void )
{
IotTaskPoolJob_t xJob;
IotTaskPoolError_t xResult;
uint32_t ulReturn;
const uint32_t ulNoFlags = 0UL;
const TickType_t xNoDelay = ( TickType_t ) 0;
size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize();

	/* Don't expect any notifications to be pending yet. */
	configASSERT( ulTaskNotifyTake( pdTRUE, xNoDelay ) == 0 );

	/* Create and schedule a job using the handle of this task as the job's
	 * context and the function that sends a notification to the task handle as
	 * the job's callback function.  The job is created as a recyclable job and in
	 * this case the memory used to hold the job status is allocated inside the
	 * create function.  As the job is persistent it can be used multiple times,
	 * as demonstrated in other examples within this demo.  In the full task pool
	 * implementation the first parameter is used to pass the handle of the task
	 * pool this recyclable job is to be associated with.  In the lean
	 * implementation of the task pool used by this demo there is only one task
	 * pool (the system task pool created within the task pool library) so the
	 * first parameter is NULL. */
	xResult = IotTaskPool_CreateRecyclableJob( NULL,
											   prvSimpleTaskNotifyCallback,
											   (void * ) xTaskGetCurrentTaskHandle(),
											   &xJob );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* This recyclable job is persistent, and in this case created dynamically,
	 * so expect there to be less heap space than when entering the function. */
	configASSERT( xPortGetFreeHeapSize() < xFreeHeapBeforeCreatingJob );

	/* In the full task pool implementation the first parameter is used to
	 * pass the handle of the task pool to schedule.  The lean task pool
	 * implementation used in this demo only supports a single task pool, which
	 * is created internally within the library, so the first parameter is NULL. */
	xResult = IotTaskPool_Schedule( NULL, xJob, ulNoFlags );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* Look for the notification coming from the job's callback function.  The
	 * priority of the task pool worker task that executes the callback is higher
	 * than the priority of this task so a block time is not needed - the task pool
	 * worker task	preempts this task and sends the notification (from the job's
	 * callback) as soon as the job is scheduled. */
	ulReturn = ulTaskNotifyTake( pdTRUE, xNoDelay );
	configASSERT( ulReturn );

	/* Clean up recyclable job.  In the full implementation of the task pool
	 * the first parameter is used to pass a handle to the task pool the job is
	 * associated with.  In the lean implementation of the task pool used by this
	 * demo there is only one task pool (the system task pool created in the
	 * task pool library itself) so the first parameter is NULL. */
	IotTaskPool_DestroyRecyclableJob( NULL, xJob );

	/* Once the job has been deleted the memory used to hold the job is
	 * returned, so the available heap should be exactly as when entering this
	 * function. */
	configASSERT( xPortGetFreeHeapSize() == xFreeHeapBeforeCreatingJob );
}
/*-----------------------------------------------------------*/

static void prvExample_ReuseRecyclableJobFromLowPriorityTask( void )
{
IotTaskPoolError_t xResult;
uint32_t ulNotificationValue;
const uint32_t ulNoFlags = 0UL;
const TickType_t xNoDelay = ( TickType_t ) 0;
IotTaskPoolJob_t xJob, xJobRecycled;
size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize(), xFreeHeapAfterCreatingJob = 0;
IotTaskPoolJobStatus_t xJobStatus;

	/* Don't expect any notifications to be pending yet. */
	configASSERT( ulTaskNotifyTake( pdTRUE, xNoDelay ) == 0 );

	/* Create a recycleable job using the handle of this task as the job's
	 * context and the function that sends a notification to the task handle as
	 * the job's callback function.  In the full task pool implementation the
	 * first parameter is used to pass the handle of the task pool this
	 * recyclable job is to be associated with.  In the lean implementation of
	 * the task pool used by this demo there is only one task pool (the system
	 * task pool created within the task pool library) so the first parameter is
	 * NULL. */
	xResult = IotTaskPool_CreateRecyclableJob( NULL,
											   prvSimpleTaskNotifyCallback,
											   (void * ) xTaskGetCurrentTaskHandle(),
											   &( xJob ) );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* The job is created as a recyclable job and in this case the memory to
	 * store the job information is allocated within the create function as at
	 * this time there are no recyclable jobs in the task pool jobs cache. So
	 * expect there to be less heap space than when entering the function. */
	xFreeHeapAfterCreatingJob = xPortGetFreeHeapSize();
	configASSERT( xFreeHeapAfterCreatingJob < xFreeHeapBeforeCreatingJob );

	/* The job has been created but not scheduled so is now ready. */
	IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
	configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_READY );

	/* In the full task pool implementation the first parameter is used to
	 * pass the handle of the task pool to schedule.  The lean task pool
	 * implementation used in this demo only supports a single task pool, which
	 * is created internally within the library, so the first parameter is NULL. */
	xResult = IotTaskPool_Schedule( NULL, xJob, ulNoFlags );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* The priority of the task pool task(s) is higher than the priority
	 * of this task, so the job's callback function should have already
	 * executed, sending a notification to this task, and incrementing this
	 * task's notification value. */
	xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */
					 0UL, /* Don't clear any bits on exit. */
					 &ulNotificationValue, /* Obtain the notification value. */
					 xNoDelay ); /* No block time, return immediately. */
	configASSERT( ulNotificationValue == 1 );

	/* The job's callback has executed so the job is now completed. */
	IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
	configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_COMPLETED );

	/* Return the job to the task pool's job cache. */
	IotTaskPool_RecycleJob( NULL, xJob );

	/* Create a recycleable job again using the handle of this task as the job's
	 * context and the function that sends a notification to the task handle as
	 * the job's callback function.  In the full task pool implementation the
	 * first parameter is used to pass the handle of the task pool this
	 * recyclable job is to be associated with.  In the lean implementation of
	 * the task pool used by this demo there is only one task pool (the system
	 * task pool created within the task pool library) so the first parameter is
	 * NULL. */
	xResult = IotTaskPool_CreateRecyclableJob( NULL,
											   prvSimpleTaskNotifyCallback,
											   (void * ) xTaskGetCurrentTaskHandle(),
											   &( xJobRecycled ) );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* Since this time the task pool's job cache had a recycleable job, it must
	 * have been re-used. Thefore expect the free heap space to be same as after
	 * the creation of first job */
	configASSERT( xPortGetFreeHeapSize() == xFreeHeapAfterCreatingJob );

	/* Expect the task pool to re-use the job in its cache as opposed to
	 * allocating a new one. */
	configASSERT( xJobRecycled == xJob );

	/* In the full task pool implementation the first parameter is used to
	 * pass the handle of the task pool to schedule.  The lean task pool
	 * implementation used in this demo only supports a single task pool, which
	 * is created internally within the library, so the first parameter is NULL. */
	xResult = IotTaskPool_Schedule( NULL, xJobRecycled, ulNoFlags );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* The priority of the task pool task(s) is higher than the priority
	 * of this task, so the job's callback function should have already
	 * executed, sending a notification to this task, and incrementing this
	 * task's notification value. */
	xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */
					 0UL, /* Don't clear any bits on exit. */
					 &ulNotificationValue, /* Obtain the notification value. */
					 xNoDelay ); /* No block time, return immediately. */
	configASSERT( ulNotificationValue == 2 );

	/* The job's callback has executed so the job is now completed. */
	IotTaskPool_GetStatus( NULL, xJobRecycled, &xJobStatus );
	configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_COMPLETED );

	/* Clean up the recyclable job.  In the full implementation of the task
	 * pool the first parameter is used to pass a handle to the task pool the job
	 * is associated with.  In the lean implementation of the task pool used by
	 * this demo there is only one task pool (the system task pool created in the
	 * task pool library itself) so the first parameter is NULL. */
	xResult = IotTaskPool_DestroyRecyclableJob( NULL, xJobRecycled );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* Clear all the notification value bits ready for the next example. */
	xTaskNotifyWait( portMAX_DELAY, /* Clear all bits on entry - portMAX_DELAY is used as it is a portable way of having all bits set. */
					 0UL, /* Don't clear any bits on exit. */
					 NULL, /* Don't need the notification value this time. */
					 xNoDelay ); /* No block time, return immediately. */
	configASSERT( ulTaskNotifyTake( pdTRUE, xNoDelay ) == 0 );

	/* Once the job has been deleted the memory used to hold the job is
	 * returned, so the available heap should be exactly as when entering this
	 * function. */
	configASSERT( xPortGetFreeHeapSize() == xFreeHeapBeforeCreatingJob );
}
/*-----------------------------------------------------------*/

static void prvExample_ReuseRecyclableJobFromHighPriorityTask( void )
{
IotTaskPoolError_t xResult;
uint32_t ulNotificationValue;
const uint32_t ulNoFlags = 0UL;
const TickType_t xNoDelay = ( TickType_t ) 0;
TickType_t xShortDelay = pdMS_TO_TICKS( 150 );
IotTaskPoolJob_t xJob, xJobRecycled;
size_t xFreeHeapBeforeCreatingJob = xPortGetFreeHeapSize(), xFreeHeapAfterCreatingJob = 0;
IotTaskPoolJobStatus_t xJobStatus;

	/* Don't expect any notifications to be pending yet. */
	configASSERT( ulTaskNotifyTake( pdTRUE, xNoDelay ) == 0 );

	/* prvExample_ReuseRecyclableJobFromLowPriorityTask() executes in a task
	 * that has a lower [task] priority than the task pool's worker tasks.
	 * Therefore a task pool worker preempts the task that calls
	 * prvExample_ReuseRecyclableJobFromHighPriorityTask() as soon as the job is
	 * scheduled.  prvExample_ReuseRecyclableJobFromHighPriorityTask() reverses the
	 * priorities - prvExample_ReuseRecyclableJobFromHighPriorityTask() raises its
	 * priority to above the task pool's worker tasks, so the worker tasks do not
	 * execute until the calling task enters the blocked state.  First raise the
	 * priority - passing NULL means raise the priority of the calling task. */
	vTaskPrioritySet( NULL, tpTASK_POOL_WORKER_PRIORITY + 1 );

	/* Create a recycleable job using the handle of this task as the job's
	 * context and the function that sends a notification to the task handle as
	 * the job's callback function.  In the full task pool implementation the
	 * first parameter is used to pass the handle of the task pool this
	 * recyclable job is to be associated with.  In the lean implementation of
	 * the task pool used by this demo there is only one task pool (the system
	 * task pool created within the task pool library) so the first parameter is
	 * NULL. */
	xResult = IotTaskPool_CreateRecyclableJob( NULL,
											   prvSimpleTaskNotifyCallback,
											   (void * ) xTaskGetCurrentTaskHandle(),
											   &( xJob ) );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* The job is created as a recyclable job and in this case the memory to
	 * store the job information is allocated within the create function as at
	 * this time there are no recyclable jobs in the task pool jobs cache. So
	 * expect there to be less heap space than when entering the function. */
	xFreeHeapAfterCreatingJob = xPortGetFreeHeapSize();
	configASSERT( xFreeHeapAfterCreatingJob < xFreeHeapBeforeCreatingJob );

	/* The job has been created but not scheduled so is now ready. */
	IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
	configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_READY );

	/* In the full task pool implementation the first parameter is used to
	 * pass the handle of the task pool to schedule.  The lean task pool
	 * implementation used in this demo only supports a single task pool, which
	 * is created internally within the library, so the first parameter is NULL. */
	xResult = IotTaskPool_Schedule( NULL, xJob, ulNoFlags );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* The priority of the task pool task(s) is lower than the priority
	 * of this task, so the job's callback function should not have executed
	 * yet, so don't expect the notification value for this task to have
	 * changed. */
	xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */
					 0UL, /* Don't clear any bits on exit. */
					 &ulNotificationValue, /* Obtain the notification value. */
					 xNoDelay ); /* No block time, return immediately. */
	configASSERT( ulNotificationValue == 0 );

	/* When this task blocks to wait for a notification, a worker thread will be
	 * able to execute - but as soon as its callback function sends a
	 * notification to this task, this task will preempt it (because it has a
	 * higher priority). So this task expects to receive one notification. */
	xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */
					 0UL, /* Don't clear any bits on exit. */
					 &ulNotificationValue, /* Obtain the notification value. */
					 xShortDelay ); /* Short delay to allow a task pool worker to execute. */
	configASSERT( ulNotificationValue == 1 );

	/* Since the scheduled job has now executed, so waiting for another
	 * notification should timeout without the notification value changing. */
	xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */
					 0UL, /* Don't clear any bits on exit. */
					 &ulNotificationValue, /* Obtain the notification value. */
					 xShortDelay ); /* Short delay to allow a task pool worker to execute. */
	configASSERT( ulNotificationValue == 1 );

	/* The job's callback has executed so the job is now completed. */
	IotTaskPool_GetStatus( NULL, xJob, &xJobStatus );
	configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_COMPLETED );

	/* Return the job to the task pool's job cache. */
	IotTaskPool_RecycleJob( NULL, xJob );

	/* Create a recycleable job again using the handle of this task as the job's
	 * context and the function that sends a notification to the task handle as
	 * the job's callback function.  In the full task pool implementation the
	 * first parameter is used to pass the handle of the task pool this
	 * recyclable job is to be associated with.  In the lean implementation of
	 * the task pool used by this demo there is only one task pool (the system
	 * task pool created within the task pool library) so the first parameter is
	 * NULL. */
	xResult = IotTaskPool_CreateRecyclableJob( NULL,
											   prvSimpleTaskNotifyCallback,
											   (void * ) xTaskGetCurrentTaskHandle(),
											   &( xJobRecycled ) );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* Since this time the task pool's job cache had a recycleable job, it must
	 * have been re-used. Thefore expect the free heap space to be same as after
	 * the creation of first job */
	configASSERT( xPortGetFreeHeapSize() == xFreeHeapAfterCreatingJob );

	/* Expect the task pool to re-use the job in its cache as opposed to
	 * allocating a new one. */
	configASSERT( xJobRecycled == xJob );

	/* In the full task pool implementation the first parameter is used to
	 * pass the handle of the task pool to schedule.  The lean task pool
	 * implementation used in this demo only supports a single task pool, which
	 * is created internally within the library, so the first parameter is NULL. */
	xResult = IotTaskPool_Schedule( NULL, xJobRecycled, ulNoFlags );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* The priority of the task pool task(s) is lower than the priority
	 * of this task, so the job's callback function should not have executed
	 * yet, so don't expect the notification value for this task to have
	 * changed. */
	xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */
					 0UL, /* Don't clear any bits on exit. */
					 &ulNotificationValue, /* Obtain the notification value. */
					 xNoDelay ); /* No block time, return immediately. */
	configASSERT( ulNotificationValue == 1 );

	/* When this task blocks to wait for a notification, a worker thread will be
	 * able to execute - but as soon as its callback function sends a
	 * notification to this task, this task will preempt it (because it has a
	 * higher priority). So this task expects to receive one notification. */
	xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */
					 0UL, /* Don't clear any bits on exit. */
					 &ulNotificationValue, /* Obtain the notification value. */
					 xShortDelay ); /* Short delay to allow a task pool worker to execute. */
	configASSERT( ulNotificationValue == 2 );

	/* Since the scheduled job has now executed, so waiting for another
	 * notification should timeout without the notification value changing. */
	xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */
					 0UL, /* Don't clear any bits on exit. */
					 &ulNotificationValue, /* Obtain the notification value. */
					 xShortDelay ); /* Short delay to allow a task pool worker to execute. */
	configASSERT( ulNotificationValue == 2 );

	/* The job's callback has executed so the job is now completed. */
	IotTaskPool_GetStatus( NULL, xJobRecycled, &xJobStatus );
	configASSERT( xJobStatus == IOT_TASKPOOL_STATUS_COMPLETED );

	/* Clean up the recyclable job.  In the full implementation of the task
	 * pool the first parameter is used to pass a handle to the task pool the job
	 * is associated with.  In the lean implementation of the task pool used by
	 * this demo there is only one task pool (the system task pool created in the
	 * task pool library itself) so the first parameter is NULL. */
	xResult = IotTaskPool_DestroyRecyclableJob( NULL, xJobRecycled );
	configASSERT( xResult == IOT_TASKPOOL_SUCCESS );

	/* Reset this task's priority. */
	vTaskPrioritySet( NULL, tskIDLE_PRIORITY );

	/* Clear all the notification value bits ready for the next example. */
	xTaskNotifyWait( portMAX_DELAY, /* Clear all bits on entry - portMAX_DELAY is used as it is a portable way of having all bits set. */
					 0UL, /* Don't clear any bits on exit. */
					 NULL, /* Don't need the notification value this time. */
					 xNoDelay ); /* No block time, return immediately. */
	configASSERT( ulTaskNotifyTake( pdTRUE, xNoDelay ) == 0 );

	/* Once the job has been deleted the memory used to hold the job is
	 * returned, so the available heap should be exactly as when entering this
	 * function. */
	configASSERT( xPortGetFreeHeapSize() == xFreeHeapBeforeCreatingJob );
}
/*-----------------------------------------------------------*/