Sha256: aeb6c4b56d3927bfa339ebdc564c538b33ecd47b2f01d256391addd972854d0d

Contents?: true

Size: 1.57 KB

Versions: 10

Compression:

Stored size: 1.57 KB

Contents

require 'thread'

module ThreadSafe
  module Test
    class Latch
      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
          @cond.wait @mutex if @count > 0
        end
      end
    end

    class Barrier < Latch
      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_COLLISION_CLASSES[rand(4)].new(key)
    end
  end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
thread_safe-0.1.3 test/test_helper.rb
thread_safe-0.1.3-java test/test_helper.rb
thread_safe-ianunruh-0.1.1 test/test_helper.rb
thread_safe-ianunruh-0.1.1-java test/test_helper.rb
thread_safe-0.1.2 test/test_helper.rb
thread_safe-0.1.2-java test/test_helper.rb
thread_safe-0.1.1 test/test_helper.rb
thread_safe-0.1.1-java test/test_helper.rb
challah-1.0.0 vendor/bundle/gems/thread_safe-0.1.0/test/test_helper.rb
thread_safe-0.1.0 test/test_helper.rb