Module: Rbgo::CoRun

Defined in:
lib/rbgo/corun.rb

Defined Under Namespace

Classes: Routine, Scheduler

Constant Summary collapse

IS_CORUN_FIBER =
:is_corun_fiber_bbc0f70e
YIELD_IO_OPERATION =
:yield_bbc0f70e

Class Method Summary collapse

Class Method Details

.is_in_corun_fiber?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/rbgo/corun.rb', line 12

def self.is_in_corun_fiber?
  !!Thread.current[IS_CORUN_FIBER]
end

.read_from(io, length: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/rbgo/corun.rb', line 16

def self.read_from(io, length: nil)
  if is_in_corun_fiber?
    return "" if length == 0
    receipt = Scheduler.instance.io_machine.do_read(io, length: length)
    Fiber.yield [YIELD_IO_OPERATION, receipt]
  else
    io.read(length)
  end
end

.write_to(io, str:) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/rbgo/corun.rb', line 26

def self.write_to(io, str:)
  if is_in_corun_fiber?
    receipt = Scheduler.instance.io_machine.do_write(io, str: str)
    Fiber.yield [YIELD_IO_OPERATION, receipt]
  else
    io.write(str)
  end
end