2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2017-06-30 01:00:04 -04:00
|
|
|
require_relative "../concurrency/share_lock"
|
2014-09-29 12:02:42 -04:00
|
|
|
|
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
|
2016-02-01 10:12:20 -05:00
|
|
|
@lock.exclusive(purpose: :load, compatible: [:load], after_compatible: [:load]) do
|
2014-09-29 12:02:42 -04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-19 18:39:21 -04:00
|
|
|
def unloading
|
2016-02-01 10:12:20 -05:00
|
|
|
@lock.exclusive(purpose: :unload, compatible: [:load, :unload], after_compatible: [:load, :unload]) do
|
2015-07-19 18:39:21 -04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-21 20:25:52 -05:00
|
|
|
def start_unloading
|
|
|
|
@lock.start_exclusive(purpose: :unload, compatible: [:load, :unload])
|
|
|
|
end
|
|
|
|
|
|
|
|
def done_unloading
|
|
|
|
@lock.stop_exclusive(compatible: [:load, :unload])
|
2014-09-29 12:16:07 -04:00
|
|
|
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
|
2016-02-06 16:54:57 -05:00
|
|
|
|
|
|
|
def permit_concurrent_loads
|
|
|
|
@lock.yield_shares(compatible: [:load]) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2016-06-10 05:55:40 -04:00
|
|
|
|
|
|
|
def raw_state(&block) # :nodoc:
|
|
|
|
@lock.raw_state(&block)
|
|
|
|
end
|
2014-09-29 12:02:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|