summaryrefslogtreecommitdiff
path: root/FreeRTOS/Demo/Common/Minimal/AbortDelay.c
blob: 9fc8cfeb0d3b984168473d993c534e01e2cdc422 (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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
 * FreeRTOS Kernel V10.2.1
 * Copyright (C) 2019 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!
 */

/*
 * This file contains some test scenarios that ensure tasks respond correctly
 * to xTaskAbortDelay() calls.  It also ensures tasks return the correct state
 * of eBlocked when blocked indefinitely in both the case where a task is
 * blocked on an object and when a task is blocked on a notification.
 */

/* Standard includes. */
#include "limits.h"

/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "event_groups.h"
#include "stream_buffer.h"

/* Demo includes. */
#include "AbortDelay.h"

/* This file can only be used if the functionality it tests is included in the
build.  Remove the whole file if this is not the case. */
#if( INCLUDE_xTaskAbortDelay == 1 )

#if( INCLUDE_xTaskGetHandle != 1 )
	#error This test file uses the xTaskGetHandle() API function so INCLUDE_xTaskGetHandle must be set to 1 in FreeRTOSConfig.h.
#endif

/* Task priorities.  Allow these to be overridden. */
#ifndef abtCONTROLLING_PRIORITY
	#define abtCONTROLLING_PRIORITY		( configMAX_PRIORITIES - 3 )
#endif

#ifndef abtBLOCKING_PRIORITY
	#define abtBLOCKING_PRIORITY	( configMAX_PRIORITIES - 2 )
#endif

/* The tests that are performed. */
#define abtNOTIFY_WAIT_ABORTS		0
#define abtNOTIFY_TAKE_ABORTS		1
#define abtDELAY_ABORTS				2
#define abtDELAY_UNTIL_ABORTS		3
#define abtSEMAPHORE_TAKE_ABORTS	4
#define abtEVENT_GROUP_ABORTS		5
#define abtQUEUE_SEND_ABORTS		6
#define abtSTREAM_BUFFER_RECEIVE	7
#define abtMAX_TESTS				8

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

/*
 * The two test tasks.  The controlling task specifies which test to executed.
 * More information is provided in the comments within the tasks.
 */
static void prvControllingTask( void *pvParameters );
static void prvBlockingTask( void *pvParameters );

/*
 * Test functions called by the blocking task.  Each function follows the same
 * pattern, but the way the task blocks is different in each case.
 *
 * In each function three blocking calls are made.  The first and third
 * blocking call is expected to time out, while the middle blocking call is
 * expected to be aborted by the controlling task half way through the block
 * time.
 */
static void prvTestAbortingTaskNotifyWait( void );
static void prvTestAbortingTaskNotifyTake( void );
static void prvTestAbortingTaskDelay( void );
static void prvTestAbortingTaskDelayUntil( void );
static void prvTestAbortingSemaphoreTake( void );
static void prvTestAbortingEventGroupWait( void );
static void prvTestAbortingQueueSend( void );
static void prvTestAbortingStreamBufferReceive( void );

/*
 * Performs a few tests to cover code paths not otherwise covered by the continuous
 * tests.
 */
static void prvPerformSingleTaskTests( void );

/*
 * Checks the amount of time a task spent in the Blocked state is within the
 * expected bounds.
 */
static void prvCheckExpectedTimeIsWithinAnAcceptableMargin( TickType_t xStartTime, TickType_t xExpectedBlockTime );

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

/* Used to ensure that tasks are still executing without error. */
static volatile BaseType_t xControllingCycles = 0, xBlockingCycles = 0;
static volatile BaseType_t xErrorOccurred = pdFALSE;

/* Each task needs to know the other tasks handle so they can send signals to
each other.  The handle is obtained from the task's name. */
static const char *pcControllingTaskName = "AbtCtrl", *pcBlockingTaskName = "AbtBlk";

/* The maximum amount of time a task will block for. */
const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 100 );
const TickType_t xHalfMaxBlockTime = pdMS_TO_TICKS( 50 );

/* The actual block time is dependent on the priority of other tasks in the
system so the actual block time might be greater than that expected, but it
should be within an acceptable upper bound. */
const TickType_t xAllowableMargin = pdMS_TO_TICKS( 7 );

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

void vCreateAbortDelayTasks( void )
{
	/* Create the two test tasks described above. */
	xTaskCreate( prvControllingTask, pcControllingTaskName, configMINIMAL_STACK_SIZE, NULL, abtCONTROLLING_PRIORITY, NULL );
	xTaskCreate( prvBlockingTask, pcBlockingTaskName, configMINIMAL_STACK_SIZE, NULL, abtBLOCKING_PRIORITY, NULL );
}
/*-----------------------------------------------------------*/

