rcu链表综合实践

news/2024/5/20 6:55:24

基础知识 

rcu-read copy update的缩写。和读写锁起到相同的效果。据说牛逼一点。对于我们普通程序员,要先学会使用,再探究其内部原理。

链表的数据结构:

struct list_head {struct list_head *next, *prev;
};

 还有一种:struct hlist_head,本文不做该链表的测试。

struct hlist_head {struct hlist_node *first;
};struct hlist_node {struct hlist_node *next, **pprev;
};

 涉及的文件:include\linux\rculist.h

初始化链表:INIT_LIST_HEAD_RCU

/** INIT_LIST_HEAD_RCU - Initialize a list_head visible to RCU readers* @list: list to be initialized** You should instead use INIT_LIST_HEAD() for normal initialization and* cleanup tasks, when readers have no access to the list being initialized.* However, if the list being initialized is visible to readers, you* need to keep the compiler from being too mischievous.*/
static inline void INIT_LIST_HEAD_RCU(struct list_head *list)
{WRITE_ONCE(list->next, list);WRITE_ONCE(list->prev, list);
}

 添加节点list_add_rcu(插入到头节点后面)

/*** 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);
}

 添加节点list_add_tail_rcu(插入到头节点前面,就是链尾) 

/*** list_add_tail_rcu - add a new entry to rcu-protected list* @new: new entry to be added* @head: list head to add it before** Insert a new entry before the specified head.* This is useful for implementing queues.** 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_tail_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_tail_rcu(struct list_head *new,struct list_head *head)
{__list_add_rcu(new, head->prev, head);
}

删除节点list_del_rcu

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

删除尾节点hlist_del_init_rcu 

/*** hlist_del_init_rcu - deletes entry from hash list with re-initialization* @n: the element to delete from the hash list.** Note: list_unhashed() on the node return true after this. It is* useful for RCU based read lockfree traversal if the writer side* must know if the list entry is still hashed or already unhashed.** In particular, it means that we can not poison the forward pointers* that may still be used for walking the hash list and we can only* zero the pprev pointer so list_unhashed() will return true after* this.** The caller must take whatever precautions are necessary (such as* holding appropriate locks) to avoid racing with another* list-mutation primitive, such as hlist_add_head_rcu() or* hlist_del_rcu(), running on this same list.  However, it is* perfectly legal to run concurrently with the _rcu list-traversal* primitives, such as hlist_for_each_entry_rcu().*/
static inline void hlist_del_init_rcu(struct hlist_node *n)
{if (!hlist_unhashed(n)) {__hlist_del(n);n->pprev = NULL;}
}

 替换list_replace_rcu:

/*** list_replace_rcu - replace old entry by new one* @old : the element to be replaced* @new : the new element to insert** The @old entry will be replaced with the @new entry atomically.* Note: @old should not be empty.*/
static inline void list_replace_rcu(struct list_head *old,struct list_head *new)
{new->next = old->next;new->prev = old->prev;rcu_assign_pointer(list_next_rcu(new->prev), new);new->next->prev = new;old->prev = LIST_POISON2;
}

计算长度

判空:list_empty

链表尾空时,返回值为1:链表不空时返回0。

        下面这段注释的大体含义就是,当你想用list_empty_rcu的时候,list_empty就足够满足需要了。

/** Why is there no list_empty_rcu()?  Because list_empty() serves this* purpose.  The list_empty() function fetches the RCU-protected pointer* and compares it to the address of the list head, but neither dereferences* this pointer itself nor provides this pointer to the caller.  Therefore,* it is not necessary to use rcu_dereference(), so that list_empty() can* be used anywhere you would want to use a list_empty_rcu().*/

获取节点对应的数据:list_entry_rcu

/*** list_entry_rcu - get the struct for this entry* @ptr:        the &struct list_head pointer.* @type:       the type of the struct this is embedded in.* @member:     the name of the list_head within the struct.** This primitive may safely run concurrently with the _rcu list-mutation* primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().*/
#define list_entry_rcu(ptr, type, member) \container_of(READ_ONCE(ptr), type, member)

