<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Where&#039;s Walden? &#187; wait_closure_only</title>
	<atom:link href="http://whereswalden.com/tag/wait_closure_only/feed/" rel="self" type="application/rss+xml" />
	<link>http://whereswalden.com</link>
	<description>Mozilla, politics, economics, law, backpacking, cycling, and other random desiderata</description>
	<lastBuildDate>Thu, 03 May 2012 09:13:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>An exercise in XPCOM programming, redux</title>
		<link>http://whereswalden.com/2009/12/08/an-exercise-in-xpcom-programming-redux/</link>
		<comments>http://whereswalden.com/2009/12/08/an-exercise-in-xpcom-programming-redux/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 15:55:05 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[blocking]]></category>
		<category><![CDATA[exercise]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[streams]]></category>
		<category><![CDATA[wait_closure_only]]></category>
		<category><![CDATA[xpcom]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=1117</guid>
		<description><![CDATA[The Exercise Last time our hero was engaged in solving this posed streams problem: Suppose you wish to complete one conceptually simple task in stream programming: copying a stream, i.e. reading all data from one stream and writing it all into another, where both streams are nonblocking. (Such a copier might buffer data read before [...]]]></description>
			<content:encoded><![CDATA[<h2>The Exercise</h2>
<p><a href="http://whereswalden.com/2009/12/01/an-exercise-in-xpcom-stream-programming/">Last time</a> our hero was engaged in solving this posed streams problem:</p>
<blockquote>
<p>Suppose you wish to complete one conceptually simple task in stream programming: <dfn>copying a stream</dfn>, <abbr title="id est, that is" lang="la">i.e.</abbr> reading all data from one stream and writing it all into another, where both streams are nonblocking. (Such a copier might buffer data read before it can be immediately written; assume this is a requirement for the purposes of this exercise.) Suppose for the moment that there is no readily available implementation of the <a href="https://developer.mozilla.org/En/NsIAsyncStreamCopier"><code>nsIAsyncStreamCopier</code></a> interface, so you have to roll your own stream copier. In what situation is it necessary to asynchronously wait with <code>flags = WAIT_CLOSURE_ONLY</code> to efficiently implement stream copying?</p>
</blockquote>
<p>(Refer to <a href="http://whereswalden.com/2009/12/01/an-exercise-in-xpcom-stream-programming/">the original post</a> for full background if you&#8217;re not familiar with streams.)</p>
<h2>The Answer</h2>
<p>Copying from one nonblocking stream (the <dfn>source</dfn>) to another (the <dfn>sink</dfn>) involves waiting for the source to be able to provide data, reading in that data, waiting for the sink to be ready to accept data, and writing out buffered data.  In the simple case there&#8217;s always data available to read and always space to write it.  Let&#8217;s break down the cases where these aren&#8217;t the case:</p>
<dl>
<dt>There&#8217;s no data available to read from the source</dt>
<dd>Just wait for data to be available (assuming the sink hasn&#8217;t hit an error, if it has the copy&#8217;s done)</dd>
<dt>There&#8217;s no space in the sink to write data</dt>
<dd>
<dl>
<dt>There&#8217;s data available to write to the sink</dt>
<dd>Wait for that amount of data, or a fraction of it, to be writable</dd>
<dt>There&#8217;s no data available to write to the sink</dt>
<dd>???</dd>
</dl>
</dd>
</dl>
<p>What should happen in the final alternation?  Suppose you waited for some amount of data to be writable, we&#8217;ll say 1 byte.  What happens when the sink becomes that far unblocked?  You&#8217;d have to be notified, and if there&#8217;s still no data to write you&#8217;re back where you started.  Maybe you can bump up the amount you wait for, but how far should you bump?  Increase arithmetically?  Double?  Any amount you bump to might result in more notifications when there&#8217;s nothing you can do.</p>
<p>There&#8217;s a further problem with waiting for some amount of data, one you&#8217;d only know if you were familiar with the async copying interfaces: the amount you specify when calling <code>asyncWait</code> to request notification when the stream&#8217;s unblocked again is <em>only a hint</em>.  That is, the implementation is free to notify whenever it wants, so long as it&#8217;s not notifying when the state of the stream is unchanged from being previously blocked and unclosed.  A stream might notify whenever <em>any</em> data is available, even if you bump up the amount.  Therefore, if you just wait for an amount of data, and wait again if you have no data to write, you&#8217;ll be notified almost immediately (after any pending tasks in the thread event loop).  Repeat this a few times and suddenly you&#8217;re spinning doing nothing, which is clearly inefficient.  The main async stream classes in the tree ignore the requested count precisely as described here, so this isn&#8217;t simply an academic problem that we could ignore.</p>
<p>Here&#8217;s where you need <code>WAIT_CLOSURE_ONLY</code>.  Until you have data to write, you don&#8217;t care about how much can be written to the sink.  What you really care about is knowing if the sink closes (or gets in an error state), so you can stop copying immediately when that happens, rather than wait (perhaps indefinitely) until you have data to write and only determine when writing it that the sink&#8217;s closed (or in error).  Using <code>WAIT_CLOSURE_ONLY</code> whenever you haven&#8217;t hit errors but don&#8217;t have any data to write neatly solves the problem of <em>efficiently</em> learning if the sink dies.</p>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2009/12/08/an-exercise-in-xpcom-programming-redux/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>An exercise in XPCOM stream programming</title>
		<link>http://whereswalden.com/2009/12/01/an-exercise-in-xpcom-stream-programming/</link>
		<comments>http://whereswalden.com/2009/12/01/an-exercise-in-xpcom-stream-programming/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 00:48:53 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[blocking]]></category>
		<category><![CDATA[exercise]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[nsIAsyncStreamCopier]]></category>
		<category><![CDATA[streams]]></category>
		<category><![CDATA[wait_closure_only]]></category>
		<category><![CDATA[xpcom]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=1109</guid>
		<description><![CDATA[If you&#8217;ve done any programming with XPCOM, at some time you&#8217;ve probably had to work with streams. A little background in case you haven&#8217;t, then a small thought exercise: Streams A stream is an object from which you read data or to which you write data. In XPCOM an input stream stream is a stream [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve done any programming with <abbr title="cross-platform component object model">XPCOM</abbr>, at some time you&#8217;ve probably had to work with streams.  A little background in case you haven&#8217;t, then a small thought exercise:</p>
<h2>Streams</h2>
<p>A <dfn>stream</dfn> is an object from which you read data or to which you write data.  In XPCOM an <dfn>input stream</dfn> stream is a stream from which you read data; an <dfn>output stream</dfn> is a stream to which you write data.  In an ideal world a stream is either open (indicating data may be read or written to it) or closed (indicating that the stream is no longer readable, or that no more data can be written to it), and that&#8217;s all there is to it.  File objects in Python function very much like ideal streams.</p>
<p>In the real world, truly useful streams have further limitations (or characteristics).  How much data can be read from an input stream <em>right now</em>?  Can a given amount of data be written to an output stream <em>right now</em>?  Should reading or writing proceed until completion when <em>right now</em> isn&#8217;t possible but <em>sometime later</em> might be, or should it halt immediately with an error indicating that reading or writing would block program execution?  One might ignore these concerns in simplistic scenarios such as those which short Python scripts might be used to address.  In complex applications, particularly those which must remain responsive to user input, these concerns may be quite important.  You can&#8217;t display a useful progress bar if the stream you&#8217;re reading from represents the download of a <abbr title="three gigabytes">3GB</abbr> file over a slow network and reading from the stream blocks program execution.</p>
<p>Streams which immediately halt with an error when reading or writing would block execution are <dfn>nonblocking</dfn> streams.  Efficient use of such streams requires a way to wait until the desired amount of data can be written to or read from a stream.  XPCOM efficiently supports nonblocking streams through an <code>asyncWait</code> method which will notify at some later time when the desired amount of data can be written to or read from the stream, without blocking.  At the moment there are two flavors of asynchronous waiting: waiting until the desired amount can be read or written, and waiting until the given stream has been closed.  At the interface level, the former is indicated by <code>flags = 0</code>, while the latter is indicated by <code>flags = WAIT_CLOSURE_ONLY</code>.</p>
<h2>The Exercise</h2>
<p>Suppose you wish to complete one conceptually simple task in stream programming: <dfn>copying a stream</dfn>, <abbr title="id est, that is" lang="la">i.e.</abbr> reading all data from one stream and writing it all into another, where both streams are nonblocking.  (Such a copier might buffer data read before it can be immediately written; assume this is a requirement for the purposes of this exercise.)  Suppose for the moment that there is no readily available implementation of the <a href="https://developer.mozilla.org/En/NsIAsyncStreamCopier"><code>nsIAsyncStreamCopier</code></a> interface, so you have to roll your own stream copier.  In what situation is it necessary to asynchronously wait with <code>flags = WAIT_CLOSURE_ONLY</code> to efficiently implement stream copying?</p>
<h2>Hints</h2>
<p>If you want a hint (arguably the answer, if you can interpret the code), take a look at the uses of <code>WAIT_CLOSURE_ONLY</code> in <a href="http://mxr.mozilla.org/mozilla-central/source/xpcom/io/nsStreamUtils.cpp"><code>xpcom/io/nsStreamUtils.cpp</code></a>.  You may perhaps find further hints in <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=513854">bug 513854</a>, the bug which brought this somewhat quirky need for <code>flags = WAIT_CLOSURE_ONLY</code> to my attention.</p>
<h2>Questions?</h2>
<p>I come to this problem with more experience and familiarity with streams than most people will have.  If anything in the above description is unclear, ask questions in the comment section &mdash; I did the best I could to make the problem and its background understandable, but I may easily have done so less well than intended.</p>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2009/12/01/an-exercise-in-xpcom-stream-programming/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

