列表的初始化:列表的最后一个列表项是不计入列表项数量的,相当于头结点链表的头结点。开始只有一个end列表项,所以(1)pxIndex索引指向end。(2)值默认初始化为全f。(3)(4)一个项的上下一项都是指向自己,(5)(6)列表项个数初始化为0.剩下两项是初始化列表完整性检查字段。
列表项的初始化只需要把pvContainer初始化为NULL就行,其他函数会初始化如任务创建函数。
插入列表项函数源码介绍:
void vListInsert( List_t * const pxList, ListItem_t * const pxNewListItem )
{
ListItem_t *pxIterator;
const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;//根据值选择插入位置
/* Only effective when configASSERT() is also defined, these tests may catch
the list data structures being overwritten in memory. They will not catch
data errors caused by incorrect configuration or use of FreeRTOS. */
listTEST_LIST_INTEGRITY( pxList );//列表完整性检查断言实现,可不看
listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
/* Insert the new list item into the list, sorted in xItemValue order.
If the list already contains a list item with the same item value then the
new list item should be placed after it. This ensures that TCB's which are
stored in ready lists (all of which have the same xItemValue value) get a
share of the CPU. However, if the xItemValue is the same as the back marker
the iteration loop below will not end. Therefore the value is checked
first, and the algorithm slightly modified if necessary. */
if( xValueOfInsertion == portMAX_DELAY )//如果等于最大值就直接插到最后
{
pxIterator = pxList->xListEnd.pxPrevious;
}
else
{
/* *** NOTE ***********************************************************
If you find your application is crashing here then likely causes are
listed below. In addition see http://www.freertos.org/FAQHelp.html for
more tips, and ensure configASSERT() is defined!
http://www.freertos.org/a00110.html#configASSERT
1) Stack overflow -
see http://www.freertos.org/Stacks-and-stack-overflow-checking.html
2) Incorrect interrupt priority assignment, especially on Cortex-M
parts where numerically high priority values denote low actual
interrupt priorities, which can seem counter intuitive. See
http://www.freertos.org/RTOS-Cortex-M3-M4.html and the definition
of configMAX_SYSCALL_INTERRUPT_PRIORITY on
http://www.freertos.org/a00110.html
3) Calling an API function from within a critical section or when
the scheduler is suspended, or calling an API function that does
not end in "FromISR" from an interrupt.
4) Using a queue or semaphore before it has been initialised or
before the scheduler has been started (are interrupts firing
before vTaskStartScheduler() has been called?).
**********************************************************************/
for( pxIterator = ( ListItem_t * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; //否则找出位置pxIterator = pxIterator->pxNext ) /*lint !e826 !e740 The mini list structure is used as the list end to save RAM. This is checked and valid. */
{
/* There is nothing to do here, just iterating to the wanted
insertion position. */
}
}
pxNewListItem->pxNext = pxIterator->pxNext;//上面确定了pxIterator就是该插入的位置,但是只是确定了pxIterator->pxNext,所以这里先赋值pxNext
pxNewListItem->pxNext->pxPrevious = pxNewListItem;//把该位置的下一项的前一项改为插入项
pxNewListItem->pxPrevious = pxIterator;//把pxIterator赋给该位置的上一项(重点)
pxIterator->pxNext = pxNewListItem;//把该位置的上一项的下一项改为插入项完成了双向链表的插入
/* Remember which list the item is in. This allows fast removal of the
item later. */
pxNewListItem->pvContainer = ( void * ) pxList;//添加列表项的位置是这个列表
( pxList->uxNumberOfItems )++;//项数+1
}
列表遍历过程:从第一个(end->next)开始,如果pxIterm->next不等于end继续遍历,如果等于end则跳过end回到第一个就完成了一次遍历。