遍历 list_for_each_entry_rcu:

/*** list_for_each_entry_rcu	-	iterate over rcu list of given type* @pos:	the type * to use as a loop cursor.* @head:	the head for your list.* @member:	the name of the list_head within the struct.* @cond:	optional lockdep expression if called from non-RCU protection.** This list-traversal primitive may safely run concurrently with* the _rcu list-mutation primitives such as list_add_rcu()* as long as the traversal is guarded by rcu_read_lock().*/
#define list_for_each_entry_rcu(pos, head, member, cond...)		\for (__list_check_rcu(dummy, ## cond, 0),			\pos = list_entry_rcu((head)->next, typeof(*pos), member);	\&pos->member != (head);					\pos = list_entry_rcu(pos->member.next, typeof(*pos), member))

链表综合实验代码

        测试内容包括添加、计算长度、遍历数据、删除节点、替换节点。最后在卸载函数中释放链表资源。

#include <linux/module.h>
#include <linux/init.h>
#include <linux/rculist.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>#define _DEBUG_INFO
#ifdef _DEBUG_INFO#define DEBUG_INFO(format,...)	\printk(KERN_ERR"%s:%d -- "format"\n",\__func__,__LINE__,##__VA_ARGS__)
#else#define DEBUG_INFO(format,...)
#endifstruct rcu_private_data{struct list_head list;
};struct my_list_node{struct list_head node;int number;
};static int list_size(struct rcu_private_data *p){struct my_list_node *pos;struct list_head *head = &p->list;int count = 0;if(list_empty(&p->list)){DEBUG_INFO("list is empty");return 0;}else{DEBUG_INFO("list is not empty");}list_for_each_entry_rcu(pos,head,node){count++;}return count;
}//遍历链表
void show_list_nodes(struct rcu_private_data *p){struct my_list_node *pos;struct list_head *head = &p->list;if(list_empty(&p->list)){DEBUG_INFO("list is empty");return;}else{DEBUG_INFO("list is not empty");}list_for_each_entry_rcu(pos,head,node){DEBUG_INFO("pos->number = %d",pos->number);}
}//清空链表
void del_list_nodes(struct rcu_private_data *p){struct my_list_node *pos;struct list_head *head = &p->list;if(list_empty(&p->list)){DEBUG_INFO("list is empty");return;}else{DEBUG_INFO("list is not empty");}list_for_each_entry_rcu(pos,head,node){DEBUG_INFO("pos->number = %d\n",pos->number);vfree(pos);}
}struct rcu_private_data *prpd;static int __init ch02_init(void){int i = 0;static struct my_list_node * new[6];struct rcu_private_data *p = (struct rcu_private_data*)vmalloc(sizeof(struct rcu_private_data));prpd = p;INIT_LIST_HEAD_RCU(&p->list);DEBUG_INFO("list_empty(&p->list) = %d",list_empty(&p->list));for(i = 0;i < 5;i++){new[i] = (struct my_list_node*)vmalloc(sizeof(struct my_list_node));INIT_LIST_HEAD_RCU(&new[i]->node);new[i]->number = i;list_add_rcu(&new[i]->node,&p->list);}DEBUG_INFO("list_size = %d",list_size(p));//添加后的结果,应该是5 4 3 2 1//遍历链表:show_list_nodes(p);//删除链表节点 new[3];list_del_rcu(&new[3]->node);vfree(new[3]);DEBUG_INFO("list_size = %d",list_size(p));//遍历链表:show_list_nodes(p);//替换一个链表节点new[5] = (struct my_list_node*)vmalloc(sizeof(struct my_list_node));INIT_LIST_HEAD_RCU(&new[5]->node);new[5]->number = i;list_replace_rcu(&new[1]->node,&new[5]->node);vfree(new[1]);//遍历链表:show_list_nodes(p);DEBUG_INFO("init");return 0;
}static void __exit ch02_exit(void){del_list_nodes(prpd);vfree(prpd);DEBUG_INFO("exit");
}module_init(ch02_init);
module_exit(ch02_exit);
MODULE_LICENSE("GPL");

