2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2017-10-21 09:11:29 -04:00
|
|
|
require "active_support/concurrency/share_lock"
|
2014-09-29 12:02:42 -04:00
|
|
|
|
2021-07-29 17:18:07 -04:00
|
|
|
module ActiveSupport # :nodoc:
|
|
|
|
module Dependencies # :nodoc:
|
2014-09-29 12:02:42 -04:00
|
|
|
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
|
|
|
|
|
2021-09-05 10:29:32 -04:00
|
|
|
def loading(&block)
|
|
|
|
@lock.exclusive(purpose: :load, compatible: [:load], after_compatible: [:load], &block)
|
2014-09-29 12:02:42 -04:00
|
|
|
end
|
|
|
|
|
2021-09-05 10:29:32 -04:00
|
|
|
def unloading(&block)
|
|
|
|
@lock.exclusive(purpose: :unload, compatible: [:load, :unload], after_compatible: [:load, :unload], &block)
|
2015-07-19 18:39:21 -04:00
|
|
|
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
|
|
|
|
|
2021-09-05 10:29:32 -04:00
|
|
|
def running(&block)
|
|
|
|
@lock.sharing(&block)
|
2014-09-29 12:02:42 -04:00
|
|
|
end
|
2016-02-06 16:54:57 -05:00
|
|
|
|
2021-09-05 10:29:32 -04:00
|
|
|
def permit_concurrent_loads(&block)
|
|
|
|
@lock.yield_shares(compatible: [:load], &block)
|
2016-02-06 16:54:57 -05:00
|
|
|
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
|