Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1 | /*
|
Richard Barry | 7818ed5 | 2009-06-21 19:07:47 +0000 | [diff] [blame] | 2 | FreeRTOS.org V5.3.1 - Copyright (C) 2003-2009 Richard Barry.
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 3 |
|
Richard Barry | 946da76 | 2006-05-28 08:17:56 +0000 | [diff] [blame] | 4 | This file is part of the FreeRTOS.org distribution.
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 5 |
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 6 | FreeRTOS.org is free software; you can redistribute it and/or modify it
|
Richard Barry | 2f40ad7 | 2009-03-14 19:20:12 +0000 | [diff] [blame] | 7 | under the terms of the GNU General Public License (version 2) as published
|
| 8 | by the Free Software Foundation and modified by the FreeRTOS exception.
|
Richard Barry | 4bf5521 | 2009-05-30 15:51:41 +0000 | [diff] [blame] | 9 | **NOTE** The exception to the GPL is included to allow you to distribute a
|
| 10 | combined work that includes FreeRTOS.org without being obliged to provide
|
| 11 | the source code for any proprietary components. Alternative commercial
|
| 12 | license and support terms are also available upon request. See the
|
| 13 | licensing section of http://www.FreeRTOS.org for full details.
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 14 |
|
Richard Barry | 2f40ad7 | 2009-03-14 19:20:12 +0000 | [diff] [blame] | 15 | FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
| 17 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
Richard Barry | 2f40ad7 | 2009-03-14 19:20:12 +0000 | [diff] [blame] | 18 | more details.
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 19 |
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 20 | You should have received a copy of the GNU General Public License along
|
| 21 | with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
|
Richard Barry | 2f40ad7 | 2009-03-14 19:20:12 +0000 | [diff] [blame] | 22 | Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 23 |
|
Richard Barry | 2f40ad7 | 2009-03-14 19:20:12 +0000 | [diff] [blame] | 24 |
|
| 25 | ***************************************************************************
|
| 26 | * *
|
| 27 | * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
|
Richard Barry | 52ba0e6 | 2009-02-09 20:21:35 +0000 | [diff] [blame] | 28 | * *
|
| 29 | * This is a concise, step by step, 'hands on' guide that describes both *
|
| 30 | * general multitasking concepts and FreeRTOS specifics. It presents and *
|
| 31 | * explains numerous examples that are written using the FreeRTOS API. *
|
| 32 | * Full source code for all the examples is provided in an accompanying *
|
| 33 | * .zip file. *
|
Richard Barry | 2f40ad7 | 2009-03-14 19:20:12 +0000 | [diff] [blame] | 34 | * *
|
| 35 | ***************************************************************************
|
| 36 |
|
| 37 | 1 tab == 4 spaces!
|
Richard Barry | 0a6d59a | 2007-04-01 20:47:49 +0000 | [diff] [blame] | 38 |
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 39 | Please ensure to read the configuration and relevant port sections of the
|
Richard Barry | c86dcf7 | 2008-02-03 19:45:58 +0000 | [diff] [blame] | 40 | online documentation.
|
| 41 |
|
Richard Barry | 2f40ad7 | 2009-03-14 19:20:12 +0000 | [diff] [blame] | 42 | http://www.FreeRTOS.org - Documentation, latest information, license and
|
Richard Barry | 527fb6a | 2008-03-25 21:22:13 +0000 | [diff] [blame] | 43 | contact details.
|
Richard Barry | c86dcf7 | 2008-02-03 19:45:58 +0000 | [diff] [blame] | 44 |
|
Richard Barry | 2f40ad7 | 2009-03-14 19:20:12 +0000 | [diff] [blame] | 45 | http://www.SafeRTOS.com - A version that is certified for use in safety
|
Richard Barry | 527fb6a | 2008-03-25 21:22:13 +0000 | [diff] [blame] | 46 | critical systems.
|
Richard Barry | c86dcf7 | 2008-02-03 19:45:58 +0000 | [diff] [blame] | 47 |
|
Richard Barry | 2f40ad7 | 2009-03-14 19:20:12 +0000 | [diff] [blame] | 48 | http://www.OpenRTOS.com - Commercial support, development, porting,
|
Richard Barry | 527fb6a | 2008-03-25 21:22:13 +0000 | [diff] [blame] | 49 | licensing and training services.
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 50 | */
|
| 51 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 52 |
|
| 53 | #include <stdio.h>
|
| 54 | #include <stdlib.h>
|
| 55 | #include <string.h>
|
| 56 |
|
| 57 | #include "FreeRTOS.h"
|
| 58 | #include "task.h"
|
Richard Barry | 4f631de | 2008-10-09 00:53:02 +0000 | [diff] [blame] | 59 | #include "StackMacros.h"
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 60 |
|
| 61 | /*
|
| 62 | * Macro to define the amount of stack available to the idle task.
|
| 63 | */
|
| 64 | #define tskIDLE_STACK_SIZE configMINIMAL_STACK_SIZE
|
| 65 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 66 | /*
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 67 | * Task control block. A task control block (TCB) is allocated to each task,
|
| 68 | * and stores the context of the task.
|
| 69 | */
|
| 70 | typedef struct tskTaskControlBlock
|
| 71 | {
|
| 72 | volatile portSTACK_TYPE *pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE STRUCT. */
|
| 73 | xListItem xGenericListItem; /*< List item used to place the TCB in ready and blocked queues. */
|
| 74 | xListItem xEventListItem; /*< List item used to place the TCB in event lists. */
|
| 75 | unsigned portBASE_TYPE uxPriority; /*< The priority of the task where 0 is the lowest priority. */
|
| 76 | portSTACK_TYPE *pxStack; /*< Points to the start of the stack. */
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 77 | signed portCHAR pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created. Facilitates debugging only. */
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 78 |
|
Richard Barry | 4f631de | 2008-10-09 00:53:02 +0000 | [diff] [blame] | 79 | #if ( portSTACK_GROWTH > 0 )
|
| 80 | portSTACK_TYPE *pxEndOfStack; /*< Used for stack overflow checking on architectures where the stack grows up from low memory. */
|
| 81 | #endif
|
| 82 |
|
Richard Barry | 2b174e5 | 2008-02-25 18:54:28 +0000 | [diff] [blame] | 83 | #if ( portCRITICAL_NESTING_IN_TCB == 1 )
|
| 84 | unsigned portBASE_TYPE uxCriticalNesting;
|
| 85 | #endif
|
| 86 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 87 | #if ( configUSE_TRACE_FACILITY == 1 )
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 88 | unsigned portBASE_TYPE uxTCBNumber; /*< This is used for tracing the scheduler and making debugging easier only. */
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 89 | #endif
|
| 90 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 91 | #if ( configUSE_MUTEXES == 1 )
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 92 | unsigned portBASE_TYPE uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 93 | #endif
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 94 |
|
Richard Barry | e939542 | 2008-04-16 07:47:02 +0000 | [diff] [blame] | 95 | #if ( configUSE_APPLICATION_TASK_TAG == 1 )
|
| 96 | pdTASK_HOOK_CODE pxTaskTag;
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 97 | #endif
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 98 |
|
| 99 | #if ( configGENERATE_RUN_TIME_STATS == 1 )
|
| 100 | unsigned portLONG ulRunTimeCounter; /*< Used for calculating how much CPU time each task is utilising. */
|
| 101 | #endif
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 102 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 103 | } tskTCB;
|
| 104 |
|
Richard Barry | 8ce7a79 | 2008-05-24 18:52:12 +0000 | [diff] [blame] | 105 | /*
|
| 106 | * Some kernel aware debuggers require data to be viewed to be global, rather
|
| 107 | * than file scope.
|
| 108 | */
|
| 109 | #ifdef portREMOVE_STATIC_QUALIFIER
|
| 110 | #define static
|
| 111 | #endif
|
| 112 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 113 | /*lint -e956 */
|
| 114 |
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 115 | tskTCB * volatile pxCurrentTCB = NULL;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 116 |
|
| 117 | /* Lists for ready and blocked tasks. --------------------*/
|
| 118 |
|
| 119 | static xList pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
|
| 120 | static xList xDelayedTaskList1; /*< Delayed tasks. */
|
| 121 | static xList xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
|
| 122 | static xList * volatile pxDelayedTaskList; /*< Points to the delayed task list currently being used. */
|
| 123 | static xList * volatile pxOverflowDelayedTaskList; /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
|
| 124 | static xList xPendingReadyList; /*< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready queue when the scheduler is resumed. */
|
| 125 |
|
| 126 | #if ( INCLUDE_vTaskDelete == 1 )
|
| 127 |
|
| 128 | static volatile xList xTasksWaitingTermination; /*< Tasks that have been deleted - but the their memory not yet freed. */
|
| 129 | static volatile unsigned portBASE_TYPE uxTasksDeleted = ( unsigned portBASE_TYPE ) 0;
|
| 130 |
|
| 131 | #endif
|
| 132 |
|
| 133 | #if ( INCLUDE_vTaskSuspend == 1 )
|
| 134 |
|
| 135 | static xList xSuspendedTaskList; /*< Tasks that are currently suspended. */
|
| 136 |
|
| 137 | #endif
|
| 138 |
|
| 139 | /* File private variables. --------------------------------*/
|
| 140 | static volatile unsigned portBASE_TYPE uxCurrentNumberOfTasks = ( unsigned portBASE_TYPE ) 0;
|
| 141 | static volatile portTickType xTickCount = ( portTickType ) 0;
|
Richard Barry | b18929e | 2006-08-27 14:09:54 +0000 | [diff] [blame] | 142 | static unsigned portBASE_TYPE uxTopUsedPriority = tskIDLE_PRIORITY;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 143 | static volatile unsigned portBASE_TYPE uxTopReadyPriority = tskIDLE_PRIORITY;
|
| 144 | static volatile signed portBASE_TYPE xSchedulerRunning = pdFALSE;
|
| 145 | static volatile unsigned portBASE_TYPE uxSchedulerSuspended = ( unsigned portBASE_TYPE ) pdFALSE;
|
| 146 | static volatile unsigned portBASE_TYPE uxMissedTicks = ( unsigned portBASE_TYPE ) 0;
|
| 147 | static volatile portBASE_TYPE xMissedYield = ( portBASE_TYPE ) pdFALSE;
|
Richard Barry | b18929e | 2006-08-27 14:09:54 +0000 | [diff] [blame] | 148 | static volatile portBASE_TYPE xNumOfOverflows = ( portBASE_TYPE ) 0;
|
Richard Barry | a7f1349 | 2009-05-03 08:28:14 +0000 | [diff] [blame] | 149 | static unsigned portBASE_TYPE uxTaskNumber = ( unsigned portBASE_TYPE ) 0;
|
Richard Barry | 6eed669 | 2008-07-30 09:34:03 +0000 | [diff] [blame] | 150 |
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 151 | #if ( configGENERATE_RUN_TIME_STATS == 1 )
|
| 152 |
|
| 153 | static portCHAR pcStatsString[ 50 ];
|
| 154 | static unsigned portLONG ulTaskSwitchedInTime = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */
|
| 155 | static void prvGenerateRunTimeStatsForTasksInList( const signed portCHAR *pcWriteBuffer, xList *pxList, unsigned portLONG ulTotalRunTime );
|
| 156 |
|
| 157 | #endif
|
| 158 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 159 | /* Debugging and trace facilities private variables and macros. ------------*/
|
| 160 |
|
| 161 | /*
|
| 162 | * The value used to fill the stack of a task when the task is created. This
|
| 163 | * is used purely for checking the high water mark for tasks.
|
| 164 | */
|
| 165 | #define tskSTACK_FILL_BYTE ( 0xa5 )
|
| 166 |
|
| 167 | /*
|
| 168 | * Macros used by vListTask to indicate which state a task is in.
|
| 169 | */
|
| 170 | #define tskBLOCKED_CHAR ( ( signed portCHAR ) 'B' )
|
| 171 | #define tskREADY_CHAR ( ( signed portCHAR ) 'R' )
|
| 172 | #define tskDELETED_CHAR ( ( signed portCHAR ) 'D' )
|
| 173 | #define tskSUSPENDED_CHAR ( ( signed portCHAR ) 'S' )
|
| 174 |
|
| 175 | /*
|
| 176 | * Macros and private variables used by the trace facility.
|
| 177 | */
|
| 178 | #if ( configUSE_TRACE_FACILITY == 1 )
|
| 179 |
|
| 180 | #define tskSIZE_OF_EACH_TRACE_LINE ( ( unsigned portLONG ) ( sizeof( unsigned portLONG ) + sizeof( unsigned portLONG ) ) )
|
| 181 | static volatile signed portCHAR * volatile pcTraceBuffer;
|
| 182 | static signed portCHAR *pcTraceBufferStart;
|
| 183 | static signed portCHAR *pcTraceBufferEnd;
|
| 184 | static signed portBASE_TYPE xTracing = pdFALSE;
|
Richard Barry | 6eed669 | 2008-07-30 09:34:03 +0000 | [diff] [blame] | 185 | static unsigned portBASE_TYPE uxPreviousTask = 255;
|
| 186 | static portCHAR pcStatusString[ 50 ];
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 187 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 188 | #endif
|
| 189 |
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 190 | /*-----------------------------------------------------------*/
|
| 191 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 192 | /*
|
| 193 | * Macro that writes a trace of scheduler activity to a buffer. This trace
|
| 194 | * shows which task is running when and is very useful as a debugging tool.
|
| 195 | * As this macro is called each context switch it is a good idea to undefine
|
| 196 | * it if not using the facility.
|
| 197 | */
|
| 198 | #if ( configUSE_TRACE_FACILITY == 1 )
|
| 199 |
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 200 | #define vWriteTraceToBuffer() \
|
| 201 | { \
|
| 202 | if( xTracing ) \
|
| 203 | { \
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 204 | if( uxPreviousTask != pxCurrentTCB->uxTCBNumber ) \
|
| 205 | { \
|
| 206 | if( ( pcTraceBuffer + tskSIZE_OF_EACH_TRACE_LINE ) < pcTraceBufferEnd ) \
|
| 207 | { \
|
| 208 | uxPreviousTask = pxCurrentTCB->uxTCBNumber; \
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 209 | *( unsigned portLONG * ) pcTraceBuffer = ( unsigned portLONG ) xTickCount; \
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 210 | pcTraceBuffer += sizeof( unsigned portLONG ); \
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 211 | *( unsigned portLONG * ) pcTraceBuffer = ( unsigned portLONG ) uxPreviousTask; \
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 212 | pcTraceBuffer += sizeof( unsigned portLONG ); \
|
| 213 | } \
|
| 214 | else \
|
| 215 | { \
|
| 216 | xTracing = pdFALSE; \
|
| 217 | } \
|
| 218 | } \
|
| 219 | } \
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 220 | }
|
| 221 |
|
| 222 | #else
|
| 223 |
|
| 224 | #define vWriteTraceToBuffer()
|
| 225 |
|
| 226 | #endif
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 227 | /*-----------------------------------------------------------*/
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 228 |
|
| 229 | /*
|
| 230 | * Place the task represented by pxTCB into the appropriate ready queue for
|
| 231 | * the task. It is inserted at the end of the list. One quirk of this is
|
| 232 | * that if the task being inserted is at the same priority as the currently
|
| 233 | * executing task, then it will only be rescheduled after the currently
|
| 234 | * executing task has been rescheduled.
|
| 235 | */
|
| 236 | #define prvAddTaskToReadyQueue( pxTCB ) \
|
| 237 | { \
|
| 238 | if( pxTCB->uxPriority > uxTopReadyPriority ) \
|
| 239 | { \
|
| 240 | uxTopReadyPriority = pxTCB->uxPriority; \
|
| 241 | } \
|
| 242 | vListInsertEnd( ( xList * ) &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ); \
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 243 | }
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 244 | /*-----------------------------------------------------------*/
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 245 |
|
| 246 | /*
|
| 247 | * Macro that looks at the list of tasks that are currently delayed to see if
|
| 248 | * any require waking.
|
| 249 | *
|
| 250 | * Tasks are stored in the queue in the order of their wake time - meaning
|
| 251 | * once one tasks has been found whose timer has not expired we need not look
|
| 252 | * any further down the list.
|
| 253 | */
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 254 | #define prvCheckDelayedTasks() \
|
| 255 | { \
|
| 256 | register tskTCB *pxTCB; \
|
| 257 | \
|
| 258 | while( ( pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ) ) != NULL ) \
|
| 259 | { \
|
| 260 | if( xTickCount < listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) ) ) \
|
| 261 | { \
|
| 262 | break; \
|
| 263 | } \
|
| 264 | vListRemove( &( pxTCB->xGenericListItem ) ); \
|
| 265 | /* Is the task waiting on an event also? */ \
|
| 266 | if( pxTCB->xEventListItem.pvContainer ) \
|
| 267 | { \
|
| 268 | vListRemove( &( pxTCB->xEventListItem ) ); \
|
| 269 | } \
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 270 | prvAddTaskToReadyQueue( pxTCB ); \
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 271 | } \
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 272 | }
|
| 273 | /*-----------------------------------------------------------*/
|
| 274 |
|
| 275 | /*
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 276 | * Several functions take an xTaskHandle parameter that can optionally be NULL,
|
| 277 | * where NULL is used to indicate that the handle of the currently executing
|
| 278 | * task should be used in place of the parameter. This macro simply checks to
|
| 279 | * see if the parameter is NULL and returns a pointer to the appropriate TCB.
|
| 280 | */
|
| 281 | #define prvGetTCBFromHandle( pxHandle ) ( ( pxHandle == NULL ) ? ( tskTCB * ) pxCurrentTCB : ( tskTCB * ) pxHandle )
|
| 282 |
|
| 283 |
|
| 284 | /* File private functions. --------------------------------*/
|
| 285 |
|
| 286 | /*
|
| 287 | * Utility to ready a TCB for a given task. Mainly just copies the parameters
|
| 288 | * into the TCB structure.
|
| 289 | */
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 290 | static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed portCHAR * const pcName, unsigned portBASE_TYPE uxPriority );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 291 |
|
| 292 | /*
|
| 293 | * Utility to ready all the lists used by the scheduler. This is called
|
| 294 | * automatically upon the creation of the first task.
|
| 295 | */
|
| 296 | static void prvInitialiseTaskLists( void );
|
| 297 |
|
| 298 | /*
|
| 299 | * The idle task, which as all tasks is implemented as a never ending loop.
|
| 300 | * The idle task is automatically created and added to the ready lists upon
|
| 301 | * creation of the first user task.
|
| 302 | *
|
| 303 | * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
|
| 304 | * language extensions. The equivalent prototype for this function is:
|
| 305 | *
|
| 306 | * void prvIdleTask( void *pvParameters );
|
| 307 | *
|
| 308 | */
|
| 309 | static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
|
| 310 |
|
| 311 | /*
|
| 312 | * Utility to free all memory allocated by the scheduler to hold a TCB,
|
| 313 | * including the stack pointed to by the TCB.
|
| 314 | *
|
| 315 | * This does not free memory allocated by the task itself (i.e. memory
|
| 316 | * allocated by calls to pvPortMalloc from within the tasks application code).
|
| 317 | */
|
| 318 | #if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 319 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 320 | static void prvDeleteTCB( tskTCB *pxTCB );
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 321 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 322 | #endif
|
| 323 |
|
| 324 | /*
|
| 325 | * Used only by the idle task. This checks to see if anything has been placed
|
| 326 | * in the list of tasks waiting to be deleted. If so the task is cleaned up
|
| 327 | * and its TCB deleted.
|
| 328 | */
|
| 329 | static void prvCheckTasksWaitingTermination( void );
|
| 330 |
|
| 331 | /*
|
| 332 | * Allocates memory from the heap for a TCB and associated stack. Checks the
|
| 333 | * allocation was successful.
|
| 334 | */
|
| 335 | static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth );
|
| 336 |
|
| 337 | /*
|
| 338 | * Called from vTaskList. vListTasks details all the tasks currently under
|
| 339 | * control of the scheduler. The tasks may be in one of a number of lists.
|
| 340 | * prvListTaskWithinSingleList accepts a list and details the tasks from
|
| 341 | * within just that list.
|
| 342 | *
|
| 343 | * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
|
| 344 | * NORMAL APPLICATION CODE.
|
| 345 | */
|
| 346 | #if ( configUSE_TRACE_FACILITY == 1 )
|
| 347 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 348 | static void prvListTaskWithinSingleList( const signed portCHAR *pcWriteBuffer, xList *pxList, signed portCHAR cStatus );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 349 |
|
| 350 | #endif
|
| 351 |
|
| 352 | /*
|
| 353 | * When a task is created, the stack of the task is filled with a known value.
|
| 354 | * This function determines the 'high water mark' of the task stack by
|
| 355 | * determining how much of the stack remains at the original preset value.
|
| 356 | */
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 357 | #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 358 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 359 | unsigned portSHORT usTaskCheckFreeStackSpace( const unsigned portCHAR * pucStackByte );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 360 |
|
| 361 | #endif
|
| 362 |
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 363 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 364 | /*lint +e956 */
|
| 365 |
|
| 366 |
|
| 367 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 368 | /*-----------------------------------------------------------
|
| 369 | * TASK CREATION API documented in task.h
|
| 370 | *----------------------------------------------------------*/
|
| 371 |
|
| 372 | signed portBASE_TYPE xTaskCreate( pdTASK_CODE pvTaskCode, const signed portCHAR * const pcName, unsigned portSHORT usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask )
|
| 373 | {
|
| 374 | signed portBASE_TYPE xReturn;
|
| 375 | tskTCB * pxNewTCB;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 376 |
|
| 377 | /* Allocate the memory required by the TCB and stack for the new task.
|
| 378 | checking that the allocation was successful. */
|
| 379 | pxNewTCB = prvAllocateTCBAndStack( usStackDepth );
|
| 380 |
|
| 381 | if( pxNewTCB != NULL )
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 382 | {
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 383 | portSTACK_TYPE *pxTopOfStack;
|
| 384 |
|
| 385 | /* Setup the newly allocated TCB with the initial state of the task. */
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 386 | prvInitialiseTCBVariables( pxNewTCB, pcName, uxPriority );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 387 |
|
| 388 | /* Calculate the top of stack address. This depends on whether the
|
| 389 | stack grows from high memory to low (as per the 80x86) or visa versa.
|
| 390 | portSTACK_GROWTH is used to make the result positive or negative as
|
| 391 | required by the port. */
|
| 392 | #if portSTACK_GROWTH < 0
|
| 393 | {
|
Richard Barry | 0a9fddb | 2009-06-30 16:32:36 +0000 | [diff] [blame] | 394 | pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 ) - ( ( usStackDepth - 1 ) % portBYTE_ALIGNMENT );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 395 | }
|
| 396 | #else
|
| 397 | {
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 398 | pxTopOfStack = pxNewTCB->pxStack;
|
Richard Barry | 4f631de | 2008-10-09 00:53:02 +0000 | [diff] [blame] | 399 |
|
| 400 | /* If we want to use stack checking on architectures that use
|
| 401 | a positive stack growth direction then we also need to store the
|
| 402 | other extreme of the stack space. */
|
| 403 | pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 404 | }
|
| 405 | #endif
|
| 406 |
|
| 407 | /* Initialize the TCB stack to look as if the task was already running,
|
| 408 | but had been interrupted by the scheduler. The return address is set
|
| 409 | to the start of the task function. Once the stack has been initialised
|
| 410 | the top of stack variable is updated. */
|
| 411 | pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pvTaskCode, pvParameters );
|
| 412 |
|
| 413 | /* We are going to manipulate the task queues to add this task to a
|
| 414 | ready list, so must make sure no interrupts occur. */
|
| 415 | portENTER_CRITICAL();
|
| 416 | {
|
| 417 | uxCurrentNumberOfTasks++;
|
| 418 | if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1 )
|
| 419 | {
|
| 420 | /* As this is the first task it must also be the current task. */
|
| 421 | pxCurrentTCB = pxNewTCB;
|
| 422 |
|
| 423 | /* This is the first task to be created so do the preliminary
|
| 424 | initialisation required. We will not recover if this call
|
| 425 | fails, but we will report the failure. */
|
| 426 | prvInitialiseTaskLists();
|
| 427 | }
|
| 428 | else
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 429 | {
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 430 | /* If the scheduler is not already running, make this task the
|
| 431 | current task if it is the highest priority task to be created
|
| 432 | so far. */
|
| 433 | if( xSchedulerRunning == pdFALSE )
|
| 434 | {
|
| 435 | if( pxCurrentTCB->uxPriority <= uxPriority )
|
| 436 | {
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 437 | pxCurrentTCB = pxNewTCB;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 438 | }
|
| 439 | }
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 440 | }
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 441 |
|
| 442 | /* Remember the top priority to make context switching faster. Use
|
| 443 | the priority in pxNewTCB as this has been capped to a valid value. */
|
| 444 | if( pxNewTCB->uxPriority > uxTopUsedPriority )
|
| 445 | {
|
| 446 | uxTopUsedPriority = pxNewTCB->uxPriority;
|
| 447 | }
|
| 448 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 449 | #if ( configUSE_TRACE_FACILITY == 1 )
|
| 450 | {
|
| 451 | /* Add a counter into the TCB for tracing only. */
|
| 452 | pxNewTCB->uxTCBNumber = uxTaskNumber;
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 453 | }
|
| 454 | #endif
|
Richard Barry | 42b60ed | 2009-03-14 15:04:44 +0000 | [diff] [blame] | 455 | uxTaskNumber++;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 456 |
|
| 457 | prvAddTaskToReadyQueue( pxNewTCB );
|
| 458 |
|
| 459 | xReturn = pdPASS;
|
Richard Barry | b8b7052 | 2008-03-03 16:32:05 +0000 | [diff] [blame] | 460 | traceTASK_CREATE( pxNewTCB );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 461 | }
|
| 462 | portEXIT_CRITICAL();
|
| 463 | }
|
| 464 | else
|
| 465 | {
|
| 466 | xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
|
Richard Barry | b8b7052 | 2008-03-03 16:32:05 +0000 | [diff] [blame] | 467 | traceTASK_CREATE_FAILED( pxNewTCB );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 468 | }
|
| 469 |
|
| 470 | if( xReturn == pdPASS )
|
| 471 | {
|
| 472 | if( ( void * ) pxCreatedTask != NULL )
|
| 473 | {
|
| 474 | /* Pass the TCB out - in an anonymous way. The calling function/
|
| 475 | task can use this as a handle to delete the task later if
|
| 476 | required.*/
|
| 477 | *pxCreatedTask = ( xTaskHandle ) pxNewTCB;
|
| 478 | }
|
| 479 |
|
| 480 | if( xSchedulerRunning != pdFALSE )
|
| 481 | {
|
| 482 | /* If the created task is of a higher priority than the current task
|
| 483 | then it should run now. */
|
| 484 | if( pxCurrentTCB->uxPriority < uxPriority )
|
| 485 | {
|
| 486 | taskYIELD();
|
| 487 | }
|
| 488 | }
|
| 489 | }
|
| 490 |
|
| 491 | return xReturn;
|
| 492 | }
|
| 493 | /*-----------------------------------------------------------*/
|
| 494 |
|
| 495 | #if ( INCLUDE_vTaskDelete == 1 )
|
| 496 |
|
| 497 | void vTaskDelete( xTaskHandle pxTaskToDelete )
|
| 498 | {
|
| 499 | tskTCB *pxTCB;
|
| 500 |
|
| 501 | taskENTER_CRITICAL();
|
| 502 | {
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 503 | /* Ensure a yield is performed if the current task is being
|
Richard Barry | 946da76 | 2006-05-28 08:17:56 +0000 | [diff] [blame] | 504 | deleted. */
|
| 505 | if( pxTaskToDelete == pxCurrentTCB )
|
| 506 | {
|
| 507 | pxTaskToDelete = NULL;
|
| 508 | }
|
| 509 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 510 | /* If null is passed in here then we are deleting ourselves. */
|
| 511 | pxTCB = prvGetTCBFromHandle( pxTaskToDelete );
|
| 512 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 513 | /* Remove task from the ready list and place in the termination list.
|
| 514 | This will stop the task from be scheduled. The idle task will check
|
| 515 | the termination list and free up any memory allocated by the
|
| 516 | scheduler for the TCB and stack. */
|
| 517 | vListRemove( &( pxTCB->xGenericListItem ) );
|
| 518 |
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 519 | /* Is the task waiting on an event also? */
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 520 | if( pxTCB->xEventListItem.pvContainer )
|
| 521 | {
|
| 522 | vListRemove( &( pxTCB->xEventListItem ) );
|
| 523 | }
|
| 524 |
|
| 525 | vListInsertEnd( ( xList * ) &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );
|
| 526 |
|
| 527 | /* Increment the ucTasksDeleted variable so the idle task knows
|
| 528 | there is a task that has been deleted and that it should therefore
|
| 529 | check the xTasksWaitingTermination list. */
|
| 530 | ++uxTasksDeleted;
|
Richard Barry | 42b60ed | 2009-03-14 15:04:44 +0000 | [diff] [blame] | 531 |
|
| 532 | /* Increment the uxTaskNumberVariable also so kernel aware debuggers
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 533 | can detect that the task lists need re-generating. */
|
Richard Barry | 42b60ed | 2009-03-14 15:04:44 +0000 | [diff] [blame] | 534 | uxTaskNumber++;
|
Richard Barry | 245bb81 | 2009-05-22 15:35:40 +0000 | [diff] [blame] | 535 |
|
| 536 | traceTASK_DELETE( pxTCB );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 537 | }
|
| 538 | taskEXIT_CRITICAL();
|
| 539 |
|
| 540 | /* Force a reschedule if we have just deleted the current task. */
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 541 | if( xSchedulerRunning != pdFALSE )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 542 | {
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 543 | if( ( void * ) pxTaskToDelete == NULL )
|
| 544 | {
|
| 545 | taskYIELD();
|
| 546 | }
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 547 | }
|
| 548 | }
|
| 549 |
|
| 550 | #endif
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 | /*-----------------------------------------------------------
|
| 558 | * TASK CONTROL API documented in task.h
|
| 559 | *----------------------------------------------------------*/
|
| 560 |
|
| 561 | #if ( INCLUDE_vTaskDelayUntil == 1 )
|
| 562 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 563 | void vTaskDelayUntil( portTickType * const pxPreviousWakeTime, portTickType xTimeIncrement )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 564 | {
|
| 565 | portTickType xTimeToWake;
|
| 566 | portBASE_TYPE xAlreadyYielded, xShouldDelay = pdFALSE;
|
| 567 |
|
| 568 | vTaskSuspendAll();
|
| 569 | {
|
| 570 | /* Generate the tick time at which the task wants to wake. */
|
| 571 | xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
|
| 572 |
|
| 573 | if( xTickCount < *pxPreviousWakeTime )
|
| 574 | {
|
| 575 | /* The tick count has overflowed since this function was
|
| 576 | lasted called. In this case the only time we should ever
|
| 577 | actually delay is if the wake time has also overflowed,
|
| 578 | and the wake time is greater than the tick time. When this
|
| 579 | is the case it is as if neither time had overflowed. */
|
| 580 | if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xTickCount ) )
|
| 581 | {
|
| 582 | xShouldDelay = pdTRUE;
|
| 583 | }
|
| 584 | }
|
| 585 | else
|
| 586 | {
|
| 587 | /* The tick time has not overflowed. In this case we will
|
| 588 | delay if either the wake time has overflowed, and/or the
|
| 589 | tick time is less than the wake time. */
|
| 590 | if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xTickCount ) )
|
| 591 | {
|
| 592 | xShouldDelay = pdTRUE;
|
| 593 | }
|
| 594 | }
|
| 595 |
|
| 596 | /* Update the wake time ready for the next call. */
|
| 597 | *pxPreviousWakeTime = xTimeToWake;
|
| 598 |
|
| 599 | if( xShouldDelay )
|
| 600 | {
|
Richard Barry | b8b7052 | 2008-03-03 16:32:05 +0000 | [diff] [blame] | 601 | traceTASK_DELAY_UNTIL();
|
| 602 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 603 | /* We must remove ourselves from the ready list before adding
|
| 604 | ourselves to the blocked list as the same list item is used for
|
| 605 | both lists. */
|
| 606 | vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
|
| 607 |
|
| 608 | /* The list item will be inserted in wake time order. */
|
| 609 | listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
|
| 610 |
|
| 611 | if( xTimeToWake < xTickCount )
|
| 612 | {
|
| 613 | /* Wake time has overflowed. Place this item in the
|
| 614 | overflow list. */
|
| 615 | vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
|
| 616 | }
|
| 617 | else
|
| 618 | {
|
| 619 | /* The wake time has not overflowed, so we can use the
|
| 620 | current block list. */
|
| 621 | vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
|
| 622 | }
|
| 623 | }
|
| 624 | }
|
| 625 | xAlreadyYielded = xTaskResumeAll();
|
| 626 |
|
| 627 | /* Force a reschedule if xTaskResumeAll has not already done so, we may
|
| 628 | have put ourselves to sleep. */
|
| 629 | if( !xAlreadyYielded )
|
| 630 | {
|
| 631 | taskYIELD();
|
| 632 | }
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 633 | }
|
| 634 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 635 | #endif
|
| 636 | /*-----------------------------------------------------------*/
|
| 637 |
|
| 638 | #if ( INCLUDE_vTaskDelay == 1 )
|
| 639 |
|
| 640 | void vTaskDelay( portTickType xTicksToDelay )
|
| 641 | {
|
| 642 | portTickType xTimeToWake;
|
| 643 | signed portBASE_TYPE xAlreadyYielded = pdFALSE;
|
| 644 |
|
| 645 | /* A delay time of zero just forces a reschedule. */
|
| 646 | if( xTicksToDelay > ( portTickType ) 0 )
|
| 647 | {
|
| 648 | vTaskSuspendAll();
|
| 649 | {
|
Richard Barry | b8b7052 | 2008-03-03 16:32:05 +0000 | [diff] [blame] | 650 | traceTASK_DELAY();
|
| 651 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 652 | /* A task that is removed from the event list while the
|
| 653 | scheduler is suspended will not get placed in the ready
|
| 654 | list or removed from the blocked list until the scheduler
|
| 655 | is resumed.
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 656 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 657 | This task cannot be in an event list as it is the currently
|
| 658 | executing task. */
|
| 659 |
|
| 660 | /* Calculate the time to wake - this may overflow but this is
|
| 661 | not a problem. */
|
| 662 | xTimeToWake = xTickCount + xTicksToDelay;
|
| 663 |
|
| 664 | /* We must remove ourselves from the ready list before adding
|
| 665 | ourselves to the blocked list as the same list item is used for
|
| 666 | both lists. */
|
| 667 | vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
|
| 668 |
|
| 669 | /* The list item will be inserted in wake time order. */
|
| 670 | listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
|
| 671 |
|
| 672 | if( xTimeToWake < xTickCount )
|
| 673 | {
|
| 674 | /* Wake time has overflowed. Place this item in the
|
| 675 | overflow list. */
|
| 676 | vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
|
| 677 | }
|
| 678 | else
|
| 679 | {
|
| 680 | /* The wake time has not overflowed, so we can use the
|
| 681 | current block list. */
|
| 682 | vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
|
| 683 | }
|
| 684 | }
|
| 685 | xAlreadyYielded = xTaskResumeAll();
|
| 686 | }
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 687 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 688 | /* Force a reschedule if xTaskResumeAll has not already done so, we may
|
| 689 | have put ourselves to sleep. */
|
| 690 | if( !xAlreadyYielded )
|
| 691 | {
|
| 692 | taskYIELD();
|
| 693 | }
|
| 694 | }
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 695 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 696 | #endif
|
| 697 | /*-----------------------------------------------------------*/
|
| 698 |
|
| 699 | #if ( INCLUDE_uxTaskPriorityGet == 1 )
|
| 700 |
|
| 701 | unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask )
|
| 702 | {
|
| 703 | tskTCB *pxTCB;
|
| 704 | unsigned portBASE_TYPE uxReturn;
|
| 705 |
|
| 706 | taskENTER_CRITICAL();
|
| 707 | {
|
| 708 | /* If null is passed in here then we are changing the
|
| 709 | priority of the calling function. */
|
| 710 | pxTCB = prvGetTCBFromHandle( pxTask );
|
| 711 | uxReturn = pxTCB->uxPriority;
|
| 712 | }
|
| 713 | taskEXIT_CRITICAL();
|
| 714 |
|
| 715 | return uxReturn;
|
| 716 | }
|
| 717 |
|
| 718 | #endif
|
| 719 | /*-----------------------------------------------------------*/
|
| 720 |
|
| 721 | #if ( INCLUDE_vTaskPrioritySet == 1 )
|
| 722 |
|
| 723 | void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority )
|
| 724 | {
|
| 725 | tskTCB *pxTCB;
|
Richard Barry | 3dcbed5 | 2006-06-22 17:18:31 +0000 | [diff] [blame] | 726 | unsigned portBASE_TYPE uxCurrentPriority, xYieldRequired = pdFALSE;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 727 |
|
| 728 | /* Ensure the new priority is valid. */
|
| 729 | if( uxNewPriority >= configMAX_PRIORITIES )
|
| 730 | {
|
| 731 | uxNewPriority = configMAX_PRIORITIES - 1;
|
| 732 | }
|
| 733 |
|
| 734 | taskENTER_CRITICAL();
|
| 735 | {
|
Richard Barry | 3f62002 | 2008-05-01 17:15:36 +0000 | [diff] [blame] | 736 | if( pxTask == pxCurrentTCB )
|
| 737 | {
|
| 738 | pxTask = NULL;
|
| 739 | }
|
| 740 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 741 | /* If null is passed in here then we are changing the
|
| 742 | priority of the calling function. */
|
| 743 | pxTCB = prvGetTCBFromHandle( pxTask );
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 744 |
|
Richard Barry | b8b7052 | 2008-03-03 16:32:05 +0000 | [diff] [blame] | 745 | traceTASK_PRIORITY_SET( pxTask, uxNewPriority );
|
| 746 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 747 | #if ( configUSE_MUTEXES == 1 )
|
| 748 | {
|
| 749 | uxCurrentPriority = pxTCB->uxBasePriority;
|
| 750 | }
|
| 751 | #else
|
| 752 | {
|
| 753 | uxCurrentPriority = pxTCB->uxPriority;
|
| 754 | }
|
| 755 | #endif
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 756 |
|
| 757 | if( uxCurrentPriority != uxNewPriority )
|
| 758 | {
|
Richard Barry | 3dcbed5 | 2006-06-22 17:18:31 +0000 | [diff] [blame] | 759 | /* The priority change may have readied a task of higher
|
| 760 | priority than the calling task. */
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 761 | if( uxNewPriority > uxCurrentPriority )
|
Richard Barry | 3dcbed5 | 2006-06-22 17:18:31 +0000 | [diff] [blame] | 762 | {
|
| 763 | if( pxTask != NULL )
|
| 764 | {
|
| 765 | /* The priority of another task is being raised. If we
|
| 766 | were raising the priority of the currently running task
|
| 767 | there would be no need to switch as it must have already
|
| 768 | been the highest priority task. */
|
| 769 | xYieldRequired = pdTRUE;
|
| 770 | }
|
| 771 | }
|
| 772 | else if( pxTask == NULL )
|
| 773 | {
|
| 774 | /* Setting our own priority down means there may now be another
|
| 775 | task of higher priority that is ready to execute. */
|
| 776 | xYieldRequired = pdTRUE;
|
| 777 | }
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 778 |
|
| 779 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 780 |
|
| 781 | #if ( configUSE_MUTEXES == 1 )
|
| 782 | {
|
| 783 | /* Only change the priority being used if the task is not
|
| 784 | currently using an inherited priority. */
|
| 785 | if( pxTCB->uxBasePriority == pxTCB->uxPriority )
|
| 786 | {
|
| 787 | pxTCB->uxPriority = uxNewPriority;
|
| 788 | }
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 789 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 790 | /* The base priority gets set whatever. */
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 791 | pxTCB->uxBasePriority = uxNewPriority;
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 792 | }
|
| 793 | #else
|
| 794 | {
|
| 795 | pxTCB->uxPriority = uxNewPriority;
|
| 796 | }
|
| 797 | #endif
|
| 798 |
|
Richard Barry | 98a9959 | 2007-09-17 10:07:48 +0000 | [diff] [blame] | 799 | listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( configMAX_PRIORITIES - ( portTickType ) uxNewPriority ) );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 800 |
|
| 801 | /* If the task is in the blocked or suspended list we need do
|
| 802 | nothing more than change it's priority variable. However, if
|
| 803 | the task is in a ready list it needs to be removed and placed
|
| 804 | in the queue appropriate to its new priority. */
|
| 805 | if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxCurrentPriority ] ), &( pxTCB->xGenericListItem ) ) )
|
| 806 | {
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 807 | /* The task is currently in its ready list - remove before adding
|
| 808 | it to it's new ready list. As we are in a critical section we
|
| 809 | can do this even if the scheduler is suspended. */
|
| 810 | vListRemove( &( pxTCB->xGenericListItem ) );
|
| 811 | prvAddTaskToReadyQueue( pxTCB );
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 812 | }
|
| 813 |
|
Richard Barry | 3dcbed5 | 2006-06-22 17:18:31 +0000 | [diff] [blame] | 814 | if( xYieldRequired == pdTRUE )
|
| 815 | {
|
| 816 | taskYIELD();
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 817 | }
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 818 | }
|
| 819 | }
|
| 820 | taskEXIT_CRITICAL();
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 821 | }
|
| 822 |
|
| 823 | #endif
|
| 824 | /*-----------------------------------------------------------*/
|
| 825 |
|
| 826 | #if ( INCLUDE_vTaskSuspend == 1 )
|
| 827 |
|
| 828 | void vTaskSuspend( xTaskHandle pxTaskToSuspend )
|
| 829 | {
|
| 830 | tskTCB *pxTCB;
|
| 831 |
|
| 832 | taskENTER_CRITICAL();
|
| 833 | {
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 834 | /* Ensure a yield is performed if the current task is being
|
Richard Barry | 946da76 | 2006-05-28 08:17:56 +0000 | [diff] [blame] | 835 | suspended. */
|
| 836 | if( pxTaskToSuspend == pxCurrentTCB )
|
| 837 | {
|
| 838 | pxTaskToSuspend = NULL;
|
| 839 | }
|
| 840 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 841 | /* If null is passed in here then we are suspending ourselves. */
|
| 842 | pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );
|
| 843 |
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 844 | traceTASK_SUSPEND( pxTCB );
|
Richard Barry | b8b7052 | 2008-03-03 16:32:05 +0000 | [diff] [blame] | 845 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 846 | /* Remove task from the ready/delayed list and place in the suspended list. */
|
| 847 | vListRemove( &( pxTCB->xGenericListItem ) );
|
| 848 |
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 849 | /* Is the task waiting on an event also? */
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 850 | if( pxTCB->xEventListItem.pvContainer )
|
| 851 | {
|
| 852 | vListRemove( &( pxTCB->xEventListItem ) );
|
| 853 | }
|
| 854 |
|
| 855 | vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );
|
| 856 | }
|
| 857 | taskEXIT_CRITICAL();
|
| 858 |
|
| 859 | /* We may have just suspended the current task. */
|
| 860 | if( ( void * ) pxTaskToSuspend == NULL )
|
| 861 | {
|
| 862 | taskYIELD();
|
| 863 | }
|
| 864 | }
|
| 865 |
|
| 866 | #endif
|
| 867 | /*-----------------------------------------------------------*/
|
| 868 |
|
| 869 | #if ( INCLUDE_vTaskSuspend == 1 )
|
| 870 |
|
Richard Barry | 2888b15 | 2008-05-19 19:17:56 +0000 | [diff] [blame] | 871 | signed portBASE_TYPE xTaskIsTaskSuspended( xTaskHandle xTask )
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 872 | {
|
| 873 | portBASE_TYPE xReturn = pdFALSE;
|
Richard Barry | 2888b15 | 2008-05-19 19:17:56 +0000 | [diff] [blame] | 874 | const tskTCB * const pxTCB = ( tskTCB * ) xTask;
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 875 |
|
| 876 | /* Is the task we are attempting to resume actually in the
|
| 877 | suspended list? */
|
| 878 | if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )
|
| 879 | {
|
| 880 | /* Has the task already been resumed from within an ISR? */
|
| 881 | if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdTRUE )
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 882 | {
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 883 | /* Is it in the suspended list because it is in the
|
| 884 | Suspended state? It is possible to be in the suspended
|
| 885 | list because it is blocked on a task with no timeout
|
| 886 | specified. */
|
Richard Barry | a06a2e4 | 2007-07-28 18:41:53 +0000 | [diff] [blame] | 887 | if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) == pdTRUE )
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 888 | {
|
| 889 | xReturn = pdTRUE;
|
| 890 | }
|
| 891 | }
|
| 892 | }
|
| 893 |
|
| 894 | return xReturn;
|
| 895 | }
|
| 896 |
|
| 897 | #endif
|
| 898 | /*-----------------------------------------------------------*/
|
| 899 |
|
| 900 | #if ( INCLUDE_vTaskSuspend == 1 )
|
| 901 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 902 | void vTaskResume( xTaskHandle pxTaskToResume )
|
| 903 | {
|
Richard Barry | de1094e | 2007-07-28 18:35:03 +0000 | [diff] [blame] | 904 | tskTCB *pxTCB;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 905 |
|
| 906 | /* Remove the task from whichever list it is currently in, and place
|
| 907 | it in the ready list. */
|
| 908 | pxTCB = ( tskTCB * ) pxTaskToResume;
|
| 909 |
|
| 910 | /* The parameter cannot be NULL as it is impossible to resume the
|
| 911 | currently executing task. */
|
Richard Barry | 3f62002 | 2008-05-01 17:15:36 +0000 | [diff] [blame] | 912 | if( ( pxTCB != NULL ) && ( pxTCB != pxCurrentTCB ) )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 913 | {
|
| 914 | taskENTER_CRITICAL();
|
| 915 | {
|
Richard Barry | 2888b15 | 2008-05-19 19:17:56 +0000 | [diff] [blame] | 916 | if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 917 | {
|
Richard Barry | b8b7052 | 2008-03-03 16:32:05 +0000 | [diff] [blame] | 918 | traceTASK_RESUME( pxTCB );
|
| 919 |
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 920 | /* As we are in a critical section we can access the ready
|
| 921 | lists even if the scheduler is suspended. */
|
| 922 | vListRemove( &( pxTCB->xGenericListItem ) );
|
| 923 | prvAddTaskToReadyQueue( pxTCB );
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 924 |
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 925 | /* We may have just resumed a higher priority task. */
|
| 926 | if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
|
| 927 | {
|
| 928 | /* This yield may not cause the task just resumed to run, but
|
| 929 | will leave the lists in the correct state for the next yield. */
|
| 930 | taskYIELD();
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 931 | }
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 932 | }
|
| 933 | }
|
| 934 | taskEXIT_CRITICAL();
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 935 | }
|
| 936 | }
|
| 937 |
|
| 938 | #endif
|
| 939 |
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 940 | /*-----------------------------------------------------------*/
|
| 941 |
|
Richard Barry | 6cc711d | 2006-08-11 10:56:12 +0000 | [diff] [blame] | 942 | #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 943 |
|
| 944 | portBASE_TYPE xTaskResumeFromISR( xTaskHandle pxTaskToResume )
|
| 945 | {
|
Richard Barry | 58a357e | 2006-08-11 10:02:38 +0000 | [diff] [blame] | 946 | portBASE_TYPE xYieldRequired = pdFALSE;
|
Richard Barry | de1094e | 2007-07-28 18:35:03 +0000 | [diff] [blame] | 947 | tskTCB *pxTCB;
|
Richard Barry | 58a357e | 2006-08-11 10:02:38 +0000 | [diff] [blame] | 948 |
|
| 949 | pxTCB = ( tskTCB * ) pxTaskToResume;
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 950 |
|
Richard Barry | 2888b15 | 2008-05-19 19:17:56 +0000 | [diff] [blame] | 951 | if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 952 | {
|
Richard Barry | b8b7052 | 2008-03-03 16:32:05 +0000 | [diff] [blame] | 953 | traceTASK_RESUME_FROM_ISR( pxTCB );
|
| 954 |
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 955 | if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 956 | {
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 957 | xYieldRequired = ( pxTCB->uxPriority >= pxCurrentTCB->uxPriority );
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 958 | vListRemove( &( pxTCB->xGenericListItem ) );
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 959 | prvAddTaskToReadyQueue( pxTCB );
|
| 960 | }
|
| 961 | else
|
| 962 | {
|
| 963 | /* We cannot access the delayed or ready lists, so will hold this
|
| 964 | task pending until the scheduler is resumed, at which point a
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 965 | yield will be performed if necessary. */
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 966 | vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 967 | }
|
Richard Barry | 17617c5 | 2006-07-03 19:39:42 +0000 | [diff] [blame] | 968 | }
|
Richard Barry | d481cff | 2006-07-03 19:34:28 +0000 | [diff] [blame] | 969 |
|
| 970 | return xYieldRequired;
|
| 971 | }
|
| 972 |
|
| 973 | #endif
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 | /*-----------------------------------------------------------
|
| 979 | * PUBLIC SCHEDULER CONTROL documented in task.h
|
| 980 | *----------------------------------------------------------*/
|
| 981 |
|
| 982 |
|
| 983 | void vTaskStartScheduler( void )
|
| 984 | {
|
| 985 | portBASE_TYPE xReturn;
|
| 986 |
|
| 987 | /* Add the idle task at the lowest priority. */
|
| 988 | xReturn = xTaskCreate( prvIdleTask, ( signed portCHAR * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
|
| 989 |
|
| 990 | if( xReturn == pdPASS )
|
| 991 | {
|
| 992 | /* Interrupts are turned off here, to ensure a tick does not occur
|
| 993 | before or during the call to xPortStartScheduler(). The stacks of
|
| 994 | the created tasks contain a status word with interrupts switched on
|
| 995 | so interrupts will automatically get re-enabled when the first task
|
| 996 | starts to run.
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 997 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 998 | STEPPING THROUGH HERE USING A DEBUGGER CAN CAUSE BIG PROBLEMS IF THE
|
| 999 | DEBUGGER ALLOWS INTERRUPTS TO BE PROCESSED. */
|
| 1000 | portDISABLE_INTERRUPTS();
|
| 1001 |
|
| 1002 | xSchedulerRunning = pdTRUE;
|
| 1003 | xTickCount = ( portTickType ) 0;
|
| 1004 |
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 1005 | /* If configGENERATE_RUN_TIME_STATS is defined then the following
|
| 1006 | macro must be defined to configure the timer/counter used to generate
|
| 1007 | the run time counter time base. */
|
| 1008 | portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
|
| 1009 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1010 | /* Setting up the timer tick is hardware specific and thus in the
|
| 1011 | portable interface. */
|
| 1012 | if( xPortStartScheduler() )
|
| 1013 | {
|
| 1014 | /* Should not reach here as if the scheduler is running the
|
| 1015 | function will not return. */
|
| 1016 | }
|
| 1017 | else
|
| 1018 | {
|
| 1019 | /* Should only reach here if a task calls xTaskEndScheduler(). */
|
| 1020 | }
|
| 1021 | }
|
| 1022 | }
|
| 1023 | /*-----------------------------------------------------------*/
|
| 1024 |
|
| 1025 | void vTaskEndScheduler( void )
|
| 1026 | {
|
| 1027 | /* Stop the scheduler interrupts and call the portable scheduler end
|
| 1028 | routine so the original ISRs can be restored if necessary. The port
|
| 1029 | layer must ensure interrupts enable bit is left in the correct state. */
|
| 1030 | portDISABLE_INTERRUPTS();
|
| 1031 | xSchedulerRunning = pdFALSE;
|
| 1032 | vPortEndScheduler();
|
| 1033 | }
|
| 1034 | /*----------------------------------------------------------*/
|
| 1035 |
|
| 1036 | void vTaskSuspendAll( void )
|
| 1037 | {
|
Richard Barry | 55c9604 | 2009-03-11 10:55:41 +0000 | [diff] [blame] | 1038 | /* A critical section is not required as the variable is of type
|
| 1039 | portBASE_TYPE. */
|
| 1040 | ++uxSchedulerSuspended;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1041 | }
|
| 1042 | /*----------------------------------------------------------*/
|
| 1043 |
|
| 1044 | signed portBASE_TYPE xTaskResumeAll( void )
|
| 1045 | {
|
| 1046 | register tskTCB *pxTCB;
|
| 1047 | signed portBASE_TYPE xAlreadyYielded = pdFALSE;
|
| 1048 |
|
| 1049 | /* It is possible that an ISR caused a task to be removed from an event
|
| 1050 | list while the scheduler was suspended. If this was the case then the
|
| 1051 | removed task will have been added to the xPendingReadyList. Once the
|
| 1052 | scheduler has been resumed it is safe to move all the pending ready
|
| 1053 | tasks from this list into their appropriate ready list. */
|
| 1054 | portENTER_CRITICAL();
|
| 1055 | {
|
| 1056 | --uxSchedulerSuspended;
|
| 1057 |
|
| 1058 | if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1059 | {
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1060 | if( uxCurrentNumberOfTasks > ( unsigned portBASE_TYPE ) 0 )
|
| 1061 | {
|
| 1062 | portBASE_TYPE xYieldRequired = pdFALSE;
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1063 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1064 | /* Move any readied tasks from the pending list into the
|
| 1065 | appropriate ready list. */
|
| 1066 | while( ( pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xPendingReadyList ) ) ) != NULL )
|
| 1067 | {
|
| 1068 | vListRemove( &( pxTCB->xEventListItem ) );
|
| 1069 | vListRemove( &( pxTCB->xGenericListItem ) );
|
| 1070 | prvAddTaskToReadyQueue( pxTCB );
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1071 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1072 | /* If we have moved a task that has a priority higher than
|
| 1073 | the current task then we should yield. */
|
| 1074 | if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
|
| 1075 | {
|
| 1076 | xYieldRequired = pdTRUE;
|
| 1077 | }
|
| 1078 | }
|
| 1079 |
|
| 1080 | /* If any ticks occurred while the scheduler was suspended then
|
| 1081 | they should be processed now. This ensures the tick count does not
|
| 1082 | slip, and that any delayed tasks are resumed at the correct time. */
|
| 1083 | if( uxMissedTicks > ( unsigned portBASE_TYPE ) 0 )
|
| 1084 | {
|
| 1085 | while( uxMissedTicks > ( unsigned portBASE_TYPE ) 0 )
|
| 1086 | {
|
| 1087 | vTaskIncrementTick();
|
| 1088 | --uxMissedTicks;
|
| 1089 | }
|
| 1090 |
|
| 1091 | /* As we have processed some ticks it is appropriate to yield
|
| 1092 | to ensure the highest priority task that is ready to run is
|
| 1093 | the task actually running. */
|
Richard Barry | dfb8e70 | 2008-01-22 18:43:03 +0000 | [diff] [blame] | 1094 | #if configUSE_PREEMPTION == 1
|
| 1095 | {
|
| 1096 | xYieldRequired = pdTRUE;
|
| 1097 | }
|
| 1098 | #endif
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1099 | }
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1100 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1101 | if( ( xYieldRequired == pdTRUE ) || ( xMissedYield == pdTRUE ) )
|
| 1102 | {
|
| 1103 | xAlreadyYielded = pdTRUE;
|
| 1104 | xMissedYield = pdFALSE;
|
| 1105 | taskYIELD();
|
| 1106 | }
|
| 1107 | }
|
| 1108 | }
|
| 1109 | }
|
| 1110 | portEXIT_CRITICAL();
|
| 1111 |
|
| 1112 | return xAlreadyYielded;
|
| 1113 | }
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 | /*-----------------------------------------------------------
|
| 1121 | * PUBLIC TASK UTILITIES documented in task.h
|
| 1122 | *----------------------------------------------------------*/
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 | portTickType xTaskGetTickCount( void )
|
| 1127 | {
|
| 1128 | portTickType xTicks;
|
| 1129 |
|
| 1130 | /* Critical section required if running on a 16 bit processor. */
|
| 1131 | taskENTER_CRITICAL();
|
| 1132 | {
|
| 1133 | xTicks = xTickCount;
|
| 1134 | }
|
| 1135 | taskEXIT_CRITICAL();
|
| 1136 |
|
| 1137 | return xTicks;
|
| 1138 | }
|
| 1139 | /*-----------------------------------------------------------*/
|
| 1140 |
|
| 1141 | unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
|
| 1142 | {
|
Richard Barry | 55c9604 | 2009-03-11 10:55:41 +0000 | [diff] [blame] | 1143 | /* A critical section is not required because the variables are of type
|
| 1144 | portBASE_TYPE. */
|
| 1145 | return uxCurrentNumberOfTasks;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1146 | }
|
| 1147 | /*-----------------------------------------------------------*/
|
| 1148 |
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 1149 | #if ( configUSE_TRACE_FACILITY == 1 )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1150 |
|
| 1151 | void vTaskList( signed portCHAR *pcWriteBuffer )
|
| 1152 | {
|
| 1153 | unsigned portBASE_TYPE uxQueue;
|
| 1154 |
|
| 1155 | /* This is a VERY costly function that should be used for debug only.
|
| 1156 | It leaves interrupts disabled for a LONG time. */
|
| 1157 |
|
Richard Barry | 946da76 | 2006-05-28 08:17:56 +0000 | [diff] [blame] | 1158 | vTaskSuspendAll();
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1159 | {
|
| 1160 | /* Run through all the lists that could potentially contain a TCB and
|
| 1161 | report the task name, state and stack high water mark. */
|
| 1162 |
|
| 1163 | pcWriteBuffer[ 0 ] = ( signed portCHAR ) 0x00;
|
| 1164 | strcat( ( portCHAR * ) pcWriteBuffer, ( const portCHAR * ) "\r\n" );
|
| 1165 |
|
| 1166 | uxQueue = uxTopUsedPriority + 1;
|
| 1167 |
|
| 1168 | do
|
| 1169 | {
|
| 1170 | uxQueue--;
|
| 1171 |
|
| 1172 | if( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) )
|
| 1173 | {
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1174 | prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1175 | }
|
| 1176 | }while( uxQueue > ( unsigned portSHORT ) tskIDLE_PRIORITY );
|
| 1177 |
|
| 1178 | if( !listLIST_IS_EMPTY( pxDelayedTaskList ) )
|
| 1179 | {
|
| 1180 | prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );
|
| 1181 | }
|
| 1182 |
|
| 1183 | if( !listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) )
|
| 1184 | {
|
| 1185 | prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );
|
| 1186 | }
|
| 1187 |
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 1188 | #if( INCLUDE_vTaskDelete == 1 )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1189 | {
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 1190 | if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )
|
| 1191 | {
|
| 1192 | prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, tskDELETED_CHAR );
|
| 1193 | }
|
| 1194 | }
|
| 1195 | #endif
|
| 1196 |
|
| 1197 | #if ( INCLUDE_vTaskSuspend == 1 )
|
| 1198 | {
|
| 1199 | if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )
|
| 1200 | {
|
| 1201 | prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, tskSUSPENDED_CHAR );
|
| 1202 | }
|
| 1203 | }
|
| 1204 | #endif
|
| 1205 | }
|
| 1206 | xTaskResumeAll();
|
| 1207 | }
|
| 1208 |
|
| 1209 | #endif
|
| 1210 | /*----------------------------------------------------------*/
|
| 1211 |
|
| 1212 | #if ( configGENERATE_RUN_TIME_STATS == 1 )
|
| 1213 |
|
| 1214 | void vTaskGetRunTimeStats( signed portCHAR *pcWriteBuffer )
|
| 1215 | {
|
| 1216 | unsigned portBASE_TYPE uxQueue;
|
| 1217 | unsigned portLONG ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
|
| 1218 |
|
| 1219 | /* This is a VERY costly function that should be used for debug only.
|
| 1220 | It leaves interrupts disabled for a LONG time. */
|
| 1221 |
|
| 1222 | vTaskSuspendAll();
|
| 1223 | {
|
| 1224 | /* Run through all the lists that could potentially contain a TCB,
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1225 | generating a table of run timer percentages in the provided
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 1226 | buffer. */
|
| 1227 |
|
| 1228 | pcWriteBuffer[ 0 ] = ( signed portCHAR ) 0x00;
|
| 1229 | strcat( ( portCHAR * ) pcWriteBuffer, ( const portCHAR * ) "\r\n" );
|
| 1230 |
|
| 1231 | uxQueue = uxTopUsedPriority + 1;
|
| 1232 |
|
| 1233 | do
|
| 1234 | {
|
| 1235 | uxQueue--;
|
| 1236 |
|
| 1237 | if( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) )
|
| 1238 | {
|
| 1239 | prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), ulTotalRunTime );
|
| 1240 | }
|
| 1241 | }while( uxQueue > ( unsigned portSHORT ) tskIDLE_PRIORITY );
|
| 1242 |
|
| 1243 | if( !listLIST_IS_EMPTY( pxDelayedTaskList ) )
|
| 1244 | {
|
| 1245 | prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, ulTotalRunTime );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1246 | }
|
| 1247 |
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 1248 | if( !listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1249 | {
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 1250 | prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, ulTotalRunTime );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1251 | }
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 1252 |
|
| 1253 | #if ( INCLUDE_vTaskDelete == 1 )
|
| 1254 | {
|
| 1255 | if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )
|
| 1256 | {
|
| 1257 | prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, ulTotalRunTime );
|
| 1258 | }
|
| 1259 | }
|
| 1260 | #endif
|
| 1261 |
|
| 1262 | #if ( INCLUDE_vTaskSuspend == 1 )
|
| 1263 | {
|
| 1264 | if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )
|
| 1265 | {
|
| 1266 | prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, ulTotalRunTime );
|
| 1267 | }
|
| 1268 | }
|
| 1269 | #endif
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1270 | }
|
Richard Barry | 946da76 | 2006-05-28 08:17:56 +0000 | [diff] [blame] | 1271 | xTaskResumeAll();
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1272 | }
|
| 1273 |
|
| 1274 | #endif
|
| 1275 | /*----------------------------------------------------------*/
|
| 1276 |
|
| 1277 | #if ( configUSE_TRACE_FACILITY == 1 )
|
| 1278 |
|
| 1279 | void vTaskStartTrace( signed portCHAR * pcBuffer, unsigned portLONG ulBufferSize )
|
| 1280 | {
|
| 1281 | portENTER_CRITICAL();
|
| 1282 | {
|
Richard Barry | 79dd981 | 2008-02-15 13:44:06 +0000 | [diff] [blame] | 1283 | pcTraceBuffer = ( signed portCHAR * )pcBuffer;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1284 | pcTraceBufferStart = pcBuffer;
|
| 1285 | pcTraceBufferEnd = pcBuffer + ( ulBufferSize - tskSIZE_OF_EACH_TRACE_LINE );
|
| 1286 | xTracing = pdTRUE;
|
| 1287 | }
|
| 1288 | portEXIT_CRITICAL();
|
| 1289 | }
|
| 1290 |
|
| 1291 | #endif
|
| 1292 | /*----------------------------------------------------------*/
|
| 1293 |
|
| 1294 | #if ( configUSE_TRACE_FACILITY == 1 )
|
| 1295 |
|
| 1296 | unsigned portLONG ulTaskEndTrace( void )
|
| 1297 | {
|
| 1298 | unsigned portLONG ulBufferLength;
|
| 1299 |
|
| 1300 | portENTER_CRITICAL();
|
| 1301 | xTracing = pdFALSE;
|
| 1302 | portEXIT_CRITICAL();
|
| 1303 |
|
| 1304 | ulBufferLength = ( unsigned portLONG ) ( pcTraceBuffer - pcTraceBufferStart );
|
| 1305 |
|
| 1306 | return ulBufferLength;
|
| 1307 | }
|
| 1308 |
|
| 1309 | #endif
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 | /*-----------------------------------------------------------
|
| 1314 | * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES
|
| 1315 | * documented in task.h
|
| 1316 | *----------------------------------------------------------*/
|
| 1317 |
|
| 1318 |
|
Richard Barry | 0d29807 | 2008-05-01 08:57:45 +0000 | [diff] [blame] | 1319 | void vTaskIncrementTick( void )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1320 | {
|
| 1321 | /* Called by the portable layer each time a tick interrupt occurs.
|
| 1322 | Increments the tick then checks to see if the new tick value will cause any
|
| 1323 | tasks to be unblocked. */
|
| 1324 | if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
|
| 1325 | {
|
| 1326 | ++xTickCount;
|
| 1327 | if( xTickCount == ( portTickType ) 0 )
|
| 1328 | {
|
| 1329 | xList *pxTemp;
|
| 1330 |
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 1331 | /* Tick count has overflowed so we need to swap the delay lists.
|
| 1332 | If there are any items in pxDelayedTaskList here then there is
|
Richard Barry | 946da76 | 2006-05-28 08:17:56 +0000 | [diff] [blame] | 1333 | an error! */
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1334 | pxTemp = pxDelayedTaskList;
|
| 1335 | pxDelayedTaskList = pxOverflowDelayedTaskList;
|
| 1336 | pxOverflowDelayedTaskList = pxTemp;
|
Richard Barry | b18929e | 2006-08-27 14:09:54 +0000 | [diff] [blame] | 1337 | xNumOfOverflows++;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1338 | }
|
| 1339 |
|
| 1340 | /* See if this tick has made a timeout expire. */
|
| 1341 | prvCheckDelayedTasks();
|
| 1342 | }
|
| 1343 | else
|
| 1344 | {
|
| 1345 | ++uxMissedTicks;
|
Richard Barry | 946da76 | 2006-05-28 08:17:56 +0000 | [diff] [blame] | 1346 |
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 1347 | /* The tick hook gets called at regular intervals, even if the
|
Richard Barry | 946da76 | 2006-05-28 08:17:56 +0000 | [diff] [blame] | 1348 | scheduler is locked. */
|
| 1349 | #if ( configUSE_TICK_HOOK == 1 )
|
| 1350 | {
|
| 1351 | extern void vApplicationTickHook( void );
|
| 1352 |
|
| 1353 | vApplicationTickHook();
|
| 1354 | }
|
| 1355 | #endif
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1356 | }
|
| 1357 |
|
| 1358 | #if ( configUSE_TICK_HOOK == 1 )
|
| 1359 | {
|
| 1360 | extern void vApplicationTickHook( void );
|
| 1361 |
|
Richard Barry | 946da76 | 2006-05-28 08:17:56 +0000 | [diff] [blame] | 1362 | /* Guard against the tick hook being called when the missed tick
|
| 1363 | count is being unwound (when the scheduler is being unlocked. */
|
| 1364 | if( uxMissedTicks == 0 )
|
| 1365 | {
|
| 1366 | vApplicationTickHook();
|
| 1367 | }
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1368 | }
|
| 1369 | #endif
|
Richard Barry | b8b7052 | 2008-03-03 16:32:05 +0000 | [diff] [blame] | 1370 |
|
| 1371 | traceTASK_INCREMENT_TICK( xTickCount );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1372 | }
|
| 1373 | /*-----------------------------------------------------------*/
|
| 1374 |
|
| 1375 | #if ( ( INCLUDE_vTaskCleanUpResources == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
|
| 1376 |
|
| 1377 | void vTaskCleanUpResources( void )
|
| 1378 | {
|
| 1379 | unsigned portSHORT usQueue;
|
| 1380 | volatile tskTCB *pxTCB;
|
| 1381 |
|
| 1382 | usQueue = ( unsigned portSHORT ) uxTopUsedPriority + ( unsigned portSHORT ) 1;
|
| 1383 |
|
| 1384 | /* Remove any TCB's from the ready queues. */
|
| 1385 | do
|
| 1386 | {
|
| 1387 | usQueue--;
|
| 1388 |
|
| 1389 | while( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ usQueue ] ) ) )
|
| 1390 | {
|
| 1391 | listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &( pxReadyTasksLists[ usQueue ] ) );
|
| 1392 | vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
|
| 1393 |
|
| 1394 | prvDeleteTCB( ( tskTCB * ) pxTCB );
|
| 1395 | }
|
| 1396 | }while( usQueue > ( unsigned portSHORT ) tskIDLE_PRIORITY );
|
| 1397 |
|
| 1398 | /* Remove any TCB's from the delayed queue. */
|
| 1399 | while( !listLIST_IS_EMPTY( &xDelayedTaskList1 ) )
|
| 1400 | {
|
| 1401 | listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList1 );
|
| 1402 | vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
|
| 1403 |
|
| 1404 | prvDeleteTCB( ( tskTCB * ) pxTCB );
|
| 1405 | }
|
| 1406 |
|
| 1407 | /* Remove any TCB's from the overflow delayed queue. */
|
| 1408 | while( !listLIST_IS_EMPTY( &xDelayedTaskList2 ) )
|
| 1409 | {
|
| 1410 | listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList2 );
|
| 1411 | vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
|
| 1412 |
|
| 1413 | prvDeleteTCB( ( tskTCB * ) pxTCB );
|
| 1414 | }
|
| 1415 |
|
| 1416 | while( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )
|
| 1417 | {
|
| 1418 | listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xSuspendedTaskList );
|
| 1419 | vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
|
| 1420 |
|
| 1421 | prvDeleteTCB( ( tskTCB * ) pxTCB );
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1422 | }
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1423 | }
|
| 1424 |
|
| 1425 | #endif
|
| 1426 | /*-----------------------------------------------------------*/
|
| 1427 |
|
Richard Barry | e939542 | 2008-04-16 07:47:02 +0000 | [diff] [blame] | 1428 | #if ( configUSE_APPLICATION_TASK_TAG == 1 )
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 1429 |
|
Richard Barry | e939542 | 2008-04-16 07:47:02 +0000 | [diff] [blame] | 1430 | void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxTagValue )
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 1431 | {
|
| 1432 | tskTCB *xTCB;
|
| 1433 |
|
| 1434 | /* If xTask is NULL then we are setting our own task hook. */
|
| 1435 | if( xTask == NULL )
|
| 1436 | {
|
| 1437 | xTCB = ( tskTCB * ) pxCurrentTCB;
|
| 1438 | }
|
| 1439 | else
|
| 1440 | {
|
| 1441 | xTCB = ( tskTCB * ) xTask;
|
| 1442 | }
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1443 |
|
Richard Barry | 55c9604 | 2009-03-11 10:55:41 +0000 | [diff] [blame] | 1444 | /* Save the hook function in the TCB. A critical section is required as
|
| 1445 | the value can be accessed from an interrupt. */
|
Richard Barry | e939542 | 2008-04-16 07:47:02 +0000 | [diff] [blame] | 1446 | portENTER_CRITICAL();
|
| 1447 | xTCB->pxTaskTag = pxTagValue;
|
| 1448 | portEXIT_CRITICAL();
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 1449 | }
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1450 |
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 1451 | #endif
|
| 1452 | /*-----------------------------------------------------------*/
|
| 1453 |
|
Richard Barry | e939542 | 2008-04-16 07:47:02 +0000 | [diff] [blame] | 1454 | #if ( configUSE_APPLICATION_TASK_TAG == 1 )
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 1455 |
|
Richard Barry | f24533b | 2009-05-30 13:30:40 +0000 | [diff] [blame] | 1456 | pdTASK_HOOK_CODE xTaskGetApplicationTaskTag( xTaskHandle xTask )
|
| 1457 | {
|
| 1458 | tskTCB *xTCB;
|
| 1459 | pdTASK_HOOK_CODE xReturn;
|
| 1460 |
|
| 1461 | /* If xTask is NULL then we are setting our own task hook. */
|
| 1462 | if( xTask == NULL )
|
| 1463 | {
|
| 1464 | xTCB = ( tskTCB * ) pxCurrentTCB;
|
| 1465 | }
|
| 1466 | else
|
| 1467 | {
|
| 1468 | xTCB = ( tskTCB * ) xTask;
|
| 1469 | }
|
| 1470 |
|
| 1471 | /* Save the hook function in the TCB. A critical section is required as
|
| 1472 | the value can be accessed from an interrupt. */
|
| 1473 | portENTER_CRITICAL();
|
| 1474 | xReturn = xTCB->pxTaskTag;
|
| 1475 | portEXIT_CRITICAL();
|
| 1476 |
|
| 1477 | return xReturn;
|
| 1478 | }
|
| 1479 |
|
| 1480 | #endif
|
| 1481 | /*-----------------------------------------------------------*/
|
| 1482 |
|
| 1483 | #if ( configUSE_APPLICATION_TASK_TAG == 1 )
|
| 1484 |
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 1485 | portBASE_TYPE xTaskCallApplicationTaskHook( xTaskHandle xTask, void *pvParameter )
|
| 1486 | {
|
| 1487 | tskTCB *xTCB;
|
| 1488 | portBASE_TYPE xReturn;
|
| 1489 |
|
| 1490 | /* If xTask is NULL then we are calling our own task hook. */
|
| 1491 | if( xTask == NULL )
|
| 1492 | {
|
| 1493 | xTCB = ( tskTCB * ) pxCurrentTCB;
|
| 1494 | }
|
| 1495 | else
|
| 1496 | {
|
| 1497 | xTCB = ( tskTCB * ) xTask;
|
| 1498 | }
|
| 1499 |
|
Richard Barry | e939542 | 2008-04-16 07:47:02 +0000 | [diff] [blame] | 1500 | if( xTCB->pxTaskTag != NULL )
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 1501 | {
|
Richard Barry | e939542 | 2008-04-16 07:47:02 +0000 | [diff] [blame] | 1502 | xReturn = xTCB->pxTaskTag( pvParameter );
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 1503 | }
|
| 1504 | else
|
| 1505 | {
|
| 1506 | xReturn = pdFAIL;
|
| 1507 | }
|
| 1508 |
|
| 1509 | return xReturn;
|
| 1510 | }
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1511 |
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 1512 | #endif
|
| 1513 | /*-----------------------------------------------------------*/
|
| 1514 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1515 | void vTaskSwitchContext( void )
|
| 1516 | {
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1517 | if( uxSchedulerSuspended != ( unsigned portBASE_TYPE ) pdFALSE )
|
| 1518 | {
|
| 1519 | /* The scheduler is currently suspended - do not allow a context
|
| 1520 | switch. */
|
| 1521 | xMissedYield = pdTRUE;
|
| 1522 | return;
|
| 1523 | }
|
| 1524 |
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1525 | traceTASK_SWITCHED_OUT();
|
| 1526 |
|
| 1527 | #if ( configGENERATE_RUN_TIME_STATS == 1 )
|
| 1528 | {
|
| 1529 | unsigned portLONG ulTempCounter = portGET_RUN_TIME_COUNTER_VALUE();
|
| 1530 |
|
| 1531 | /* Add the amount of time the task has been running to the accumulated
|
| 1532 | time so far. The time the task started running was stored in
|
| 1533 | ulTaskSwitchedInTime. Note that there is no overflow protection here
|
| 1534 | so count values are only valid until the timer overflows. Generally
|
| 1535 | this will be about 1 hour assuming a 1uS timer increment. */
|
| 1536 | pxCurrentTCB->ulRunTimeCounter += ( ulTempCounter - ulTaskSwitchedInTime );
|
| 1537 | ulTaskSwitchedInTime = ulTempCounter;
|
| 1538 | }
|
| 1539 | #endif
|
| 1540 |
|
Richard Barry | 4f631de | 2008-10-09 00:53:02 +0000 | [diff] [blame] | 1541 | taskFIRST_CHECK_FOR_STACK_OVERFLOW();
|
| 1542 | taskSECOND_CHECK_FOR_STACK_OVERFLOW();
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 1543 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1544 | /* Find the highest priority queue that contains ready tasks. */
|
| 1545 | while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )
|
| 1546 | {
|
| 1547 | --uxTopReadyPriority;
|
| 1548 | }
|
| 1549 |
|
| 1550 | /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the tasks of the
|
| 1551 | same priority get an equal share of the processor time. */
|
| 1552 | listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );
|
Richard Barry | b8b7052 | 2008-03-03 16:32:05 +0000 | [diff] [blame] | 1553 |
|
| 1554 | traceTASK_SWITCHED_IN();
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1555 | vWriteTraceToBuffer();
|
| 1556 | }
|
| 1557 | /*-----------------------------------------------------------*/
|
| 1558 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 1559 | void vTaskPlaceOnEventList( const xList * const pxEventList, portTickType xTicksToWait )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1560 | {
|
| 1561 | portTickType xTimeToWake;
|
| 1562 |
|
| 1563 | /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
|
| 1564 | SCHEDULER SUSPENDED. */
|
| 1565 |
|
| 1566 | /* Place the event list item of the TCB in the appropriate event list.
|
| 1567 | This is placed in the list in priority order so the highest priority task
|
| 1568 | is the first to be woken by the event. */
|
| 1569 | vListInsert( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );
|
| 1570 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1571 | /* We must remove ourselves from the ready list before adding ourselves
|
| 1572 | to the blocked list as the same list item is used for both lists. We have
|
| 1573 | exclusive access to the ready lists as the scheduler is locked. */
|
| 1574 | vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
|
| 1575 |
|
Richard Barry | 6083a3a | 2006-10-28 09:47:41 +0000 | [diff] [blame] | 1576 |
|
| 1577 | #if ( INCLUDE_vTaskSuspend == 1 )
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1578 | {
|
Richard Barry | 6083a3a | 2006-10-28 09:47:41 +0000 | [diff] [blame] | 1579 | if( xTicksToWait == portMAX_DELAY )
|
Richard Barry | 96d4684 | 2006-10-22 20:28:16 +0000 | [diff] [blame] | 1580 | {
|
Richard Barry | 6083a3a | 2006-10-28 09:47:41 +0000 | [diff] [blame] | 1581 | /* Add ourselves to the suspended task list instead of a delayed task
|
| 1582 | list to ensure we are not woken by a timing event. We will block
|
| 1583 | indefinitely. */
|
| 1584 | vListInsertEnd( ( xList * ) &xSuspendedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
|
Richard Barry | 96d4684 | 2006-10-22 20:28:16 +0000 | [diff] [blame] | 1585 | }
|
| 1586 | else
|
| 1587 | {
|
Richard Barry | 6083a3a | 2006-10-28 09:47:41 +0000 | [diff] [blame] | 1588 | /* Calculate the time at which the task should be woken if the event does
|
| 1589 | not occur. This may overflow but this doesn't matter. */
|
| 1590 | xTimeToWake = xTickCount + xTicksToWait;
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1591 |
|
Richard Barry | 6083a3a | 2006-10-28 09:47:41 +0000 | [diff] [blame] | 1592 | listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1593 |
|
Richard Barry | 6083a3a | 2006-10-28 09:47:41 +0000 | [diff] [blame] | 1594 | if( xTimeToWake < xTickCount )
|
| 1595 | {
|
| 1596 | /* Wake time has overflowed. Place this item in the overflow list. */
|
| 1597 | vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
|
| 1598 | }
|
| 1599 | else
|
| 1600 | {
|
| 1601 | /* The wake time has not overflowed, so we can use the current block list. */
|
| 1602 | vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
|
| 1603 | }
|
Richard Barry | 96d4684 | 2006-10-22 20:28:16 +0000 | [diff] [blame] | 1604 | }
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1605 | }
|
Richard Barry | 6083a3a | 2006-10-28 09:47:41 +0000 | [diff] [blame] | 1606 | #else
|
| 1607 | {
|
| 1608 | /* Calculate the time at which the task should be woken if the event does
|
| 1609 | not occur. This may overflow but this doesn't matter. */
|
| 1610 | xTimeToWake = xTickCount + xTicksToWait;
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1611 |
|
Richard Barry | 6083a3a | 2006-10-28 09:47:41 +0000 | [diff] [blame] | 1612 | listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1613 |
|
Richard Barry | 6083a3a | 2006-10-28 09:47:41 +0000 | [diff] [blame] | 1614 | if( xTimeToWake < xTickCount )
|
| 1615 | {
|
| 1616 | /* Wake time has overflowed. Place this item in the overflow list. */
|
| 1617 | vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
|
| 1618 | }
|
| 1619 | else
|
| 1620 | {
|
| 1621 | /* The wake time has not overflowed, so we can use the current block list. */
|
| 1622 | vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
|
| 1623 | }
|
| 1624 | }
|
| 1625 | #endif
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1626 | }
|
| 1627 | /*-----------------------------------------------------------*/
|
| 1628 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 1629 | signed portBASE_TYPE xTaskRemoveFromEventList( const xList * const pxEventList )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1630 | {
|
| 1631 | tskTCB *pxUnblockedTCB;
|
| 1632 | portBASE_TYPE xReturn;
|
| 1633 |
|
| 1634 | /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
|
| 1635 | SCHEDULER SUSPENDED. It can also be called from within an ISR. */
|
| 1636 |
|
| 1637 | /* The event list is sorted in priority order, so we can remove the
|
| 1638 | first in the list, remove the TCB from the delayed list, and add
|
| 1639 | it to the ready list.
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1640 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1641 | If an event is for a queue that is locked then this function will never
|
| 1642 | get called - the lock count on the queue will get modified instead. This
|
| 1643 | means we can always expect exclusive access to the event list here. */
|
| 1644 | pxUnblockedTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
|
| 1645 | vListRemove( &( pxUnblockedTCB->xEventListItem ) );
|
| 1646 |
|
| 1647 | if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
|
| 1648 | {
|
| 1649 | vListRemove( &( pxUnblockedTCB->xGenericListItem ) );
|
| 1650 | prvAddTaskToReadyQueue( pxUnblockedTCB );
|
| 1651 | }
|
| 1652 | else
|
| 1653 | {
|
| 1654 | /* We cannot access the delayed or ready lists, so will hold this
|
| 1655 | task pending until the scheduler is resumed. */
|
| 1656 | vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
|
| 1657 | }
|
| 1658 |
|
| 1659 | if( pxUnblockedTCB->uxPriority >= pxCurrentTCB->uxPriority )
|
| 1660 | {
|
| 1661 | /* Return true if the task removed from the event list has
|
| 1662 | a higher priority than the calling task. This allows
|
| 1663 | the calling task to know if it should force a context
|
| 1664 | switch now. */
|
| 1665 | xReturn = pdTRUE;
|
| 1666 | }
|
| 1667 | else
|
| 1668 | {
|
| 1669 | xReturn = pdFALSE;
|
| 1670 | }
|
| 1671 |
|
| 1672 | return xReturn;
|
| 1673 | }
|
Richard Barry | b18929e | 2006-08-27 14:09:54 +0000 | [diff] [blame] | 1674 | /*-----------------------------------------------------------*/
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1675 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 1676 | void vTaskSetTimeOutState( xTimeOutType * const pxTimeOut )
|
Richard Barry | b18929e | 2006-08-27 14:09:54 +0000 | [diff] [blame] | 1677 | {
|
| 1678 | pxTimeOut->xOverflowCount = xNumOfOverflows;
|
| 1679 | pxTimeOut->xTimeOnEntering = xTickCount;
|
| 1680 | }
|
| 1681 | /*-----------------------------------------------------------*/
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1682 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 1683 | portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType * const pxTimeOut, portTickType * const pxTicksToWait )
|
Richard Barry | b18929e | 2006-08-27 14:09:54 +0000 | [diff] [blame] | 1684 | {
|
| 1685 | portBASE_TYPE xReturn;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1686 |
|
Richard Barry | ed28aa2 | 2008-03-23 16:00:51 +0000 | [diff] [blame] | 1687 | portENTER_CRITICAL();
|
| 1688 | {
|
| 1689 | #if ( INCLUDE_vTaskSuspend == 1 )
|
| 1690 | /* If INCLUDE_vTaskSuspend is set to 1 and the block time specified is
|
| 1691 | the maximum block time then the task should block indefinitely, and
|
| 1692 | therefore never time out. */
|
| 1693 | if( *pxTicksToWait == portMAX_DELAY )
|
| 1694 | {
|
| 1695 | xReturn = pdFALSE;
|
| 1696 | }
|
| 1697 | else /* We are not blocking indefinitely, perform the checks below. */
|
| 1698 | #endif
|
| 1699 |
|
| 1700 | if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xTickCount >= pxTimeOut->xTimeOnEntering ) )
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 1701 | {
|
Richard Barry | ed28aa2 | 2008-03-23 16:00:51 +0000 | [diff] [blame] | 1702 | /* The tick count is greater than the time at which vTaskSetTimeout()
|
| 1703 | was called, but has also overflowed since vTaskSetTimeOut() was called.
|
| 1704 | It must have wrapped all the way around and gone past us again. This
|
| 1705 | passed since vTaskSetTimeout() was called. */
|
| 1706 | xReturn = pdTRUE;
|
| 1707 | }
|
| 1708 | else if( ( xTickCount - pxTimeOut->xTimeOnEntering ) < *pxTicksToWait )
|
| 1709 | {
|
| 1710 | /* Not a genuine timeout. Adjust parameters for time remaining. */
|
| 1711 | *pxTicksToWait -= ( xTickCount - pxTimeOut->xTimeOnEntering );
|
| 1712 | vTaskSetTimeOutState( pxTimeOut );
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 1713 | xReturn = pdFALSE;
|
| 1714 | }
|
Richard Barry | ed28aa2 | 2008-03-23 16:00:51 +0000 | [diff] [blame] | 1715 | else
|
| 1716 | {
|
| 1717 | xReturn = pdTRUE;
|
| 1718 | }
|
| 1719 | }
|
| 1720 | portEXIT_CRITICAL();
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1721 |
|
Richard Barry | b18929e | 2006-08-27 14:09:54 +0000 | [diff] [blame] | 1722 | return xReturn;
|
| 1723 | }
|
| 1724 | /*-----------------------------------------------------------*/
|
| 1725 |
|
| 1726 | void vTaskMissedYield( void )
|
| 1727 | {
|
| 1728 | xMissedYield = pdTRUE;
|
| 1729 | }
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1730 |
|
| 1731 | /*
|
| 1732 | * -----------------------------------------------------------
|
| 1733 | * The Idle task.
|
| 1734 | * ----------------------------------------------------------
|
| 1735 | *
|
| 1736 | * The portTASK_FUNCTION() macro is used to allow port/compiler specific
|
| 1737 | * language extensions. The equivalent prototype for this function is:
|
| 1738 | *
|
| 1739 | * void prvIdleTask( void *pvParameters );
|
| 1740 | *
|
| 1741 | */
|
| 1742 | static portTASK_FUNCTION( prvIdleTask, pvParameters )
|
| 1743 | {
|
| 1744 | /* Stop warnings. */
|
| 1745 | ( void ) pvParameters;
|
| 1746 |
|
| 1747 | for( ;; )
|
| 1748 | {
|
| 1749 | /* See if any tasks have been deleted. */
|
| 1750 | prvCheckTasksWaitingTermination();
|
| 1751 |
|
| 1752 | #if ( configUSE_PREEMPTION == 0 )
|
| 1753 | {
|
| 1754 | /* If we are not using preemption we keep forcing a task switch to
|
| 1755 | see if any other task has become available. If we are using
|
| 1756 | preemption we don't need to do this as any task becoming available
|
| 1757 | will automatically get the processor anyway. */
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1758 | taskYIELD();
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1759 | }
|
| 1760 | #endif
|
| 1761 |
|
| 1762 | #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
|
| 1763 | {
|
| 1764 | /* When using preemption tasks of equal priority will be
|
| 1765 | timesliced. If a task that is sharing the idle priority is ready
|
| 1766 | to run then the idle task should yield before the end of the
|
| 1767 | timeslice.
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1768 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1769 | A critical region is not required here as we are just reading from
|
| 1770 | the list, and an occasional incorrect value will not matter. If
|
| 1771 | the ready list at the idle priority contains more than one task
|
| 1772 | then a task other than the idle task is ready to execute. */
|
| 1773 | if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( unsigned portBASE_TYPE ) 1 )
|
| 1774 | {
|
| 1775 | taskYIELD();
|
| 1776 | }
|
| 1777 | }
|
| 1778 | #endif
|
| 1779 |
|
| 1780 | #if ( configUSE_IDLE_HOOK == 1 )
|
| 1781 | {
|
| 1782 | extern void vApplicationIdleHook( void );
|
| 1783 |
|
| 1784 | /* Call the user defined function from within the idle task. This
|
| 1785 | allows the application designer to add background functionality
|
| 1786 | without the overhead of a separate task.
|
| 1787 | NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
|
| 1788 | CALL A FUNCTION THAT MIGHT BLOCK. */
|
| 1789 | vApplicationIdleHook();
|
| 1790 | }
|
| 1791 | #endif
|
| 1792 | }
|
| 1793 | } /*lint !e715 pvParameters is not accessed but all task functions require the same prototype. */
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 | /*-----------------------------------------------------------
|
| 1802 | * File private functions documented at the top of the file.
|
| 1803 | *----------------------------------------------------------*/
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 1807 | static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed portCHAR * const pcName, unsigned portBASE_TYPE uxPriority )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1808 | {
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1809 | /* Store the function name in the TCB. */
|
Richard Barry | f24533b | 2009-05-30 13:30:40 +0000 | [diff] [blame] | 1810 | #if configMAX_TASK_NAME_LEN > 1
|
| 1811 | {
|
| 1812 | /* Don't bring strncpy into the build unnecessarily. */
|
| 1813 | strncpy( ( char * ) pxTCB->pcTaskName, ( const char * ) pcName, ( unsigned portSHORT ) configMAX_TASK_NAME_LEN );
|
| 1814 | }
|
| 1815 | #endif
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1816 | pxTCB->pcTaskName[ ( unsigned portSHORT ) configMAX_TASK_NAME_LEN - ( unsigned portSHORT ) 1 ] = '\0';
|
| 1817 |
|
| 1818 | /* This is used as an array index so must ensure it's not too large. */
|
| 1819 | if( uxPriority >= configMAX_PRIORITIES )
|
| 1820 | {
|
| 1821 | uxPriority = configMAX_PRIORITIES - 1;
|
| 1822 | }
|
| 1823 |
|
| 1824 | pxTCB->uxPriority = uxPriority;
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 1825 | #if ( configUSE_MUTEXES == 1 )
|
| 1826 | {
|
| 1827 | pxTCB->uxBasePriority = uxPriority;
|
| 1828 | }
|
| 1829 | #endif
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1830 |
|
| 1831 | vListInitialiseItem( &( pxTCB->xGenericListItem ) );
|
| 1832 | vListInitialiseItem( &( pxTCB->xEventListItem ) );
|
| 1833 |
|
| 1834 | /* Set the pxTCB as a link back from the xListItem. This is so we can get
|
| 1835 | back to the containing TCB from a generic item in a list. */
|
| 1836 | listSET_LIST_ITEM_OWNER( &( pxTCB->xGenericListItem ), pxTCB );
|
| 1837 |
|
| 1838 | /* Event lists are always in priority order. */
|
| 1839 | listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
|
| 1840 | listSET_LIST_ITEM_OWNER( &( pxTCB->xEventListItem ), pxTCB );
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 1841 |
|
| 1842 | #if ( portCRITICAL_NESTING_IN_TCB == 1 )
|
| 1843 | {
|
| 1844 | pxTCB->uxCriticalNesting = ( unsigned portBASE_TYPE ) 0;
|
| 1845 | }
|
| 1846 | #endif
|
| 1847 |
|
Richard Barry | e939542 | 2008-04-16 07:47:02 +0000 | [diff] [blame] | 1848 | #if ( configUSE_APPLICATION_TASK_TAG == 1 )
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 1849 | {
|
Richard Barry | e939542 | 2008-04-16 07:47:02 +0000 | [diff] [blame] | 1850 | pxTCB->pxTaskTag = NULL;
|
Richard Barry | da6d27b | 2008-04-12 09:46:19 +0000 | [diff] [blame] | 1851 | }
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1852 | #endif
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 1853 |
|
| 1854 | #if ( configGENERATE_RUN_TIME_STATS == 1 )
|
| 1855 | {
|
| 1856 | pxTCB->ulRunTimeCounter = 0UL;
|
| 1857 | }
|
| 1858 | #endif
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1859 | }
|
| 1860 | /*-----------------------------------------------------------*/
|
| 1861 |
|
| 1862 | static void prvInitialiseTaskLists( void )
|
| 1863 | {
|
| 1864 | unsigned portBASE_TYPE uxPriority;
|
| 1865 |
|
| 1866 | for( uxPriority = 0; uxPriority < configMAX_PRIORITIES; uxPriority++ )
|
| 1867 | {
|
| 1868 | vListInitialise( ( xList * ) &( pxReadyTasksLists[ uxPriority ] ) );
|
| 1869 | }
|
| 1870 |
|
| 1871 | vListInitialise( ( xList * ) &xDelayedTaskList1 );
|
| 1872 | vListInitialise( ( xList * ) &xDelayedTaskList2 );
|
| 1873 | vListInitialise( ( xList * ) &xPendingReadyList );
|
| 1874 |
|
| 1875 | #if ( INCLUDE_vTaskDelete == 1 )
|
| 1876 | {
|
| 1877 | vListInitialise( ( xList * ) &xTasksWaitingTermination );
|
| 1878 | }
|
| 1879 | #endif
|
| 1880 |
|
| 1881 | #if ( INCLUDE_vTaskSuspend == 1 )
|
| 1882 | {
|
| 1883 | vListInitialise( ( xList * ) &xSuspendedTaskList );
|
| 1884 | }
|
| 1885 | #endif
|
| 1886 |
|
| 1887 | /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
|
| 1888 | using list2. */
|
| 1889 | pxDelayedTaskList = &xDelayedTaskList1;
|
| 1890 | pxOverflowDelayedTaskList = &xDelayedTaskList2;
|
| 1891 | }
|
| 1892 | /*-----------------------------------------------------------*/
|
| 1893 |
|
| 1894 | static void prvCheckTasksWaitingTermination( void )
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1895 | {
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1896 | #if ( INCLUDE_vTaskDelete == 1 )
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1897 | {
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1898 | portBASE_TYPE xListIsEmpty;
|
| 1899 |
|
| 1900 | /* ucTasksDeleted is used to prevent vTaskSuspendAll() being called
|
| 1901 | too often in the idle task. */
|
| 1902 | if( uxTasksDeleted > ( unsigned portBASE_TYPE ) 0 )
|
| 1903 | {
|
| 1904 | vTaskSuspendAll();
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1905 | xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1906 | xTaskResumeAll();
|
| 1907 |
|
| 1908 | if( !xListIsEmpty )
|
| 1909 | {
|
| 1910 | tskTCB *pxTCB;
|
| 1911 |
|
| 1912 | portENTER_CRITICAL();
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1913 | {
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1914 | pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xTasksWaitingTermination ) );
|
| 1915 | vListRemove( &( pxTCB->xGenericListItem ) );
|
| 1916 | --uxCurrentNumberOfTasks;
|
| 1917 | --uxTasksDeleted;
|
| 1918 | }
|
| 1919 | portEXIT_CRITICAL();
|
| 1920 |
|
| 1921 | prvDeleteTCB( pxTCB );
|
| 1922 | }
|
| 1923 | }
|
| 1924 | }
|
| 1925 | #endif
|
| 1926 | }
|
| 1927 | /*-----------------------------------------------------------*/
|
| 1928 |
|
| 1929 | static tskTCB *prvAllocateTCBAndStack( unsigned portSHORT usStackDepth )
|
| 1930 | {
|
| 1931 | tskTCB *pxNewTCB;
|
| 1932 |
|
| 1933 | /* Allocate space for the TCB. Where the memory comes from depends on
|
| 1934 | the implementation of the port malloc function. */
|
| 1935 | pxNewTCB = ( tskTCB * ) pvPortMalloc( sizeof( tskTCB ) );
|
| 1936 |
|
| 1937 | if( pxNewTCB != NULL )
|
| 1938 | {
|
| 1939 | /* Allocate space for the stack used by the task being created.
|
| 1940 | The base of the stack memory stored in the TCB so the task can
|
| 1941 | be deleted later if required. */
|
| 1942 | pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMalloc( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) );
|
| 1943 |
|
| 1944 | if( pxNewTCB->pxStack == NULL )
|
| 1945 | {
|
| 1946 | /* Could not allocate the stack. Delete the allocated TCB. */
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1947 | vPortFree( pxNewTCB );
|
| 1948 | pxNewTCB = NULL;
|
| 1949 | }
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1950 | else
|
| 1951 | {
|
| 1952 | /* Just to help debugging. */
|
| 1953 | memset( pxNewTCB->pxStack, tskSTACK_FILL_BYTE, usStackDepth * sizeof( portSTACK_TYPE ) );
|
| 1954 | }
|
| 1955 | }
|
| 1956 |
|
| 1957 | return pxNewTCB;
|
| 1958 | }
|
| 1959 | /*-----------------------------------------------------------*/
|
| 1960 |
|
| 1961 | #if ( configUSE_TRACE_FACILITY == 1 )
|
| 1962 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 1963 | static void prvListTaskWithinSingleList( const signed portCHAR *pcWriteBuffer, xList *pxList, signed portCHAR cStatus )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1964 | {
|
| 1965 | volatile tskTCB *pxNextTCB, *pxFirstTCB;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 1966 | unsigned portSHORT usStackRemaining;
|
| 1967 |
|
| 1968 | /* Write the details of all the TCB's in pxList into the buffer. */
|
| 1969 | listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
|
| 1970 | do
|
| 1971 | {
|
| 1972 | listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
|
| 1973 | usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned portCHAR * ) pxNextTCB->pxStack );
|
| 1974 | sprintf( pcStatusString, ( portCHAR * ) "%s\t\t%c\t%u\t%u\t%u\r\n", pxNextTCB->pcTaskName, cStatus, ( unsigned int ) pxNextTCB->uxPriority, usStackRemaining, ( unsigned int ) pxNextTCB->uxTCBNumber );
|
| 1975 | strcat( ( portCHAR * ) pcWriteBuffer, ( portCHAR * ) pcStatusString );
|
| 1976 |
|
| 1977 | } while( pxNextTCB != pxFirstTCB );
|
| 1978 | }
|
| 1979 |
|
| 1980 | #endif
|
| 1981 | /*-----------------------------------------------------------*/
|
| 1982 |
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 1983 | #if ( configGENERATE_RUN_TIME_STATS == 1 )
|
| 1984 |
|
| 1985 | static void prvGenerateRunTimeStatsForTasksInList( const signed portCHAR *pcWriteBuffer, xList *pxList, unsigned portLONG ulTotalRunTime )
|
| 1986 | {
|
| 1987 | volatile tskTCB *pxNextTCB, *pxFirstTCB;
|
| 1988 | unsigned portLONG ulStatsAsPercentage;
|
| 1989 |
|
| 1990 | /* Write the run time stats of all the TCB's in pxList into the buffer. */
|
| 1991 | listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
|
| 1992 | do
|
| 1993 | {
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1994 | /* Get next TCB in from the list. */
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 1995 | listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
|
| 1996 |
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 1997 | /* Divide by zero check. */
|
| 1998 | if( ulTotalRunTime > 0UL )
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 1999 | {
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 2000 | /* Has the task run at all? */
|
| 2001 | if( pxNextTCB->ulRunTimeCounter == 0 )
|
| 2002 | {
|
| 2003 | /* The task has used no CPU time at all. */
|
| 2004 | sprintf( pcStatsString, ( portCHAR * ) "%s\t\t0\t\t0%%\r\n", pxNextTCB->pcTaskName );
|
| 2005 | }
|
| 2006 | else
|
| 2007 | {
|
| 2008 | /* What percentage of the total run time as the task used?
|
| 2009 | This will always be rounded down to the nearest integer. */
|
| 2010 | ulStatsAsPercentage = ( 100UL * pxNextTCB->ulRunTimeCounter ) / ulTotalRunTime;
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 2011 |
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 2012 | if( ulStatsAsPercentage > 0UL )
|
| 2013 | {
|
Richard Barry | cd7fb3f | 2009-05-28 20:35:28 +0000 | [diff] [blame] | 2014 | sprintf( pcStatsString, ( portCHAR * ) "%s\t\t%u\t\t%u%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 2015 | }
|
| 2016 | else
|
| 2017 | {
|
| 2018 | /* If the percentage is zero here then the task has
|
Richard Barry | cd7fb3f | 2009-05-28 20:35:28 +0000 | [diff] [blame] | 2019 | consumed less than 1% of the total run time. */
|
| 2020 | sprintf( pcStatsString, ( portCHAR * ) "%s\t\t%u\t\t<1%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter );
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 2021 | }
|
| 2022 | }
|
| 2023 |
|
| 2024 | strcat( ( portCHAR * ) pcWriteBuffer, ( portCHAR * ) pcStatsString );
|
| 2025 | }
|
Richard Barry | 8b4ef53 | 2009-05-19 10:38:26 +0000 | [diff] [blame] | 2026 |
|
| 2027 | } while( pxNextTCB != pxFirstTCB );
|
| 2028 | }
|
| 2029 |
|
| 2030 | #endif
|
| 2031 | /*-----------------------------------------------------------*/
|
| 2032 |
|
Richard Barry | 03acab1 | 2008-10-29 10:11:22 +0000 | [diff] [blame] | 2033 | #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 2034 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 2035 | unsigned portSHORT usTaskCheckFreeStackSpace( const unsigned portCHAR * pucStackByte )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 2036 | {
|
| 2037 | register unsigned portSHORT usCount = 0;
|
| 2038 |
|
| 2039 | while( *pucStackByte == tskSTACK_FILL_BYTE )
|
| 2040 | {
|
| 2041 | pucStackByte -= portSTACK_GROWTH;
|
| 2042 | usCount++;
|
| 2043 | }
|
| 2044 |
|
| 2045 | usCount /= sizeof( portSTACK_TYPE );
|
| 2046 |
|
| 2047 | return usCount;
|
| 2048 | }
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 2049 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 2050 | #endif
|
| 2051 | /*-----------------------------------------------------------*/
|
| 2052 |
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 2053 | #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
|
| 2054 |
|
Richard Barry | 47a7f01 | 2008-03-05 10:12:35 +0000 | [diff] [blame] | 2055 | unsigned portBASE_TYPE uxTaskGetStackHighWaterMark( xTaskHandle xTask )
|
Richard Barry | b8b7052 | 2008-03-03 16:32:05 +0000 | [diff] [blame] | 2056 | {
|
Richard Barry | 47a7f01 | 2008-03-05 10:12:35 +0000 | [diff] [blame] | 2057 | tskTCB *pxTCB;
|
Richard Barry | 4f631de | 2008-10-09 00:53:02 +0000 | [diff] [blame] | 2058 | unsigned portCHAR *pcEndOfStack;
|
Richard Barry | 47a7f01 | 2008-03-05 10:12:35 +0000 | [diff] [blame] | 2059 |
|
| 2060 | pxTCB = prvGetTCBFromHandle( xTask );
|
Richard Barry | 4f631de | 2008-10-09 00:53:02 +0000 | [diff] [blame] | 2061 |
|
| 2062 | #if portSTACK_GROWTH < 0
|
| 2063 | {
|
| 2064 | pcEndOfStack = ( unsigned portCHAR * ) pxTCB->pxStack;
|
| 2065 | }
|
| 2066 | #else
|
| 2067 | {
|
| 2068 | pcEndOfStack = ( unsigned portCHAR * ) pxTCB->pxEndOfStack;
|
| 2069 | }
|
| 2070 | #endif
|
| 2071 |
|
| 2072 | return usTaskCheckFreeStackSpace( pcEndOfStack );
|
Richard Barry | b8b7052 | 2008-03-03 16:32:05 +0000 | [diff] [blame] | 2073 | }
|
Richard Barry | 39f6b0b | 2008-03-03 20:56:55 +0000 | [diff] [blame] | 2074 |
|
Richard Barry | b8b7052 | 2008-03-03 16:32:05 +0000 | [diff] [blame] | 2075 | #endif
|
| 2076 | /*-----------------------------------------------------------*/
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 2077 |
|
| 2078 | #if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )
|
| 2079 |
|
| 2080 | static void prvDeleteTCB( tskTCB *pxTCB )
|
| 2081 | {
|
| 2082 | /* Free up the memory allocated by the scheduler for the task. It is up to
|
| 2083 | the task to free any memory allocated at the application level. */
|
| 2084 | vPortFree( pxTCB->pxStack );
|
| 2085 | vPortFree( pxTCB );
|
| 2086 | }
|
| 2087 |
|
| 2088 | #endif
|
| 2089 |
|
| 2090 |
|
| 2091 | /*-----------------------------------------------------------*/
|
| 2092 |
|
| 2093 | #if ( INCLUDE_xTaskGetCurrentTaskHandle == 1 )
|
| 2094 |
|
| 2095 | xTaskHandle xTaskGetCurrentTaskHandle( void )
|
| 2096 | {
|
Richard Barry | 55c9604 | 2009-03-11 10:55:41 +0000 | [diff] [blame] | 2097 | /* A critical section is not required as this is not called from
|
| 2098 | an interrupt and the current TCB will always be the same for any
|
| 2099 | individual execution thread. */
|
| 2100 | return pxCurrentTCB;
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 2101 | }
|
| 2102 |
|
| 2103 | #endif
|
| 2104 |
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 2105 | /*-----------------------------------------------------------*/
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 2106 |
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 2107 | #if ( INCLUDE_xTaskGetSchedulerState == 1 )
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 2108 |
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 2109 | portBASE_TYPE xTaskGetSchedulerState( void )
|
| 2110 | {
|
| 2111 | portBASE_TYPE xReturn;
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 2112 |
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 2113 | if( xSchedulerRunning == pdFALSE )
|
| 2114 | {
|
| 2115 | xReturn = taskSCHEDULER_NOT_STARTED;
|
| 2116 | }
|
| 2117 | else
|
| 2118 | {
|
| 2119 | if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
|
| 2120 | {
|
| 2121 | xReturn = taskSCHEDULER_RUNNING;
|
| 2122 | }
|
| 2123 | else
|
| 2124 | {
|
| 2125 | xReturn = taskSCHEDULER_SUSPENDED;
|
| 2126 | }
|
| 2127 | }
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 2128 |
|
Richard Barry | 7a8eb50 | 2007-07-28 16:33:07 +0000 | [diff] [blame] | 2129 | return xReturn;
|
| 2130 | }
|
| 2131 |
|
| 2132 | #endif
|
Richard Barry | 2b174e5 | 2008-02-25 18:54:28 +0000 | [diff] [blame] | 2133 | /*-----------------------------------------------------------*/
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 2134 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 2135 | #if ( configUSE_MUTEXES == 1 )
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 2136 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 2137 | void vTaskPriorityInherit( xTaskHandle * const pxMutexHolder )
|
| 2138 | {
|
| 2139 | tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;
|
| 2140 |
|
| 2141 | if( pxTCB->uxPriority < pxCurrentTCB->uxPriority )
|
| 2142 | {
|
| 2143 | /* Adjust the mutex holder state to account for its new priority. */
|
| 2144 | listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxCurrentTCB->uxPriority );
|
| 2145 |
|
Richard Barry | dfb8e70 | 2008-01-22 18:43:03 +0000 | [diff] [blame] | 2146 | /* If the task being modified is in the ready state it will need to
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 2147 | be moved in to a new list. */
|
| 2148 | if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ) )
|
| 2149 | {
|
| 2150 | vListRemove( &( pxTCB->xGenericListItem ) );
|
| 2151 |
|
| 2152 | /* Inherit the priority before being moved into the new list. */
|
| 2153 | pxTCB->uxPriority = pxCurrentTCB->uxPriority;
|
| 2154 | prvAddTaskToReadyQueue( pxTCB );
|
| 2155 | }
|
| 2156 | else
|
| 2157 | {
|
| 2158 | /* Just inherit the priority. */
|
| 2159 | pxTCB->uxPriority = pxCurrentTCB->uxPriority;
|
| 2160 | }
|
| 2161 | }
|
| 2162 | }
|
| 2163 |
|
| 2164 | #endif
|
Richard Barry | 2b174e5 | 2008-02-25 18:54:28 +0000 | [diff] [blame] | 2165 | /*-----------------------------------------------------------*/
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 2166 |
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 2167 | #if ( configUSE_MUTEXES == 1 )
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 2168 |
|
| 2169 | void vTaskPriorityDisinherit( xTaskHandle * const pxMutexHolder )
|
| 2170 | {
|
| 2171 | tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;
|
| 2172 |
|
| 2173 | if( pxMutexHolder != NULL )
|
| 2174 | {
|
| 2175 | if( pxTCB->uxPriority != pxTCB->uxBasePriority )
|
| 2176 | {
|
| 2177 | /* We must be the running task to be able to give the mutex back.
|
| 2178 | Remove ourselves from the ready list we currently appear in. */
|
| 2179 | vListRemove( &( pxTCB->xGenericListItem ) );
|
| 2180 |
|
| 2181 | /* Disinherit the priority before adding ourselves into the new
|
| 2182 | ready list. */
|
| 2183 | pxTCB->uxPriority = pxTCB->uxBasePriority;
|
| 2184 | listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxTCB->uxPriority );
|
| 2185 | prvAddTaskToReadyQueue( pxTCB );
|
| 2186 | }
|
| 2187 | }
|
| 2188 | }
|
| 2189 |
|
| 2190 | #endif
|
Richard Barry | 2b174e5 | 2008-02-25 18:54:28 +0000 | [diff] [blame] | 2191 | /*-----------------------------------------------------------*/
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 2192 |
|
Richard Barry | 2b174e5 | 2008-02-25 18:54:28 +0000 | [diff] [blame] | 2193 | #if ( portCRITICAL_NESTING_IN_TCB == 1 )
|
| 2194 |
|
| 2195 | void vTaskEnterCritical( void )
|
| 2196 | {
|
| 2197 | portDISABLE_INTERRUPTS();
|
| 2198 |
|
| 2199 | if( xSchedulerRunning != pdFALSE )
|
| 2200 | {
|
| 2201 | pxCurrentTCB->uxCriticalNesting++;
|
| 2202 | }
|
| 2203 | }
|
| 2204 |
|
| 2205 | #endif
|
| 2206 | /*-----------------------------------------------------------*/
|
| 2207 |
|
| 2208 | #if ( portCRITICAL_NESTING_IN_TCB == 1 )
|
| 2209 |
|
| 2210 | void vTaskExitCritical( void )
|
| 2211 | {
|
| 2212 | if( xSchedulerRunning != pdFALSE )
|
| 2213 | {
|
| 2214 | if( pxCurrentTCB->uxCriticalNesting > 0 )
|
| 2215 | {
|
| 2216 | pxCurrentTCB->uxCriticalNesting--;
|
| 2217 |
|
| 2218 | if( pxCurrentTCB->uxCriticalNesting == 0 )
|
| 2219 | {
|
| 2220 | portENABLE_INTERRUPTS();
|
| 2221 | }
|
| 2222 | }
|
| 2223 | }
|
| 2224 | }
|
| 2225 |
|
| 2226 | #endif
|
| 2227 | /*-----------------------------------------------------------*/
|
| 2228 |
|
Richard Barry | 60338bd | 2007-08-21 16:54:48 +0000 | [diff] [blame] | 2229 |
|
Richard Barry | c66301a | 2009-05-21 12:23:24 +0000 | [diff] [blame] | 2230 |
|
Richard Barry | b6df57c | 2006-05-02 09:39:15 +0000 | [diff] [blame] | 2231 |
|