list_del函数代码分析

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值