Class: Farce::Port
- Inherits:
-
Ractor::Port
- Object
- Ractor::Port
- Farce::Port
- Includes:
- Abstract::Port
- Defined in:
- lib/farce/port.rb,
lib/farce/port.rb
Overview
Ractor::Port subclass with additional features, namely ownership tracking and
mode based sending.
Instance Attribute Summary collapse
-
#mode ⇒ Symbol
readonly
The port's default mode.
Instance Method Summary collapse
-
#auto_local? ⇒ Boolean
Whether or not auto_local is enabled by default.
-
#initialize(mode: :copy, auto_local: false) ⇒ BasicObject
constructor
Creates a new port with the given default mode.
-
#inspect ⇒ String
A string representation of the port, including its class name and mode.
-
#receive(timeout: nil) ⇒ BasicObject?
Waits for a message or the timeout.
-
#send(message, move: nil, mode: nil, auto_local: nil) ⇒ self
Sends a message through the port.
Methods included from Abstract::Port
#<<, #close, #closed?, #owned?, #pop, #push
Methods included from Shareable
Methods included from Internal::Noncopyable
Methods inherited from Ractor::Port
Constructor Details
#initialize(mode: :copy, auto_local: false) ⇒ BasicObject
Creates a new port with the given default mode.
Valid modes are:
:copy- The value will be copied between Ractors. This is the default mode.:make_shareable- The value will be made Ractor-shareable using Ractor.make_shareable.:move- The value will be moved between Ractors. This saves memory compared to copying, and supports values that can't be copied but moved (like IO objects). However, the value will no longer be accessible on the Ractor that pushed it.:mutable- A Mutable instance will be created for the value. This isn't done recursively and thus will fail for nested unshareable values.:local- The value will be kept local to the Ractor that pushed it. Another ractor trying to receive it will get an error. Useful for usage contained within a single Ractor.:proxy- The value will be wrapped in aFarce::Proxythat executes calls in the original Ractor.:raise- An error will be raised if the value is not Ractor-shareable. Useful for enforcing shareability.:dedup- The value will be deduplicated using Farce.dedup, then made Ractor-shareable. This may update and freeze the original. Already-shareable values pass through unchanged.:shareable_copy- The value will be copied and the copy will be made Ractor-shareable.
|
|
# File 'lib/farce/port.rb', line 57
|
Instance Attribute Details
#mode ⇒ Symbol (readonly)
The port's default mode. Set via #initialize.
72 |
# File 'lib/farce/port.rb', line 72 def mode = self.class.mode |
Instance Method Details
#auto_local? ⇒ Boolean
Whether or not auto_local is enabled by default. Set via #initialize.
77 |
# File 'lib/farce/port.rb', line 77 def auto_local? = self.class.auto_local? |
#inspect ⇒ String
Returns A string representation of the port, including its class name and mode.
132 |
# File 'lib/farce/port.rb', line 132 def inspect = super.sub(/\A#<.+? (?=(?:to|id):#?\d+)/, "#<Farce::Port mode:#{mode} ") |
#receive(timeout: nil) ⇒ BasicObject?
Waits for a message or the timeout. Only the owning Ractor can receive. Automatically opens envelopes created by this port's mode manager.
125 126 127 128 129 |
# File 'lib/farce/port.rb', line 125 def receive(timeout: nil) # Omit the absent keyword instead of allocating arguments for super. result = timeout.nil? ? super() : super MANAGER.unwrap(result) end |
#send(message, move: nil, mode: nil, auto_local: nil) ⇒ self
Sends a message through the port.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/farce/port.rb', line 99 def send(, move: nil, mode: nil, auto_local: nil) return super() if Ractor.shareable?() raise Ractor::ClosedError, "port is closed" if closed? auto_local = auto_local? if auto_local.nil? if auto_local && owned? mode = :local elsif mode.nil? mode = self.mode case move when true then mode = :move when false then mode = :copy if mode == :move when nil # no-op else raise ArgumentError, "invalid move: #{move.inspect}" end end case mode when :copy then super() when :move then super(, move: true) else super(MANAGER.wrap(, mode:)) end end |