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.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 133

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



159
160
161
162
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 159

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

#eof?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 190

def eof?
  write_io.closed?
end

#fork?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 178

def fork?
  @in_fork
end

#goodbyeObject



164
165
166
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 164

def goodbye
  read_io.close unless read_io.closed?
end

#read_ioObject



182
183
184
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 182

def read_io
  fork? ? @child_read : @parent_read
end

#recvObject



198
199
200
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 198

def recv
  recv_message(read_io)
end

#recv_message(io) ⇒ Object



173
174
175
176
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 173

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

#send(*msg) ⇒ Object



194
195
196
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 194

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

#send_message(io, msg) ⇒ Object



168
169
170
171
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 168

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

#waitObject



202
203
204
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 202

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

#write_ioObject



186
187
188
# File 'opal/lib/opal/builder_scheduler/prefork.rb', line 186

def write_io
  fork? ? @child_write : @parent_write
end