]> git.sur5r.net Git - freertos/commitdiff
Replace the #define that maps the uxRecursiveCallCount to the pcReadFrom pointer...
authorrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Mon, 24 Jun 2013 12:20:00 +0000 (12:20 +0000)
committerrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Mon, 24 Jun 2013 12:20:00 +0000 (12:20 +0000)
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@1945 1d2547de-c912-0410-9cb9-b8ca96c0e9e2

FreeRTOS/Source/queue.c

index 4a6745b7befa15ddb5a50143a01aebb814bd8ee2..2698099f57842122b9b9c031e6919e959e7b46ef 100644 (file)
@@ -96,13 +96,22 @@ task.h is included from an application file. */
 \r
 #define queueERRONEOUS_UNBLOCK                 ( -1 )\r
 \r
-/* Effectively make a union out of the xQUEUE structure. */\r
+/* When the xQUEUE structure is used to represent a base queue its pcHead and\r
+pcTail members are used as pointers into the queue storage area.  When the\r
+xQUEUE structure is used to represent a mutex pcHead and pcTail pointers are\r
+not necessary, and the pcHead pointer is set to NULL to indicate that the\r
+pcTail pointer actually points to the mutex holder (if any).  Map alternative\r
+names to the pcHead and pcTail structure members to ensure the readability of\r
+the code is maintained despite this dual use of two structure members.  An\r
+alternative implementation would be to use a union, but use of a union is\r
+against the coding standard (although an exception to the standard has been\r
+permitted where the dual use also significantly changes the type of the\r
+structure member). */\r
 #define pxMutexHolder                                  pcTail\r
 #define uxQueueType                                            pcHead\r
-#define uxRecursiveCallCount                   pcReadFrom\r
 #define queueQUEUE_IS_MUTEX                            NULL\r
 \r
-/* Semaphores do not actually store or copy data, so have an items size of\r
+/* Semaphores do not actually store or copy data, so have an item size of\r
 zero. */\r
 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portBASE_TYPE ) 0 )\r
 #define queueDONT_BLOCK                                         ( ( portTickType ) 0U )\r
@@ -119,7 +128,12 @@ typedef struct QueueDefinition
        signed char *pcTail;                                    /*< Points to the byte at the end of the queue storage area.  Once more byte is allocated than necessary to store the queue items, this is used as a marker. */\r
 \r
        signed char *pcWriteTo;                                 /*< Points to the free next place in the storage area. */\r
-       signed char *pcReadFrom;                                /*< Points to the last place that a queued item was read from. */\r
+\r
+       union                                                                   /* Use of a union is an exception to the coding standard to ensure two mutually exclusive structure members don't appear simultaneously (wasting RAM). */\r
+       {\r
+               signed char *pcReadFrom;                        /*< Points to the last place that a queued item was read from when the structure is used as a queue. */\r
+               unsigned portBASE_TYPE uxRecursiveCallCount;/*< Maintains a count of the numebr of times a recursive mutex has been recursively 'taken' when the structure is used as a mutex. */\r
+       } u;\r
 \r
        xList xTasksWaitingToSend;                              /*< List of tasks that are blocked waiting to post onto this queue.  Stored in priority order. */\r
        xList xTasksWaitingToReceive;                   /*< List of tasks that are blocked waiting to read from this queue.  Stored in priority order. */\r
@@ -241,7 +255,7 @@ xQUEUE *pxQueue;
                pxQueue->pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize );\r
                pxQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;\r
                pxQueue->pcWriteTo = pxQueue->pcHead;\r
-               pxQueue->pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - ( unsigned portBASE_TYPE ) 1U ) * pxQueue->uxItemSize );\r
+               pxQueue->u.pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - ( unsigned portBASE_TYPE ) 1U ) * pxQueue->uxItemSize );\r
                pxQueue->xRxLock = queueUNLOCKED;\r
                pxQueue->xTxLock = queueUNLOCKED;\r
 \r
