123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Support::Mutex Private

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Mutex
Instance Chain:
self, Mutex
Inherits: Mutex
  • Object
Defined in: rspec-support/lib/rspec/support/mutex.rb,
rspec-support/lib/rspec/support/reentrant_mutex.rb

Overview

On 1.8.7, it’s in the stdlib. We don’t want to load the stdlib, b/c this is a test tool, and can affect the test environment, causing tests to pass where they should fail.

So we’re transcribing/modifying it from github.com/ruby/ruby/blob/v1_8_7_374/lib/thread.rb#L56 Some methods we don’t need are deleted. Anything I don’t understand (there’s quite a bit, actually) is left in.

Some formatting changes are made to appease the robot overlord:

https://travis-ci.org/rspec/rspec-core/jobs/54410874

Constant Summary

Class Method Summary

  • .new constructor Internal use only

    See additional method definition at file rspec-support/lib/rspec/support/mutex.rb line 18.

Instance Method Summary

Instance Method Details

#lock

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/mutex.rb', line 26

def lock
  while Thread.critical = true && @locked
    @waiting.push Thread.current
    Thread.stop
  end
  @locked = true
  Thread.critical = false
  self
end

#synchronize

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/mutex.rb', line 46

def synchronize
  lock
  begin
    yield
  ensure
    unlock
  end
end

#unlock

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/mutex.rb', line 37

def unlock
  return unless @locked
  Thread.critical = true
  @locked = false
  wakeup_and_run_waiting_thread
  self
end

#wakeup_and_run_waiting_thread (private)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/mutex.rb', line 57

def wakeup_and_run_waiting_thread
  begin
    t = @waiting.shift
    t.wakeup if t
  rescue ThreadError
    retry
  end
  Thread.critical = false
  begin
    t.run if t
  rescue ThreadError
    :noop
  end
end