about summary refs log tree commit homepage
path: root/lib/rainbows/fiber/coolio
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rainbows/fiber/coolio')
-rw-r--r--lib/rainbows/fiber/coolio/heartbeat.rb15
-rw-r--r--lib/rainbows/fiber/coolio/methods.rb47
-rw-r--r--lib/rainbows/fiber/coolio/server.rb32
-rw-r--r--lib/rainbows/fiber/coolio/sleeper.rb15
4 files changed, 109 insertions, 0 deletions
diff --git a/lib/rainbows/fiber/coolio/heartbeat.rb b/lib/rainbows/fiber/coolio/heartbeat.rb
new file mode 100644
index 0000000..f48f7ef
--- /dev/null
+++ b/lib/rainbows/fiber/coolio/heartbeat.rb
@@ -0,0 +1,15 @@
+# -*- encoding: binary -*-
+# :enddoc:
+class Rainbows::Fiber::Coolio::Heartbeat < Coolio::TimerWatcher
+  G = Rainbows::G
+
+  # ZZ gets populated by read_expire in rainbows/fiber/io/methods
+  ZZ = Rainbows::Fiber::ZZ
+  def on_timer
+    exit if (! G.tick && G.cur <= 0)
+    now = Time.now
+    fibs = []
+    ZZ.delete_if { |fib, time| now >= time ? fibs << fib : ! fib.alive? }
+    fibs.each { |fib| fib.resume if fib.alive? }
+  end
+end
diff --git a/lib/rainbows/fiber/coolio/methods.rb b/lib/rainbows/fiber/coolio/methods.rb
new file mode 100644
index 0000000..64b0ee6
--- /dev/null
+++ b/lib/rainbows/fiber/coolio/methods.rb
@@ -0,0 +1,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
diff --git a/lib/rainbows/fiber/coolio/server.rb b/lib/rainbows/fiber/coolio/server.rb
new file mode 100644
index 0000000..0de1ab3
--- /dev/null
+++ b/lib/rainbows/fiber/coolio/server.rb
@@ -0,0 +1,32 @@
+# -*- encoding: binary -*-
+# :enddoc:
+class Rainbows::Fiber::Coolio::Server < Coolio::IOWatcher
+  G = Rainbows::G
+  include Rainbows::ProcessClient
+
+  def to_io
+    @io
+  end
+
+  def initialize(io)
+    @io = io
+    super(self, :r)
+  end
+
+  def close
+    detach if attached?
+    @io.close
+  end
+
+  def on_readable
+    return if G.cur >= MAX
+    c = @io.kgio_tryaccept and Fiber.new { process(c) }.resume
+  end
+
+  def process(io)
+    G.cur += 1
+    process_client(io)
+  ensure
+    G.cur -= 1
+  end
+end
diff --git a/lib/rainbows/fiber/coolio/sleeper.rb b/lib/rainbows/fiber/coolio/sleeper.rb
new file mode 100644
index 0000000..a11623a
--- /dev/null
+++ b/lib/rainbows/fiber/coolio/sleeper.rb
@@ -0,0 +1,15 @@
+# -*- encoding: binary -*-
+# :enddoc:
+class Rainbows::Fiber::Coolio::Sleeper < Coolio::TimerWatcher
+
+  def initialize(seconds)
+    @f = Fiber.current
+    super(seconds, false)
+    attach(Coolio::Loop.default)
+    Fiber.yield
+  end
+
+  def on_timer
+    @f.resume
+  end
+end