您的位置:首页 > 其它

内核同步机制API之read_seqbegin

2018-02-28 08:19 309 查看
seqlock所示一种写优先的锁。其分为读顺序锁和写顺序锁。但是写优先。
其读顺序锁的例程如下:
static inline int grow_chain32(struct ufs_inode_info *ufsi,
struct buffer_head *bh, __fs32 *v,
Indirect *from, Indirect *to)
{
Indirect *p;
unsigned seq;
to->bh = bh;
do {
seq = read_seqbegin(&ufsi->meta_lock);
to->key32 = *(__fs32 *)(to->p = v);
for (p = from; p <= to && p->key32 == *(__fs32 *)p->p; p++)
;
} while (read_seqretry(&ufsi->meta_lock, seq));
return (p > to);
}
其源码分析如下:
可以看到这个函数没实现为内联函数
static inline unsigned read_seqbegin(const seqlock_t *sl)
{
return read_seqcount_begin(&sl->seqcount);
}
static inline unsigned read_seqcount_begin(const seqcount_t *s)
{
#没有定义CONFIG_DEBUG_LOCK_ALLOC的时候为空函数
seqcount_lockdep_reader_access(s);
return raw_read_seqcount_begin(s);
}
static inline unsigned raw_read_seqcount_begin(const seqcount_t *s)
{
#开始一个seq-read的critical section,without barrier
unsigned ret = __read_seqcount_begin(s);
#内存barrier
smp_rmb();
return ret;
}

static inline unsigned __read_seqcount_begin(const seqcount_t *s)
{
unsigned ret;

repeat:
#这里尝试从读取s->sequence的值,如果其返回为1的话,则说明可能正在写,则读尝试继续读取。
#从这里可以很明显的看到顺序锁是写优先的。因为如果读获取不到锁的话,会一直等待,直到写释放锁.
ret = READ_ONCE(s->sequence);
if (unlikely(ret & 1)) {
cpu_relax();
goto repeat;
}
return ret;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: