RCU框架分析
Read-Copy-Update的基本思想是在多读者少写者的情况下,读者可以无负担读取数据,读者与读者可以并行,读者与写者可以并行,从而提高系统的实时性。写者首先复制旧的数据,然后修改,然后用新数据替换旧的数据,最后再删除旧的数据。
对于一个受RCU保护的资源,能够删除旧数据的状态称为静止状态(quiecstate);等待读者全部退出的时间段称为grace period。对于一个线程释放所有持有的受RCU保护的资源时,称线程进入静止状态。 Linux2.6 RCU实现 1 主要数据结构
1. rcu_ctrlblk 全局一个变量
struct rcu_ctrlblk {
long cur; /* Current batch number. */
long completed; /* Number of the last completed batch */
int next_pending; /* Is the next batch already waiting? */
int signaled;
spinlock_t lock ____cacheline_internodealigned_in_smp;
cpumask_t cpumask; /* CPUs that need to switch in order */
/* for current batch to proceed. */
}
rcu_data 每个cpu一个变量
struct rcu_data {
/* 1) quiescent state handling : */
long quiescbatch; /* Batch # for grace period */
int passed_quiesc; /* User-mode/idle loop etc. */
int qs_pending; /* core waits for quiesc state */
/* 2) batch handling */
long batch; /* Batch # for current RCU batch */
struct rcu_head *nxtlist;
struct rcu_head **nxttail;
long qlen; /* # of queued callbacks */
struct rcu_head *curlist;
struct rcu_head **curtail;
struct rcu_head *donelist;
struct rcu_head **donetail;
long blimit; /* Upper limit on a processed batch */
int cpu;
struct rcu_head barrier;
};
if (!rcp->next_pending) { //如果下一个batch没有等待标志,
/* and start it/schedule start if it's a new batch */
spin_lock(&rcp->lock);
rcp->next_pending = 1;
rcu_start_batch(rcp);
spin_unlock(&rcp->lock);
}
}