static void prvControllingTask( void *pvParameters )
{
TaskHandle_t xBlockingTask;
uint32_t ulTestToPerform = abtNOTIFY_WAIT_ABORTS;
TickType_t xTimeAtStart;
const TickType_t xStartMargin = 2UL;

/* Used to control whether to use xTaskAbortDelay() or xTaskAbortDelayFromISR() so
both are used with all the tests. */
BaseType_t xUseFromISRVersion = pdFALSE, xHigherPriorityTaskWoken;

	/* Just to remove compiler warnings. */
	( void ) pvParameters;

	xBlockingTask = xTaskGetHandle( pcBlockingTaskName );
	configASSERT( xBlockingTask );

	for( ;; )
	{
		/* Tell the secondary task to perform the next test. */
		xTimeAtStart = xTaskGetTickCount();
		xTaskNotify( xBlockingTask, ulTestToPerform, eSetValueWithOverwrite );

		/* The secondary task has a higher priority, so will now be in the
		Blocked state to wait for a maximum of xMaxBlockTime.  It expects that
		period to complete with a timeout.  It will then block for
		xMaxBlockTimeAgain, but this time it expects to the block time to abort
		half way through.  Block until it is time to send the abort to the
		secondary task.  xStartMargin is used because this task takes timing
		from the beginning of the test, whereas the blocking task takes timing
		from the entry into the Blocked state - and as the tasks run at
		different priorities, there may be some discrepancy.  Also, temporarily
		raise the priority of the controlling task to that of the blocking
		task to minimise discrepancies. */
		vTaskPrioritySet( NULL, abtBLOCKING_PRIORITY );

		vTaskDelay( xMaxBlockTime + xHalfMaxBlockTime + xStartMargin );

		/* For test coverage sometimes xTaskAbortDelay() is used and sometimes
		xTaskAbortDelayFromISR() is used. */
		if( xUseFromISRVersion == pdFALSE )
		{
			if( xTaskAbortDelay( xBlockingTask ) != pdPASS )
			{
				xErrorOccurred = pdTRUE;
			}
		}
		else
		{
			xHigherPriorityTaskWoken = pdFALSE;

			/* For test coverage, sometimes xHigherPriorityTaskWoken is used, and
			sometimes NULL is used. */

			if( ( xControllingCycles % 2 ) == 0 )
			{
				if( xTaskAbortDelayFromISR( xBlockingTask, &xHigherPriorityTaskWoken ) != pdPASS )
				{
					xErrorOccurred = pdTRUE;
				}
			}
			else
			{
				if( xTaskAbortDelayFromISR( xBlockingTask, NULL ) != pdPASS )
				{
					xErrorOccurred = pdTRUE;
				}
			}

			/* The tasks have the same priority so xHigherPriorityTaskWoken should
			never get set. */
			if( xHigherPriorityTaskWoken != pdFALSE )
			{
				xErrorOccurred = pdTRUE;
			}
		}

		/* Reset the priority to the normal controlling priority. */
		vTaskPrioritySet( NULL, abtCONTROLLING_PRIORITY );

		/* Now wait to be notified that the secondary task has completed its
		test. */
		ulTaskNotifyTake( pdTRUE, portMAX_DELAY );

		/* Did the entire test run for the expected time, which is two full
		block times plus the half block time caused by calling
		xTaskAbortDelay()? */
		prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, ( xMaxBlockTime + xMaxBlockTime + xHalfMaxBlockTime ) );

		/* Move onto the next test. */
		ulTestToPerform++;

		if( ulTestToPerform >= abtMAX_TESTS )
		{
			ulTestToPerform = 0;
		}

		/* To indicate this task is still executing. */
		xControllingCycles++;

		if( ( xControllingCycles % abtMAX_TESTS ) == 0 )
		{
			/* Looped through all the tests.  Switch between using xTaskAbortDelay()
			and xTaskAbortDelayFromISR() for the next round of tests. */
			xUseFromISRVersion = !xUseFromISRVersion;
		}
	}
}
/*-----------------------------------------------------------*/

static void prvBlockingTask( void *pvParameters )
{
TaskHandle_t xControllingTask;
uint32_t ulNotificationValue;
const uint32_t ulMax = 0xffffffffUL;

	/* Just to remove compiler warnings. */
	( void ) pvParameters;

	/* Start by performing a few tests to cover code not exercised in the loops
	below. */
	prvPerformSingleTaskTests();

	xControllingTask = xTaskGetHandle( pcControllingTaskName );
	configASSERT( xControllingTask );

	for( ;; )
	{
		/* Wait to be notified of the test that is to be performed next. */
		xTaskNotifyWait( 0, ulMax, &ulNotificationValue, portMAX_DELAY );

		switch( ulNotificationValue )
		{
			case abtNOTIFY_WAIT_ABORTS:
				prvTestAbortingTaskNotifyWait();
				break;

			case abtNOTIFY_TAKE_ABORTS:
				prvTestAbortingTaskNotifyTake();
				break;

			case abtDELAY_ABORTS:
				prvTestAbortingTaskDelay();
				break;

			case abtDELAY_UNTIL_ABORTS:
				prvTestAbortingTaskDelayUntil();
				break;

			case abtSEMAPHORE_TAKE_ABORTS:
				prvTestAbortingSemaphoreTake();
				break;

			case abtEVENT_GROUP_ABORTS:
				prvTestAbortingEventGroupWait();
				break;

			case abtQUEUE_SEND_ABORTS:
				prvTestAbortingQueueSend();
				break;

			case abtSTREAM_BUFFER_RECEIVE:
				prvTestAbortingStreamBufferReceive();
				break;

			default:
				/* Should not get here. */
				break;
		}

		/* Let the primary task know the test is complete. */
		xTaskNotifyGive( xControllingTask );

		/* To indicate this task is still executing. */
		xBlockingCycles++;
	}
}
/*-----------------------------------------------------------*/

static void prvPerformSingleTaskTests( void )
{
TaskHandle_t xThisTask;
BaseType_t xReturned;

	/* Try unblocking this task using both the task and ISR versions of the API -
	both should return false as this task is not blocked. */
	xThisTask = xTaskGetCurrentTaskHandle();

	xReturned = xTaskAbortDelay( xThisTask );
	if( xReturned != pdFALSE )
	{
		xErrorOccurred = pdTRUE;
	}

	xReturned = xTaskAbortDelayFromISR( xThisTask, NULL );
	if( xReturned != pdFALSE )
	{
		xErrorOccurred = pdTRUE;
	}
}
/*-----------------------------------------------------------*/

static void prvTestAbortingTaskDelayUntil( void )
{
TickType_t xTimeAtStart, xLastBlockTime;

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* Take a copy of the time as it is updated in the call to
	vTaskDelayUntil() but its original value is needed to determine the actual
	time spend in the Blocked state. */
	xLastBlockTime = xTimeAtStart;

	/* This first delay should just time out. */
	vTaskDelayUntil( &xLastBlockTime, xMaxBlockTime );
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );

	/* This second delay should be aborted by the primary task half way
	through.  Again take a copy of the time as it is updated in the call to
	vTaskDelayUntil() buts its original value is needed to determine the amount
	of time actually spent in the Blocked state. */
	xTimeAtStart = xTaskGetTickCount();
	xLastBlockTime = xTimeAtStart;
	vTaskDelayUntil( &xLastBlockTime, xMaxBlockTime );
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xHalfMaxBlockTime );

	/* As with the other tests, the third block period should not time out. */
	xTimeAtStart = xTaskGetTickCount();
	xLastBlockTime = xTimeAtStart;
	vTaskDelayUntil( &xLastBlockTime, xMaxBlockTime );
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );
}
/*-----------------------------------------------------------*/

static void prvTestAbortingTaskDelay( void )
{
TickType_t xTimeAtStart;

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This first delay should just time out. */
	vTaskDelay( xMaxBlockTime );
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This second delay should be aborted by the primary task half way
	through. */
	vTaskDelay( xMaxBlockTime );
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xHalfMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This third delay should just time out again. */
	vTaskDelay( xMaxBlockTime );
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );
}
/*-----------------------------------------------------------*/

static void prvTestAbortingTaskNotifyTake( void )
{
TickType_t xTimeAtStart;
uint32_t ulReturn;

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This first delay should just time out. */
	ulReturn = ulTaskNotifyTake( pdFALSE, xMaxBlockTime );
	if( ulReturn != 0 )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This second delay should be aborted by the primary task half way
	through. */
	ulReturn = ulTaskNotifyTake( pdFALSE, xMaxBlockTime );
	if( ulReturn != 0 )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xHalfMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This third delay should just time out again. */
	ulReturn = ulTaskNotifyTake( pdFALSE, xMaxBlockTime );
	if( ulReturn != 0 )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );
}
/*-----------------------------------------------------------*/

