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
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb
svix-0.53.1 vendor/bundle/ruby/2.7.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb
svix-0.53.0 vendor/bundle/ruby/2.7.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rspec-support-3.10.0/lib/rspec/support/reentrant_mutex.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rspec-support-3.10.0/lib/rspec/support/reentrant_mutex.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rspec-support-3.10.0/lib/rspec/support/reentrant_mutex.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rspec-support-3.10.0/lib/rspec/support/reentrant_mutex.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rspec-support-3.10.0/lib/rspec/support/reentrant_mutex.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rspec-support-3.10.0/lib/rspec/support/reentrant_mutex.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb
ory-kratos-client-0.8.0.alpha2 vendor/bundle/ruby/2.5.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb
ory-client-0.0.1.alpha21 vendor/bundle/ruby/2.5.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb
ory-keto-client-0.7.0.alpha1 vendor/bundle/ruby/2.5.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb
ory-keto-client-0.7.0.alpha0 vendor/bundle/ruby/2.5.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb
ory-kratos-client-0.7.6.alpha7 vendor/bundle/ruby/2.5.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb
ory-kratos-client-0.7.6.alpha6 vendor/bundle/ruby/2.5.0/gems/rspec-support-3.10.2/lib/rspec/support/reentrant_mutex.rb