File: threadsafe_test.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; javascript: 1,114; ansic: 288; makefile: 10; sh: 6
file content (72 lines) | stat: -rw-r--r-- 1,747 bytes parent folder | download | duplicates (3)
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
module Concurrent
  module ThreadSafe
    module Test

      THREADS = (RUBY_ENGINE == 'ruby' ? 100 : 10)

      class Barrier
        def initialize(count = 1)
          @count = count
          @mutex = Mutex.new
          @cond  = ConditionVariable.new
        end

        def release
          @mutex.synchronize do
            @count -= 1 if @count > 0
            @cond.broadcast if @count.zero?
          end
        end

        def await
          @mutex.synchronize do
            if @count.zero? # fall through
            elsif @count > 0
              @count -= 1
              @count.zero? ? @cond.broadcast : @cond.wait(@mutex)
            end
          end
        end
      end

      class HashCollisionKey
        attr_reader :hash, :key
        def initialize(key, hash = key.hash % 3)
          @key  = key
          @hash = hash
        end

        def eql?(other)
          other.kind_of?(self.class) && @key.eql?(other.key)
        end

        def even?
          @key.even?
        end

        def <=>(other)
          @key <=> other.key
        end
      end

      # having 4 separate HCK classes helps for a more thorough CHMV8 testing
      class HashCollisionKey2 < HashCollisionKey; end
      class HashCollisionKeyNoCompare < HashCollisionKey
        def <=>(other)
          0
        end
      end
      class HashCollisionKey4 < HashCollisionKeyNoCompare; end

      HASH_COLLISION_CLASSES = [HashCollisionKey, HashCollisionKey2, HashCollisionKeyNoCompare, HashCollisionKey4]

      def self.HashCollisionKey(key, hash = key.hash % 3)
        HASH_COLLISION_CLASSES[rand(4)].new(key, hash)
      end

      class HashCollisionKeyNonComparable < HashCollisionKey
        undef <=>
      end
    end
  end
end