rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob 3dc97c36cfc3f6568bc2d64d09ca7a385492693e 995 bytes (raw)
$ git show v2.1.0:lib/rainbows/fiber.rb	# shows this blob on the CLI

 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
 
# -*- encoding: binary -*-
# :stopdoc:
begin
  require 'fiber'
rescue LoadError
  defined?(NeverBlock) or raise
end
# :startdoc:

# core namespace for all things that use Fibers in \Rainbows!
module Rainbows::Fiber

  # :stopdoc:
  # blocked readers (key: fileno, value: Rainbows::Fiber::IO object)
  RD = []

  # blocked writers (key: fileno, value: Rainbows::Fiber::IO object)
  WR = []

  # sleeping fibers go here (key: Fiber object, value: wakeup time)
  ZZ = {}
  # :startdoc:

  # puts the current Fiber into uninterruptible sleep for at least
  # +seconds+.  Unlike Kernel#sleep, this it is not possible to sleep
  # indefinitely to be woken up (nobody wants that in a web server,
  # right?).  Calling this directly is deprecated, use
  # Rainbows.sleep(seconds) instead.
  def self.sleep(seconds)
    ZZ[Fiber.current] = Time.now + seconds
    Fiber.yield
  end

  autoload :Base, 'rainbows/fiber/base'
  autoload :Queue, 'rainbows/fiber/queue'
  autoload :IO, 'rainbows/fiber/io'
end

git clone https://yhbt.net/rainbows.git