Linux内核同步机制
Atomic Operations(原子操作)
Spin Locks(自旋锁)
只在多处理器的情况下有效。
APIs: spin_lock_init()
spin_lock()
spin_unlock()
Read/Write Spin Locks()
在被rwlock保护的临界区,允许有多个读操作,但只允许一个写操作。而且读写操作的优先级是一样的,即是说,写操作也必须在当前的读操作完成之后才能进行。
rwlock 结构体的 lock 成员在初始话是被置为 0x01000000 (bit24 置1) 。 状态转移图如下:
0x01000000 (初始值) ---> 0x00000000 (写操作, bit 24 clear)
---> 0x00ffffff (读操作,只有一个reader)
---> 0x00ffffff (读操作,两个reader,以此类推)
Seqlocks
和 rwlock 类似, 但是 写操作比读操作有更高的优先级。 因此,写操作的延时可以降低。但是,读操作可以需要被执行多次。
Read-Copy Update(RCU) http://lse.sourceforge.net/locking/rcupdate.html
* Classic RCU (CONFIG_CLASSIC_RCU)
Sleeping is forbidden in reader side critical sections.
* srcu (Sleepable RCU)
The primary challenge in designing an SRCU is to prevent any
given task sleeping in an RCU read-side critical section from
preventing an unbounded number of RCU callbacks.
* qrcu (quick srcu)
The current srcu implementation is very good for readers,
lock/unlock are extremely cheap. But for that reason it is not
possible to avoid synchronize_sched() and polling in
synchronize_srcu().
'qrcu' behaves the same as srcu but optimized for writers. The
fast path for synchronize_qrcu() is mutex_lock() + atomic_read()
+ mutex_unlock(). The slow path is __wait_event(), no
polling. However, the reader does atomic inc/dec on lock/unlock,
and the counters are not per-cpu.
Also, unlike srcu, qrcu read lock/unlock can be used in interrupt
context, and 'qrcu_struct' can be compile-time initialized.
* Tree RCU (CONFIG_TREE_RCU)
* Preempt RCU (CONFIG_PREEMPT_RCU)
* rcu torture (CONFIG_RCU_TORTURE_TEST)
RCU test module.
Create RCU reader and writer threads to test RCU.
* Source code
rcupdate.c : common code, provide synchronize_rcu()
阅读(638) | 评论(0) | 转发(0) |