module Polyphony

Polyphony API

Constants

TP_EVENTS
VERSION

Attributes

original_pid[RW]

Public Class Methods

backend_accept(p1, p2) click to toggle source
VALUE Polyphony_backend_accept(VALUE self, VALUE server_socket, VALUE socket_class) {
  return Backend_accept(BACKEND(), server_socket, socket_class);
}
backend_accept_loop(p1, p2) click to toggle source
VALUE Polyphony_backend_accept_loop(VALUE self, VALUE server_socket, VALUE socket_class) {
  return Backend_accept_loop(BACKEND(), server_socket, socket_class);
}
backend_connect(p1, p2, p3) click to toggle source
VALUE Polyphony_backend_connect(VALUE self, VALUE io, VALUE addr, VALUE port) {
  return Backend_connect(BACKEND(), io, addr, port);
}
backend_feed_loop(p1, p2, p3) click to toggle source
VALUE Polyphony_backend_feed_loop(VALUE self, VALUE io, VALUE receiver, VALUE method) {
  return Backend_feed_loop(BACKEND(), io, receiver, method);
}
backend_read(p1, p2, p3, p4, p5) click to toggle source
VALUE Polyphony_backend_read(VALUE self, VALUE io, VALUE str, VALUE length, VALUE to_eof, VALUE pos) {
  return Backend_read(BACKEND(), io, str, length, to_eof, pos);
}
backend_read_loop(p1, p2) click to toggle source
VALUE Polyphony_backend_read_loop(VALUE self, VALUE io, VALUE maxlen) {
  return Backend_read_loop(BACKEND(), io, maxlen);
}
backend_recv(p1, p2, p3, p4) click to toggle source
VALUE Polyphony_backend_recv(VALUE self, VALUE io, VALUE str, VALUE length, VALUE pos) {
  return Backend_recv(BACKEND(), io, str, length, pos);
}
backend_recv_feed_loop(p1, p2, p3) click to toggle source
VALUE Polyphony_backend_recv_feed_loop(VALUE self, VALUE io, VALUE receiver, VALUE method) {
  return Backend_recv_feed_loop(BACKEND(), io, receiver, method);
}
backend_recv_loop(p1, p2) click to toggle source
VALUE Polyphony_backend_recv_loop(VALUE self, VALUE io, VALUE maxlen) {
  return Backend_recv_loop(BACKEND(), io, maxlen);
}
backend_send(p1, p2, p3) click to toggle source
VALUE Polyphony_backend_send(VALUE self, VALUE io, VALUE msg, VALUE flags) {
  return Backend_send(BACKEND(), io, msg, flags);
}
backend_sendv(p1, p2, p3) click to toggle source
VALUE Polyphony_backend_sendv(VALUE self, VALUE io, VALUE ary, VALUE flags) {
  return Backend_sendv(BACKEND(), io, ary, flags);
}
backend_sleep(p1) click to toggle source
VALUE Polyphony_backend_sleep(VALUE self, VALUE duration) {
  return Backend_sleep(BACKEND(), duration);
}
backend_splice(p1, p2, p3) click to toggle source
VALUE Polyphony_backend_splice(VALUE self, VALUE src, VALUE dest, VALUE maxlen) {
  return Backend_splice(BACKEND(), src, dest, maxlen);
}
backend_splice_to_eof(p1, p2, p3) click to toggle source
VALUE Polyphony_backend_splice_to_eof(VALUE self, VALUE src, VALUE dest, VALUE chunksize) {
  return Backend_splice_to_eof(BACKEND(), src, dest, chunksize);
}
backend_timeout(*args) click to toggle source
VALUE Polyphony_backend_timeout(int argc,VALUE *argv, VALUE self) {
  return Backend_timeout(argc, argv, BACKEND());
}
backend_timer_loop(p1) click to toggle source
VALUE Polyphony_backend_timer_loop(VALUE self, VALUE interval) {
  return Backend_timer_loop(BACKEND(), interval);
}
backend_wait_event(p1) click to toggle source
VALUE Polyphony_backend_wait_event(VALUE self, VALUE raise) {
  return Backend_wait_event(BACKEND(), raise);
}
backend_wait_io(p1, p2) click to toggle source
VALUE Polyphony_backend_wait_io(VALUE self, VALUE io, VALUE write) {
  return Backend_wait_io(BACKEND(), io, write);
}
backend_waitpid(p1) click to toggle source
VALUE Polyphony_backend_waitpid(VALUE self, VALUE pid) {
  return Backend_waitpid(BACKEND(), pid);
}
backend_write(*args) click to toggle source
VALUE Polyphony_backend_write(int argc, VALUE *argv, VALUE self) {
  return Backend_write_m(argc, argv, BACKEND());
}
exit_forked_process() click to toggle source
# File lib/polyphony.rb, line 64
def exit_forked_process
  terminate_threads
  Fiber.current.shutdown_all_children

  # Since fork could be called from any fiber, we explicitly call exit here.
  # Otherwise, the fiber might want to pass execution to another fiber that
  # previously transferred execution to the forking fiber, but doesn't exist
  # anymore...
  exit
