Sha256: e7a1811c3c0d7d3227bb8ee70b9ba30475332bdc1a7e3f679c79b61e5bd4f245

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

require 'modulation/gem'

export_default :Polyphony

require 'fiber'
require_relative './gyro_ext'

Thread.event_selector = Gyro::Selector
Thread.current.setup_fiber_scheduling

import './polyphony/extensions/core'
import './polyphony/extensions/thread'
import './polyphony/extensions/fiber'
import './polyphony/extensions/io'

# Main Polyphony API
module Polyphony
  GlobalAPI = import './polyphony/core/global_api'
  ::Object.include GlobalAPI

  exceptions = import './polyphony/core/exceptions'
  Cancel    = exceptions::Cancel
  MoveOn    = exceptions::MoveOn
  Restart   = exceptions::Restart
  Terminate = exceptions::Terminate

  Net = import './polyphony/net'

  auto_import(
    Channel:      './polyphony/core/channel',
    FS:           './polyphony/adapters/fs',
    ResourcePool: './polyphony/core/resource_pool',
    Sync:         './polyphony/core/sync',
    ThreadPool:   './polyphony/core/thread_pool',
    Throttler:    './polyphony/core/throttler',
    Trace:        './polyphony/adapters/trace'
  )

  class << self
    def wait_for_signal(sig)
      fiber = Fiber.current
      Gyro.ref
      old_trap = trap(sig) do
        Gyro.unref
        fiber.schedule(sig)
        trap(sig, old_trap)
      end
      suspend
    end

    def fork(&block)
      pid = Kernel.fork do
        Gyro.post_fork
        Fiber.current.setup_main_fiber
        block.()
      ensure
        Fiber.current.terminate_all_children
        Fiber.current.await_all_children
      end
      pid
    end
  end
end

# install signal handlers

def install_terminating_signal_handler(signal, exception_class)
  trap(signal) do
    exception = exception_class.new
    Thread.current.break_out_of_ev_loop(Thread.current.main_fiber, exception)
  end
end

install_terminating_signal_handler('SIGTERM', SystemExit)
install_terminating_signal_handler('SIGINT', Interrupt)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polyphony-0.33 lib/polyphony.rb