liburcu 库
今天在看sheepdog源代码的时候,发现它的log系统使用了liburcu这个库里面的函数,详细调研了一下。
读取拷贝更新,意思就是说,在要进行update的时候,先保留旧数据,然后替换新数据;再等待旧数据无人使用后,删除旧数据。从它的思想可以看到它写入数据的开销比较大,因为要等待所有读取线程都不在使用旧数据为止。这种结构比较适合用在很多线程读,但只有少量写的地方。
Linux核心提供了基于RCU的锁函数。但在用户态下,需要自己调用其他库,比如liburcu这个库。
liburcu is a LGPLv2.1 userspace RCU (read-copy-update) library. This data synchronization library provides read-side access which scales linearly with the number of cores. It does so by allowing multiples copies of a given data structure to live at the same time, and by monitoring the data structure accesses to detect grace periods after which memory reclamation is possible.
liburcu-cds provides efficient data structures based on RCU and lock-free algorithms. Those structures include hash tables, queues, stacks, and doubly-linked lists.
等详细看完sheepdog如何使用这些库的时候,再更新这个博客.
这个文档只有在0.7.5版本的doc目录下有,rcu-api.txt uatomic-api.txt
介绍RCU:
http://lwn.net/Articles/262464/
liburcu库:
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步