root/webserver/example/freeRTOS/Source/tasks.c

Revision 14, 68.0 kB (checked in by phil, 15 years ago)

added unmodified FreeRTOS package V5.4.1 with only web srv demo source for LPC2368 for CrossWorks?

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