共享/独占锁,也称为读/写锁。
- E
- N
- S
- Y
- MonitorMixin
类公共方法
new() 链接
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/concurrency/share_lock.rb, line 50 def initialize super() @cv = new_cond @sharing = Hash.new(0) @waiting = {} @sleeping = {} @exclusive_thread = nil @exclusive_depth = 0 end
实例公共方法
exclusive(purpose: nil, compatible: [], after_compatible: [], no_wait: false) 链接
在持有独占锁时执行提供的代码块。如果设置了 no_wait
且锁不可立即使用,则返回 nil
而不会生成。否则,返回代码块的结果。
有关其他选项,请参见 start_exclusive
。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/concurrency/share_lock.rb, line 148 def exclusive(purpose: nil, compatible: [], after_compatible: [], no_wait: false) if start_exclusive(purpose: purpose, compatible: compatible, no_wait: no_wait) begin yield ensure stop_exclusive(compatible: after_compatible) end end end
sharing() 链接
在持有共享锁时执行提供的代码块。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/concurrency/share_lock.rb, line 159 def sharing start_sharing begin yield ensure stop_sharing end end
start_exclusive(purpose: nil, compatible: [], no_wait: false) 链接
如果设置了 no_wait
且锁不可立即使用,则返回 false。否则,在获取锁后返回 true。
purpose
和 compatible
协同工作;当此线程正在等待独占锁时,它会将其共享(如果有)让渡给任何其他尝试,其 purpose
出现于此尝试的 compatible
列表中。这允许“松散”升级,由于较不严格,因此可以防止某些类别的死锁。
对于许多资源,松散升级就足够了:如果一个线程正在等待锁,则它不会运行任何其他代码。通过匹配 purpose
,可以仅让渡给其他线程,其活动不会干扰。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/concurrency/share_lock.rb, line 76 def start_exclusive(purpose: nil, compatible: [], no_wait: false) synchronize do unless @exclusive_thread == Thread.current if busy_for_exclusive?(purpose) return false if no_wait yield_shares(purpose: purpose, compatible: compatible, block_share: true) do wait_for(:start_exclusive) { busy_for_exclusive?(purpose) } end end @exclusive_thread = Thread.current end @exclusive_depth += 1 true end end
start_sharing() 链接
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/concurrency/share_lock.rb, line 114 def start_sharing synchronize do if @sharing[Thread.current] > 0 || @exclusive_thread == Thread.current # We already hold a lock; nothing to wait for elsif @waiting[Thread.current] # We're nested inside a +yield_shares+ call: we'll resume as # soon as there isn't an exclusive lock in our way wait_for(:start_sharing) { @exclusive_thread } else # This is an initial / outermost share call: any outstanding # requests for an exclusive lock get to go first wait_for(:start_sharing) { busy_for_sharing?(false) } end @sharing[Thread.current] += 1 end end
stop_exclusive(compatible: []) 链接
放弃独占锁。只能由调用 start_exclusive
的线程调用(且当前持有锁)。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/concurrency/share_lock.rb, line 96 def stop_exclusive(compatible: []) synchronize do raise "invalid unlock" if @exclusive_thread != Thread.current @exclusive_depth -= 1 if @exclusive_depth == 0 @exclusive_thread = nil if eligible_waiters?(compatible) yield_shares(compatible: compatible, block_share: true) do wait_for(:stop_exclusive) { @exclusive_thread || eligible_waiters?(compatible) } end end @cv.broadcast end end end
stop_sharing() 链接
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/concurrency/share_lock.rb, line 131 def stop_sharing synchronize do if @sharing[Thread.current] > 1 @sharing[Thread.current] -= 1 else @sharing.delete Thread.current @cv.broadcast end end end
yield_shares(purpose: nil, compatible: [], block_share: false) 链接
在执行所提供的代码块时暂时放弃所有持有的 Share 锁,允许任何 compatible
独占锁请求继续进行。
来源:显示 | 在 GitHub 上