Class: ShadowLink::Shadow

Inherits:
Object
  • Object
show all
Defined in:
lib/shadow_link/shadow.rb

Overview

Shadow is a class that manages the resources required to use ShadowLink from other Ractors. It provides methods to shadowing processes and manage objects between original and shadow. ShadowLink manages resources in units of the Shadow.

Instance Method Summary collapse

Constructor Details

#initialize(threads:, make_shareables:) ⇒ Shadow

Returns a new instance of Shadow.



8
9
10
11
# File 'lib/shadow_link/shadow.rb', line 8

def initialize(threads:, make_shareables:)
  @threads = threads
  @make_shareables = make_shareables.freeze
end

Instance Method Details

#closeObject



23
24
25
26
27
28
29
30
31
# File 'lib/shadow_link/shadow.rb', line 23

def close
  raise 'Shadow is not illuminated' unless @port

  @port.close
  @thread_pool.each(&:exit)
  @object_map = nil

  self
end

#scoop(object_id) ⇒ Object



48
49
50
# File 'lib/shadow_link/shadow.rb', line 48

def scoop(object_id)
  @object_map[object_id].original
end

#seek(shadow) ⇒ Object



44
45
46
# File 'lib/shadow_link/shadow.rb', line 44

def seek(shadow)
  scoop(shadow.instance_variable_get(:@object_id))
end

#sink(original) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/shadow_link/shadow.rb', line 33

def sink(original)
  raise 'Shadow is not illuminated' unless @port

  return original if Ractor.shareable?(original)
  return Ractor.make_shareable(original) if ShadowLink.make_shareable?(original, make_shareables: @make_shareables)

  object_id = original.object_id
  mirror = @object_map[object_id] ||= Mirror.new(original, ShadowLink.new(@port, object_id, make_shareables: @make_shareables))
  mirror.shadow
end

#startObject



13
14
15
16
17
18
19
20
21
# File 'lib/shadow_link/shadow.rb', line 13

def start
  raise 'Shadow is already illuminated' if @port

  @port = Ractor::Port.new
  @object_map = {}
  @thread_pool = @threads.times.map { Thread.new { observe } }

  self
end