File: mutex.rb

package info (click to toggle)
ruby-rspec 3.5.0c3e0m0s0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,312 kB
  • ctags: 4,788
  • sloc: ruby: 62,572; sh: 785; makefile: 100
file content (73 lines) | stat: -rw-r--r-- 1,737 bytes parent folder | download | duplicates (4)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module RSpec
  module Support
    # 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
    # https://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
    # @private
    class Mutex
      def initialize
        @waiting = []
        @locked = false
        @waiting.taint
        taint
      end

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

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

      # @private
      def synchronize
        lock
        begin
          yield
        ensure
          unlock
        end
      end

    private

      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

      # Avoid warnings for library wide checks spec
    end unless defined?(::RSpec::Support::Mutex) || defined?(::Mutex)
  end
end