Sha256: 809d45ceb48512a1c43c3e831c8eb71147f254deeb301523ee7d6caeaebdc534

Contents?: true

Size: 745 Bytes

Versions: 4

Compression:

Stored size: 745 Bytes

Contents

module Daybreak
  # A queue for threaded implementations of ruby without a GIL
  # @api private
  class Queue
    def initialize
      @mutex = Mutex.new
      @full = ConditionVariable.new
      @empty = ConditionVariable.new
      @queue = []
    end

    def <<(x)
      @mutex.synchronize do
        @queue << x
        @full.signal
      end
    end

    def pop
      @mutex.synchronize do
        @queue.shift
        @empty.signal if @queue.empty?
      end
    end

    def first
      @mutex.synchronize do
        @full.wait(@mutex) while @queue.empty?
        @queue.first
      end
    end

    def flush
      @mutex.synchronize do
        @empty.wait(@mutex) until @queue.empty?
      end
    end

    def close
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
daybreak-0.3.0 lib/daybreak/queue/threaded.rb
daybreak-0.2.4 lib/daybreak/queue/threaded.rb
daybreak-0.2.3 lib/daybreak/queue/threaded.rb
daybreak-0.2.2 lib/daybreak/queue/threaded.rb