File: C:/Ruby27-x64/share/doc/ruby/html/SizedQueue.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>class SizedQueue - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "./";
var index_rel_prefix = "./";
</script>
<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>
<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="class">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="./index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="./table_of_contents.html#pages">Pages</a>
<a href="./table_of_contents.html#classes">Classes</a>
<a href="./table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="class-metadata">
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>
<p class="link"><a href="Object.html">Object</a>
</div>
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-new">::new</a>
<li ><a href="#method-i-3C-3C">#<<</a>
<li ><a href="#method-i-clear">#clear</a>
<li ><a href="#method-i-close">#close</a>
<li ><a href="#method-i-deq">#deq</a>
<li ><a href="#method-i-empty-3F">#empty?</a>
<li ><a href="#method-i-enq">#enq</a>
<li ><a href="#method-i-length">#length</a>
<li ><a href="#method-i-max">#max</a>
<li ><a href="#method-i-max-3D">#max=</a>
<li ><a href="#method-i-num_waiting">#num_waiting</a>
<li ><a href="#method-i-pop">#pop</a>
<li ><a href="#method-i-push">#push</a>
<li ><a href="#method-i-shift">#shift</a>
<li ><a href="#method-i-size">#size</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="class-SizedQueue">
<h1 id="class-SizedQueue" class="class">
class SizedQueue
</h1>
<section class="description">
<p>This class represents queues of specified size capacity. The push operation may be blocked if the capacity is full.</p>
<p>See <a href="Queue.html"><code>Queue</code></a> for an example of how a <a href="SizedQueue.html"><code>SizedQueue</code></a> works.</p>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section id="public-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Class Methods</h3>
</header>
<div id="method-c-new" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
new(max)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Creates a fixed-length queue with a maximum size of <code>max</code>.</p>
<div class="method-source-code" id="new-source">
<pre>static VALUE
rb_szqueue_initialize(VALUE self, VALUE vmax)
{
long max;
struct rb_szqueue *sq = szqueue_ptr(self);
max = NUM2LONG(vmax);
if (max <= 0) {
rb_raise(rb_eArgError, "queue size must be positive");
}
RB_OBJ_WRITE(self, &sq->q.que, ary_buf_new());
list_head_init(szqueue_waitq(sq));
list_head_init(szqueue_pushq(sq));
sq->max = max;
return self;
}</pre>
</div>
</div>
</div>
</section>
<section id="public-instance-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Instance Methods</h3>
</header>
<div id="method-i-3C-3C" class="method-detail method-alias">
<div class="method-heading">
<span class="method-name"><<</span><span
class="method-args">(*args)</span>
</div>
<div class="method-description">
</div>
<div class="aliases">
Alias for: <a href="SizedQueue.html#method-i-push">push</a>
</div>
</div>
<div id="method-i-clear" class="method-detail ">
<div class="method-heading">
<span class="method-name">clear</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Removes all objects from the queue.</p>
<div class="method-source-code" id="clear-source">
<pre>static VALUE
rb_szqueue_clear(VALUE self)
{
struct rb_szqueue *sq = szqueue_ptr(self);
rb_ary_clear(check_array(self, sq->q.que));
wakeup_all(szqueue_pushq(sq));
return self;
}</pre>
</div>
</div>
</div>
<div id="method-i-close" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
close
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Similar to <a href="Queue.html#method-i-close"><code>Queue#close</code></a>.</p>
<p>The difference is behavior with waiting enqueuing threads.</p>
<p>If there are waiting enqueuing threads, they are interrupted by raising ClosedQueueError('queue closed').</p>
<div class="method-source-code" id="close-source">
<pre>static VALUE
rb_szqueue_close(VALUE self)
{
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;
}</pre>
</div>
</div>
</div>
<div id="method-i-deq" class="method-detail method-alias">
<div class="method-heading">
<span class="method-name">deq</span><span
class="method-args">(*args)</span>
</div>
<div class="method-description">
</div>
<div class="aliases">
Alias for: <a href="SizedQueue.html#method-i-pop">pop</a>
</div>
</div>
<div id="method-i-empty-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
empty?
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if the queue is empty.</p>
<div class="method-source-code" id="empty-3F-source">
<pre>static VALUE
rb_szqueue_empty_p(VALUE self)
{
struct rb_szqueue *sq = szqueue_ptr(self);
return queue_length(self, &sq->q) == 0 ? Qtrue : Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-enq" class="method-detail method-alias">
<div class="method-heading">
<span class="method-name">enq</span><span
class="method-args">(*args)</span>
</div>
<div class="method-description">
</div>
<div class="aliases">
Alias for: <a href="SizedQueue.html#method-i-push">push</a>
</div>
</div>
<div id="method-i-length" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
length
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
size
</span>
</div>
<div class="method-description">
<p>Returns the length of the queue.</p>
<div class="method-source-code" id="length-source">
<pre>static VALUE
rb_szqueue_length(VALUE self)
{
struct rb_szqueue *sq = szqueue_ptr(self);
return LONG2NUM(queue_length(self, &sq->q));
}</pre>
</div>
</div>
<div class="aliases">
Also aliased as: <a href="SizedQueue.html#method-i-size">size</a>
</div>
</div>
<div id="method-i-max" class="method-detail ">
<div class="method-heading">
<span class="method-name">max</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the maximum size of the queue.</p>
<div class="method-source-code" id="max-source">
<pre>static VALUE
rb_szqueue_max_get(VALUE self)
{
return LONG2NUM(szqueue_ptr(self)->max);
}</pre>
</div>
</div>
</div>
<div id="method-i-max-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
max=(number)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Sets the maximum size of the queue to the given <code>number</code>.</p>
<div class="method-source-code" id="max-3D-source">
<pre>static VALUE
rb_szqueue_max_set(VALUE self, VALUE vmax)
{
long max = NUM2LONG(vmax);
long diff = 0;
struct rb_szqueue *sq = szqueue_ptr(self);
if (max <= 0) {
rb_raise(rb_eArgError, "queue size must be positive");
}
if (max > sq->max) {
diff = max - sq->max;
}
sq->max = max;
sync_wakeup(szqueue_pushq(sq), diff);
return vmax;
}</pre>
</div>
</div>
</div>
<div id="method-i-num_waiting" class="method-detail ">
<div class="method-heading">
<span class="method-name">num_waiting</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the number of threads waiting on the queue.</p>
<div class="method-source-code" id="num_waiting-source">
<pre>static VALUE
rb_szqueue_num_waiting(VALUE self)
{
struct rb_szqueue *sq = szqueue_ptr(self);
return INT2NUM(sq->q.num_waiting + sq->num_waiting_push);
}</pre>
</div>
</div>
</div>
<div id="method-i-pop" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
pop(non_block=false)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
deq(non_block=false)
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
shift(non_block=false)
</span>
</div>
<div class="method-description">
<p>Retrieves data from the queue.</p>
<p>If the queue is empty, the calling thread is suspended until data is pushed onto the queue. If <code>non_block</code> is true, the thread isn't suspended, and <code>ThreadError</code> is raised.</p>
<div class="method-source-code" id="pop-source">
<pre>static VALUE
rb_szqueue_pop(int argc, VALUE *argv, VALUE self)
{
int should_block = queue_pop_should_block(argc, argv);
return szqueue_do_pop(self, should_block);
}</pre>
</div>
</div>
<div class="aliases">
Also aliased as: <a href="SizedQueue.html#method-i-deq">deq</a>, <a href="SizedQueue.html#method-i-shift">shift</a>
</div>
</div>
<div id="method-i-push" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
push(object, non_block=false)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
enq(object, non_block=false)
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
<<(object)
</span>
</div>
<div class="method-description">
<p>Pushes <code>object</code> to the queue.</p>
<p>If there is no space left in the queue, waits until space becomes available, unless <code>non_block</code> is true. If <code>non_block</code> is true, the thread isn't suspended, and <code>ThreadError</code> is raised.</p>
<div class="method-source-code" id="push-source">
<pre>static VALUE
rb_szqueue_push(int argc, VALUE *argv, VALUE self)
{
struct rb_szqueue *sq = szqueue_ptr(self);
int should_block = szqueue_push_should_block(argc, argv);
while (queue_length(self, &sq->q) >= sq->max) {
if (!should_block) {
rb_raise(rb_eThreadError, "queue full");
}
else if (queue_closed_p(self)) {
goto closed;
}
else {
struct queue_waiter qw;
struct list_head *pushq = szqueue_pushq(sq);
qw.w.th = GET_THREAD();
qw.as.sq = sq;
list_add_tail(pushq, &qw.w.node);
sq->num_waiting_push++;
rb_ensure(queue_sleep, self, szqueue_sleep_done, (VALUE)&qw);
}
}
if (queue_closed_p(self)) {
closed:
raise_closed_queue_error(self);
}
return queue_do_push(self, &sq->q, argv[0]);
}</pre>
</div>
</div>
<div class="aliases">
Also aliased as: <a href="SizedQueue.html#method-i-enq">enq</a>, <a href="SizedQueue.html#method-i-3C-3C"><<</a>
</div>
</div>
<div id="method-i-shift" class="method-detail method-alias">
<div class="method-heading">
<span class="method-name">shift</span><span
class="method-args">(*args)</span>
</div>
<div class="method-description">
</div>
<div class="aliases">
Alias for: <a href="SizedQueue.html#method-i-pop">pop</a>
</div>
</div>
<div id="method-i-size" class="method-detail method-alias">
<div class="method-heading">
<span class="method-name">size</span><span
class="method-args">()</span>
</div>
<div class="method-description">
</div>
<div class="aliases">
Alias for: <a href="SizedQueue.html#method-i-length">length</a>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>