end
fork(&block) click to toggle source
# File lib/polyphony.rb, line 24
def fork(&block)
  Kernel.fork do
    # A race condition can arise if a TERM or INT signal is received before
    # the forked process has finished initializing. To prevent this we restore
    # the default signal handlers, and then reinstall the custom Polyphony
    # handlers just before running the given block.
    trap('SIGTERM', 'DEFAULT')
    trap('SIGINT', 'DEFAULT')

    # Since the fiber doing the fork will become the main fiber of the
    # forked process, we leave it behind by transferring to a new fiber
    # created in the context of the forked process, which rescues *all*
    # exceptions, including Interrupt and SystemExit.
    spin_forked_block(&block).transfer
  end
end
install_at_exit_handler() click to toggle source
# File lib/polyphony.rb, line 97
def install_at_exit_handler
  @original_pid = ::Process.pid

  # This at_exit handler is needed only when the original process exits. Due to
  # the behaviour of fibers on fork (and especially on exit from forked
  # processes,) we use a separate mechanism to terminate fibers in forked
  # processes (see Polyphony.fork).
  at_exit do
    next unless @original_pid == ::Process.pid

    Polyphony.terminate_threads
    Fiber.current.shutdown_all_children
  end
end
install_terminating_signal_handlers() click to toggle source
# File lib/polyphony.rb, line 79
def install_terminating_signal_handlers
  trap('SIGTERM') { raise SystemExit }
  orig_trap('SIGINT') do
    orig_trap('SIGINT') { exit! }
    Fiber.schedule_priority_oob_fiber { raise Interrupt }
  end
end
run_forked_block(&block) click to toggle source
# File lib/polyphony.rb, line 54
def run_forked_block(&block)
  Thread.current.setup
  Fiber.current.setup_main_fiber
  Thread.current.backend.post_fork

  install_terminating_signal_handlers

  block.()
end
spin_forked_block(&block) click to toggle source
# File lib/polyphony.rb, line 41
def spin_forked_block(&block)
  Fiber.new do
    run_forked_block(&block)
  rescue SystemExit
    # fall through to ensure
  rescue Exception => e
    STDERR << e.full_message
    exit!
  ensure
    exit_forked_process
  end
end
start_debug_server(socket_path) click to toggle source
# File lib/polyphony/debugger.rb, line 14
def self.start_debug_server(socket_path)
  server = DebugServer.new(socket_path)
  controller = DebugController.new(server)      
  trace = TracePoint.new(*TP_EVENTS) { |tp| controller.handle_tp(trace, tp) }
  trace.enable

  at_exit do
    Kernel.trace "program terminated"
    trace.disable
    server.stop
  end
end
terminate_threads() click to toggle source
# File lib/polyphony.rb, line 87
def terminate_threads
  threads = Thread.list - [Thread.current]
  return if threads.empty?

  threads.each(&:kill)
  threads.each(&:join)
end
watch_process(cmd = nil, &block) click to toggle source
# File lib/polyphony.rb, line 75
def watch_process(cmd = nil, &block)
  Polyphony::Process.watch(cmd, &block)
end

Public Instance Methods

connection_pool_default_options() click to toggle source
# File lib/polyphony/adapters/sequel.rb, line 41
def connection_pool_default_options
  { pool_class: FiberConnectionPool }
end