内核同步机制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; }
相关文章推荐
- 内核同步机制 - 读写锁 read_lock()/write_lock
- 内核同步机制API之up
- 内核同步机制API之write_seqcount_begin
- 内核同步机制API之down_xx
- Linux内核同步机制之二-----Read/Write spin lock
- Linux内核同步机制之(五):Read/Write spin lock
- 内核同步机制API之__down_common
- 内核中的同步机制(三)
- 内核同步机制-信号量/互斥锁/读-写信号量 sema ,mutex ,rwsem
- Linux内核同步机制之completion【转】
- Linux内核同步 - Read/Write spin lock
- 内核定时机制API之timer_setup
- Linux内核学习笔记七——内核同步机制和实现方式
- 内核定时机制API之mktime
- 并发基础 -- Linux 内核同步机制
- Linux 内核同步机制——互斥锁(转)
- Linux内核同步机制的自旋锁原理及综合应用实例
- Linux内核源码系列(二):探究内核基础层数据结构,同步机制的应用
- Linux内核的同步机制-自旋锁
- Linux内核学习笔记七——内核同步机制和实现方式