static void prvTestAbortingEventGroupWait( void )
{
TickType_t xTimeAtStart;
EventGroupHandle_t xEventGroup;
EventBits_t xBitsToWaitFor = ( EventBits_t ) 0x01, xReturn;

	#if( configSUPPORT_STATIC_ALLOCATION == 1 )
	{
		static StaticEventGroup_t xEventGroupBuffer;

		/* Create the event group.  Statically allocated memory is used so the
		creation cannot fail. */
		xEventGroup = xEventGroupCreateStatic( &xEventGroupBuffer );
	}
	#else
	{
		xEventGroup = xEventGroupCreate();
		configASSERT( xEventGroup );
	}
	#endif

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This first delay should just time out. */
	xReturn = xEventGroupWaitBits( xEventGroup, xBitsToWaitFor, pdTRUE, pdTRUE, xMaxBlockTime );
	if( xReturn != 0x00 )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This second delay should be aborted by the primary task half way
	through. */
	xReturn = xEventGroupWaitBits( xEventGroup, xBitsToWaitFor, pdTRUE, pdTRUE, xMaxBlockTime );
	if( xReturn != 0x00 )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xHalfMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This third delay should just time out again. */
	xReturn = xEventGroupWaitBits( xEventGroup, xBitsToWaitFor, pdTRUE, pdTRUE, xMaxBlockTime );
	if( xReturn != 0x00 )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );

	/* Not really necessary in this case, but for completeness. */
	vEventGroupDelete( xEventGroup );
}
/*-----------------------------------------------------------*/

static void prvTestAbortingStreamBufferReceive( void )
{
TickType_t xTimeAtStart;
StreamBufferHandle_t xStreamBuffer;
size_t xReturn;
const size_t xTriggerLevelBytes = ( size_t ) 1;
uint8_t uxRxData;

	#if( configSUPPORT_STATIC_ALLOCATION == 1 )
	{
		/* Defines the memory that will actually hold the streams within the
		stream buffer. */
		static uint8_t ucStorageBuffer[ sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) + 1 ];

		/* The variable used to hold the stream buffer structure. */
		StaticStreamBuffer_t xStreamBufferStruct;


		xStreamBuffer = xStreamBufferCreateStatic( sizeof( ucStorageBuffer ),
												   xTriggerLevelBytes,
												   ucStorageBuffer,
												   &xStreamBufferStruct );
	}
	#else
	{
		xStreamBuffer = xStreamBufferCreate( sizeof( uint8_t ), xTriggerLevelBytes );
		configASSERT( xStreamBuffer );
	}
	#endif

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This first delay should just time out. */
	xReturn = xStreamBufferReceive( xStreamBuffer, &uxRxData, sizeof( uxRxData ), xMaxBlockTime );
	if( xReturn != 0x00 )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This second delay should be aborted by the primary task half way
	through xMaxBlockTime. */
	xReturn = xStreamBufferReceive( xStreamBuffer, &uxRxData, sizeof( uxRxData ), xMaxBlockTime );
	if( xReturn != 0x00 )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xHalfMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This third delay should just time out again. */
	xReturn = xStreamBufferReceive( xStreamBuffer, &uxRxData, sizeof( uxRxData ), xMaxBlockTime );
	if( xReturn != 0x00 )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );

	/* Not really necessary in this case, but for completeness. */
	vStreamBufferDelete( xStreamBuffer );
}
/*-----------------------------------------------------------*/

static void prvTestAbortingQueueSend( void )
{
TickType_t xTimeAtStart;
BaseType_t xReturn;
const UBaseType_t xQueueLength = ( UBaseType_t ) 1;
QueueHandle_t xQueue;
uint8_t ucItemToQueue;

	#if( configSUPPORT_STATIC_ALLOCATION == 1 )
	{
		static StaticQueue_t xQueueBuffer;
		static uint8_t ucQueueStorage[ sizeof( uint8_t ) ];

		/* Create the queue.  Statically allocated memory is used so the
		creation cannot fail. */
		xQueue = xQueueCreateStatic( xQueueLength, sizeof( uint8_t ), ucQueueStorage, &xQueueBuffer );
	}
	#else
	{
		xQueue = xQueueCreate( xQueueLength, sizeof( uint8_t ) );
		configASSERT( xQueue );
	}
	#endif

	/* This function tests aborting when in the blocked state waiting to send,
	so the queue must be full.  There is only one space in the queue. */
	xReturn = xQueueSend( xQueue, &ucItemToQueue, xMaxBlockTime );
	if( xReturn != pdPASS )
	{
		xErrorOccurred = pdTRUE;
	}

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This first delay should just time out. */
	xReturn = xQueueSend( xQueue, &ucItemToQueue, xMaxBlockTime );
	if( xReturn != pdFALSE )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This second delay should be aborted by the primary task half way
	through. */
	xReturn = xQueueSend( xQueue, &ucItemToQueue, xMaxBlockTime );
	if( xReturn != pdFALSE )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xHalfMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This third delay should just time out again. */
	xReturn = xQueueSend( xQueue, &ucItemToQueue, xMaxBlockTime );
	if( xReturn != pdFALSE )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );

	/* Not really necessary in this case, but for completeness. */
	vQueueDelete( xQueue );
}
/*-----------------------------------------------------------*/

