]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/Common/Minimal/semtest.c
Notes:
[freertos] / FreeRTOS / Demo / Common / Minimal / semtest.c
index fb0fa6c86b1d23b5b69b2f1dbc0576bddf3a954c..7a1f330f4b7acc4732dbb32c4375935866fc6c9b 100644 (file)
 */\r
 \r
 /*\r
- * Creates two sets of two tasks.  The tasks within a set share a variable, access \r
+ * Creates two sets of two tasks.  The tasks within a set share a variable, access\r
  * to which is guarded by a semaphore.\r
- * \r
- * Each task starts by attempting to obtain the semaphore.  On obtaining a \r
- * semaphore a task checks to ensure that the guarded variable has an expected \r
- * value.  It then clears the variable to zero before counting it back up to the \r
- * expected value in increments of 1.  After each increment the variable is checked \r
- * to ensure it contains the value to which it was just set. When the starting \r
- * value is again reached the task releases the semaphore giving the other task in \r
- * the set a chance to do exactly the same thing.  The starting value is high \r
+ *\r
+ * Each task starts by attempting to obtain the semaphore.  On obtaining a\r
+ * semaphore a task checks to ensure that the guarded variable has an expected\r
+ * value.  It then clears the variable to zero before counting it back up to the\r
+ * expected value in increments of 1.  After each increment the variable is checked\r
+ * to ensure it contains the value to which it was just set. When the starting\r
+ * value is again reached the task releases the semaphore giving the other task in\r
+ * the set a chance to do exactly the same thing.  The starting value is high\r
  * enough to ensure that a tick is likely to occur during the incrementing loop.\r
  *\r
- * An error is flagged if at any time during the process a shared variable is \r
- * found to have a value other than that expected.  Such an occurrence would \r
- * suggest an error in the mutual exclusion mechanism by which access to the \r
+ * An error is flagged if at any time during the process a shared variable is\r
+ * found to have a value other than that expected.  Such an occurrence would\r
+ * suggest an error in the mutual exclusion mechanism by which access to the\r
  * variable is restricted.\r
  *\r
- * The first set of two tasks poll their semaphore.  The second set use blocking \r
+ * The first set of two tasks poll their semaphore.  The second set use blocking\r
  * calls.\r
  *\r
  */\r
@@ -139,11 +139,12 @@ const TickType_t xBlockTime = ( TickType_t ) 100;
        if( pxFirstSemaphoreParameters != NULL )\r
        {\r
                /* Create the semaphore used by the first two tasks. */\r
-               pxFirstSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();\r
-               xSemaphoreGive( pxFirstSemaphoreParameters->xSemaphore );\r
+               pxFirstSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();              \r
 \r
                if( pxFirstSemaphoreParameters->xSemaphore != NULL )\r
                {\r
+                       xSemaphoreGive( pxFirstSemaphoreParameters->xSemaphore );\r
+                       \r
                        /* Create the variable which is to be shared by the first two tasks. */\r
                        pxFirstSemaphoreParameters->pulSharedVariable = ( uint32_t * ) pvPortMalloc( sizeof( uint32_t ) );\r
 \r
@@ -156,36 +157,44 @@ const TickType_t xBlockTime = ( TickType_t ) 100;
                        /* Spawn the first two tasks.  As they poll they operate at the idle priority. */\r
                        xTaskCreate( prvSemaphoreTest, "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );\r
                        xTaskCreate( prvSemaphoreTest, "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );\r
+\r
+                       /* vQueueAddToRegistry() adds the semaphore to the registry, if one\r
+                       is in use.  The registry is provided as a means for kernel aware\r
+                       debuggers to locate semaphores and has no purpose if a kernel aware\r
+                       debugger is not being used.  The call to vQueueAddToRegistry() will\r
+                       be removed by the pre-processor if configQUEUE_REGISTRY_SIZE is not\r
+                       defined or is defined to be less than 1. */\r
+                       vQueueAddToRegistry( ( QueueHandle_t ) pxFirstSemaphoreParameters->xSemaphore, "Counting_Sem_1" );\r
                }\r
        }\r
 \r
-       /* Do exactly the same to create the second set of tasks, only this time \r
+       /* Do exactly the same to create the second set of tasks, only this time\r
        provide a block time for the semaphore calls. */\r
        pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
        if( pxSecondSemaphoreParameters != NULL )\r
        {\r
-               pxSecondSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();\r
-               xSemaphoreGive( pxSecondSemaphoreParameters->xSemaphore );\r
+               pxSecondSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();             \r
 \r
                if( pxSecondSemaphoreParameters->xSemaphore != NULL )\r
                {\r
+                       xSemaphoreGive( pxSecondSemaphoreParameters->xSemaphore );\r
+                       \r
                        pxSecondSemaphoreParameters->pulSharedVariable = ( uint32_t * ) pvPortMalloc( sizeof( uint32_t ) );\r
                        *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;\r
                        pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_PERIOD_MS;\r
 \r
                        xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );\r
                        xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );\r
+\r
+                       /* vQueueAddToRegistry() adds the semaphore to the registry, if one\r
+                       is in use.  The registry is provided as a means for kernel aware\r
+                       debuggers to locate semaphores and has no purpose if a kernel aware\r
+                       debugger is not being used.  The call to vQueueAddToRegistry() will\r
+                       be removed by the pre-processor if configQUEUE_REGISTRY_SIZE is not\r
+                       defined or is defined to be less than 1. */\r
+                       vQueueAddToRegistry( ( QueueHandle_t ) pxSecondSemaphoreParameters->xSemaphore, "Counting_Sem_2" );\r
                }\r
        }\r
-\r
-       /* vQueueAddToRegistry() adds the semaphore to the registry, if one is\r
-       in use.  The registry is provided as a means for kernel aware \r
-       debuggers to locate semaphores 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
-       defined to be less than 1. */\r
-       vQueueAddToRegistry( ( QueueHandle_t ) pxFirstSemaphoreParameters->xSemaphore, "Counting_Sem_1" );\r
-       vQueueAddToRegistry( ( QueueHandle_t ) pxSecondSemaphoreParameters->xSemaphore, "Counting_Sem_2" );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -196,14 +205,14 @@ volatile uint32_t *pulSharedVariable, ulExpectedValue;
 uint32_t ulCounter;\r
 short sError = pdFALSE, sCheckVariableToUse;\r
 \r
-       /* See which check variable to use.  sNextCheckVariable is not semaphore \r
+       /* See which check variable to use.  sNextCheckVariable is not semaphore\r
        protected! */\r
        portENTER_CRITICAL();\r
                sCheckVariableToUse = sNextCheckVariable;\r
                sNextCheckVariable++;\r
        portEXIT_CRITICAL();\r
 \r
-       /* A structure is passed in as the parameter.  This contains the shared \r
+       /* A structure is passed in as the parameter.  This contains the shared\r
        variable being guarded. */\r
        pxParameters = ( xSemaphoreParameters * ) pvParameters;\r
        pulSharedVariable = pxParameters->pulSharedVariable;\r
@@ -231,7 +240,7 @@ short sError = pdFALSE, sCheckVariableToUse;
                        {\r
                                sError = pdTRUE;\r
                        }\r
-                       \r
+\r
                        /* Clear the variable, then count it back up to the expected value\r
                        before releasing the semaphore.  Would expect a context switch or\r
                        two during this time. */\r