123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::AbstractExchanger

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Concurrent::Synchronization::Object
Defined in: lib/concurrent-ruby/concurrent/exchanger.rb

Constant Summary

Class Attribute Summary

Class Method Summary

Synchronization::Object - Inherited

.atomic_attribute?, .atomic_attributes,
.attr_atomic

Creates methods for reading and writing to a instance variable with volatile (Java) semantic as .attr_volatile does.

.attr_volatile

Creates methods for reading and writing (as attr_accessor does) to a instance variable with volatile (Java) semantic.

.ensure_safe_initialization_when_final_fields_are_present

For testing purposes, quite slow.

.new

Has to be called by children.

.safe_initialization!, .define_initialize_atomic_fields

Synchronization::AbstractObject - Inherited

Instance Method Summary

Synchronization::Object - Inherited

Synchronization::Volatile - Included

Synchronization::AbstractObject - Inherited

Constructor Details

.newAbstractExchanger

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/exchanger.rb', line 44

def initialize
  super
end

Instance Method Details

#do_exchange(value, timeout) ⇒ Object, CANCEL (private)

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. The timeout value indicates the approximate number of seconds the method should block while waiting for the exchange. When the timeout value is nil the method will block indefinitely.

Parameters:

  • value (Object)

    the value to exchange with another thread

  • timeout (Numeric, nil)

    in seconds, nil blocks indefinitely

Returns:

Raises:

  • (NotImplementedError)
[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/exchanger.rb', line 122

def do_exchange(value, timeout)
  raise NotImplementedError
end

#exchange(value, timeout = nil) ⇒ Object

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. The timeout value indicates the approximate number of seconds the method should block while waiting for the exchange. When the timeout value is nil the method will block indefinitely.

In some edge cases when a timeout is given a return value of nil may be ambiguous. Specifically, if nil is a valid value in the exchange it will be impossible to tell whether nil is the actual return value or if it signifies timeout. When nil is a valid value in the exchange consider using #exchange! or #try_exchange instead.

Parameters:

  • value (Object)

    the value to exchange with another thread

  • timeout (Numeric, nil) (defaults to: nil)

    in seconds, nil blocks indefinitely

Returns:

  • (Object)

    the value exchanged by the other thread or nil on timeout

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/exchanger.rb', line 69

def exchange(value, timeout = nil)
  (value = do_exchange(value, timeout)) == CANCEL ? nil : value
end

#exchange!(value, timeout = nil) ⇒ Object

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. The timeout value indicates the approximate number of seconds the method should block while waiting for the exchange. When the timeout value is nil the method will block indefinitely.

On timeout a TimeoutError exception will be raised.

Parameters:

  • value (Object)

    the value to exchange with another thread

  • timeout (Numeric, nil) (defaults to: nil)

    in seconds, nil blocks indefinitely

Returns:

  • (Object)

    the value exchanged by the other thread

Raises:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/exchanger.rb', line 80

def exchange!(value, timeout = nil)
  if (value = do_exchange(value, timeout)) == CANCEL
    raise Concurrent::TimeoutError
  else
    value
  end
end

#try_exchange(value, timeout = nil) ⇒ Concurrent::Maybe

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. The timeout value indicates the approximate number of seconds the method should block while waiting for the exchange. When the timeout value is nil the method will block indefinitely.

The return value will be a Maybe set to Just on success or Nothing on timeout.

Examples:

exchanger = Concurrent::Exchanger.new

result = exchanger.exchange(:foo, 0.5)

if result.just?
  puts result.value #=> :bar
else
  puts 'timeout'
end

Parameters:

  • value (Object)

    the value to exchange with another thread

  • timeout (Numeric, nil) (defaults to: nil)

    in seconds, nil blocks indefinitely

Returns:

  • (Concurrent::Maybe)

    on success a Just maybe will be returned with the item exchanged by the other thread as ‘#value`; on timeout a Nothing maybe will be returned with TimeoutError as #reason

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/exchanger.rb', line 109

def try_exchange(value, timeout = nil)
  if (value = do_exchange(value, timeout)) == CANCEL
    Concurrent::Maybe.nothing(Concurrent::TimeoutError)
  else
    Concurrent::Maybe.just(value)
  end
end