Class: Rage::FiberScheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/fiber_scheduler.rb

Instance Method Summary collapse

Constructor Details

#initializeFiberScheduler

Returns a new instance of FiberScheduler.



6
7
8
# File 'lib/rage/fiber_scheduler.rb', line 6

def initialize
  @root_fiber = Fiber.current
end

Instance Method Details

#address_resolve(hostname) ⇒ Object

result end



74
75
76
# File 'lib/rage/fiber_scheduler.rb', line 74

def address_resolve(hostname)
  Resolv.getaddresses(hostname)
end

#block(blocker, timeout = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/rage/fiber_scheduler.rb', line 78

def block(blocker, timeout = nil)
  f = Fiber.current
  ::Iodine.subscribe("unblock:#{f.object_id}") do
    ::Iodine.defer { ::Iodine.unsubscribe("unblock:#{f.object_id}") }
    f.resume
  end
  # TODO support timeout
  Fiber.yield
end

#closeObject



107
108
109
# File 'lib/rage/fiber_scheduler.rb', line 107

def close
  ::Iodine::Scheduler.close
end

#fiber(&block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rage/fiber_scheduler.rb', line 92

def fiber(&block)
  f = Fiber.current
  inner_schedule = f != @root_fiber

  fiber = Fiber.new(blocking: false) do
    Fiber.current.__set_result(block.call)
  ensure
    # send a message for `Fiber.await` to work
    Iodine.publish("await:#{f.object_id}", "") if inner_schedule
  end
  fiber.resume

  fiber
end

#io_read(io, buffer, length, offset = 0) ⇒ Object

TODO: this is more synchronous than asynchronous right now



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rage/fiber_scheduler.rb', line 19

def io_read(io, buffer, length, offset = 0)
  loop do
    string = ::Iodine::Scheduler.read(io.fileno, length, offset)

    if string.nil?
      return offset
    end

    if string.empty?
      io_wait(io, IO::READABLE)
      next
    end

    buffer.set_string(string, offset)
    offset += string.bytesize

    size = string.bytesize
    break if size >= length
    length -= size
  end

  offset
end

#io_wait(io, events, timeout = nil) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/rage/fiber_scheduler.rb', line 10

def io_wait(io, events, timeout = nil)
  f = Fiber.current
  ::Iodine::Scheduler.attach(io.fileno, events, timeout&.ceil || 0) { f.resume }
  Fiber.yield

  events
end

#io_write(io, buffer, length, offset = 0) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/rage/fiber_scheduler.rb', line 43

def io_write(io, buffer, length, offset = 0)
  bytes_to_write = length
  bytes_to_write = buffer.size if length == 0

  ::Iodine::Scheduler.write(io.fileno, buffer.get_string, bytes_to_write, offset)

  buffer.size - offset
end

#kernel_sleep(duration = nil) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/rage/fiber_scheduler.rb', line 52

def kernel_sleep(duration = nil)
  if duration
    f = Fiber.current
    ::Iodine.run_after((duration * 1000).to_i) { f.resume } 
    Fiber.yield
  end
end

#unblock(_blocker, fiber) ⇒ Object



88
89
90
# File 'lib/rage/fiber_scheduler.rb', line 88

def unblock(_blocker, fiber)
  ::Iodine.publish("unblock:#{fiber.object_id}", "")
end