@@ -354,7 +368,7 @@ xQueueHandle xReturn = NULL;
                        /* Queues used as a mutex no data is actually copied into or out\r
                        of the queue. */\r
                        pxNewQueue->pcWriteTo = NULL;\r
-                       pxNewQueue->pcReadFrom = NULL;\r
+                       pxNewQueue->u.pcReadFrom = NULL;\r
 \r
                        /* Each mutex has a length of 1 (like a binary semaphore) and\r
                        an item size of 0 as nothing is actually copied into or out\r
@@ -444,7 +458,7 @@ xQueueHandle xReturn = NULL;
                this is the only condition we are interested in it does not matter if\r
                pxMutexHolder is accessed simultaneously by another task.  Therefore no\r
                mutual exclusion is required to test the pxMutexHolder variable. */\r
-               if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )\r
+               if( pxMutex->pxMutexHolder == ( void * ) xTaskGetCurrentTaskHandle() )\r
                {\r
                        traceGIVE_MUTEX_RECURSIVE( pxMutex );\r
 \r
@@ -453,10 +467,10 @@ xQueueHandle xReturn = NULL;
                        uxRecursiveCallCount is only modified by the mutex holder, and as\r
                        there can only be one, no mutual exclusion is required to modify the\r
                        uxRecursiveCallCount member. */\r
-                       ( pxMutex->uxRecursiveCallCount )--;\r
+                       ( pxMutex->u.uxRecursiveCallCount )--;\r
 \r
                        /* Have we unwound the call count? */\r
-                       if( pxMutex->uxRecursiveCallCount == 0 )\r
+                       if( pxMutex->u.uxRecursiveCallCount == 0 )\r
                        {\r
                                /* Return the mutex.  This will automatically unblock any other\r
                                task that might be waiting to access the mutex. */\r
@@ -494,9 +508,9 @@ xQueueHandle xReturn = NULL;
 \r
                traceTAKE_MUTEX_RECURSIVE( pxMutex );\r
 \r
-               if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )\r
+               if( pxMutex->pxMutexHolder == ( void * )  xTaskGetCurrentTaskHandle() )\r
                {\r
-                       ( pxMutex->uxRecursiveCallCount )++;\r
+                       ( pxMutex->u.uxRecursiveCallCount )++;\r
                        xReturn = pdPASS;\r
                }\r
                else\r
@@ -507,7 +521,7 @@ xQueueHandle xReturn = NULL;
                        we may have blocked to reach here. */\r
                        if( xReturn == pdPASS )\r
                        {\r
-                               ( pxMutex->uxRecursiveCallCount )++;\r
+                               ( pxMutex->u.uxRecursiveCallCount )++;\r
                        }\r
                        else\r
                        {\r
@@ -800,7 +814,7 @@ xQUEUE *pxQueue;
                                if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
                                {\r
                                        /* Remember our read position in case we are just peeking. */\r
-                                       pcOriginalReadPosition = pxQueue->pcReadFrom;\r
+                                       pcOriginalReadPosition = pxQueue->u.pcReadFrom;\r
 \r
                                        prvCopyDataFromQueue( pxQueue, pvBuffer );\r
 \r
@@ -817,7 +831,7 @@ xQUEUE *pxQueue;
                                                        {\r
                                                                /* Record the information required to implement\r
                                                                priority inheritance should it become necessary. */\r
-                                                               pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();\r
+                                                               pxQueue->pxMutexHolder = ( void * ) xTaskGetCurrentTaskHandle();\r
                                                        }\r
                                                }\r
                                                #endif\r
@@ -836,7 +850,7 @@ xQUEUE *pxQueue;
 \r
                                                /* We are not removing the data, so reset our read\r
                                                pointer. */\r
-                                               pxQueue->pcReadFrom = pcOriginalReadPosition;\r
+                                               pxQueue->u.pcReadFrom = pcOriginalReadPosition;\r
 \r
                                                /* The data is being left in the queue, so see if there are\r
                                                any other tasks waiting for the data. */\r
@@ -1033,7 +1047,7 @@ xQUEUE *pxQueue;
                        if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
                        {\r
                                /* Remember our read position in case we are just peeking. */\r
-                               pcOriginalReadPosition = pxQueue->pcReadFrom;\r
+                               pcOriginalReadPosition = pxQueue->u.pcReadFrom;\r
 \r
                                prvCopyDataFromQueue( pxQueue, pvBuffer );\r
 \r
@@ -1050,7 +1064,7 @@ xQUEUE *pxQueue;
                                                {\r
                                                        /* Record the information required to implement\r
                                                        priority inheritance should it become necessary. */\r
-                                                       pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();\r
+                                                       pxQueue->pxMutexHolder = ( void * ) xTaskGetCurrentTaskHandle();\r
                                                }\r
                                        }\r
                                        #endif\r
@@ -1069,7 +1083,7 @@ xQUEUE *pxQueue;
 \r
                                        /* The data is not being removed, so reset the read\r
                                        pointer. */\r
-                                       pxQueue->pcReadFrom = pcOriginalReadPosition;\r
+                                       pxQueue->u.pcReadFrom = pcOriginalReadPosition;\r
 \r
                                        /* The data is being left in the queue, so see if there are\r
                                        any other tasks waiting for the data. */\r
@@ -1319,11 +1333,11 @@ static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, port
        }\r
        else\r
        {\r
-               memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize );\r
-               pxQueue->pcReadFrom -= pxQueue->uxItemSize;\r
-               if( pxQueue->pcReadFrom < pxQueue->pcHead )\r
+               memcpy( ( void * ) pxQueue->u.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize );\r
+               pxQueue->u.pcReadFrom -= pxQueue->uxItemSize;\r
+               if( pxQueue->u.pcReadFrom < pxQueue->pcHead )\r
                {\r
-                       pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );\r
+                       pxQueue->u.pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );\r
                }\r
        }\r
 \r
