Class: OpenEndedQueue Private

Inherits:
Object
  • Object
show all
Defined in:
lib/cinch/open_ended_queue.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Like Ruby’s Queue class, but allowing both pushing and unshifting objects.

Instance Method Summary collapse

Constructor Details

#initializeOpenEndedQueue

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of OpenEndedQueue.



8
9
10
11
12
# File 'lib/cinch/open_ended_queue.rb', line 8

def initialize
  @queue = []
  @mutex = Mutex.new
  @cv = ConditionVariable.new
end

Instance Method Details

#<<(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



14
15
16
# File 'lib/cinch/open_ended_queue.rb', line 14

def <<(obj)
  push(obj)
end

#clearObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
# File 'lib/cinch/open_ended_queue.rb', line 55

def clear
  @mutex.synchronize { @queue.clear }
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


45
46
47
# File 'lib/cinch/open_ended_queue.rb', line 45

def empty?
  @mutex.synchronize { @queue.empty? }
end

#pop(non_block = false) ⇒ Object Also known as: shift, deq

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
35
36
37
38
39
40
# File 'lib/cinch/open_ended_queue.rb', line 32

def pop(non_block = false)
  @mutex.synchronize do
    while @queue.empty?
      raise ThreadError, "queue empty" if non_block
      @cv.wait(@mutex)
    end
    @queue.shift
  end
end

#push(obj) ⇒ Object Also known as: enq

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
21
22
23
# File 'lib/cinch/open_ended_queue.rb', line 18

def push(obj)
  @mutex.synchronize do
    @queue.push(obj)
    @cv.signal
  end
end

#sizeObject Also known as: length

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
# File 'lib/cinch/open_ended_queue.rb', line 49

def size
  @mutex.synchronize { @queue.size }
end

#unshift(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
28
29
30
# File 'lib/cinch/open_ended_queue.rb', line 25

def unshift(obj)
  @mutex.synchronize do
    @queue.unshift(obj)
    @cv.signal
  end
end