1.函数作用
删除双向链表中的一个项
2.函数源码
文件路径:include/linux/list.h
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty() on entry does not return true after this, the entry is
* in an undefined state.
*/
static inline void list_del(struct list_head *entry)
{
__list_del_entry(entry);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
3.函数分析
文件路径:include/linux/list.h
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
//核心是这两行代码
next->prev = prev;
WRITE_ONCE(prev->next, next);
//比如a<==>b<==>C,现在要删除b,就是
//c->prev=a; a->next=c,b就被移出链表了
}
/*让我产生疑问的是这两行代码为什么一个用WRITE_ONCE一个不用,WRITE_ONCE是用于确保写操作的原子性
和可见性的宏,在并发环境中该宏可以提供更安全的写操作,但是为什么next->prev = prev不用。我没有
完全明白,如果你有好的见解可以私信我,我目前保持的理解是:内核中常用链表的next指针遍历和操作链
表向,所以为了安全使用WRITE_ONCE保护,而prev指针用的比较少,同时使用WRITE_ONCE会消耗更多的性
能,所以平衡考虑这里不用WRITE_ONCE保护next->prev = prev。*/
static inline void __list_del_entry(struct list_head *entry)
{
if (!__list_del_entry_valid(entry))
return;
__list_del(entry->prev, entry->next); //将要删除的项的前一项和要删除的项的后一项传入
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty() on entry does not return true after this, the entry is
* in an undefined state.
*/
static inline void list_del(struct list_head *entry)
{
__list_del_entry(entry); //这是核心,我们进这个函数看一下
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
1487

被折叠的 条评论
为什么被折叠?



