Class: RestFtpDaemon::JobQueue
- Inherits:
-
Queue
- Object
- Queue
- RestFtpDaemon::JobQueue
- Defined in:
- lib/rest-ftp-daemon/job_queue.rb
Instance Method Summary collapse
- #all ⇒ Object
- #all_size ⇒ Object
- #clear ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ JobQueue
constructor
A new instance of JobQueue.
- #num_waiting ⇒ Object
-
#pop(non_block = false) ⇒ Object
(also: #shift, #deq)
Retrieves data from the queue.
- #popped ⇒ Object
- #popped_size ⇒ Object
- #push(obj) ⇒ Object (also: #<<, #enq)
- #queued ⇒ Object
- #queued_size ⇒ Object
Constructor Details
#initialize ⇒ JobQueue
Returns a new instance of JobQueue.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/rest-ftp-daemon/job_queue.rb', line 6 def initialize @queued = [] @popped = [] @waiting = [] @queued.taint # enable tainted communication @waiting.taint self.taint @mutex = Mutex.new end |
Instance Method Details
#all ⇒ Object
31 32 33 |
# File 'lib/rest-ftp-daemon/job_queue.rb', line 31 def all @queued + @popped end |
#all_size ⇒ Object
34 35 36 |
# File 'lib/rest-ftp-daemon/job_queue.rb', line 34 def all_size popped_size + queued_size end |
#clear ⇒ Object
80 81 82 |
# File 'lib/rest-ftp-daemon/job_queue.rb', line 80 def clear @queued.clear end |
#empty? ⇒ Boolean
76 77 78 |
# File 'lib/rest-ftp-daemon/job_queue.rb', line 76 def empty? @queued.empty? end |
#num_waiting ⇒ Object
84 85 86 |
# File 'lib/rest-ftp-daemon/job_queue.rb', line 84 def num_waiting @waiting.size end |
#pop(non_block = false) ⇒ Object Also known as: shift, deq
Retrieves data from the queue. If the queue is empty, the calling thread is suspended until data is pushed onto the queue. If non_block
is true, the thread isn’t suspended, and an exception is raised.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rest-ftp-daemon/job_queue.rb', line 60 def pop(non_block=false) @mutex.synchronize{ while true if @queued.empty? raise ThreadError, "queue empty" if non_block @waiting.push Thread.current @mutex.sleep else return pick end end } end |
#popped ⇒ Object
24 25 26 |
# File 'lib/rest-ftp-daemon/job_queue.rb', line 24 def popped @popped end |
#popped_size ⇒ Object
27 28 29 |
# File 'lib/rest-ftp-daemon/job_queue.rb', line 27 def popped_size @popped.length end |
#push(obj) ⇒ Object Also known as: <<, enq
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rest-ftp-daemon/job_queue.rb', line 38 def push(obj) # Check that itme responds to "priorty" method raise "object should respond to priority method" unless obj.respond_to? :priority @mutex.synchronize{ @queued.push obj begin t = @waiting.shift t.wakeup if t rescue ThreadError retry end } end |
#queued ⇒ Object
17 18 19 |
# File 'lib/rest-ftp-daemon/job_queue.rb', line 17 def queued @queued end |
#queued_size ⇒ Object
20 21 22 |
# File 'lib/rest-ftp-daemon/job_queue.rb', line 20 def queued_size @queued.length end |