Class: Farce::Envelope::Copy

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

Overview

An envelope that copies its contents. Can be opened by multiple Ractors. The value will be copied once when the envelope is created, and then once per Ractor that opens the envelope.

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) ⇒ Copy

Returns a new instance of Copy.

Parameters:

  • value (Object) —

    The value to wrap in the envelope.



63
64
65
66
67
# File 'lib/farce/envelope.rb', line 63

def initialize(value, auto_unwrap = nil)
  @vault = VAULT_ATOM.store_if_absent { Internal::Vault.new }
  super
  @vault.copy_in(self, value)
end

Class Method Details

.new(value) ⇒ Copy, Share

Returns A new envelope wrapping the given value.

Returns:

  • (Copy, Share) —

    A new envelope wrapping the given value.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/farce/envelope.rb', line 58

class Copy < Farce::Envelope
  include Copyable

  # @overload initialize(value)
  #   @param [Object] value The value to wrap in the envelope.
  def initialize(value, auto_unwrap = nil)
    @vault = VAULT_ATOM.store_if_absent { Internal::Vault.new }
    super
    @vault.copy_in(self, value)
  end

  # @api private
  def marshal_dump
    storage = Internal::Storage.ractor
    [1, storage.key?(self) ? storage[self] : retrieve, auto_unwrap]
  end

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

  # (see Envelope#claim)
  def claim = self

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

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

  protected

  def retrieve = @vault.copy_out(vault_key)

  private

  def initialize_dup(other)
    value      = other.retrieve
    @vault_key = Object.new.freeze
    super
    @vault.copy_in(@vault_key, value)
  end

  def initialize_clone(other, freeze: nil)
    value      = other.retrieve
    @vault_key = Object.new.freeze
    super
    @vault.copy_in(@vault_key, value)
  end

  def vault_key = defined?(@vault_key) ? @vault_key : self
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.



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

def claim = self

#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.



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

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.



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

def owned? = true