Class: LightIO::Core::IOloop
- Inherits:
-
Object
- Object
- LightIO::Core::IOloop
- Extended by:
- Forwardable
- Defined in:
- lib/lightio/core/ioloop.rb
Overview
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
Constant Summary collapse
- RAW_THREAD =
::Thread
Class Method Summary collapse
-
.current ⇒ Object
return current ioloop or create new one.
Instance Method Summary collapse
-
#initialize ⇒ IOloop
constructor
A new instance of IOloop.
- #transfer ⇒ Object
-
#wait(watcher) ⇒ Object
Wait a watcher, watcher can be a timer or socket.
Constructor Details
Class Method Details
.current ⇒ Object
return current ioloop or create new one
52 53 54 55 56 57 58 |
# File 'lib/lightio/core/ioloop.rb', line 52 def current key = :"lightio.ioloop" unless RAW_THREAD.current.thread_variable?(key) RAW_THREAD.current.thread_variable_set(key, IOloop.new) end RAW_THREAD.current.thread_variable_get(key) end |
Instance Method Details
#transfer ⇒ Object
46 47 48 |
# File 'lib/lightio/core/ioloop.rb', line 46 def transfer @fiber.transfer end |
#wait(watcher) ⇒ Object
Wait a watcher, watcher can be a timer or socket. see LightIO::Watchers module for detail
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/lightio/core/ioloop.rb', line 22 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 |