]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/tasks.c
Demo application related:
[freertos] / FreeRTOS / Source / tasks.c
index fc06935d8a24eb5c7fe9341b39cf905839997b08..28dafd93f0e1454d512841461c4a3e831b397f0e 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-    FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
+    FreeRTOS V8.0.1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
     All rights reserved\r
 \r
     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
     the terms of the GNU General Public License (version 2) as published by the\r
     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
 \r
-    >>! NOTE: The modification to the GPL is included to allow you to distribute\r
-    >>! a combined work that includes FreeRTOS without being obliged to provide\r
-    >>! the source code for proprietary components outside of the FreeRTOS\r
-    >>! kernel.\r
+    >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
+    >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
+    >>!   obliged to provide the source code for proprietary components     !<<\r
+    >>!   outside of the FreeRTOS kernel.                                   !<<\r
 \r
     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
@@ -146,6 +146,7 @@ typedef struct tskTaskControlBlock
 \r
        #if ( configUSE_MUTEXES == 1 )\r
                UBaseType_t     uxBasePriority;         /*< The priority last assigned to the task - used by the priority inheritance mechanism. */\r
+               UBaseType_t     uxMutexesHeld;\r
        #endif\r
 \r
        #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
@@ -167,7 +168,11 @@ typedef struct tskTaskControlBlock
                struct  _reent xNewLib_reent;\r
        #endif\r
 \r
-} TCB_t;\r
+} tskTCB;\r
+\r
+/* The old tskTCB name is maintained above then typedefed to the new TCB_t name\r
+below to enable the use of older kernel aware debuggers. */\r
+typedef tskTCB TCB_t;\r
 \r
 /*\r
  * Some kernel aware debuggers require the data the debugger needs access to to\r
@@ -756,7 +761,11 @@ TCB_t * pxNewTCB;
                        {\r
                                /* Reset the next expected unblock time in case it referred to\r
                                the task that has just been deleted. */\r
-                               prvResetNextTaskUnblockTime();\r
+                               taskENTER_CRITICAL();\r
+                               {\r
+                                       prvResetNextTaskUnblockTime();\r
+                               }\r
+                               taskEXIT_CRITICAL();\r
                        }\r
                }\r
        }\r
@@ -1255,7 +1264,11 @@ TCB_t * pxNewTCB;
                                /* A task other than the currently running task was suspended,\r
                                reset the next expected unblock time in case it referred to the\r
                                task that is now in the Suspended state. */\r
-                               prvResetNextTaskUnblockTime();\r
+                               taskENTER_CRITICAL();\r
+                               {\r
+                                       prvResetNextTaskUnblockTime();\r
+                               }\r
+                               taskEXIT_CRITICAL();\r
                        }\r
                        else\r
                        {\r
@@ -1286,7 +1299,7 @@ TCB_t * pxNewTCB;
                        /* Has the task already been resumed from within an ISR? */\r
                        if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )\r
                        {\r
-                               /* Is it in the suspended list because it is in the     Suspended \r
+                               /* Is it in the suspended list because it is in the     Suspended\r
                                state, or because is is blocked with no timeout? */\r
                                if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )\r
                                {\r
@@ -2138,11 +2151,13 @@ void vTaskSwitchContext( void )
                }\r
                #endif /* configGENERATE_RUN_TIME_STATS */\r
 \r
+               /* Check for stack overflow, if configured. */\r
                taskFIRST_CHECK_FOR_STACK_OVERFLOW();\r
                taskSECOND_CHECK_FOR_STACK_OVERFLOW();\r
 \r
+               /* Select a new task to run using either the generic C or port\r
+               optimised asm code. */\r
                taskSELECT_HIGHEST_PRIORITY_TASK();\r
-\r
                traceTASK_SWITCHED_IN();\r
 \r
                #if ( configUSE_NEWLIB_REENTRANT == 1 )\r
@@ -2335,7 +2350,7 @@ BaseType_t xReturn;
        called from a critical section within an ISR. */\r
 \r
        /* The event list is sorted in priority order, so the first in the list can\r
-       be removed as it is known to be the highest priority.  Remove the TCB from \r
+       be removed as it is known to be the highest priority.  Remove the TCB from\r
        the delayed list, and add it to the ready list.\r
 \r
        If an event is for a queue that is locked then this function will never\r
@@ -2725,6 +2740,7 @@ UBaseType_t x;
        #if ( configUSE_MUTEXES == 1 )\r
        {\r
                pxTCB->uxBasePriority = uxPriority;\r
+               pxTCB->uxMutexesHeld = 0;\r
        }\r
        #endif /* configUSE_MUTEXES */\r
 \r
@@ -3074,8 +3090,13 @@ TCB_t *pxNewTCB;
                want to allocate and clean RAM statically. */\r
                portCLEAN_UP_TCB( pxTCB );\r
 \r
-               /* Free up the memory allocated by the scheduler for the task.  It is up to\r
-               the task to free any memory allocated at the application level. */\r
+               /* Free up the memory allocated by the scheduler for the task.  It is up\r
+               to the task to free any memory allocated at the application level. */\r
+               #if ( configUSE_NEWLIB_REENTRANT == 1 )\r
+               {\r
+                       _reclaim_reent( &( pxTCB->xNewLib_reent ) );\r
+               }\r
+               #endif /* configUSE_NEWLIB_REENTRANT */\r
                vPortFreeAligned( pxTCB->pxStack );\r
                vPortFree( pxTCB );\r
        }\r
