Sha256: bbe6fc3b7e22ad75b6f64bbae725155e6ac6f9be41397ce4c40581a854cc0081

Contents?: true

Size: 945 Bytes

Versions: 11

Compression:

Stored size: 945 Bytes

Contents

# -*- encoding: binary -*-
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

    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rainbows-0.94.0 lib/rainbows/fiber/queue.rb
rainbows-0.93.0 lib/rainbows/fiber/queue.rb
rainbows-0.92.0 lib/rainbows/fiber/queue.rb
rainbows-0.91.1 lib/rainbows/fiber/queue.rb
rainbows-0.91.0 lib/rainbows/fiber/queue.rb
rainbows-0.90.2 lib/rainbows/fiber/queue.rb
rainbows-0.90.1 lib/rainbows/fiber/queue.rb
rainbows-0.90.0 lib/rainbows/fiber/queue.rb
rainbows-0.9.0 lib/rainbows/fiber/queue.rb
rainbows-0.8.0 lib/rainbows/fiber/queue.rb
rainbows-0.7.0 lib/rainbows/fiber/queue.rb