aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/dependencies/interlock.rb
blob: c3601bac13b9334f6b6ba19ef28a4f15d7f35a7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require 'active_support/concurrency/share_lock'

module ActiveSupport
  module Dependencies #:nodoc:
    class Interlock
      def initialize
        @lock = ActiveSupport::Concurrency::ShareLock.new(true)
      end

      def loading
        @lock.exclusive do
          yield
        end
      end

      def start_running
        @lock.start_sharing
      end

      def done_running
        @lock.stop_sharing
      end

      def running
        @lock.sharing do
          yield
        end
      end

      # Match the Mutex API, so we can be used by Rack::Lock
      alias :lock :start_running
      alias :unlock :done_running
    end
  end
end