Created
August 21, 2010 19:38
-
-
Save samleb/542751 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FiberGroup | |
| attr_reader :remaining | |
| def self.run(&block) | |
| new(&block).join | |
| end | |
| def initialize | |
| @remaining = 0 | |
| @fiber = Fiber.current | |
| yield self if block_given? | |
| end | |
| def join | |
| Fiber.yield unless @remaining.zero? | |
| end | |
| def add(proc = nil, &block) | |
| raise ArgumentError, "block or proc required." unless proc ||= block | |
| @remaining += 1 | |
| Fiber.new { | |
| proc.call | |
| @remaining -= 1 # DO NOT MOVE UP ;) | |
| begin | |
| @fiber.resume if @remaining.zero? | |
| rescue FiberError | |
| end | |
| }.resume | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of usage: http://gist.github.com/542772