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.



207
208
209
210
211
# File 'lib/farce/envelope.rb', line 207

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.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/farce/envelope.rb', line 202

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.



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

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.



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

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.



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

def owned? = @owner == Ractor.current