@@ -3218,41 +3239,50 @@ TCB_t *pxTCB;
 \r
 #if ( configUSE_MUTEXES == 1 )\r
 \r
-       void vTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )\r
+       BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )\r
        {\r
        TCB_t * const pxTCB = ( TCB_t * ) pxMutexHolder;\r
+       BaseType_t xReturn = pdFALSE;\r
 \r
                if( pxMutexHolder != NULL )\r
                {\r
                        if( pxTCB->uxPriority != pxTCB->uxBasePriority )\r
                        {\r
-                               /* We must be the running task to be able to give the mutex back.\r
-                               Remove ourselves from the ready list we currently appear in. */\r
-                               if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )\r
-                               {\r
-                                       taskRESET_READY_PRIORITY( pxTCB->uxPriority );\r
-                               }\r
-                               else\r
+                               /* Only disinherit if no other mutexes are held. */\r
+                               if( pxTCB->uxMutexesHeld == 0 )\r
                                {\r
-                                       mtCOVERAGE_TEST_MARKER();\r
-                               }\r
+                                       /* The holding task must be the running task to be able to give\r
+                                       the mutex back.  Remove the holding task from the ready list. */\r
+                                       if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )\r
+                                       {\r
+                                               taskRESET_READY_PRIORITY( pxTCB->uxPriority );\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
 \r
-                               /* Disinherit the priority before adding the task into the new\r
-                               ready list. */\r
-                               traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );\r
-                               pxTCB->uxPriority = pxTCB->uxBasePriority;\r
+                                       /* Disinherit the priority before adding the task into the new\r
+                                       ready list. */\r
+                                       traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );\r
+                                       pxTCB->uxPriority = pxTCB->uxBasePriority;\r
 \r
-                               /* Only reset the event list item value if the value is not\r
-                               being used for anything else. */\r
-                               if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )\r
-                               {\r
+                                       /* Reset the event list item value.  It cannot be in use for\r
+                                       any other purpose if this task is running, and it must be\r
+                                       running to give back the mutex. */\r
                                        listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
+                                       prvAddTaskToReadyList( pxTCB );\r
+\r
+                                       /* Return true to indicate that a context switch is required.\r
+                                       This is only actually required in the corner case whereby\r
+                                       multiple mutexes were held and the mutexes were given back\r
+                                       in an order different to that in which they were taken. */\r
+                                       xReturn = pdTRUE;\r
                                }\r
                                else\r
                                {\r
                                        mtCOVERAGE_TEST_MARKER();\r
                                }\r
-                               prvAddTaskToReadyList( pxTCB );\r
                        }\r
                        else\r
                        {\r
@@ -3263,6 +3293,8 @@ TCB_t *pxTCB;
                {\r
                        mtCOVERAGE_TEST_MARKER();\r
                }\r
+\r
+               return xReturn;\r
        }\r
 \r
 #endif /* configUSE_MUTEXES */\r
@@ -3277,6 +3309,18 @@ TCB_t *pxTCB;
                if( xSchedulerRunning != pdFALSE )\r
                {\r
                        ( pxCurrentTCB->uxCriticalNesting )++;\r
+\r
+                       /* This is not the interrupt safe version of the enter critical\r
+                       function so     assert() if it is being called from an interrupt\r
+                       context.  Only API functions that end in "FromISR" can be used in an\r
+                       interrupt.  Only assert if the critical nesting count is 1 to\r
+                       protect against recursive calls if the assert function also uses a\r
+                       critical section. */\r
+                       if( pxCurrentTCB->uxCriticalNesting == 1 )\r
+                       {\r
+                               portASSERT_IF_IN_ISR();\r
+                       }\r
+\r
                }\r
                else\r
                {\r
@@ -3415,6 +3459,12 @@ TCB_t *pxTCB;
        volatile UBaseType_t uxArraySize, x;\r
        uint32_t ulTotalTime, ulStatsAsPercentage;\r
 \r
+               #if( configUSE_TRACE_FACILITY != 1 )\r
+               {\r
+                       #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().\r
+               }\r
+               #endif\r
+\r
                /*\r
                 * PLEASE NOTE:\r
                 *\r
@@ -3534,6 +3584,36 @@ TickType_t uxReturn;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+void vTaskIncrementMutexHeldCount( void )\r
+{\r
+       #if ( configUSE_MUTEXES == 1 )\r
+       {\r
+               /* If xSemaphoreCreateMutex() is called before any tasks have been created\r
+               then pxCurrentTCB will be NULL. */\r
+               if( pxCurrentTCB != NULL )\r
+               {\r
+                       ( pxCurrentTCB->uxMutexesHeld )++;\r
+               }\r
+       }\r
+       #endif\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vTaskDecrementMutexHeldCount( void )\r
+{\r
+       #if ( configUSE_MUTEXES == 1 )\r
+       {\r
+               /* If xSemaphoreCreateMutex() is called before any tasks have been created\r
+               then pxCurrentTCB will be NULL. */\r
+               if( pxCurrentTCB != NULL )\r
+               {\r
+                       configASSERT( pxCurrentTCB->uxMutexesHeld );\r
+                       ( pxCurrentTCB->uxMutexesHeld )--;\r
+               }\r
+       }\r
+       #endif\r
+}\r
+\r
 #ifdef FREERTOS_MODULE_TEST\r
        #include "tasks_test_access_functions.h"\r
 #endif\r