Sha256: 4abe3c40b36b4e42419af2c09fb314f59efb94170ef1cc4800fd398f79837445

Contents?: true

Size: 1.66 KB

Versions: 104

Compression:

Stored size: 1.66 KB

Contents

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 = key.hash % 3)
      HASH_COLLISION_CLASSES[rand(4)].new(key, hash)
    end

    class HashCollisionKeyNonComparable < HashCollisionKey
      undef <=>
    end
  end
end

Version data entries

104 entries across 98 versions & 30 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/thread_safe-0.3.6/spec/support/threadsafe_test.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/thread_safe-0.3.6/spec/support/threadsafe_test.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/thread_safe-0.3.6/spec/support/threadsafe_test.rb
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/thread_safe-0.3.6/spec/support/threadsafe_test.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/thread_safe-0.3.6/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/thread_safe-0.3.6/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/spec/support/threadsafe_test.rb