<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Michael's Blog</title>
	<atom:link href="http://michael.procter.org.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://michael.procter.org.uk</link>
	<description></description>
	<lastBuildDate>Thu, 15 Sep 2011 05:46:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='michael.procter.org.uk' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Michael's Blog</title>
		<link>http://michael.procter.org.uk</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://michael.procter.org.uk/osd.xml" title="Michael&#039;s Blog" />
	<atom:link rel='hub' href='http://michael.procter.org.uk/?pushpress=hub'/>
		<item>
		<title>Secure Web Surfing</title>
		<link>http://michael.procter.org.uk/2010/02/28/secure-web-surfing/</link>
		<comments>http://michael.procter.org.uk/2010/02/28/secure-web-surfing/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 20:03:20 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://michael.procter.org.uk/?p=16</guid>
		<description><![CDATA[Is CMU&#8217;s Perspectives project the best way to secure web browsing?  This chap seems to think so, and I broadly agree.  However, there is a potential privacy leak when using Perspectives that is enough to stop me using it all the time.  Then I realised that with a trivial change, I could have the best [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michael.procter.org.uk&amp;blog=1435391&amp;post=16&amp;subd=mprocter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Is CMU&#8217;s <a href="http://www.cs.cmu.edu/~perspectives/">Perspectives</a> project the best way to secure web browsing?  <a href="http://blogs.techrepublic.com.com/security/?p=2516">This chap</a> seems to think so, and I broadly agree.  However, there is a potential privacy leak when using Perspectives that is enough to stop me using it all the time.  Then I realised that with a trivial change, I could have the best of both worlds &#8211; using Perspectives whilst preserving my privacy.</p>
<p><span id="more-16"></span>I like the <a title="Perspectives" href="http://www.cs.cmu.edu/~perspectives/">Perspectives project</a>:  the Firefox plugin gives me confidence that I am not about to be the victim of a MITM attack, and it also gives me enough confidence in the longevity of a certificate that I can usefully accept self-signed certificates for many sites.</p>
<p>One minor concern that I have is that my browsing habits could be determined by those that run the notary services.  The Notary Server Privacy Policy states:</p>
<p id="line185" style="padding-left:30px;">All notary servers adhere to a strict policy of never recording client IP addresses, period.  The Perspectives project and its software will only contact Notary servers that follow this privacy policy.  Your privacy is important to us.</p>
<p>I trust these people (although I&#8217;ve never met them!), but I&#8217;d prefer not to <em>need</em> to trust them.  For all I know, the traffic to/from CMU might already be monitored by some 3rd party unbeknownst to them.</p>
<p>I like the <a title="The Onion Router" href="http://www.torproject.org/">Tor project</a> for several reasons, but I find it too slow for normal use.  However, if I could use Tor for my notary requests&#8230;</p>
<p>FoxyProxy is a useful Firefox plugin.  Once installed with Tor support, I can add the following URL pattern to my Tor proxy rule:</p>
<pre>*.ron.lcs.mit.edu:8080/*</pre>
<p>And all the notary lookups will be performed over Tor!</p>
<p>If you plan to do this yourself, I&#8217;d recommend confirming correct operation with <a href="http://www.wireshark.org/">Wireshark</a>.  Periodic checking with Wireshark also strikes me as wise, in case the notary addresses change and you need to update the rule.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mprocter.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mprocter.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mprocter.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mprocter.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mprocter.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mprocter.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mprocter.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mprocter.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mprocter.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mprocter.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mprocter.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mprocter.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mprocter.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mprocter.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michael.procter.org.uk&amp;blog=1435391&amp;post=16&amp;subd=mprocter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://michael.procter.org.uk/2010/02/28/secure-web-surfing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bfb4056a662101da808d05457455504f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mprocter</media:title>
		</media:content>
	</item>
		<item>
		<title>The Development of Gyroc</title>
		<link>http://michael.procter.org.uk/2008/04/29/the-development-of-gyroc/</link>
		<comments>http://michael.procter.org.uk/2008/04/29/the-development-of-gyroc/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 13:26:49 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[embedded]]></category>
		<category><![CDATA[projects]]></category>

		<guid isPermaLink="false">http://mprocter.wordpress.com/?p=6</guid>
		<description><![CDATA[I wrote this several years ago (maybe in 2002?), and recently stumbled across it again. I&#8217;m adding it here, so that I have a convenient place to point, when I am asked questions. Gyroc 1 was the first actively stabilised model rocket developed and flown in the UK (to our knowledge). Using a custom gyroscope [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michael.procter.org.uk&amp;blog=1435391&amp;post=6&amp;subd=mprocter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><em>I wrote this several years ago (maybe in 2002?), and recently stumbled across it again.  I&#8217;m adding it here, so that I have a convenient place to point, when I am asked questions.</em></p>
<p>Gyroc 1 was the first actively stabilised model rocket developed and flown in the UK (to our knowledge).  Using a custom gyroscope to give an inertial platform, a digital flight computer and a gimbal-mounted rocket motor, Gyroc 1 was able to remain vertical whilst under power.  The project has been predominantly developed by James Macfarlane and Michael Procter, although others have also made valuable contributions.<br />
<span id="more-6"></span><br />
<em>Please note that this project has nothing to do with the Estes model rocket kit of the same name.</em></p>
<h1>Gyroc 1</h1>
<p>My involvement with Gyroc 1 began in 1996, a few weeks before the <a href="http://irw.rocketry.org.uk">International Rocket Weekend</a> in Largs.  Prior to this, James had already developed much of the hardware and mechanics.  Hoping to complete the maiden flight at the IRW, he enlisted my help for the software.  The first flight computer (based on a PIC16C84) was built and the majority of the software was written in one rather frantic weekend. Subsequent refinements took place (particularly of the software, which I had hacked together somewhat too rapidly for bug-free maintainable code to emerge) before and during the IRW.  Finally, we were in a position to launch the rocket.  In what was to become the traditional launch schedule for Gyroc, the launch pad was set up as dusk started to fall.  All but the most foolhardy (myself and James) were sent to retire to an extremely safe distance, and the countdown began.</p>
<p>The first flight was made on a D12, giving an initial peak thrust of around 12-14 Newtons, tailing off to just under 10 Newtons. The all-up mass of Gyroc was around 1kg.  Consequently, Gyroc managed to clear the 1-foot launch rail, but only just.  It managed to hover next to the rail until the motor burnt out, whereupon it fell to Earth.  The crowd cheered &#8211; we had all just witnessed the first amateur actively stabilised rocket to be launched in the UK, and the flight had been a success.</p>
<p>This pattern was not to be repeated for several more launch attempts. James and I accepted that the first flight was probably more down to luck than judgement, and various changes were made to improve the reliability and repeatability of stable flight.  With hindsight, it seems clear that if that first flight had not been a success, we may not have had sufficient confidence in the approach to make it work.  But with the tantalising carrot of that first flight, we continued developments over the years.</p>
<h1>Gyroc 2</h1>
<p>Gyroc 2 was first flown in February 2002.  Whilst externally, little appeared to have changed, there were some substantial internal developments.  The most significant change was the removal of the custom gyro and the installation of 2 commercial solid-state gyros (at a not inconsiderable expense).  We had come to the conclusion (correctly, as it turned out) that many of our problems could be traced to the mechanical soundness (or lack of) of the custom gyro.  Repairing the graphite bearing and containment box after every flight should have been a warning that the structure was not ideally suited to the task in hand.  Replacing the gyro was a major step forward in the development of Gyroc.</p>
<p>In addition, the avionics was rebuilt around a PIC16F877, thus removing the need for an external ADC.  Since the custom gyro was no longer fitted, the gyro spin-up circuit could be disposed of.  Finally, the original parachute timer was removed and firing the parachute was entrusted to the microcontroller.  The final change was to enhance the gimbal motor mount to support larger motors.  Now, with Aerotech 32mm motors, high altitude (&gt;20&#8242;) testing was possible.</p>
<p>After another frantic weekend of software development (changing the processor, adding calibration offset code for the gyros, implementing a small integrator), Gyroc 2 was ready to fly.  As was customary, the launch took place as late in the day as the RSO would permit.  All the extra development proved to be worthwhile, as the flight appeared to be almost flawless.  The rocket ascended gently under power, drifting slightly downwind.  It remained pointing vertically throughout the burn, with barely discernible deviation.  The parachute deployed on schedule, but failed to deploy completely due to the presence of excess masking tape.  Since that flight, Gyroc 2 has flown twice more.  An mpeg of one of these flights is linked below.</p>
<h1>What next?</h1>
<p>Since we are now in the rather pleasant position of having achieved our primary goal (namely to actively stabilise a model rocket), we are able to decide where we wish to take the project from here.  There are one or two obvious directions that could be taken, but only tentative plans have been made at this stage. <em>I haven&#8217;t done anything of note in the last 6 years on this project, although James occasionally makes productive-sounding noises.  I&#8217;m still at the tentative plans stage!</em></p>
<h1>Further information</h1>
<p>I have omitted much technical information concerning Gyroc from this page, for two reasons.  Firstly, I didn&#8217;t have all the details to hand during the initial composition, and secondly because a friend has already recorded much of the information on <a href="http://www.ukrocketman.com/rocketry/gimbal.shtml">another site</a>. Over time, I anticipate adding more material to this page, as I remember and also as I scan suitable pictures.  In the meantime, a <a href="http://files.michaelp.googlepages.com/Gyroc.mpeg">large MPEG</a> of Gyroc 2 in flight is available.  This flight was made in February 2002 in Lincolnshire.  Many thanks to David Warman for providing me with the footage.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mprocter.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mprocter.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mprocter.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mprocter.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mprocter.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mprocter.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mprocter.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mprocter.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mprocter.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mprocter.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mprocter.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mprocter.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mprocter.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mprocter.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mprocter.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mprocter.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michael.procter.org.uk&amp;blog=1435391&amp;post=6&amp;subd=mprocter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://michael.procter.org.uk/2008/04/29/the-development-of-gyroc/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
<enclosure url="http://files.michaelp.googlepages.com/Gyroc.mpeg" length="1703492" type="video/mpeg" />
	
		<media:content url="http://1.gravatar.com/avatar/bfb4056a662101da808d05457455504f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mprocter</media:title>
		</media:content>
	</item>
		<item>
		<title>The old XOR swapping trick</title>
		<link>http://michael.procter.org.uk/2008/01/28/the-old-xor-swapping-trick/</link>
		<comments>http://michael.procter.org.uk/2008/01/28/the-old-xor-swapping-trick/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 22:48:59 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[bit-twiddling]]></category>
		<category><![CDATA[embedded]]></category>

		<guid isPermaLink="false">http://mprocter.wordpress.com/?p=5</guid>
		<description><![CDATA[Many people have heard of how to swap two values without using temporary storage, using XORs. This is often summarised as: a ^= b ^= a ^= b; There are other operators that can be used to do the swap, but in the degenerate case, this essentially becomes the XOR trick again. Firstly, I ought [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michael.procter.org.uk&amp;blog=1435391&amp;post=5&amp;subd=mprocter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Many people have heard of how to swap two values without using temporary storage, using XORs.  This is often summarised as:</p>
<p><code>a ^= b ^= a ^= b;</code></p>
<p>There are other operators that can be used to do the swap, but in the degenerate case, this essentially becomes the XOR trick again.</p>
<p><span id="more-5"></span></p>
<p>Firstly, I ought to address the fact that the above statement isn&#8217;t strictly valid C.  Multiple side effects within a single expression leads to undefined results.  Most compilers will get the &#8216;right&#8217; answer, but why take the risk for the sake of a few extra characters?  It is easy to rewrite correctly (I&#8217;ll also remove the combined xor-assignment operator, for clarity later):</p>
<p><code>a = a ^ b;<br />
b = a ^ b;<br />
a = a ^ b;<br />
</code></p>
<p>If you haven&#8217;t seen this trick before, it is probably worth working through an example or two on paper.  There are quite a few explanations on the net, if you get stuck.</p>
<p>The reason this works is that <i>xor</i> is a reversible operation.  It is further simplified by the fact that <i>xor</i> is its own inverse &#8211; that is to say:</p>
<p><code>(a ^ b) ^ b = a</code></p>
<p>You can use other operations instead of <i>xor</i>, such as addition and subtraction.  Subtraction is the inverse of addition, so you can write the swap as:</p>
<table width="400">
<tr>
<td>&nbsp;</td>
<td>a=A</td>
<td>b=B</td>
</tr>
<tr>
<td><code>a = a + b</code></td>
<td><b>a=(A+B)</b></td>
<td>b=B</td>
</tr>
<tr>
<td><code>b = a - b</code></td>
<td>a=(A+B)</td>
<td><b>b=(A+B)-B=A</b></td>
</tr>
<tr>
<td><code>a = a - b</code></td>
<td><b>a=(A+B)-A=B</b></td>
<td>b=A</td>
</tr>
</table>
<p>The 2nd and 3rd columns show how the values change during execution, and that the end result is that the values in <i>a</i> and <i>b</i> are swapped.  It isn&#8217;t quite as neat as <i>xor</i> since you need to use two different operators (+ and -) rather than just one, but it is easier to grasp since most of us are more familiar with + and &#8211; than with <i>xor</i>.    The similarities in structure to the <i>xor</i> version above should be clear.</p>
<p>One thing to note in the above is that I have completely ignored the issues associated with finite register width.  What happens when the intermediate values generate a carry? I&#8217;ll work through an example, assuming a 4-bit register width.  We should use large numbers, since we are interested in seeing what happens when we overflow a 4-bit number.  Lets try to swap 15 and 14.  In this example, the numbers in brackets indicate what the full-precision result is, before truncating to a 4-bit unsigned representation.</p>
<table width="400">
<tr>
<td>&nbsp;</td>
<td>a=15</td>
<td>b=14</td>
</tr>
<tr>
<td><code>a = a + b</code></td>
<td><b>a=13(29)</b></td>
<td>b=14</td>
</tr>
<tr>
<td><code>b = a - b</code></td>
<td>a=13</td>
<td><b>b=15(-1)</b></td>
</tr>
<tr>
<td><code>a = a - b</code></td>
<td><b>a=14(-2)</b></td>
<td>b=15</td>
</tr>
</table>
<p>So, both a and b end up with the right (swapped) values.  This means that instead of using <i>xor</i>, we can swap values using addition and subtraction and ignoring any carry.  By truncating all the numbers in these examples to 4 bits, we are simply doing <i>modulo 16 arithmetic</i>.</p>
<p>We can go further than this, by shrinking the register width.  Instead of 4 bits, lets think about 1-bit registers (which will be <i>modulo 2 arithmetic</i>).  Rather than work through some examples, we can look at the truth tables for 1-bit addition and subtraction.  We can compare them to <i>xor</i> too.</p>
<table width="400">
<tr>
<td>&nbsp;</td>
<td align="center"><b>ADD</b></td>
<td align="center"><b>SUBTRACT</b></td>
<td align="center"><b>XOR</b></td>
</tr>
<tr>
<td>0 0</td>
<td align="center">0</td>
<td align="center">0</td>
<td align="center">0</td>
</tr>
<tr>
<td>0 1</td>
<td align="center">1</td>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td>1 0</td>
<td align="center">1</td>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td>1 1</td>
<td align="center">0</td>
<td align="center">0</td>
<td align="center">0</td>
</tr>
</table>
<p>Once you move down to single-bit operations, and ignore the carry, an odd thing happens.  Addition and subtraction become the same operation, and both are the same as an <i>xor</i>.  This <i>modulo 2</i> equivalence of operators should start to suggest another way of looking at the original <i>xor</i> formulation.  We could say that it is equivalent to performing multiple 1-bit additions/subtractions, in parallel.  In other words, it is simply the vector processing version of the addition/subtraction approach with each bit being calculated with <i>modulo 2</i> operations!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mprocter.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mprocter.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mprocter.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mprocter.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mprocter.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mprocter.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mprocter.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mprocter.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mprocter.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mprocter.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mprocter.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mprocter.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mprocter.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mprocter.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mprocter.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mprocter.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michael.procter.org.uk&amp;blog=1435391&amp;post=5&amp;subd=mprocter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://michael.procter.org.uk/2008/01/28/the-old-xor-swapping-trick/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bfb4056a662101da808d05457455504f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mprocter</media:title>
		</media:content>
	</item>
		<item>
		<title>Spin Buffers</title>
		<link>http://michael.procter.org.uk/2007/08/02/spin-buffers/</link>
		<comments>http://michael.procter.org.uk/2007/08/02/spin-buffers/#comments</comments>
		<pubDate>Thu, 02 Aug 2007 23:04:15 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[embedded]]></category>
		<category><![CDATA[spin buffers]]></category>

		<guid isPermaLink="false">http://michael.procter.org.uk/2007/08/02/spin-buffers/</guid>
		<description><![CDATA[There has been a flurry of discussion recently over the buffering technique known as Spin Buffers. Much of this has centred on the author&#8217;s claim that they eliminate the need for synchronization. This has been met with scepticism by some, and discussed in more detail by MenTaLguY. This article lists the key assumptions made by [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michael.procter.org.uk&amp;blog=1435391&amp;post=3&amp;subd=mprocter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There has been a <a href="http://programming.reddit.com/info/27bgm/comments" title="DO NOT USE comments">flurry</a> of discussion recently over the buffering technique known as <a href="http://www.ddj.com/architect/199902669" title="DDJ Article">Spin Buffers</a>.  Much of this has centred on the author&#8217;s claim that they <em>eliminate the need for synchronization</em>.  This has been met with scepticism by some, and discussed in more detail by <a href="http://moonbase.rydia.net/mental/blog/programming/spin-buffers-do-not-use.html" title="Moonbase blog">MenTaLguY</a>.  This article lists the key assumptions made by the author, and then discusses systems where these apply.  Finally, an attempt is made to find a niche for this technique &#8211; when can Spin Buffers be usefully applied?<br />
<span id="more-3"></span></p>
<p><strong>Assumptions</strong></p>
<p>The first assumption to note is that this technique should be applied to single-source/single-sink buffers.  That is to say there should be only one &#8216;setter&#8217; and one &#8216;getter&#8217; using this buffer.</p>
<p>The second assumption is that in the following sequence, the italicised items happen in order:</p>
<blockquote><p>    1.  <em>Put the item into the write buffer</em>.<br />
2. If the next buffer is free, make the free buffer become the write buffer, and the <em>current write buffer becomes free</em>.</p></blockquote>
<p>(There is a corresponding pair in the algorithm for retrieving data too.)  When this assumption breaks down, we can find both the producer and consumer accessing the same underlying buffer whilst assuming that they have exclusive access.  This assumption is the crux of the problem discussed by MenTaLguY.</p>
<p>None of these problems are insurmountable, but solving them requires the use of some form of synchronisation (from scattering <em>volatile</em> around to putting a lock or two in place).   This seems to defeat the point of Spin Buffers, so instead, let&#8217;s find some applications where the assumptions hold true.</p>
<p><strong>Embedded Systems</strong></p>
<p>One place where efficiency of buffering techniques is often paramount is within the <a href="http://en.wikipedia.org/wiki/Embedded_system" title="Embedded Systems on Wikipedia">embedded systems</a> world.  Embedded systems often use (by modern PC standards) underpowered processors, lacking in such features as multiple processors or bus transaction reordering.  This sort of environment is one in which the assumptions required for Spin Buffers can hold true.  Not all embedded platforms are like this, but they are far from being rare.</p>
<p>In this sort of environment, a traditional ring buffer is a popular choice.  By ensuring that data is written and read at the right times relative to the pointer increments, you can make a safe ring buffer without locks.  This is based on the fact that memory writes occur in a specific order, which can be verified and confirmed on many embedded platforms.</p>
<p><strong>Comparing Spin Buffers with Ring Buffers</strong></p>
<p>So, given a platform where the Spin Buffer approach would work, and where a simple ring buffer would also work, why would you choose Spin Buffers over ring buffers?  What differentiates Spin Buffers from ring buffers?</p>
<p>For Spin Buffers, you can avoid dealing with writing data over a buffer boundary (the buffer wrap problem), since you only need to write up to the end of each of the individual buffers.  This is an advantage when writing multiple elements at once, but a fairly minor one in most cases.</p>
<p>Another difference is the total capacity for a given allocation of <em>n</em> elements for buffering.  For a simple ring buffer, the total capacity is usually <em>n-1</em> (an element is often sacrificed to simplify full/empty disambiguation).  For Spin Buffers, the capacity is limited to <em>2/3 n</em>, because at any one time the &#8216;free&#8217; buffer must be empty.  Sacrificing a third of your buffer is not always possible due to memory constraints.</p>
<p>The code required to implement a Spin Buffer compared to a ring buffer is more complex.  Examining the author&#8217;s examples for both techniques shows that a Spin Buffer implementation is approximately twice as long as a ring buffer implementation.  Whilst the execution time for Spin Buffers is likely to be marginally slower, I think a more important aspect is that greater complexity often leads to more bugs and higher maintenance costs.  An example of this can be seen in the author&#8217;s implementation of the &#8216;put&#8217; routine for the Spin Buffer.  There is a bug which will cause a lock-up if the current write buffer ever becomes completely full.</p>
<p>Finally, if a partially full buffer is not written to for a period of time, then the entire Spin Buffer can seize up preventing the items in the last buffer used for writing from being read.  The author highlights this in the caveats section, along with a workaround, but this adds additional complexity.</p>
<p><strong>Conclusions</strong></p>
<p>To conclude, I think that using Spin Buffers in certain environments can be safe.  But in such environments, the advantages over a traditional ring buffer are slim and do not outweigh the costs in terms of code complexity and sacrificed capacity.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mprocter.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mprocter.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mprocter.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mprocter.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mprocter.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mprocter.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mprocter.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mprocter.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mprocter.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mprocter.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mprocter.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mprocter.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mprocter.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mprocter.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mprocter.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mprocter.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=michael.procter.org.uk&amp;blog=1435391&amp;post=3&amp;subd=mprocter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://michael.procter.org.uk/2007/08/02/spin-buffers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bfb4056a662101da808d05457455504f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mprocter</media:title>
		</media:content>
	</item>
	</channel>
</rss>
