about summary refs log tree commit homepage
path: root/lib/rainbows/fiber/coolio/methods.rb
blob: 64b0ee605afb1df4304add7bdd8101b861213b25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- encoding: binary -*-
# :enddoc:
module Rainbows::Fiber::Coolio::Methods
  class Watcher < Coolio::IOWatcher
    def initialize(fio, flag)
      @f = Fiber.current
      super(fio, flag)
      attach(Coolio::Loop.default)
    end

    def on_readable
      @f.resume
    end

    alias on_writable on_readable
  end

  def close
    @w.detach if defined?(@w) && @w.attached?
    @r.detach if defined?(@r) && @r.attached?
    super
  end

  def kgio_wait_writable
    @w = Watcher.new(self, :w) unless defined?(@w)
    @w.enable unless @w.enabled?
    Fiber.yield
    @w.disable
  end

  def kgio_wait_readable
    @r = Watcher.new(self, :r) unless defined?(@r)
    @r.enable unless @r.enabled?
    Fiber.yield
    @r.disable
  end
end

[
  Rainbows::Fiber::IO,
  Rainbows::Client,
  # the next two trigger autoload, ugh, oh well...
  Rainbows::Fiber::IO::Socket,
  Rainbows::Fiber::IO::Pipe
].each do |klass|
  klass.__send__(:include, Rainbows::Fiber::Coolio::Methods)
end