Class: OnceQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/netutils/oncequeue.rb

Instance Method Summary collapse

Constructor Details

#initializeOnceQueue

Returns a new instance of OnceQueue.



2
3
4
5
6
7
8
9
10
11
# File 'lib/netutils/oncequeue.rb', line 2

def initialize
	@element = []
	@queue = []
	@mutex = Thread::Mutex.new
	@cond = Thread::ConditionVariable.new
	@empty = Thread::ConditionVariable.new
	@processing = 0
	@total = 0
	@errors = 0
end

Instance Method Details

#countObject



18
19
20
21
22
# File 'lib/netutils/oncequeue.rb', line 18

def count
	@mutex.synchronize do
		__count
	end
end

#dequeueObject



45
46
47
48
49
50
51
# File 'lib/netutils/oncequeue.rb', line 45

def dequeue
	@mutex.synchronize do
		@cond.wait(@mutex) while @queue.empty?
		@processing += 1
			@queue.shift
	end
end

#done(v) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/netutils/oncequeue.rb', line 53

def done(v)
	@mutex.synchronize do
		@processing -= 1
		@empty.signal if __count === 0
		@total += 1
	end
end

#enqueue(v) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/netutils/oncequeue.rb', line 36

def enqueue(v)
	@mutex.synchronize do
		break if @element.include?(v)
		@element.push(v)
		@queue.push(v)
		@cond.signal
	end
end

#errorObject



61
62
63
64
65
# File 'lib/netutils/oncequeue.rb', line 61

def error
	@mutex.synchronize do
		@errors += 1
	end
end

#errorsObject



30
31
32
33
34
# File 'lib/netutils/oncequeue.rb', line 30

def errors
	@mutex.synchronize do
		@errors
	end
end

#synchronizeObject



67
68
69
70
71
# File 'lib/netutils/oncequeue.rb', line 67

def synchronize
	@mutex.synchronize do
		yield
	end
end

#totalObject



24
25
26
27
28
# File 'lib/netutils/oncequeue.rb', line 24

def total
	@mutex.synchronize do
		@total
	end
end

#wait_allObject



73
74
75
76
77
# File 'lib/netutils/oncequeue.rb', line 73

def wait_all
	@mutex.synchronize do
		@empty.wait(@mutex) if __count > 0
	end
end