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