@@ -1335,12 +1349,12 @@ static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
 {\r
        if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )\r
        {\r
-               pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
-               if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
+               pxQueue->u.pcReadFrom += pxQueue->uxItemSize;\r
+               if( pxQueue->u.pcReadFrom >= pxQueue->pcTail )\r
                {\r
-                       pxQueue->pcReadFrom = pxQueue->pcHead;\r
+                       pxQueue->u.pcReadFrom = pxQueue->pcHead;\r
                }\r
-               memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( size_t ) pxQueue->uxItemSize );\r
+               memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( size_t ) pxQueue->uxItemSize );\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
@@ -1633,13 +1647,13 @@ signed portBASE_TYPE xReturn;
                        if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
                        {\r
                                /* Data is available from the queue. */\r
-                               pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
-                               if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
+                               pxQueue->u.pcReadFrom += pxQueue->uxItemSize;\r
+                               if( pxQueue->u.pcReadFrom >= pxQueue->pcTail )\r
                                {\r
-                                       pxQueue->pcReadFrom = pxQueue->pcHead;\r
+                                       pxQueue->u.pcReadFrom = pxQueue->pcHead;\r
                                }\r
                                --( pxQueue->uxMessagesWaiting );\r
-                               memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
+                               memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
 \r
                                xReturn = pdPASS;\r
 \r
@@ -1717,13 +1731,13 @@ signed portBASE_TYPE xReturn;
                if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
                {\r
                        /* Copy the data from the queue. */\r
-                       pxQueue->pcReadFrom += pxQueue->uxItemSize;\r
-                       if( pxQueue->pcReadFrom >= pxQueue->pcTail )\r
+                       pxQueue->u.pcReadFrom += pxQueue->uxItemSize;\r
+                       if( pxQueue->u.pcReadFrom >= pxQueue->pcTail )\r
                        {\r
-                               pxQueue->pcReadFrom = pxQueue->pcHead;\r
+                               pxQueue->u.pcReadFrom = pxQueue->pcHead;\r
                        }\r
                        --( pxQueue->uxMessagesWaiting );\r
-                       memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
+                       memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
 \r
                        if( ( *pxCoRoutineWoken ) == pdFALSE )\r
                        {\r