class LightIO::Core::IOloop

IOloop like a per-threaded EventMachine (cause fiber cannot resume cross threads)

IOloop handle io waiting and schedule beams, user do not supposed to directly use this class

Constants

THREAD_PROXY

Public Class Methods

current() click to toggle source

return current ioloop or create new one

# File lib/lightio/core/ioloop.rb, line 55
def current
  key = :"lightio.ioloop"
  current_thread = THREAD_PROXY.send(:current)
  unless THREAD_PROXY.instance_send(current_thread, :thread_variable?, key)
    THREAD_PROXY.instance_send(current_thread, :thread_variable_set, key, IOloop.new)
  end
  THREAD_PROXY.instance_send(current_thread, :thread_variable_get, key)
end
new() click to toggle source
# File lib/lightio/core/ioloop.rb, line 10
def initialize
  @fiber = Fiber.new {run}
  @backend = Backend::NIO.new
end

Public Instance Methods

transfer() click to toggle source
# File lib/lightio/core/ioloop.rb, line 45
def transfer
  @fiber.transfer
end
wait(watcher) click to toggle source

Wait a watcher, watcher can be a timer or socket. see LightIO::Watchers module for detail

# File lib/lightio/core/ioloop.rb, line 21
def wait(watcher)
  future = Future.new
  # add watcher to loop
  id = Object.new
  watcher.set_callback {|err| future.transfer([id, err])}
  watcher.start(self)
  # trigger a fiber switch
  # wait until watcher is ok
  # then do work
  response_id, err = future.value
  current_beam = LightIO::Core::Beam.current
  if response_id != id
    raise LightIO::InvalidTransferError, "expect #{id}, but get #{response_id}"
  elsif err
    # if future return a err
    # simulate Thread#raise to Beam , that we can shutdown beam blocking by socket accepting
    # transfer back to which beam occur this err
    # not sure this is a right way to do it
    current_beam.raise(err) if current_beam.is_a?(LightIO::Core::Beam)
  end
  # check beam error after wait
  current_beam.send(:check_and_raise_error) if current_beam.is_a?(LightIO::Core::Beam)
end