Class: HashQueue::Queue

Inherits:
Object
  • Object
show all
Includes:
Lockable
Defined in:
lib/hash_queue/queue.rb

Instance Method Summary collapse

Methods included from Lockable

#count_locks, #lock, #locked?, #unlock, #unlock_all

Constructor Details

#initializeQueue

Returns a new instance of Queue.



8
9
10
11
12
13
# File 'lib/hash_queue/queue.rb', line 8

def initialize
  @mutex = Mutex.new 
  @queue = []
  @locks = []
  @waiting = []
end

Instance Method Details

#clearObject



73
74
75
76
77
# File 'lib/hash_queue/queue.rb', line 73

def clear
  @mutex.synchronize do
    @queue.clear
  end
end

#empty?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/hash_queue/queue.rb', line 67

def empty?
  @mutex.synchronize do
    _empty?
  end
end

#peek(options = {}) ⇒ Object



53
54
55
56
57
# File 'lib/hash_queue/queue.rb', line 53

def peek(options = {})
  @mutex.synchronize do
    _peek(options)
  end
end

#pop(options = {}, results = []) ⇒ Object Also known as: shift



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hash_queue/queue.rb', line 29

def pop(options = {}, results = [])
  @mutex.synchronize do
    loop do
      if options[:blocking] and not can_pop?(options[:size] || 1)
        @waiting.push OpenStruct.new(thread: Thread.current, pop_size: options[:size] || 1) 
        @mutex.sleep
      else
        if block_given?
          should_pop = yield _peek(options)
          
          if should_pop
            return _pop(options,results)
          else
            return 
          end
        else
          return _pop(options,results)
        end
      end
    end
  end
end

#queue(*objs) ⇒ Object Also known as: enqueue, push, <<, queue_many, enqueue_many, push_many



15
16
17
18
19
20
21
# File 'lib/hash_queue/queue.rb', line 15

def queue(*objs)
  @mutex.synchronize do
    @queue.concat objs
    
    wake_waiting
  end
end

#sizeObject Also known as: count, length



59
60
61
62
63
# File 'lib/hash_queue/queue.rb', line 59

def size
  @mutex.synchronize do
    @queue.size
  end
end