Class: Farce::Strict::Exchanger

Inherits:
Abstract::Exchanger show all
Includes:
Farce::Shareable::Unfreezable
Defined in:
lib/farce/strict/exchanger.rb

Overview

A Ractor-shareable rendezvous that exchanges shareable values directly. Values retain their identity and envelopes are returned unopened.

Instance Method Summary collapse

Methods included from Farce::Shareable

#ractor_shareable?

Methods included from Internal::Noncopyable

#duplicable?

Constructor Details

#initialize ⇒ Exchanger

Returns a new instance of Exchanger.



12
13
14
15
# File 'lib/farce/strict/exchanger.rb', line 12

def initialize
  @exchanger = Internal::Exchanger.new
  super
end

Instance Method Details

#exchange(offered, timeout: nil) { ... } ⇒ BasicObject?

Wait for a partner and return the partner's value. Offering nil allows a caller to receive a value without sending a payload. A timeout of zero only exchanges with a partner that is already waiting. A successful exchange of nil does not invoke the fallback.

Parameters:

  • offered (BasicObject, nil) —

    the value to give to the partner

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

    the maximum seconds to wait, or nil to wait indefinitely

Yields:

  • called without arguments when the timeout expires

Returns:

  • (BasicObject, nil) —

    the partner's value, or the fallback result or nil on timeout

Raises:

  • (ArgumentError) —

    if the timeout is negative or not finite

  • (Ractor::IsolationError) —

    if the offered value is not Ractor-shareable



19
20
21
22
# File 'lib/farce/strict/exchanger.rb', line 19

def exchange(offered, timeout: nil, &)
  raise Ractor::IsolationError, "value must be Ractor-shareable" unless Ractor.shareable?(offered)
  super
end