Class: SizedQueue
Overview
This class represents queues of specified size capacity. The push operation may be blocked if the capacity is full.
See Queue for an example of how a SizedQueue works.
Instance Method Summary collapse
-
#max=(size) ⇒ Object
constructor
Sets the maximum size of the queue.
-
#max ⇒ Object
Returns the maximum size of the queue.
-
#max=(size) ⇒ Object
Sets the maximum size of the queue.
-
#num_waiting ⇒ Object
Returns the number of threads waiting on the queue.
-
#pop(*args) ⇒ Object
(also: #shift, #deq)
call_seq: pop(non_block=false).
-
#push(obj) ⇒ Object
(also: #<<, #enq)
Pushes
obj
to the queue.
Methods inherited from Queue
#clear, #empty?, #length, #marshal_dump, #marshal_load
Constructor Details
#max=(size) ⇒ Object
Sets the maximum size of the queue.
374 375 376 377 378 379 380 |
# File 'lib/thread.rb', line 374 def initialize(max) raise ArgumentError, "queue size must be positive" unless max > 0 @max = max @queue_wait = [] @queue_wait.taint # enable tainted comunication super() end |
Instance Method Details
#max ⇒ Object
Returns the maximum size of the queue.
385 386 387 |
# File 'lib/thread.rb', line 385 def max @max end |
#max=(size) ⇒ Object
Sets the maximum size of the queue.
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/thread.rb', line 392 def max=(max) Thread.critical = true if max <= @max @max = max Thread.critical = false else diff = max - @max @max = max Thread.critical = false diff.times do begin t = @queue_wait.shift t.run if t rescue ThreadError retry end end end max end |
#num_waiting ⇒ Object
Returns the number of threads waiting on the queue.
473 474 475 |
# File 'lib/thread.rb', line 473 def num_waiting @waiting.size + @queue_wait.size end |
#pop(*args) ⇒ Object Also known as: shift, deq
call_seq: pop(non_block=false)
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.
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/thread.rb', line 440 def pop(*args) retval = super Thread.critical = true if @que.length < @max begin t = @queue_wait.shift t.wakeup if t rescue ThreadError retry ensure Thread.critical = false end begin t.run if t rescue ThreadError end end retval end |