Sha256: b0ffbf872d0b38d8aba1826402dc1a5b5fdbeceaf23a73f58d5418818fe19ed5

Contents?: true

Size: 1.49 KB

Versions: 177

Compression:

Stored size: 1.49 KB

Contents

module RSpec
  module Support
    # Allows a thread to lock out other threads from a critical section of code,
    # while allowing the thread with the lock to reenter that section.
    #
    # Based on Monitor as of 2.2 -
    # https://github.com/ruby/ruby/blob/eb7ddaa3a47bf48045d26c72eb0f263a53524ebc/lib/monitor.rb#L9
    #
    # Depends on Mutex, but Mutex is only available as part of core since 1.9.1:
    #   exists - http://ruby-doc.org/core-1.9.1/Mutex.html
    #   dne    - http://ruby-doc.org/core-1.9.0/Mutex.html
    #
    # @private
    class ReentrantMutex
      def initialize
        @owner = nil
        @count = 0
        @mutex = Mutex.new
      end

      def synchronize
        enter
        yield
      ensure
        exit
      end

    private

      def enter
        @mutex.lock if @owner != Thread.current
        @owner = Thread.current
        @count += 1
      end

      def exit
        @count -= 1
        return unless @count == 0
        @owner = nil
        @mutex.unlock
      end
    end

    if defined? ::Mutex
      # On 1.9 and up, this is in core, so we just use the real one
      class Mutex < ::Mutex
        # If you mock Mutex.new you break our usage of Mutex, so
        # instead we capture the original method to return Mutexs.
        NEW_MUTEX_METHOD = Mutex.method(:new)

        def self.new
          NEW_MUTEX_METHOD.call
        end
      end
    else # For 1.8.7
      # :nocov:
      RSpec::Support.require_rspec_support "mutex"
      # :nocov:
    end
  end
end

Version data entries

177 entries across 132 versions & 29 rubygems

Version Path
cloudsmith-api-0.51.93 vendor/bundle/ruby/2.3.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
yoshioka_gem-0.1.0 vendor/bundle/ruby/2.7.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
coingecko_client-0.1.1 vendor/bundle/ruby/2.7.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
coingecko_client-0.1.0 vendor/bundle/ruby/2.7.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
logstash-output-scalyr-0.1.5 vendor/bundle/jruby/2.5.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
cloudsmith-api-0.51.38 vendor/bundle/ruby/2.6.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
cloudsmith-api-0.51.37 vendor/bundle/ruby/2.6.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
logstash-output-scalyr-0.1.4 vendor/bundle/jruby/2.5.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
cloudsmith-api-0.51.34 vendor/bundle/ruby/2.6.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
cloudsmith-api-0.51.22 vendor/bundle/ruby/2.6.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
logstash-output-scalyr-0.1.3 vendor/bundle/jruby/2.5.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
tdiary-5.1.2 vendor/bundle/ruby/2.7.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
logstash-output-scalyr-0.1.2 vendor/bundle/jruby/2.5.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
rentdynamics-0.2.0 .bundle/ruby/2.6.0/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
rentdynamics-0.2.0 .gems/gems/rspec-support-3.9.3/lib/rspec/support/reentrant_mutex.rb
rspec-support-3.9.3 lib/rspec/support/reentrant_mutex.rb