【Linux 内核 内存管理】RCU 机制 ③ ( RCU 模式下添加链表项 list_add_rcu 函数 | RCU 模式下删除链表项 list_del_rcu 函数 )
文章目錄
- 一、RCU 模式下添加鏈表項 list_add_rcu 函數(shù)
- 二、RCU 模式下刪除鏈表項 list_del_rcu 函數(shù)
一、RCU 模式下添加鏈表項 list_add_rcu 函數(shù)
在 Linux 源碼 linux-5.6.18\include\linux\rculist.h 頭文件中定義的就是 RCU 鏈表的操作 ,
其中定義的
static inline void list_add_rcu(struct list_head *new, struct list_head *head)函數(shù) , 就是 向 鏈表中 添加元素 的 函數(shù) ;
list_add_rcu 函數(shù)中 , 主要是調用了 __list_add_rcu 函數(shù) ,
在 __list_add_rcu 函數(shù)中 , 將新添加的 鏈表項 添加到了 struct list_head *prev 和 struct list_head *next 兩個鏈表項的中間 ;
list_add_rcu 函數(shù)原型 :
/** Insert a new entry between two known consecutive entries.** This is only for internal list manipulation where we know* the prev/next entries already!*/ static inline void __list_add_rcu(struct list_head *new,struct list_head *prev, struct list_head *next) {if (!__list_add_valid(new, prev, next))return;new->next = next;new->prev = prev;rcu_assign_pointer(list_next_rcu(prev), new);next->prev = new; }/*** list_add_rcu - add a new entry to rcu-protected list* @new: new entry to be added* @head: list head to add it after** Insert a new entry after the specified head.* This is good for implementing stacks.** The caller must take whatever precautions are necessary* (such as holding appropriate locks) to avoid racing* with another list-mutation primitive, such as list_add_rcu()* or list_del_rcu(), running on this same list.* However, it is perfectly legal to run concurrently with* the _rcu list-traversal primitives, such as* list_for_each_entry_rcu().*/ static inline void list_add_rcu(struct list_head *new, struct list_head *head) {__list_add_rcu(new, head, head->next); }源碼路徑 : linux-5.6.18\include\linux\rculist.h#105
二、RCU 模式下刪除鏈表項 list_del_rcu 函數(shù)
在 Linux 源碼 linux-5.6.18\include\linux\rculist.h 頭文件中定義的就是 RCU 鏈表的操作 ,
其中定義的
static inline void list_del_rcu(struct list_head *entry)函數(shù) , 就是 從 鏈表中 刪除元素 的 函數(shù) ;
list_del_rcu 函數(shù)中 , 主要是調用了 __list_del_entry 函數(shù) ,
在 __list_del_entry 函數(shù)中 , 又調用了 __list_del 函數(shù) ;
list_del_rcu 函數(shù)原型 :
/*** list_del_rcu - deletes entry from list without re-initialization* @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. It is useful for RCU based* lockfree traversal.** In particular, it means that we can not poison the forward* pointers that may still be used for walking the list.** The caller must take whatever precautions are necessary* (such as holding appropriate locks) to avoid racing* with another list-mutation primitive, such as list_del_rcu()* or list_add_rcu(), running on this same list.* However, it is perfectly legal to run concurrently with* the _rcu list-traversal primitives, such as* list_for_each_entry_rcu().** Note that the caller is not permitted to immediately free* the newly deleted entry. Instead, either synchronize_rcu()* or call_rcu() must be used to defer freeing until an RCU* grace period has elapsed.*/ static inline void list_del_rcu(struct list_head *entry) {__list_del_entry(entry);entry->prev = LIST_POISON2; }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; }源碼路徑 : linux-5.6.18\include\linux\rculist.h#156
總結
以上是生活随笔為你收集整理的【Linux 内核 内存管理】RCU 机制 ③ ( RCU 模式下添加链表项 list_add_rcu 函数 | RCU 模式下删除链表项 list_del_rcu 函数 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux 内核 内存管理】RCU 机
- 下一篇: 【Linux 内核 内存管理】RCU 机