Sha256: 1e552f8a96dd1a68c48a5acc73962e988a7a926e1ba7c4b1bbc3ceaf1abb3367

Contents?: true

Size: 1.89 KB

Versions: 21

Compression:

Stored size: 1.89 KB

Contents

module Concurrent

  # A synchronization point at which threads can pair and swap elements within
  # pairs. Each thread presents some object on entry to the exchange method,
  # matches with a partner thread, and receives its partner's object on return.
  #
  # Uses `MVar` to manage synchronization of the individual elements.
  # Since `MVar` is also a `Dereferenceable`, the exchanged values support all
  # dereferenceable options. The constructor options hash will be passed to
  # the `MVar` constructors.
  # 
  # @see Concurrent::MVar
  # @see Concurrent::Concern::Dereferenceable
  # @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Exchanger.html java.util.concurrent.Exchanger
  #
  # @!macro edge_warning
  class Exchanger

    EMPTY = Object.new

    # Create a new `Exchanger` object.
    #
    # @param [Hash] opts the options controlling how the managed references
    #   will be processed
    def initialize(opts = {})
      @first = MVar.new(EMPTY, opts)
      @second = MVar.new(MVar::EMPTY, opts)
    end

    # Waits for another thread to arrive at this exchange point (unless the
    # current thread is interrupted), and then transfers the given object to
    # it, receiving its object in return.
    #
    # @param [Object] value the value to exchange with an other thread
    # @param [Numeric] timeout the maximum time in second to wait for one other
    #   thread. nil (default value) means no timeout
    # @return [Object] the value exchanged by the other thread; nil if timed out
    def exchange(value, timeout = nil)
      first = @first.take(timeout)
      if first == MVar::TIMEOUT
        nil
      elsif first == EMPTY
        @first.put value
        second = @second.take timeout
        if second == MVar::TIMEOUT
          nil
        else
          second
        end
      else
        @first.put EMPTY
        @second.put value
        first
      end
    end

  end
end

Version data entries

21 entries across 19 versions & 5 rubygems

Version Path
logstash-filter-zabbix-0.1.2 vendor/bundle/jruby/1.9/gems/concurrent-ruby-0.9.2-java/lib/concurrent/exchanger.rb
logstash-filter-zabbix-0.1.1 vendor/bundle/jruby/1.9/gems/concurrent-ruby-0.9.2-java/lib/concurrent/exchanger.rb
ivanvc-logstash-input-s3-3.1.1.4 vendor/local/gems/concurrent-ruby-0.9.2-java/lib/concurrent/exchanger.rb
ivanvc-logstash-input-s3-3.1.1.3 vendor/local/gems/concurrent-ruby-0.9.2-java/lib/concurrent/exchanger.rb
ivanvc-logstash-input-s3-3.1.1.2 vendor/local/gems/concurrent-ruby-0.9.2-java/lib/concurrent/exchanger.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/concurrent-ruby-0.9.2-java/lib/concurrent/exchanger.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/logstash-codec-json-2.0.3/vendor/gems/concurrent-ruby-0.9.1-java/lib/concurrent/exchanger.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/concurrent-ruby-0.9.1-java/lib/concurrent/exchanger.rb
logstash-codec-json-2.0.3 vendor/gems/concurrent-ruby-0.9.1-java/lib/concurrent/exchanger.rb
concurrent-ruby-0.9.2-java lib/concurrent/exchanger.rb
concurrent-ruby-0.9.2 lib/concurrent/exchanger.rb
logstash-input-beats-0.9.2 vendor/jruby/1.9/gems/concurrent-ruby-0.9.1-java/lib/concurrent/exchanger.rb
logstash-input-beats-0.9.1 vendor/jruby/1.9/gems/concurrent-ruby-0.9.1-java/lib/concurrent/exchanger.rb
concurrent-ruby-0.9.1 lib/concurrent/exchanger.rb
concurrent-ruby-0.9.1-java lib/concurrent/exchanger.rb
concurrent-ruby-0.9.0 lib/concurrent/exchanger.rb
concurrent-ruby-0.9.0-java lib/concurrent/exchanger.rb
concurrent-ruby-0.9.0.pre3-java lib/concurrent/exchanger.rb
concurrent-ruby-0.9.0.pre3 lib/concurrent/exchanger.rb
concurrent-ruby-0.9.0.pre2 lib/concurrent/exchanger.rb