diff options
author | Eric Wong <normalperson@yhbt.net> | 2010-12-26 23:52:02 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2010-12-26 23:52:02 +0000 |
commit | 2e131bfd21f5ec5acc3c86233e5e292cec7aa67d (patch) | |
tree | 490f2f5a46d7294929f2db369592369e4037dc6f /lib/rainbows/fiber | |
parent | 712ef17547291fed37e79d37d0b6e0128ed43e0d (diff) | |
download | rainbows-2e131bfd21f5ec5acc3c86233e5e292cec7aa67d.tar.gz |
This also cleans up some constant resolution for the root Fiber class.
Diffstat (limited to 'lib/rainbows/fiber')
-rw-r--r-- | lib/rainbows/fiber/queue.rb | 54 |
1 files changed, 24 insertions, 30 deletions
diff --git a/lib/rainbows/fiber/queue.rb b/lib/rainbows/fiber/queue.rb index e7118f3..f752a65 100644 --- a/lib/rainbows/fiber/queue.rb +++ b/lib/rainbows/fiber/queue.rb @@ -1,36 +1,30 @@ # -*- encoding: binary -*- # :enddoc: -module Rainbows - module Fiber - - # a self-sufficient Queue implementation for Fiber-based concurrency - # models. This requires no external scheduler, so it may be used with - # Revactor as well as FiberSpawn and FiberPool. - class Queue < Struct.new(:queue, :waiters) - - def initialize(queue = [], waiters = []) - # move elements of the Queue into an Array - if queue.class.name == "Queue" - queue = queue.length.times.map { queue.pop } - end - super queue, waiters - end - - def shift - # ah the joys of not having to deal with race conditions - if queue.empty? - waiters << ::Fiber.current - ::Fiber.yield - end - queue.shift - end - - def <<(obj) - queue << obj - blocked = waiters.shift and blocked.resume - queue # not quite 100% compatible but no-one's looking :> - end +# +# a self-sufficient Queue implementation for Fiber-based concurrency +# models. This requires no external scheduler, so it may be used with +# Revactor as well as FiberSpawn and FiberPool. +class Rainbows::Fiber::Queue < Struct.new(:queue, :waiters) + def initialize(queue = [], waiters = []) + # move elements of the Queue into an Array + if queue.class.name == "Queue" + queue = queue.length.times.map { queue.pop } + end + super queue, waiters + end + def shift + # ah the joys of not having to deal with race conditions + if queue.empty? + waiters << Fiber.current + Fiber.yield end + queue.shift + end + + def <<(obj) + queue << obj + blocked = waiters.shift and blocked.resume + queue # not quite 100% compatible but no-one's looking :> end end |