FreeRTOS Real Time Operating System, Modified from Kenji Arai's initial port. See freertos.org for full documentation.

Fork of FreeRTOS_on_mbed_v1 by Kenji Arai

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers list.c Source File

list.c

00001 /*
00002     FreeRTOS V6.0.3 - Copyright (C) 2010 Real Time Engineers Ltd.
00003 
00004     ***************************************************************************
00005     *                                                                         *
00006     * If you are:                                                             *
00007     *                                                                         *
00008     *    + New to FreeRTOS,                                                   *
00009     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *
00010     *    + Looking for basic training,                                        *
00011     *    + Wanting to improve your FreeRTOS skills and productivity           *
00012     *                                                                         *
00013     * then take a look at the FreeRTOS eBook                                  *
00014     *                                                                         *
00015     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *
00016     *                  http://www.FreeRTOS.org/Documentation                  *
00017     *                                                                         *
00018     * A pdf reference manual is also available.  Both are usually delivered   *
00019     * to your inbox within 20 minutes to two hours when purchased between 8am *
00020     * and 8pm GMT (although please allow up to 24 hours in case of            *
00021     * exceptional circumstances).  Thank you for your support!                *
00022     *                                                                         *
00023     ***************************************************************************
00024 
00025     This file is part of the FreeRTOS distribution.
00026 
00027     FreeRTOS is free software; you can redistribute it and/or modify it under
00028     the terms of the GNU General Public License (version 2) as published by the
00029     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
00030     ***NOTE*** The exception to the GPL is included to allow you to distribute
00031     a combined work that includes FreeRTOS without being obliged to provide the
00032     source code for proprietary components outside of the FreeRTOS kernel.
00033     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
00034     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00035     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00036     more details. You should have received a copy of the GNU General Public 
00037     License and the FreeRTOS license exception along with FreeRTOS; if not it 
00038     can be viewed here: http://www.freertos.org/a00114.html and also obtained 
00039     by writing to Richard Barry, contact details for whom are available on the
00040     FreeRTOS WEB site.
00041 
00042     1 tab == 4 spaces!
00043 
00044     http://www.FreeRTOS.org - Documentation, latest information, license and
00045     contact details.
00046 
00047     http://www.SafeRTOS.com - A version that is certified for use in safety
00048     critical systems.
00049 
00050     http://www.OpenRTOS.com - Commercial support, development, porting,
00051     licensing and training services.
00052 */
00053 
00054 
00055 #include "stdlib.h"
00056 #include "./FreeRTOS/Source/include/FreeRTOS.h"
00057 #include "./FreeRTOS/Source/include/list.h"
00058 
00059 /*-----------------------------------------------------------
00060  * PUBLIC LIST API documented in list.h
00061  *----------------------------------------------------------*/
00062 
00063 void vListInitialise( xList *pxList )
00064 {
00065     /* The list structure contains a list item which is used to mark the
00066     end of the list.  To initialise the list the list end is inserted
00067     as the only list entry. */
00068     pxList->pxIndex = ( xListItem * ) &( pxList->xListEnd );
00069 
00070     /* The list end value is the highest possible value in the list to
00071     ensure it remains at the end of the list. */
00072     pxList->xListEnd.xItemValue = portMAX_DELAY;
00073 
00074     /* The list end next and previous pointers point to itself so we know
00075     when the list is empty. */
00076     pxList->xListEnd.pxNext = ( xListItem * ) &( pxList->xListEnd );
00077     pxList->xListEnd.pxPrevious = ( xListItem * ) &( pxList->xListEnd );
00078 
00079     pxList->uxNumberOfItems = 0;
00080 }
00081 /*-----------------------------------------------------------*/
00082 
00083 void vListInitialiseItem( xListItem *pxItem )
00084 {
00085     /* Make sure the list item is not recorded as being on a list. */
00086     pxItem->pvContainer = NULL;
00087 }
00088 /*-----------------------------------------------------------*/
00089 
00090 void vListInsertEnd( xList *pxList, xListItem *pxNewListItem )
00091 {
00092 volatile xListItem * pxIndex;
00093 
00094     /* Insert a new list item into pxList, but rather than sort the list,
00095     makes the new list item the last item to be removed by a call to
00096     pvListGetOwnerOfNextEntry.  This means it has to be the item pointed to by
00097     the pxIndex member. */
00098     pxIndex = pxList->pxIndex;
00099 
00100     pxNewListItem->pxNext = pxIndex->pxNext;
00101     pxNewListItem->pxPrevious = pxList->pxIndex;
00102     pxIndex->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;
00103     pxIndex->pxNext = ( volatile xListItem * ) pxNewListItem;
00104     pxList->pxIndex = ( volatile xListItem * ) pxNewListItem;
00105 
00106     /* Remember which list the item is in. */
00107     pxNewListItem->pvContainer = ( void * ) pxList;
00108 
00109     ( pxList->uxNumberOfItems )++;
00110 }
00111 /*-----------------------------------------------------------*/
00112 
00113 void vListInsert( xList *pxList, xListItem *pxNewListItem )
00114 {
00115 volatile xListItem *pxIterator;
00116 portTickType xValueOfInsertion;
00117 
00118     /* Insert the new list item into the list, sorted in ulListItem order. */
00119     xValueOfInsertion = pxNewListItem->xItemValue;
00120 
00121     /* If the list already contains a list item with the same item value then
00122     the new list item should be placed after it.  This ensures that TCB's which
00123     are stored in ready lists (all of which have the same ulListItem value)
00124     get an equal share of the CPU.  However, if the xItemValue is the same as 
00125     the back marker the iteration loop below will not end.  This means we need
00126     to guard against this by checking the value first and modifying the 
00127     algorithm slightly if necessary. */
00128     if( xValueOfInsertion == portMAX_DELAY )
00129     {
00130         pxIterator = pxList->xListEnd.pxPrevious;
00131     }
00132     else
00133     {
00134         /* *** NOTE ***********************************************************
00135         If you find your application is crashing here then likely causes are:
00136             1) Stack overflow - 
00137                see http://www.freertos.org/Stacks-and-stack-overflow-checking.html
00138             2) Incorrect interrupt priority assignment, especially on Cortex M3 
00139                parts where numerically high priority values denote low actual 
00140                interrupt priories, which can seem counter intuitive.  See 
00141                configMAX_SYSCALL_INTERRUPT_PRIORITY on http://www.freertos.org/a00110.html
00142             3) Calling an API function from within a critical section or when
00143                the scheduler is suspended.
00144             4) Using a queue or semaphore before it has been initialised or
00145                before the scheduler has been started (are interrupts firing
00146                before vTaskStartScheduler() has been called?).
00147         See http://www.freertos.org/FAQHelp.html for more tips. 
00148         **********************************************************************/
00149         
00150         for( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )
00151         {
00152             /* There is nothing to do here, we are just iterating to the
00153             wanted insertion position. */
00154         }
00155     }
00156 
00157     pxNewListItem->pxNext = pxIterator->pxNext;
00158     pxNewListItem->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;
00159     pxNewListItem->pxPrevious = pxIterator;
00160     pxIterator->pxNext = ( volatile xListItem * ) pxNewListItem;
00161 
00162     /* Remember which list the item is in.  This allows fast removal of the
00163     item later. */
00164     pxNewListItem->pvContainer = ( void * ) pxList;
00165 
00166     ( pxList->uxNumberOfItems )++;
00167 }
00168 /*-----------------------------------------------------------*/
00169 
00170 void vListRemove( xListItem *pxItemToRemove )
00171 {
00172 xList * pxList;
00173 
00174     pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
00175     pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
00176     
00177     /* The list item knows which list it is in.  Obtain the list from the list
00178     item. */
00179     pxList = ( xList * ) pxItemToRemove->pvContainer;
00180 
00181     /* Make sure the index is left pointing to a valid item. */
00182     if( pxList->pxIndex == pxItemToRemove )
00183     {
00184         pxList->pxIndex = pxItemToRemove->pxPrevious;
00185     }
00186 
00187     pxItemToRemove->pvContainer = NULL;
00188     ( pxList->uxNumberOfItems )--;
00189 }
00190 /*-----------------------------------------------------------*/
00191