Class: ForkBreak::Process

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

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug = false, &block) ⇒ Process

Returns a new instance of Process.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fork_break.rb', line 17

def initialize(debug = false, &block)
  @debug = debug
  @fork = Fork.new(:return, :to_fork, :from_fork) do |child_fork|
    self.class.breakpoint_setter = breakpoints = BreakpointSetter.new(child_fork, debug)

    breakpoints << :forkbreak_start
    block.call(breakpoints)
    breakpoints << :forkbreak_end

    self.class.breakpoint_setter = nil
  end
end

Class Attribute Details

.breakpoint_setterObject

Returns the value of attribute breakpoint_setter.



14
15
16
# File 'lib/fork_break.rb', line 14

def breakpoint_setter
  @breakpoint_setter
end

Instance Method Details

#finishObject



50
51
52
# File 'lib/fork_break.rb', line 50

def finish
  run_until(:forkbreak_end)
end

#run_until(breakpoint) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/fork_break.rb', line 30

def run_until(breakpoint)
  @next_breakpoint = breakpoint
  @fork.execute unless @fork.pid
  puts "Parent is sending object #{breakpoint} to #{@fork.pid}" if @debug
  @fork.send_object(breakpoint)
  self
end

#waitObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fork_break.rb', line 38

def wait
  loop do
    brk = @fork.receive_object
    puts "Parent is receiving object #{brk} from #{@fork.pid}" if @debug
    if brk == @next_breakpoint
      return self
    elsif brk == :forkbreak_end
      raise BreakpointNotReachedError.new("Never reached breakpoint #{@next_breakpoint.inspect}")
    end
  end
end