Class: Farce::Envelope::Local

Inherits:
Farce::Envelope show all
Defined in:
lib/farce/envelope.rb

Overview

An envelope that keeps its contents local to the Ractor that created it. Can only be opened by the Ractor that created it.

Constant Summary

Constants inherited from Farce::Envelope

AlreadyClaimed

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Internal::Copyable

#duplicable?

Methods inherited from Farce::Envelope

#blank?, #claim!, #same_value?, #value

Methods included from Abstract::Value

#blank?, #unwrap, #value

Methods included from Shareable

#ractor_shareable?

Methods included from Internal::Noncopyable

#duplicable?

Constructor Details

#initialize(value) ⇒ Local

Returns a new instance of Local.

Parameters:

  • value (Object) —

    The value to wrap in the envelope.



161
162
163
164
165
# File 'lib/farce/envelope.rb', line 161

def initialize(value, auto_unwrap = nil)
  @owner = Ractor.current
  Internal::Storage.ractor[self] = value
  super
end

Class Method Details

.new(value) ⇒ Local, Share

Returns A new envelope wrapping the given value.

Returns:

  • (Local, Share) —

    A new envelope wrapping the given value.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/farce/envelope.rb', line 156

class Local < Farce::Envelope
  include Copyable

  # @overload initialize(value)
  #   @param value [Object] The value to wrap in the envelope.
  def initialize(value, auto_unwrap = nil)
    @owner = Ractor.current
    Internal::Storage.ractor[self] = value
    super
  end

  # @api private
  def marshal_dump
    raise TypeError, "local envelope belongs to another Ractor" unless owned?
    [1, Internal::Storage.ractor[self], auto_unwrap]
  end

  # @api private
  def marshal_load(data)
    value, manager = Internal::MarshalSupport.payload(data, 2)
    initialize(value, manager)
  end

  # (see Envelope#claim)
  def claim = owned? ? self : nil

  # (see Envelope#claimed?)
  def claimed? = true

  # (see Envelope#owned?)
  def owned? = @owner == Ractor.current

  private

  def initialize_copy(other)
    super
    other.claim!
    Internal::Storage.ractor[self] = other.value.dup
  end
end

Instance Method Details

#claim ⇒ Envelope?

Attempts to claim the envelope for the current Ractor.

Returns:

  • (Envelope, nil) —

    The envelope if the claim was successful, or nil if the envelope has already been claimed by another Ractor.



180
# File 'lib/farce/envelope.rb', line 180

def claim = owned? ? self : nil

#claimed? ⇒ Boolean

Returns true if the envelope has been claimed by any Ractor, false otherwise.

Returns:

  • (Boolean) —

    true if the envelope has been claimed by any Ractor, false otherwise.



183
# File 'lib/farce/envelope.rb', line 183

def claimed? = true

#owned? ⇒ Boolean

Returns true if the envelope has been claimed by the current Ractor, false otherwise.

Returns:

  • (Boolean) —

    true if the envelope has been claimed by the current Ractor, false otherwise.



186
# File 'lib/farce/envelope.rb', line 186

def owned? = @owner == Ractor.current