static void prvTestAbortingSemaphoreTake( void )
{
TickType_t xTimeAtStart;
BaseType_t xReturn;
SemaphoreHandle_t xSemaphore;

	#if( configSUPPORT_STATIC_ALLOCATION == 1 )
	{
		static StaticSemaphore_t xSemaphoreBuffer;

		/* Create the semaphore.  Statically allocated memory is used so the
		creation cannot fail. */
		xSemaphore = xSemaphoreCreateBinaryStatic( &xSemaphoreBuffer );
	}
	#else
	{
		xSemaphore = xSemaphoreCreateBinary();
	}
	#endif

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This first delay should just time out. */
	xReturn = xSemaphoreTake( xSemaphore, xMaxBlockTime );
	if( xReturn != pdFALSE )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This second delay should be aborted by the primary task half way
	through xMaxBlockTime. */
	xReturn = xSemaphoreTake( xSemaphore, portMAX_DELAY );
	if( xReturn != pdFALSE )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xHalfMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This third delay should just time out again. */
	xReturn = xSemaphoreTake( xSemaphore, xMaxBlockTime );
	if( xReturn != pdFALSE )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );

	/* Not really necessary in this case, but for completeness. */
	vSemaphoreDelete( xSemaphore );
}
/*-----------------------------------------------------------*/

static void prvTestAbortingTaskNotifyWait( void )
{
TickType_t xTimeAtStart;
BaseType_t xReturn;

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This first delay should just time out. */
	xReturn = xTaskNotifyWait( 0, 0, NULL, xMaxBlockTime );
	if( xReturn != pdFALSE )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This second delay should be aborted by the primary task half way
	through xMaxBlockTime. */
	xReturn = xTaskNotifyWait( 0, 0, NULL, portMAX_DELAY );
	if( xReturn != pdFALSE )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xHalfMaxBlockTime );

	/* Note the time before the delay so the length of the delay is known. */
	xTimeAtStart = xTaskGetTickCount();

	/* This third delay should just time out again. */
	xReturn = xTaskNotifyWait( 0, 0, NULL, xMaxBlockTime );
	if( xReturn != pdFALSE )
	{
		xErrorOccurred = pdTRUE;
	}
	prvCheckExpectedTimeIsWithinAnAcceptableMargin( xTimeAtStart, xMaxBlockTime );
}
/*-----------------------------------------------------------*/

static void prvCheckExpectedTimeIsWithinAnAcceptableMargin( TickType_t xStartTime, TickType_t xExpectedBlockTime )
{
TickType_t xTimeNow, xActualBlockTime;

	xTimeNow = xTaskGetTickCount();
	xActualBlockTime = xTimeNow - xStartTime;

	/* The actual block time should not be less than the expected block time. */
	if( xActualBlockTime < xExpectedBlockTime )
	{
		xErrorOccurred = pdTRUE;
	}

	/* The actual block time can be greater than the expected block time, as it
	depends on the priority of the other tasks, but it should be within an
	acceptable margin. */
	if( xActualBlockTime > ( xExpectedBlockTime + xAllowableMargin ) )
	{
		xErrorOccurred = pdTRUE;
	}
}
/*-----------------------------------------------------------*/

BaseType_t xAreAbortDelayTestTasksStillRunning( void )
{
static BaseType_t xLastControllingCycleCount = 0, xLastBlockingCycleCount = 0;
BaseType_t xReturn = pdPASS;

	/* Have both tasks performed at least one cycle since this function was
	last called? */
	if( xControllingCycles == xLastControllingCycleCount )
	{
		xReturn = pdFAIL;
	}

	if( xBlockingCycles == xLastBlockingCycleCount )
	{
		xReturn = pdFAIL;
	}

	if( xErrorOccurred == pdTRUE )
	{
		xReturn = pdFAIL;
	}

	xLastBlockingCycleCount = xBlockingCycles;
	xLastControllingCycleCount = xControllingCycles;

	return xReturn;
}

#endif /* INCLUDE_xTaskAbortDelay == 1 */