Class: RSpec::Support::Mutex

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/support/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 formating changes are made to appease the robot overlord:

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

Instance Method Summary collapse

Constructor Details

#initializeMutex

Returns a new instance of Mutex.



16
17
18
19
20
21
# File 'lib/rspec/support/mutex.rb', line 16

def initialize
  @waiting = []
  @locked = false
  @waiting.taint
  taint
end

Instance Method Details

#lockObject



24
25
26
27
28
29
30
31
32
# File 'lib/rspec/support/mutex.rb', line 24

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

#synchronizeObject



44
45
46
47
48
49
50
51
# File 'lib/rspec/support/mutex.rb', line 44

def synchronize
  lock
  begin
    yield
  ensure
    unlock
  end
end

#unlockObject



35
36
37
38
39
40
41
# File 'lib/rspec/support/mutex.rb', line 35

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