Class: Thread::SizedQueue
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
Queue
|
|
|
Instance Chain:
self,
Queue
|
|
| Inherits: |
Thread::Queue
|
| Defined in: | thread_sync.rb |
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.
Class Method Summary
-
.new(max) ⇒ SizedQueue
constructor
Document-method: .new
Queue - Inherited
Instance Attribute Summary
-
#max
rw
Returns the maximum size of the queue.
-
#max=(number)
rw
Sets the maximum size of the queue to the given
number.
Queue - Inherited
Instance Method Summary
-
#<<(object, non_block = false, timeout: nil)
Alias for #push.
-
#clear
Removes all objects from the queue.
-
#close
Similar to Queue#close.
-
#deq(non_block = false, timeout: nil)
Alias for #pop.
-
#enq(object, non_block = false, timeout: nil)
Alias for #push.
-
#num_waiting
Returns the number of threads waiting on the queue.
-
#pop(non_block = false, timeout: nil)
(also: #deq, #shift)
Retrieves data from the queue.
-
#push(object, non_block = false, timeout: nil)
(also: #enq, #<<)
Pushes
objectto the queue. -
#shift(non_block = false, timeout: nil)
Alias for #pop.
Queue - Inherited
| #<< | Alias for Queue#push. |
| #clear | Removes all objects from the queue. |
| #close | Closes the queue. |
| #deq | Alias for Queue#pop. |
| #enq | Alias for Queue#push. |
| #freeze | The queue can’t be frozen, so this method raises an exception: |
| #length | Returns the length of the queue. |
| #num_waiting | Returns the number of threads waiting on the queue. |
| #pop | Retrieves data from the queue. |
| #push | Pushes the given |
| #shift | Alias for Queue#pop. |
| #size | Alias for Queue#length. |
| #marshal_dump | |
Constructor Details
.new(max) ⇒ SizedQueue
Document-method: .new
Creates a fixed-length queue with a maximum size of #max.
# File 'thread_sync.rb', line 198
def initialize(vmax) Primitive.szqueue_initialize(vmax) end
Instance Attribute Details
#max (rw)
Returns the maximum size of the queue.
# File 'thread_sync.rb', line 286
def max Primitive.cexpr!('LONG2NUM(szqueue_ptr(self)->max)') end
#max=(number) (rw)
Sets the maximum size of the queue to the given number.
# File 'thread_sync.rb', line 293
def max=(vmax) Primitive.cstmt! %{ long max = NUM2LONG(vmax); if (max <= 0) { rb_raise(rb_eArgError, "queue size must be positive"); } long diff = 0; struct rb_szqueue *sq = szqueue_ptr(self); if (max > sq->max) { diff = max - sq->max; } sq->max = max; sync_wakeup(szqueue_pushq(sq), diff); return vmax; } end
Instance Method Details
#<<(object, non_block = false, timeout: nil)
Alias for #push.
# File 'thread_sync.rb', line 243
alias_method :<<, :push
#clear
Removes all objects from the queue.
# File 'thread_sync.rb', line 268
def clear Primitive.cstmt! %{ struct rb_szqueue *sq = szqueue_ptr(self); queue_clear(&sq->q); wakeup_all(szqueue_pushq(sq)); return self; } end
#close
Similar to Queue#close.
The difference is behavior with waiting enqueuing threads.
If there are waiting enqueuing threads, they are interrupted by raising ClosedQueueError(‘queue closed’).
# File 'thread_sync.rb', line 254
def close Primitive.cstmt! %{ if (!queue_closed_p(self)) { struct rb_szqueue *sq = szqueue_ptr(self); FL_SET(self, QUEUE_CLOSED); wakeup_all(szqueue_waitq(sq)); wakeup_all(szqueue_pushq(sq)); } return self; } end
#deq(non_block = false, timeout: nil)
Alias for #pop.
# File 'thread_sync.rb', line 219
alias_method :deq, :pop
#enq(object, non_block = false, timeout: nil)
Alias for #push.
# File 'thread_sync.rb', line 242
alias_method :enq, :push
#num_waiting
Returns the number of threads waiting on the queue.
# File 'thread_sync.rb', line 278
def num_waiting Primitive.cstmt! %{ struct rb_szqueue *sq = szqueue_ptr(self); return INT2NUM(sq->q.num_waiting + sq->num_waiting_push); } end
#pop(non_block = false, timeout: nil) Also known as: #deq, #shift
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 ::ThreadError is raised.
If timeout seconds have passed and no data is available nil is returned. If timeout is 0 it returns immediately.
# File 'thread_sync.rb', line 213
def pop(non_block = false, timeout: nil) if non_block && timeout raise ArgumentError, "can't set a timeout if non_block is enabled" end Primitive.rb_szqueue_pop(non_block, timeout) end
#push(object, non_block = false, timeout: nil)
#enq(object, non_block = false, timeout: nil)
#<<(object)
Also known as: #enq, #<<
Pushes object to the queue.
If there is no space left in the queue, waits until space becomes available, unless non_block is true. If non_block is true, the thread isn’t suspended, and ::ThreadError is raised.
If timeout seconds have passed and no space is available nil is returned. If timeout is 0 it returns immediately. Otherwise it returns self.
# File 'thread_sync.rb', line 236
def push(object, non_block = false, timeout: nil) if non_block && timeout raise ArgumentError, "can't set a timeout if non_block is enabled" end Primitive.rb_szqueue_push(object, non_block, timeout) end
#shift(non_block = false, timeout: nil)
Alias for #pop.
# File 'thread_sync.rb', line 220
alias_method :shift, :pop