2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 11:58:50 -04:00
|
|
|
require "thread"
|
|
|
|
require "monitor"
|
2014-09-29 12:02:42 -04:00
|
|
|
|
|
|
|
module ActiveSupport
|
|
|
|
module Concurrency
|
2015-07-08 15:03:14 -04:00
|
|
|
# A share/exclusive lock, otherwise known as a read/write lock.
|
|
|
|
#
|
|
|
|
# https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock
|
2014-09-29 12:02:42 -04:00
|
|
|
class ShareLock
|
|
|
|
include MonitorMixin
|
|
|
|
|
|
|
|
# We track Thread objects, instead of just using counters, because
|
|
|
|
# we need exclusive locks to be reentrant, and we need to be able
|
|
|
|
# to upgrade share locks to exclusive.
|
|
|
|
|
2016-06-10 05:55:40 -04:00
|
|
|
def raw_state # :nodoc:
|
|
|
|
synchronize do
|
|
|
|
threads = @sleeping.keys | @sharing.keys | @waiting.keys
|
|
|
|
threads |= [@exclusive_thread] if @exclusive_thread
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
|
|
|
|
threads.each do |thread|
|
|
|
|
purpose, compatible = @waiting[thread]
|
|
|
|
|
|
|
|
data[thread] = {
|
|
|
|
thread: thread,
|
|
|
|
sharing: @sharing[thread],
|
|
|
|
exclusive: @exclusive_thread == thread,
|
|
|
|
purpose: purpose,
|
|
|
|
compatible: compatible,
|
|
|
|
waiting: !!@waiting[thread],
|
|
|
|
sleeper: @sleeping[thread],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# NB: Yields while holding our *internal* synchronize lock,
|
|
|
|
# which is supposed to be used only for a few instructions at
|
|
|
|
# a time. This allows the caller to inspect additional state
|
|
|
|
# without things changing out from underneath, but would have
|
|
|
|
# disastrous effects upon normal operation. Fortunately, this
|
|
|
|
# method is only intended to be called when things have
|
|
|
|
# already gone wrong.
|
|
|
|
yield data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-19 18:39:21 -04:00
|
|
|
def initialize
|
2014-09-29 12:02:42 -04:00
|
|
|
super()
|
|
|
|
|
|
|
|
@cv = new_cond
|
|
|
|
|
|
|
|
@sharing = Hash.new(0)
|
2015-07-19 18:39:21 -04:00
|
|
|
@waiting = {}
|
2016-06-10 05:55:40 -04:00
|
|
|
@sleeping = {}
|
2014-09-29 12:02:42 -04:00
|
|
|
@exclusive_thread = nil
|
|
|
|
@exclusive_depth = 0
|
|
|
|
end
|
|
|
|
|
2015-07-19 18:39:21 -04:00
|
|
|
# Returns false if +no_wait+ is set and the lock is not
|
2015-07-08 15:03:14 -04:00
|
|
|
# immediately available. Otherwise, returns true after the lock
|
|
|
|
# has been acquired.
|
2015-07-19 18:39:21 -04:00
|
|
|
#
|
|
|
|
# +purpose+ and +compatible+ work together; while this thread is
|
|
|
|
# waiting for the exclusive lock, it will yield its share (if any)
|
|
|
|
# to any other attempt whose +purpose+ appears in this attempt's
|
|
|
|
# +compatible+ list. This allows a "loose" upgrade, which, being
|
|
|
|
# less strict, prevents some classes of deadlocks.
|
|
|
|
#
|
|
|
|
# For many resources, loose upgrades are sufficient: if a thread
|
|
|
|
# is awaiting a lock, it is not running any other code. With
|
|
|
|
# +purpose+ matching, it is possible to yield only to other
|
|
|
|
# threads whose activity will not interfere.
|
|
|
|
def start_exclusive(purpose: nil, compatible: [], no_wait: false)
|
2014-09-29 12:02:42 -04:00
|
|
|
synchronize do
|
|
|
|
unless @exclusive_thread == Thread.current
|
2016-02-01 08:11:07 -05:00
|
|
|
if busy_for_exclusive?(purpose)
|
2015-07-19 18:39:21 -04:00
|
|
|
return false if no_wait
|
2014-09-29 12:02:42 -04:00
|
|
|
|
2016-02-07 13:52:56 -05:00
|
|
|
yield_shares(purpose: purpose, compatible: compatible, block_share: true) do
|
2016-06-10 05:55:40 -04:00
|
|
|
wait_for(:start_exclusive) { busy_for_exclusive?(purpose) }
|
2015-07-20 22:18:21 -04:00
|
|
|
end
|
2015-07-19 18:39:21 -04:00
|
|
|
end
|
2014-09-29 12:02:42 -04:00
|
|
|
@exclusive_thread = Thread.current
|
|
|
|
end
|
|
|
|
@exclusive_depth += 1
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-08 15:03:14 -04:00
|
|
|
# Relinquish the exclusive lock. Must only be called by the thread
|
|
|
|
# that called start_exclusive (and currently holds the lock).
|
2016-02-01 10:12:20 -05:00
|
|
|
def stop_exclusive(compatible: [])
|
2014-09-29 12:02:42 -04:00
|
|
|
synchronize do
|
|
|
|
raise "invalid unlock" if @exclusive_thread != Thread.current
|
|
|
|
|
|
|
|
@exclusive_depth -= 1
|
|
|
|
if @exclusive_depth == 0
|
|
|
|
@exclusive_thread = nil
|
2016-02-01 10:12:20 -05:00
|
|
|
|
2016-02-07 09:06:21 -05:00
|
|
|
if eligible_waiters?(compatible)
|
2016-02-07 13:52:56 -05:00
|
|
|
yield_shares(compatible: compatible, block_share: true) do
|
2016-06-10 05:55:40 -04:00
|
|
|
wait_for(:stop_exclusive) { @exclusive_thread || eligible_waiters?(compatible) }
|
2016-02-07 09:06:21 -05:00
|
|
|
end
|
2016-02-01 10:12:20 -05:00
|
|
|
end
|
2016-02-06 16:54:57 -05:00
|
|
|
@cv.broadcast
|
2014-09-29 12:02:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-07 13:52:56 -05:00
|
|
|
def start_sharing
|
2014-09-29 12:02:42 -04:00
|
|
|
synchronize do
|
2016-02-07 09:06:21 -05:00
|
|
|
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
|
2016-06-10 05:55:40 -04:00
|
|
|
wait_for(:start_sharing) { @exclusive_thread }
|
2016-02-07 09:06:21 -05:00
|
|
|
else
|
|
|
|
# This is an initial / outermost share call: any outstanding
|
|
|
|
# requests for an exclusive lock get to go first
|
2016-06-10 05:55:40 -04:00
|
|
|
wait_for(:start_sharing) { busy_for_sharing?(false) }
|
2014-09-29 12:02:42 -04:00
|
|
|
end
|
|
|
|
@sharing[Thread.current] += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_sharing
|
|
|
|
synchronize do
|
|
|
|
if @sharing[Thread.current] > 1
|
|
|
|
@sharing[Thread.current] -= 1
|
|
|
|
else
|
|
|
|
@sharing.delete Thread.current
|
|
|
|
@cv.broadcast
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-08 15:03:14 -04:00
|
|
|
# Execute the supplied block while holding the Exclusive lock. If
|
|
|
|
# +no_wait+ is set and the lock is not immediately available,
|
|
|
|
# returns +nil+ without yielding. Otherwise, returns the result of
|
|
|
|
# the block.
|
2015-07-19 18:39:21 -04:00
|
|
|
#
|
|
|
|
# See +start_exclusive+ for other options.
|
2016-02-01 10:12:20 -05:00
|
|
|
def exclusive(purpose: nil, compatible: [], after_compatible: [], no_wait: false)
|
2015-07-19 18:39:21 -04:00
|
|
|
if start_exclusive(purpose: purpose, compatible: compatible, no_wait: no_wait)
|
2014-09-29 12:02:42 -04:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
2016-02-01 10:12:20 -05:00
|
|
|
stop_exclusive(compatible: after_compatible)
|
2014-09-29 12:02:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-08 15:03:14 -04:00
|
|
|
# Execute the supplied block while holding the Share lock.
|
2014-09-29 12:02:42 -04:00
|
|
|
def sharing
|
|
|
|
start_sharing
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
stop_sharing
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-07 09:06:21 -05:00
|
|
|
# Temporarily give up all held Share locks while executing the
|
|
|
|
# supplied block, allowing any +compatible+ exclusive lock request
|
|
|
|
# to proceed.
|
2016-02-07 13:52:56 -05:00
|
|
|
def yield_shares(purpose: nil, compatible: [], block_share: false)
|
2016-02-06 16:54:57 -05:00
|
|
|
loose_shares = previous_wait = nil
|
|
|
|
synchronize do
|
|
|
|
if loose_shares = @sharing.delete(Thread.current)
|
|
|
|
if previous_wait = @waiting[Thread.current]
|
|
|
|
purpose = nil unless purpose == previous_wait[0]
|
|
|
|
compatible &= previous_wait[1]
|
|
|
|
end
|
2016-02-07 13:52:56 -05:00
|
|
|
compatible |= [false] unless block_share
|
2016-02-06 16:54:57 -05:00
|
|
|
@waiting[Thread.current] = [purpose, compatible]
|
2016-02-07 09:06:21 -05:00
|
|
|
end
|
2016-04-24 12:56:34 -04:00
|
|
|
|
|
|
|
@cv.broadcast
|
2016-02-06 16:54:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
synchronize do
|
2016-06-10 05:55:40 -04:00
|
|
|
wait_for(:yield_shares) { @exclusive_thread && @exclusive_thread != Thread.current }
|
2016-02-06 16:54:57 -05:00
|
|
|
|
|
|
|
if previous_wait
|
|
|
|
@waiting[Thread.current] = previous_wait
|
|
|
|
else
|
|
|
|
@waiting.delete Thread.current
|
|
|
|
end
|
|
|
|
@sharing[Thread.current] = loose_shares if loose_shares
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-29 12:02:42 -04:00
|
|
|
private
|
2016-09-14 04:57:52 -04:00
|
|
|
# Must be called within synchronize
|
2016-08-06 13:55:02 -04:00
|
|
|
def busy_for_exclusive?(purpose)
|
|
|
|
busy_for_sharing?(purpose) ||
|
|
|
|
@sharing.size > (@sharing[Thread.current] > 0 ? 1 : 0)
|
|
|
|
end
|
2016-02-01 08:11:07 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def busy_for_sharing?(purpose)
|
|
|
|
(@exclusive_thread && @exclusive_thread != Thread.current) ||
|
|
|
|
@waiting.any? { |t, (_, c)| t != Thread.current && !c.include?(purpose) }
|
|
|
|
end
|
2016-02-01 10:12:20 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def eligible_waiters?(compatible)
|
|
|
|
@waiting.any? { |t, (p, _)| compatible.include?(p) && @waiting.all? { |t2, (_, c2)| t == t2 || c2.include?(p) } }
|
|
|
|
end
|
2016-06-10 05:55:40 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def wait_for(method)
|
|
|
|
@sleeping[Thread.current] = method
|
|
|
|
@cv.wait_while { yield }
|
|
|
|
ensure
|
|
|
|
@sleeping.delete Thread.current
|
|
|
|
end
|
2014-09-29 12:02:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|