Class: Opal::BuilderScheduler::Prefork::Fork

Inherits:
Object
  • Object
show all
Defined in:
opal/lib/opal/builder_scheduler/prefork.rb

Instance Method Summary collapse

Constructor Details

#initialize(forkset) ⇒ Fork

Returns a new instance of Fork.



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
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 80

def initialize(forkset)
  @parent_read, @child_write = IO.pipe
  @child_read, @parent_write = IO.pipe
  @forkset = forkset
  @in_fork = false

  @pid = fork do
    @in_fork = true

    begin
      @parent_read.close
      @parent_write.close

      yield(self)
    rescue => error
      send(:exception, error)
    ensure
      send(:close) unless write_io.closed?
      @child_write.close
    end
  end

  @child_read.close
  @child_write.close
end

Instance Method Details

#closeObject



106
107
108
109
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 106

def close
  send(:close)
  @parent_write.close
end

#eof?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 137

def eof?
  write_io.closed?
end

#fork?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 125

def fork?
  @in_fork
end

#goodbyeObject



111
112
113
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 111

def goodbye
  read_io.close unless read_io.closed?
end

#read_ioObject



129
130
131
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 129

def read_io
  fork? ? @child_read : @parent_read
end

#recvObject



145
146
147
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 145

def recv
  recv_message(read_io)
end

#recv_message(io) ⇒ Object



120
121
122
123
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 120

def recv_message(io)
  length, = *io.read(8).unpack('Q')
  Marshal.load(io.read(length)) # rubocop:disable Security/MarshalLoad
end

#send(*msg) ⇒ Object



141
142
143
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 141

def send(*msg)
  send_message(write_io, msg)
end

#send_message(io, msg) ⇒ Object



115
116
117
118
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 115

def send_message(io, msg)
  msg = Marshal.dump(msg)
  io.write([msg.length].pack('Q') + msg)
end

#waitObject



149
150
151
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 149

def wait
  Process.waitpid(@pid, Process::WNOHANG)
end

#write_ioObject



133
134
135
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 133

def write_io
  fork? ? @child_write : @parent_write
end