\r
/* For test purposes the priority of the sending task is changed after every\r
queuesetPRIORITY_CHANGE_LOOPS number of values are sent to a queue. */\r
-#define queuesetPRIORITY_CHANGE_LOOPS 100UL\r
+#define queuesetPRIORITY_CHANGE_LOOPS ( ( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH ) * 3 )\r
\r
/* The ISR sends to the queue every queuesetISR_TX_PERIOD ticks. */\r
#define queuesetISR_TX_PERIOD ( 100UL )\r
*/\r
static portBASE_TYPE prvCheckReceivedValueWithinExpectedRange( unsigned long ulReceived, unsigned long ulExpectedReceived );\r
\r
+/*\r
+ * Local pseudo random number seed and return functions. Used to avoid calls\r
+ * to the standard library.\r
+ */\r
+static unsigned long prvRand( void );\r
+static void prvSRand( unsigned long ulSeed );\r
+\r
/*-----------------------------------------------------------*/\r
\r
/* The queues that are added to the set. */\r
expected. */\r
static volatile unsigned long ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;\r
\r
+/* Used by the pseudo random number generator. */\r
+static unsigned long ulNextRand = 0;\r
+\r
/*-----------------------------------------------------------*/\r
\r
void vStartQueueSetTasks( void )\r
\r
static void prvQueueSetSendingTask( void *pvParameters )\r
{\r
-unsigned long ulTaskTxValue = 0;\r
-portBASE_TYPE xQueueToWriteTo;\r
+unsigned long ulTaskTxValue = 0, ulQueueToWriteTo;\r
xQueueHandle xQueueInUse;\r
unsigned portBASE_TYPE uxPriority = queuesetMEDIUM_PRIORITY, ulLoops = 0;\r
\r
/* Remove compiler warning about the unused parameter. */\r
( void ) pvParameters;\r
\r
- srand( ( unsigned int ) &ulTaskTxValue );\r
+ /* Seed mini pseudo random number generator. */\r
+ prvSRand( ( unsigned long ) &ulTaskTxValue );\r
\r
for( ;; )\r
{\r
/* Generate the index for the queue to which a value is to be sent. */\r
- xQueueToWriteTo = rand() % queuesetNUM_QUEUES_IN_SET;\r
- xQueueInUse = xQueues[ xQueueToWriteTo ];\r
+ ulQueueToWriteTo = prvRand() % queuesetNUM_QUEUES_IN_SET;\r
+ xQueueInUse = xQueues[ ulQueueToWriteTo ];\r
\r
/* Note which index is being written to to ensure all the queues are\r
used. */\r
- ( ulQueueUsedCounter[ xQueueToWriteTo ] )++;\r
+ ( ulQueueUsedCounter[ ulQueueToWriteTo ] )++;\r
\r
/* Send to the queue to unblock the task that is waiting for data to\r
arrive on a queue within the queue set to which this queue belongs. */\r
/* Let the ISR access the queues also. */\r
xSetupComplete = pdTRUE;\r
}\r
+/*-----------------------------------------------------------*/\r
+\r
+static unsigned long prvRand( void )\r
+{\r
+ ulNextRand = ( ulNextRand * 1103515245UL ) + 12345UL;\r
+ return (ulNextRand / 65536UL ) % 32768UL;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvSRand( unsigned long ulSeed )\r
+{\r
+ ulNextRand = ulSeed;\r
+}\r
+\r
*\r
* One counter task loops indefinitely, incrementing the shared count variable\r
* on each iteration. To ensure it has exclusive access to the variable it\r
- * raises it's priority above that of the controller task before each\r
- * increment, lowering it again to it's original priority before starting the\r
+ * raises its priority above that of the controller task before each\r
+ * increment, lowering it again to its original priority before starting the\r
* next iteration.\r
*\r
* The other counter task increments the shared count variable on each\r
- * iteration of it's loop until the count has reached a limit of 0xff - at\r
+ * iteration of its loop until the count has reached a limit of 0xff - at\r
* which point it suspends itself. It will not start a new loop until the\r
- * controller task has made it "ready" again by calling vTaskResume ().\r
+ * controller task has made it "ready" again by calling vTaskResume().\r
* This second counter task operates at a higher priority than controller\r
* task so does not need to worry about mutual exclusion of the counter\r
* variable.\r
* continuous count task, and moves on to its second section.\r
*\r
* At the start of the second section the shared variable is cleared to zero.\r
- * The limited count task is then woken from it's suspension by a call to\r
+ * The limited count task is then woken from its suspension by a call to\r
* vTaskResume (). As this counter task operates at a higher priority than\r
* the controller task the controller task should not run again until the\r
* shared variable has been counted up to the limited value causing the counter\r
\r
/* Handles to the two counter tasks. These could be passed in as parameters\r
to the controller task to prevent them having to be file scope. */\r
-static xTaskHandle xContinousIncrementHandle, xLimitedIncrementHandle;\r
+static xTaskHandle xContinuousIncrementHandle, xLimitedIncrementHandle;\r
\r
/* The shared counter variable. This is passed in as a parameter to the two\r
counter variables for demonstration purposes. */\r
-static unsigned long ulCounter;\r
+static volatile unsigned long ulCounter;\r
\r
/* Variables used to check that the tasks are still operating without error.\r
Each complete iteration of the controller task increments this variable\r
defined to be less than 1. */\r
vQueueAddToRegistry( xSuspendedTestQueue, ( signed char * ) "Suspended_Test_Queue" );\r
\r
- xTaskCreate( vContinuousIncrementTask, ( signed char * ) "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinousIncrementHandle );\r
+ xTaskCreate( vContinuousIncrementTask, ( signed char * ) "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );\r
xTaskCreate( vLimitedIncrementTask, ( signed char * ) "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );\r
xTaskCreate( vCounterControlTask, ( signed char * ) "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
xTaskCreate( vQueueSendWhenSuspendedTask, ( signed char * ) "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
*/\r
static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )\r
{\r
-unsigned long *pulCounter;\r
+volatile unsigned long *pulCounter;\r
unsigned portBASE_TYPE uxOurPriority;\r
\r
/* Take a pointer to the shared variable from the parameters passed into\r
\r
for( ;; )\r
{\r
- /* Raise our priority above the controller task to ensure a context\r
- switch does not occur while we are accessing this variable. */\r
+ /* Raise the priority above the controller task to ensure a context\r
+ switch does not occur while the variable is being accessed. */\r
vTaskPrioritySet( NULL, uxOurPriority + 1 );\r
+ {\r
+ configASSERT( ( uxTaskPriorityGet( NULL ) == ( uxOurPriority + 1 ) ) );\r
( *pulCounter )++;\r
+ }\r
vTaskPrioritySet( NULL, uxOurPriority );\r
+ configASSERT( ( uxTaskPriorityGet( NULL ) == uxOurPriority ) );\r
}\r
}\r
/*-----------------------------------------------------------*/\r
for( sLoops = 0; sLoops < priLOOPS; sLoops++ )\r
{\r
/* Suspend the continuous count task so we can take a mirror of the\r
- shared variable without risk of corruption. */\r
- vTaskSuspend( xContinousIncrementHandle );\r
+ shared variable without risk of corruption. This is not really\r
+ needed as the other task raises its priority above this task's\r
+ priority. */\r
+ vTaskSuspend( xContinuousIncrementHandle );\r
+ {\r
+ #if( INCLUDE_eTaskGetState == 1 )\r
+ {\r
+ configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eSuspended );\r
+ }\r
+ #endif /* INCLUDE_eTaskGetState */\r
+\r
ulLastCounter = ulCounter;\r
- vTaskResume( xContinousIncrementHandle );\r
+ }\r
+ vTaskResume( xContinuousIncrementHandle );\r
+\r
+ #if( INCLUDE_eTaskGetState == 1 )\r
+ {\r
+ configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eReady );\r
+ }\r
+ #endif /* INCLUDE_eTaskGetState */\r
\r
/* Now delay to ensure the other task has processor time. */\r
vTaskDelay( priSLEEP_TIME );\r
xTaskResumeAll();\r
}\r
\r
-\r
/* Second section: */\r
\r
- /* Suspend the continuous counter task so it stops accessing the shared variable. */\r
- vTaskSuspend( xContinousIncrementHandle );\r
+ /* Suspend the continuous counter task so it stops accessing the shared\r
+ variable. */\r
+ vTaskSuspend( xContinuousIncrementHandle );\r
\r
/* Reset the variable. */\r
ulCounter = ( unsigned long ) 0;\r
\r
+ #if( INCLUDE_eTaskGetState == 1 )\r
+ {\r
+ configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );\r
+ }\r
+ #endif /* INCLUDE_eTaskGetState */\r
+\r
/* Resume the limited count task which has a higher priority than us.\r
We should therefore not return from this call until the limited count\r
task has suspended itself with a known value in the counter variable. */\r
vTaskResume( xLimitedIncrementHandle );\r
\r
+ /* This task should not run again until xLimitedIncrementHandle has\r
+ suspended itself. */\r
+ #if( INCLUDE_eTaskGetState == 1 )\r
+ {\r
+ configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );\r
+ }\r
+ #endif /* INCLUDE_eTaskGetState */\r
+\r
/* Does the counter variable have the expected value? */\r
if( ulCounter != priMAX_COUNT )\r
{\r
}\r
\r
/* Resume the continuous count task and do it all again. */\r
- vTaskResume( xContinousIncrementHandle );\r
+ vTaskResume( xContinuousIncrementHandle );\r
}\r
}\r
/*-----------------------------------------------------------*/\r
\r
This demo creates three tasks all of which access the same recursive mutex:\r
\r
- prvRecursiveMutexControllingTask() has the highest priority so executes \r
- first and grabs the mutex. It then performs some recursive accesses - \r
- between each of which it sleeps for a short period to let the lower \r
+ prvRecursiveMutexControllingTask() has the highest priority so executes\r
+ first and grabs the mutex. It then performs some recursive accesses -\r
+ between each of which it sleeps for a short period to let the lower\r
priority tasks execute. When it has completed its demo functionality\r
it gives the mutex back before suspending itself.\r
\r
prvRecursiveMutexBlockingTask() attempts to access the mutex by performing\r
- a blocking 'take'. The blocking task has a lower priority than the \r
+ a blocking 'take'. The blocking task has a lower priority than the\r
controlling task so by the time it executes the mutex has already been\r
- taken by the controlling task, causing the blocking task to block. It \r
- does not unblock until the controlling task has given the mutex back, \r
- and it does not actually run until the controlling task has suspended \r
+ taken by the controlling task, causing the blocking task to block. It\r
+ does not unblock until the controlling task has given the mutex back,\r
+ and it does not actually run until the controlling task has suspended\r
itself (due to the relative priorities). When it eventually does obtain\r
- the mutex all it does is give the mutex back prior to also suspending \r
- itself. At this point both the controlling task and the blocking task are \r
+ the mutex all it does is give the mutex back prior to also suspending\r
+ itself. At this point both the controlling task and the blocking task are\r
suspended.\r
\r
prvRecursiveMutexPollingTask() runs at the idle priority. It spins round\r
a tight loop attempting to obtain the mutex with a non-blocking call. As\r
the lowest priority task it will not successfully obtain the mutex until\r
- both the controlling and blocking tasks are suspended. Once it eventually \r
+ both the controlling and blocking tasks are suspended. Once it eventually\r
does obtain the mutex it first unsuspends both the controlling task and\r
blocking task prior to giving the mutex back - resulting in the polling\r
task temporarily inheriting the controlling tasks priority.\r
/* Misc. */\r
#define recmuSHORT_DELAY ( 20 / portTICK_RATE_MS )\r
#define recmuNO_DELAY ( ( portTickType ) 0 )\r
-#define recmuTWO_TICK_DELAY ( ( portTickType ) 2 )\r
+#define recmuTHREE_TICK_DELAY ( ( portTickType ) 3 )\r
\r
/* The three tasks as described at the top of this file. */\r
static void prvRecursiveMutexControllingTask( void *pvParameters );\r
static volatile portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;\r
static volatile unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles = 0, uxPollingCycles = 0;\r
\r
-/* Handles of the two higher priority tasks, required so they can be resumed \r
+/* Handles of the two higher priority tasks, required so they can be resumed\r
(unsuspended). */\r
static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;\r
\r
xMutex = xSemaphoreCreateRecursiveMutex();\r
\r
/* vQueueAddToRegistry() adds the mutex to the registry, if one is\r
- in use. The registry is provided as a means for kernel aware \r
+ in use. The registry is provided as a means for kernel aware\r
debuggers to locate mutex and has no purpose if a kernel aware debugger\r
is not being used. The call to vQueueAddToRegistry() will be removed\r
- by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
+ by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
defined to be less than 1. */\r
vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Recursive_Mutex" );\r
\r
{\r
/* We should now be able to take the mutex as many times as\r
we like.\r
- \r
+\r
The first time through the mutex will be immediately available, on\r
subsequent times through the mutex will be held by the polling task\r
at this point and this Take will cause the polling task to inherit\r
long enough to ensure the polling task will execute again before the\r
block time expires. If the block time does expire then the error\r
flag will be set here. */\r
- if( xSemaphoreTakeRecursive( xMutex, recmuTWO_TICK_DELAY ) != pdPASS )\r
+ if( xSemaphoreTakeRecursive( xMutex, recmuTHREE_TICK_DELAY ) != pdPASS )\r
{\r
xErrorOccurred = pdTRUE;\r
}\r
\r
/* Ensure the other task attempting to access the mutex (and the\r
other demo tasks) are able to execute to ensure they either block\r
- (where a block time is specified) or return an error (where no \r
+ (where a block time is specified) or return an error (where no\r
block time is specified) as the mutex is held by this task. */\r
vTaskDelay( recmuSHORT_DELAY );\r
}\r
xErrorOccurred = pdTRUE;\r
}\r
\r
- /* Keep count of the number of cycles this task has performed so a \r
+ /* Keep count of the number of cycles this task has performed so a\r
stall can be detected. */\r
uxControllingCycles++;\r
\r
{\r
/* This task will run while the controlling task is blocked, and the\r
controlling task will block only once it has the mutex - therefore\r
- this call should block until the controlling task has given up the \r
- mutex, and not actually execute past this call until the controlling \r
- task is suspended. */\r
- if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS )\r
+ this call should block until the controlling task has given up the\r
+ mutex, and not actually execute past this call until the controlling\r
+ task is suspended. portMAX_DELAY - 1 is used instead of portMAX_DELAY\r
+ to ensure the task's state is reported as Blocked and not Suspended in\r
+ a later call to configASSERT() (within the polling task). */\r
+ if( xSemaphoreTakeRecursive( xMutex, ( portMAX_DELAY - 1 ) ) == pdPASS )\r
{\r
if( xControllingIsSuspended != pdTRUE )\r
{\r
xErrorOccurred = pdTRUE;\r
}\r
\r
- /* Keep count of the number of cycles this task has performed so a \r
+ /* Keep count of the number of cycles this task has performed so a\r
stall can be detected. */\r
uxBlockingCycles++;\r
}\r
happen when the controlling task is also suspended. */\r
if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )\r
{\r
+ #if( INCLUDE_eTaskGetState == 1 )\r
+ {\r
+ configASSERT( eTaskGetState( xControllingTaskHandle ) == eSuspended );\r
+ configASSERT( eTaskGetState( xBlockingTaskHandle ) == eSuspended );\r
+ }\r
+ #endif /* INCLUDE_eTaskGetState */\r
+\r
/* Is the blocking task suspended? */\r
if( ( xBlockingIsSuspended != pdTRUE ) || ( xControllingIsSuspended != pdTRUE ) )\r
{\r
}\r
else\r
{\r
- /* Keep count of the number of cycles this task has performed \r
+ /* Keep count of the number of cycles this task has performed\r
so a stall can be detected. */\r
uxPollingCycles++;\r
\r
mutex by the time this fixed period has expired. */\r
vTaskResume( xBlockingTaskHandle );\r
vTaskResume( xControllingTaskHandle );\r
- \r
+\r
/* The other two tasks should now have executed and no longer\r
be suspended. */\r
if( ( xBlockingIsSuspended == pdTRUE ) || ( xControllingIsSuspended == pdTRUE ) )\r
{\r
xErrorOccurred = pdTRUE;\r
- } \r
- \r
+ }\r
+\r
+ #if( INCLUDE_uxTaskPriorityGet == 1 )\r
+ {\r
+ configASSERT( uxTaskPriorityGet( NULL ) == recmuCONTROLLING_TASK_PRIORITY );\r
+ }\r
+ #endif /* INCLUDE_uxTaskPriorityGet */\r
+\r
+ #if( INCLUDE_eTaskGetState == 1 )\r
+ {\r
+ configASSERT( eTaskGetState( xControllingTaskHandle ) == eBlocked );\r
+ configASSERT( eTaskGetState( xBlockingTaskHandle ) == eBlocked );\r
+ }\r
+ #endif /* INCLUDE_eTaskGetState */\r
+\r
/* Release the mutex, disinheriting the higher priority again. */\r
if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
{\r