2014-09-29 12:02:42 -04:00
|
|
|
require 'active_support/concurrency/share_lock'
|
|
|
|
|
2015-07-08 15:03:14 -04:00
|
|
|
module ActiveSupport #:nodoc:
|
2014-09-29 12:02:42 -04:00
|
|
|
module Dependencies #:nodoc:
|
|
|
|
class Interlock
|
2015-07-08 15:03:14 -04:00
|
|
|
def initialize # :nodoc:
|
2015-07-19 18:39:21 -04:00
|
|
|
@lock = ActiveSupport::Concurrency::ShareLock.new
|
2014-09-29 12:02:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def loading
|
2015-07-19 18:39:21 -04:00
|
|
|
@lock.exclusive(purpose: :load, compatible: [:load]) do
|
2014-09-29 12:02:42 -04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-19 18:39:21 -04:00
|
|
|
def unloading
|
|
|
|
@lock.exclusive(purpose: :unload, compatible: [:load, :unload]) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Attempt to obtain an "unloading" (exclusive) lock. If possible,
|
2014-09-29 12:16:07 -04:00
|
|
|
# execute the supplied block while holding the lock. If there is
|
|
|
|
# concurrent activity, return immediately (without executing the
|
|
|
|
# block) instead of waiting.
|
2015-07-19 18:39:21 -04:00
|
|
|
def attempt_unloading
|
|
|
|
@lock.exclusive(purpose: :unload, compatible: [:load, :unload], no_wait: true) do
|
2014-09-29 12:16:07 -04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-29 12:02:42 -04:00
|
|
|
def start_running
|
|
|
|
@lock.start_sharing
|
|
|
|
end
|
|
|
|
|
|
|
|
def done_running
|
|
|
|
@lock.stop_sharing
|
|
|
|
end
|
|
|
|
|
|
|
|
def running
|
|
|
|
@lock.sharing do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|