测试

加载模块

卸载模块 

小结


http://www.mrgr.cn/p/43536880

相关文章

【分布鲁棒、状态估计】分布式鲁棒优化电力系统状态估计研究[几种算法进行比较](Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

VAE-根据李宏毅视频总结的最通俗理解

1.VAE的直观理解 先简单了解一下自编码器&#xff0c;也就是常说的Auto-Encoder。Auto-Encoder包括一个编码器&#xff08;Encoder&#xff09;和一个解码器&#xff08;Decoder&#xff09;。其结构如下&#xff1a; 自编码器是一种先把输入数据压缩为某种编码, 后仅通过该编…

Python - Opencv + pyzbar实时摄像头识别二维码

直接上代码&#xff1a; import cv2 from pyzbar.pyzbar import decodecap cv2.VideoCapture(0) # 打开摄像头while True: # 循环读取摄像头帧ret, frame cap.read()# 在循环中&#xff0c;将每一帧作为图像输入&#xff0c;使用pyzbar的decode()函数识别二维码barcodes …

np.bincount、np.digitize、np.unique、np.histogram、np.searchsorted

np.bincount 简介 np.bincount是统计数组中数字出现数量的函数&#xff0c;数值n在输入数组x中每出现1次&#xff0c;则输出o的o[n]1。 函数 官方文档 函数参数&#xff1a; x: 输入&#xff0c;1维非负数组weights: 权重数组, 可选参数&#xff0c;如果指定了这一参数&am…

函数指针数组

前面学习过数组 指针数组&#xff1a;用来存放数组指针&#xff08;地址&#xff09;的数组 int main() {int arr1[] { 0 };int arr2[] { 0 };int arr3[] { 0 };int* p[3] { arr1,arr2,arr3 };//指针数组return 0; }那么函数指针数组&#xff0c;就是用来存放几个类型相同…

PyTorch - GPU入门教程1

1. 安装GPU版本的PyTorch 登录PyTorch官网https://pytorch.org/&#xff0c;下载对应CUDA版本的PyTorch【不能直接pip install&#xff0c;否则安装上的是CPU版本的】 2. 查看GPU信息 &#xff08;1&#xff09;重要信息 !nvidia-smi我的GPU版本很垃圾&#xff0c;本blog仅…

GitHub上怎么寻找项目?

前言 下面由我精心整理的关于github项目资源搜索的一些方法&#xff0c;这些方法可以帮助你更快更精确的搜寻到你需要的符合你要求的项目。 写文章不易&#xff0c;如果这一篇问文章对你有帮助&#xff0c;求点赞求收藏~ 好&#xff0c;下面我们直接进入正题——> 首先我…

RS485或RS232转ETHERCAT连接ethercat转换器

最近&#xff0c;生产管理设备中经常会遇到两种协议不相同的情况&#xff0c;这严重阻碍了设备之间的通讯&#xff0c;串口设备的数据不能直接传输给ETHERCAT。这可怎么办呢&#xff1f; 别担心&#xff0c;捷米JM-ECT-RS485/232来了&#xff01;这是一款自主研发的ETHERCAT从站…

多线程案例 | 单例模式、阻塞队列、定时器、线程池

多线程案例 1、案例一&#xff1a;线程安全的单例模式 单例模式 单例模式是设计模式的一种 什么是设计模式&#xff1f; 设计模式好比象棋中的 “棋谱”&#xff0c;红方当头炮&#xff0c;黑方马来跳&#xff0c;针对红方的一些走法&#xff0c;黑方应招的时候有一些固定的…

scrcpy2.0+实时将手机画面显示在屏幕上并用鼠标模拟点击2023.7.26

想要用AI代打手游&#xff0c;除了模拟器登录&#xff0c;也可以直接使用第三方工具Scrcpy&#xff0c;来自github&#xff0c;它是一个开源的屏幕镜像工具&#xff0c;可以在电脑上显示Android设备的画面&#xff0c;并支持使用鼠标进行交互。 目录 1. 下载安装2. scrcpy的高级…

【Vue3+Ts+Vite】配置滚动条样式

一、先看效果 二、直接上代码 <template><div class"main-container"><h1 v-for"index in 50" :key"index">这是home页面</h1></div> </template> <style lang"scss" scoped> .main-conta…

Failed to load local font resource:微信小程序加载第三方字体

加载本地字体.ttf 将ttf转换为base64格式&#xff1a;https://transfonter.org/ 步骤如下 将下载后的stylesheet.css 里的font-family属性名字改一下&#xff0c;然后引进页面里就行了&#xff0c;全局样式就放app.scss&#xff0c;单页面就引入单页面 注&#xff1a; .title…

目标检测之3维合成

现在有一系列的图片&#xff0c;图片之间可以按照z轴方向进行排列。图片经过了目标检测&#xff0c;输出了一系列的检测框&#xff0c;现在的需求是将检测框按类别进行合成&#xff0c;以在3维上生成检测结果。 思路&#xff1a;将图片按照z轴方向排列&#xff0c;以z轴索引作…

web流程自动化详解

今天给大家带来Selenium的相关解释操作 一、Selenium Selenium是一个用于自动化Web浏览器操作的开源工具和框架。它提供了一组API&#xff08;应用程序接口&#xff09;&#xff0c;可以让开发人员使用多种编程语言&#xff08;如Java、Python、C#等&#xff09;编写测试脚本&…

掌握无人机遥感数据预处理的全链条理论与实践流程、典型农林植被性状的估算理论与实践方法、利用MATLAB进行编程实践(脚本与GUI开发)以及期刊论文插图制作等

目录 专题一 认识主被动无人机遥感数据 专题二 预处理无人机遥感数据 专题三 定量估算农林植被关键性状 专题四 期刊论文插图精细制作与Appdesigner应用开发 近地面无人机植被定量遥感与生理参数反演 更多推荐 遥感技术作为一种空间大数据手段&#xff0c;能够从多时、多…

使用wxPython和pillow开发拼图小游戏(四)

上一篇介绍了使用本地图片来初始化游戏的方法&#xff0c;通过前边三篇&#xff0c;该小游戏的主要内容差不多介绍完了&#xff0c;最后这一篇来介绍下游戏用时的计算、重置游戏和关闭窗口事件处理 游戏用时的计算 对于游戏用时的记录&#xff0c;看过前几篇的小伙伴可能也发现…

Flutter:flutter_local_notifications——消息推送的学习

前言 注&#xff1a; 刚开始学习&#xff0c;如果某些案例使用时遇到问题&#xff0c;可以自行百度、查看官方案例、官方github。 简介 Flutter Local Notifications是一个用于在Flutter应用程序中显示本地通知的插件。它提供了一个简单而强大的方法来在设备上发送通知&#…

flask 点赞系统

dianzan.html页面 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>点赞系统</title> </head> <body><h2>这是一个点赞系统</h2><table border"1"><…

数据分析-关于指标和指标体系

一、电商指标体系 二、指标体系的作用 三、统计学中基本的分析手段

Python+OpenCV实现自动扫雷,挑战扫雷世界记录!

目录 准备 - 扫雷软件 实现思路 - 01 窗体截取 - 02 雷块分割 - 03 雷块识别 - 04 扫雷算法实现 福利&#xff1a;文末有Python全套资料哦 我们一起来玩扫雷吧。用PythonOpenCV实现了自动扫雷&#xff0c;突破世界记录&#xff0c;我们先来看一下效果吧。 中级 - 0.74秒 …