<?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>Came for the beer, stayed for the Freedom</title>
	<atom:link href="http://audidude.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://audidude.com</link>
	<description>Programming is not a spectator sport</description>
	<lastBuildDate>Thu, 26 Aug 2010 01:09:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>GDateTime</title>
		<link>http://audidude.com/?p=477</link>
		<comments>http://audidude.com/?p=477#comments</comments>
		<pubDate>Thu, 26 Aug 2010 01:09:18 +0000</pubDate>
		<dc:creator>chergert</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Gnome]]></category>
		<category><![CDATA[Gtk]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[glib c gnome]]></category>

		<guid isPermaLink="false">http://audidude.com/?p=477</guid>
		<description><![CDATA[Some readers may remember that I spent some time working on a DateTime structure for GLib last year.  After much refinement by others better than myself, it has been included in GLib master.  Much thanks to Thiago Sousa Santos, Emmanuele Bassi, and the countless reviewers.
You can find some quick examples here.
GDateTime is both [...]]]></description>
			<content:encoded><![CDATA[<p>Some readers may remember that I spent some time working on a DateTime structure for GLib last year.  After much refinement by others better than myself, it has been included in GLib master.  Much thanks to Thiago Sousa Santos, Emmanuele Bassi, and the countless reviewers.</p>
<p>You can find some quick examples <a href="http://github.com/chergert/glib-cookbook/blob/master/datetime.c">here</a>.</p>
<p>GDateTime is both immutable and reference counted.  See <a href="http://git.gnome.org/browse/glib/tree/glib/gdatetime.h">gdatetime.h</a> for relevant API.</p>
<pre class="c"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  GDateTime *dt;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #993333;">char</span> *str;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  dt = g_date_time_new_now<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  str = g_date_time_printf<span style="color: #66cc66;">&#40;</span>dt, <span style="color: #ff0000;">&quot;It is currently %x %X %z&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  g_print<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, str<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  g_date_time_unref<span style="color: #66cc66;">&#40;</span>dt<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  g_free<span style="color: #66cc66;">&#40;</span>str<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
]]></content:encoded>
			<wfw:commentRss>http://audidude.com/?feed=rss2&amp;p=477</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Realtime Graphing (again)</title>
		<link>http://audidude.com/?p=470</link>
		<comments>http://audidude.com/?p=470#comments</comments>
		<pubDate>Mon, 09 Aug 2010 19:50:10 +0000</pubDate>
		<dc:creator>chergert</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Gnome]]></category>
		<category><![CDATA[Gtk]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[graphing]]></category>
		<category><![CDATA[x11]]></category>

		<guid isPermaLink="false">http://audidude.com/?p=470</guid>
		<description><![CDATA[As suggested by Benjamin, I switched UberGraph to use a single GdkPixmap instead of two.  Previously I copied the content back and forth between Pixmaps as I added the new second's worth of data.  That wasted memory in X as well as being unnecessary.  Now it uses a single GdkPixmap as a [...]]]></description>
			<content:encoded><![CDATA[<p>As suggested by <a href="http://blogs.gnome.org/otte/">Benjamin</a>, I switched <a href="http://github.com/chergert/uber-graph/tree/master/abstracted/">UberGraph</a> to use a single GdkPixmap instead of two.  Previously I copied the content back and forth between Pixmaps as I added the new second's worth of data.  That wasted memory in X as well as being unnecessary.  Now it uses a single GdkPixmap as a circular buffer and blits in two steps (no fear, as we get double buffering for free from gtk).</p>
<p style="text-align: center;"><img src="http://x.dronelabs.com/chris/blog/screenshots/graph/pixmap-ring.png" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://audidude.com/?feed=rss2&amp;p=470</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Realtime Graphing</title>
		<link>http://audidude.com/?p=404</link>
		<comments>http://audidude.com/?p=404#comments</comments>
		<pubDate>Fri, 06 Aug 2010 08:47:49 +0000</pubDate>
		<dc:creator>chergert</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Gnome]]></category>
		<category><![CDATA[Gtk]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://audidude.com/?p=404</guid>
		<description><![CDATA[I recently had the need for a realtime graph and thought it would be a fun hack.  I spent some time thinking about a few ways to optimize the drawing to reduce both the client and server side overhead.  Having a way to visualize data without a significant effect on system performance is [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had the need for a realtime graph and thought it would be a fun hack.  I spent some time thinking about a few ways to optimize the drawing to reduce both the client and server side overhead.  Having a way to visualize data without a significant effect on system performance is a must.</p>
<p>The result is UberGraph.  For those that care less about the how and why and more about seeing the code, you can find it <a href="http://github.com/chergert/uber-graph">here</a>.  I'm working on a few different ideas in there.  But please, keep in mind that the data collectors are ugly hacks and only used to test the widget.</p>
<p>For the impatient, here is a comparison of the drawing overhead to System Monitor.</p>
<p><a href="http://x.dronelabs.com/chris/blog/screenshots/graph/gsm-vs-uber.png"><img src="http://x.dronelabs.com/chris/blog/screenshots/graph/gsm-vs-uber-thumb.png"/></a></p>
<h2>Requirements</h2>
<p>In it's most simplist form, the graph must be able to present a grid with two scales; the x and y axis.  Presented upon these scales are the samples which will animate right-to-left.  In the <b>most unoptimized form</b>, this seems like a simple problem and we might as well start to attack it like such and optimize from there.</p>
<ol>
<li>Create a new widget inheriting from <a href="http://library.gnome.org/devel/gtk/2.90/GtkDrawingArea.html">GtkDrawingArea</a>.</li>
<li>On the expose event, draw the entire graph (clipping to exposure area).  The graph contents are retrieved from an array of nodes in the graph. The value is scaled to the pixel range and drawing using <a href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve">Bézier curves</a> with <a href="http://cairographics.org/samples/curve_to/">cairo_curve_to</a>().</li>
<li>Invalide the widget contents after (1 / Frames-Per-Second) seconds have passed; forcing another exposure.</li>
</ol>
<h2>Implementation</h2>
<p>This is essentially how the graphs are done Gnome System Monitor.  I don't mean to pick on G-S-M, but it was an interesting problem to me (being that the CPU usage every time I ran G-S-M skewed my results) and it was a good learning experience.</p>
<p>There are many places for which this can be optimized.  To begin with, it is important to consider the design of the X11 protocol and how to take advantage of it.  X11 provides pixmaps, which are a "server-side" texture.  This means you push your content to the server over the X11 protocol, and then reference by a unique-id going forward.  If designed correctly, we can exploit this to do the sliding animation efficiently without sending a new image for every frame.  This results in less work for X, less work for our process, and less bandwidth to X (resulting in better performance over ssh -X).  When you draw onto a <a href="http://library.gnome.org/devel/gdk/2.90/gdk3-Windows.html">GdkWindow</a>, you are creating the pixmap on the client, and sending the contents over the wire to the X server via the X11 protocol and exposing it to the window.  Now, if you are drawing a graph on every exposure (up to say 20 frames per second), you can imagine how this can take a hit on your systems performance. You are rendering the pixmap, encoding the request, delivering it to the server (via UNIX/TCP socket), decoding the request, loading the pixmap, and then copying it into the framebuffer (through the driver infrastructure).  Thats a lot of work to do 20x per second.  Even more so over ssh forwarding.</p>
<blockquote><p>If you ever want to get a <b>quick and dirty</b> idea of the amount of data you are sending to the X server, use strace.  "strace -ewritev" seems to do the trick for me.  The X socket typically ends up as FD 3 in my experience.  Just grab the last value in the line for number of bytes sent.  Sadly, I haven't found an easy way to determine this with xtrace, yet.</p></blockquote>
<p>To allow us to get some of the desired speedups, we must first split the foreground and background into separate pixmaps.  This way, the only content we ever need to continually ship to the X server is the foreground.  The background is an opaque pixmap and the foreground will have mostly transparent pixels (which should compress well too!).  Thanks to cairo, we can easily composite the transparent pixmap onto the background using <a href="http://library.gnome.org/devel/gdk/stable/gdk-Cairo-Interaction.html#gdk-cairo-set-source-pixmap">gdk_cairo_set_source_pixmap</a>().  Alternatively, on older systems you can blend using the <a href="http://library.gnome.org/devel/gdk/stable/gdk-Graphics-Contexts.html#GDK-XOR:CAPS">GDK_XOR</a> operation (which will only blit a portion of the foreground based on its comparison to the background).  When using XOR, you need to pre XOR your drawing colors so they result in the proper color.</p>
<p>So now that we have our foreground and background separated, we can move on to getting some decent CPU savings.  If we make one small sacrifice, we can keep the CPU from doing a significant amount of work repeatedly.  This sacrifice is allowing ourself up to 1 second of latency between the time of a sample and presenting it on the screen.  This is useful because it allows us to render the new data outside the visible range and slide the pixmap right-to-left on the server.  Doing this means we only draw once per second, rather than 20 times.</p>
<p>Lets take a quick look at what I mean.</p>
<p><img src="http://x.dronelabs.com/chris/blog/screenshots/graph/overview.png"/></p>
<p>Using this model, we can render the new version of the graph and send the pixmap to the server.  Then, in our frame-expired callback every (1 / Frames-Per-Second) seconds, we simply adjust the position at which the pixmap is rendered by the horizontal distance between frames.</p>
<p>At 20 frames-per-second, this saves us 19 iterations of rendering, encoding, sending, decoding, and copying to the framebuffer per second.  Instead, we simply tell the server that we wish to render the pixmap at a given offset and it will copy it into the framebuffer for us.</p>
<p>However, why should we send the entire content once per second?  Surely the historical data hasn't changed.  In fact, if we use two pixmaps on the server-side to flip between, we can get another large X bandwidth savings.  We start by copying the first pixmap into the second pixmap at its new offset.  Then, on the client, we render the new second worth of content (properly clipped with cairo_clip()) and send it to the X server.  The content is moved to the "New Data" region of the pixmap and then animated into view over the following second.  This saves us <i>((Width - WidthPerSecond) * Height)</i> pixels to send across the wire every second.  This is good since that will often exceed the size of a single packet (again, helping over ssh -X).  Locally, the speedup is modest.  However, remotely it allows us to provide almost the same experience even on low-bandwidth links (ssh -X from my n900 via AT&T Edge for example).</p>
<p>Autoscaling the graph to handle values outside the visible range is done by checking the value bounds when retrieving the value.  Scaling down is done during a callback to see if we can shrink (currently every 5 seconds).  No sense in having this in a hot path, it's simply not that important.</p>
<p>It is worth adding, that while I mention X throughout this post, this should work similarly on backends other than X11 since GdkPixmap abstracts that.</p>
<p><a href="http://github.com/chergert/uber-graph">Fork me on Github</a>.</p>
<p>Thanks for reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://audidude.com/?feed=rss2&amp;p=404</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Conquering DBus</title>
		<link>http://audidude.com/?p=387</link>
		<comments>http://audidude.com/?p=387#comments</comments>
		<pubDate>Thu, 13 May 2010 04:05:36 +0000</pubDate>
		<dc:creator>chergert</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://audidude.com/?p=387</guid>
		<description><![CDATA[I haven't written in a while because I've been pretty heads down on a project I'm working on.  Hopefully, it will be in a shape where it makes sense to share with everyone before too long.
In this project, I needed an RPC system that could have multiple transport and be fully asynchronous (with GAsyncResult, [...]]]></description>
			<content:encoded><![CDATA[<p>I haven't written in a while because I've been pretty heads down on a project I'm working on.  Hopefully, it will be in a shape where it makes sense to share with everyone before too long.</p>
<p>In this project, I needed an RPC system that could have multiple transport and be fully asynchronous (with <a href="http://library.gnome.org/devel/gio/stable/GAsyncResult.html">GAsyncResult</a>, etc).  So I took a little time and wrote a crappy little python program that generates the following peices in GObject/C from a simple input format.</p>
<ul>
<li>Transport agnostic (currently has DBus backend).
<li>A client library with lowlevel RPC calls and an object model built on top of the lowlevel calls.  Consumer can choose which best fits their use case. (Object model not finished).</li>
<li>A server implementation (also transport agnostic).</li>
<li>Fully Asynchronous.  Synchronous implementation is built on top of the asynchronous implementation.</li>
<li>Unit tests (for testing RPC round trips, etc).</li>
<li>Generated code should be clean to read and debug.</li>
<li>Tracing support for debugging. (Use -DDISABLE_TRACING to turn off).</li>
<li>Generate DBus Introspection XML.</li>
</ul>
<p>Anyway, code is <a href="http://github.com/chergert/yosemite">here</a>[1].  There is a sample video below where I generate a mockup service for getting information on "Books" and "Authors".  See the Makefile in the outdir directory for how to build things.  Keep in mind I have absolutely <b>NO INTENTION</b> of maintaining this.  But at minimum, I figured it might serve useful for someone.</p>
<p><video controls="controls" src="http://x.dronelabs.com/chris/blog/screencasts/rpcgen1.ogv"><a href="http://x.dronelabs.com/chris/blog/screencasts/rpcgen1.ogv">Watch the video here</a>.</video></p>
<p>So a quick example of the imput format would be like the following.  I've removed the header lines for succinctness.</p>
<pre class="python"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #008000;">object</span> Book <span style="color: black;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #808080; font-style: italic;"># books are identified by a unique int (string also supported).</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #008000;">id</span> <span style="color: #008000;">int</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">      Document your rpc's with python style docs.  Comments persist into</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">      the generated code's gtkdoc.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">      &quot;</span><span style="color: #483d8b;">&quot;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      rpc get_name <span style="color: black;">&#40;</span>out <span style="color: #dc143c;">string</span> name<span style="color: black;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">      Retrieves the book's author.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">      &quot;</span><span style="color: #483d8b;">&quot;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      rpc get_author <span style="color: black;">&#40;</span>out Author author<span style="color: black;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: black;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #008000;">object</span> Author <span style="color: black;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #008000;">id</span> <span style="color: #008000;">int</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">      Retrieves the books by this author.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">      &quot;</span><span style="color: #483d8b;">&quot;&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      rpc get_books <span style="color: black;">&#40;</span>out Book<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> books<span style="color: black;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: black;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #008000;">object</span> Manager <span style="color: black;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #808080; font-style: italic;"># only one of these per application (/org/blah/Project/Manager).</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #808080; font-style: italic;"># might consider this later to simply be the default servce such as</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #808080; font-style: italic;"># (/org/blah/Project).</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      singleton</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      rpc get_books <span style="color: black;">&#40;</span>out Book<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> books<span style="color: black;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      rpc get_authors <span style="color: black;">&#40;</span>out Author<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> authors<span style="color: black;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: black;">&#125;</span></div></li></ol></pre>
<p>Thats pretty much it.  The code is uglier than anything you've ever seen, but at least the generated C is pretty.  The code parser/generator is not super resilient or anything.  I pretty much hack it to have what I need as I go.</p>
<p>[1] <a href="http://github.com/chergert/yosemite">http://github.com/chergert/yosemite</a></p>
]]></content:encoded>
			<wfw:commentRss>http://audidude.com/?feed=rss2&amp;p=387</wfw:commentRss>
		<slash:comments>5</slash:comments>
<enclosure url="http://x.dronelabs.com/chris/blog/screencasts/rpcgen1.ogv" length="7925500" type="video/ogg" />
		</item>
		<item>
		<title>Random Updates</title>
		<link>http://audidude.com/?p=366</link>
		<comments>http://audidude.com/?p=366#comments</comments>
		<pubDate>Tue, 09 Feb 2010 22:27:56 +0000</pubDate>
		<dc:creator>chergert</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Gnome]]></category>
		<category><![CDATA[Gtk]]></category>
		<category><![CDATA[perfkit]]></category>
		<category><![CDATA[protobuf]]></category>
		<category><![CDATA[yosemite]]></category>

		<guid isPermaLink="false">http://audidude.com/?p=366</guid>
		<description><![CDATA[It's been a while, so I figured I'd just jot a few things down that I've been doing in my spare time.

Went to Yosemite again in January.   We usually go about 3 times a year.  We have a bigger trip coming up in March.  If you are in the area and want to join, [...]]]></description>
			<content:encoded><![CDATA[<p>It's been a while, so I figured I'd just jot a few things down that I've been doing in my spare time.</p>
<ul>
<li>Went to Yosemite again in January.   We usually go about 3 times a year.  We have a bigger trip coming up in March.  If you are in the area and want to join, contact me.  My email is available in my source code.</li>
<li><a title="EggBuffer - Protocol Buffer Reader/Writer" href="http://github.com/chergert/egg-buffer" target="_blank">EggBuffer</a> - A simple reader/writer for the protocol-buffer wire format.  This does not include a message generator or anything.  I simply wanted to be able to crack open message blobs.</li>
<li><a title="A set of GObject C code generators" href="http://github.com/chergert/generators" target="_blank">generators</a> - A small set of basic code generators I use to bootstrap new objects/structs/licenses/etc.  I'd like to convert these to that fancy vim snippet plugin.</li>
<li><a title="EggFmt - A mysql style table formatter" href="http://github.com/chergert/egg-fmt" target="_blank">EggFmt</a> - A crappy little formatter helper for doing mysql style tables in console.</li>
<li><a title="EggLine - Simplified interface to readline" href="http://github.com/chergert/egg-line" target="_blank">EggLine</a> - A simplified, glib-ish, interface to libreadline.</li>
</ul>
<p>As soon as it's ready, I'll share the project I'm using these tools to build.  Feel free to speculate.</p>
<p>Also, I'll be back in LA, my favorite home, for <a href="http://www.socallinuxexpo.org/scale8x/">Scale8x</a> February 19-22nd.  Your rockstar Gnome Sys-Admin, <a href="http://www.digitalprognosis.com/" target="_blank">Jeff Schroeder</a>, and I will be hosting the Gnome booth along with <a href="http://jordanlarrigan.com" target="_blank">Jordan</a>.</p>
<p><a href="http://www.flickr.com/photos/jordanlarrigan/4311260293/in/set-72157601307169888/"><img class="aligncenter" src="http://farm3.static.flickr.com/2516/4311260293_34397065cc.jpg" alt="" width="333" height="500" /></a></p>
<p><a href="http://www.flickr.com/photos/jordanlarrigan/4312004096/in/set-72157601307169888/"><img class="aligncenter" src="http://farm5.static.flickr.com/4021/4312004096_22279a9729.jpg" alt="" width="500" height="333" /></a></p>
<p><a href="http://www.flickr.com/photos/jordanlarrigan/4322259079/in/set-72157601307169888/"><img class="aligncenter" src="http://farm3.static.flickr.com/2703/4322259079_e689ccf694.jpg" alt="" width="333" height="500" /></a></p>
<p><a href="http://www.flickr.com/photos/jordanlarrigan/4322259275/in/set-72157601307169888/"><img class="aligncenter" src="http://farm3.static.flickr.com/2738/4322259275_26a3f8e989.jpg" alt="" width="500" height="333" /></a></p>
<p><a rel="cc:attributionURL" href="http://www.flickr.com/photos/jordanlarrigan/">http://www.flickr.com/photos/jordanlarrigan/</a> / <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/2.0/">CC BY-NC-SA 2.0</a></p>
]]></content:encoded>
			<wfw:commentRss>http://audidude.com/?feed=rss2&amp;p=366</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Introduction to lock-free/wait-free and the ABA problem</title>
		<link>http://audidude.com/?p=363</link>
		<comments>http://audidude.com/?p=363#comments</comments>
		<pubDate>Fri, 25 Dec 2009 10:07:53 +0000</pubDate>
		<dc:creator>chergert</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[lock-free]]></category>
		<category><![CDATA[wait-free]]></category>

		<guid isPermaLink="false">http://audidude.com/blog/?p=363</guid>
		<description><![CDATA[I recently read a paper on Hazard Pointers[2] and thought I would share what I learned and some general information on lock-free/wait-free programming.  I hope it is useful to others getting started.
I'm not very clever, so keep in mind this is mostly regurgitating knowledge I've found in various places.  If you find anything [...]]]></description>
			<content:encoded><![CDATA[<p>I recently read a paper on Hazard Pointers[2] and thought I would share what I learned and some general information on lock-free/wait-free programming.  I hope it is useful to others getting started.</p>
<p>I'm not very clever, so keep in mind this is mostly regurgitating knowledge I've found in various places.  If you find anything wrong or that could use more/better explaination leave a comment and we can cooperatively improve this.</p>
<h2>Traditional problems in Mutli-Processor programming</h2>
<p>As we reached the speed-limit of serialized CPU instructions we started to add more cores.  Obviously, adding more cores means we have to change how we process information.  Instead of optimizing for the fastest serial processing of data, we look for ways to split the work into multiple tasks.  That way, we can put tasks on different cores and efficiently use the resources available.</p>
<p>So now we have lots of individual tasks.  It would be most excellent if they could communicate.  Often times we end up using a Mutex or other synchronization primitive to lock access to some shared state.  Unfortunately, this doesn't scale well as you add more concurrent workers.  This ends up creating more lock-contention and reduces the overall usefulness of splitting the problem into tasks.</p>
<p>What if we could communicate to manage shared state without introducing locks?  That is precisely what some very smart folks have been designing for years.  Now the rest of us are catching up.</p>
<h2>What are lock-free algorithms</h2>
<p>Lock-free algorithms provide a way to perform operations on shared state without the need to perform costly synchronization between threads.  Think about the times you've used a mutex to protect access to shared state such as a queue.  In highly-threaded scenarios you probably noticed CPU time wasted in lock-contention (often "time in kernel").  There are many types of synchronization, but many of these result in the same perception by users: inefficient use of their computing resources.  That is not very green.  The polar bears are crying.</p>
<p>Lets think for a moment about why there is so much CPU time wasted under traditional locking scenarios.  For the sake of simplicity, lets build a very simple spinlock.</p>
<pre class="c"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #993333;">int</span> s;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> spinlock_t;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">void</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">spinlock_lock<span style="color: #66cc66;">&#40;</span>spinlock_t *l<span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>!g_atomic_int_compare_and_exchange<span style="color: #66cc66;">&#40;</span>&amp;l-&gt;s, <span style="color: #000000; font-weight: bold;">FALSE</span>, <span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">void</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">spinlock_unlock<span style="color: #66cc66;">&#40;</span>spinlock_t *l<span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	l-&gt;s = <span style="color: #000000; font-weight: bold;">FALSE</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p>Experienced readers will forgive me for the naivete.  What essentially happens is that we try to set the lock bit of the structure until we succeed; potentially blocking for long periods of time (in terms of CPU cycles).  Not to mention that the CPU will spin up at full speed and waste power in the meantime.</p>
<p>Why is this generally a bad approach?  Well, to begin with, this is just a synchronization primitive to acquire our lock.  Once acquired, the critical section is performed.  Meanwhile, all those other threads are still spinning!  If only they were doing something constructive!  In addition, each Compare and Swap (CAS here-forward) requires a Memory Barrier.  This means that each call to the method requires that the cache line containing the structure be flushed from the CPU cache and fetched from main-memory.  This alone can take hundreds of CPU cycles and now each thread is doing it repeatedly.</p>
<p>Now, that doesn't mean that CAS is a scary or bad thing!  We just need to use it more appropriately.</p>
<p>For good measure I should mention that there are times when spin-locks and variants there-of are the right data structure.  For example, many locks now days are complex and contain multiple steps such as spinning for a short period of time followed by a sleep back-off upon failure.</p>
<p>This non-deterministic behavior is precisely what lock-free algorithms are designed to avoid.  Lock-free algorithms are carefully designed data-structures and functions to allow for multiple threads to attempt to make progress independently of one-another.  This means that you do not try to acquire a lock before performing your critical region.  Instead, you independently update a local copy of a portion of the data-structure and then apply it atomically to the shared structure with a CAS.</p>
<h2>Wait-Free, a better Lock-Free</h2>
<p>Those experienced with lock-free programming will likely know of a superset called wait-free programming.  Wait-free is the same as lock-free except it provides a stellar guarantee.  Each thread is guaranteed to be progressing itself or a cooperative thread.  This is an important phenomenon because wait-free uses concurrent access to a data-structure to cooperatively progress the data-structure as a whole.</p>
<h2>Constructing Lock-Free/Wait-Free algorithms</h2>
<p>Lets take a look a lock-free/wait-free implementation of a Queue.  This is a fairly strait-forward implementation of the design by M.M. Michael and M.L. Scott[1].</p>
<pre class="c"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> _Node Node;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> _Queue Queue;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">struct</span> _Node <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #993333;">void</span> *data;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Node *next;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">struct</span> _Queue <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Node *head;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Node *tail;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Queue*</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">queue_new<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">void</span><span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Queue *q = g_slice_new<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #66cc66;">&#40;</span>Queue<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	q-&gt;head = q-&gt;tail = g_slice_new0<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #66cc66;">&#40;</span>Node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> q;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">void</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">queue_enqueue<span style="color: #66cc66;">&#40;</span>Queue *q,</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">              gpointer data<span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Node *node, *tail, *next;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	node = g_slice_new<span style="color: #66cc66;">&#40;</span>Node<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	node-&gt;data = data;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	node-&gt;next = <span style="color: #000000; font-weight: bold;">NULL</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		tail = q-&gt;tail;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		next = tail-&gt;next;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>tail != q-&gt;tail<span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">continue</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>next != <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			CAS<span style="color: #66cc66;">&#40;</span>&amp;q-&gt;tail, tail, next<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">continue</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>CAS<span style="color: #66cc66;">&#40;</span>&amp;tail-&gt;next, <span style="color: #000000; font-weight: bold;">null</span>, node<span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #000000; font-weight: bold;">break</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	CAS<span style="color: #66cc66;">&#40;</span>&amp;q-&gt;tail, tail, node<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">gpointer</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">queue_dequeue<span style="color: #66cc66;">&#40;</span>Queue *q<span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Node *node, *tail, *next;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		head = q-&gt;head;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		tail = q-&gt;tail;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		next = head-&gt;next;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>head != q-&gt;head<span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">continue</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>next == <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>; <span style="color: #808080; font-style: italic;">// Empty</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>head == tail<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			CAS<span style="color: #66cc66;">&#40;</span>&amp;q-&gt;tail, tail, next<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">continue</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		data = next-&gt;data;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>CAS<span style="color: #66cc66;">&#40;</span>&amp;q-&gt;head, head, next<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #000000; font-weight: bold;">break</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	g_slice_free<span style="color: #66cc66;">&#40;</span>Node, head<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// This isn't safe, we'll discuss why.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> data;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p>The FIFO queue is constructed using a singly-linked list.  We keep track of the head and tail of the queue.  Because the structure uses a linked-list, we can CAS the node's next pointer to atomically perform updates.</p>
<p>I won't go into too much detail on the implementation of the queue.  The gist is this.  To enqueue a new node, we work locally to prepare the new node and try to apply it atomically to the tail.  If we detect an incomplete operation we help cleanup after it.  To dequeue, we try to take the first item off the queue atomically.  Again, we help proceed an inconsistent state.</p>
<h2>The ABA Problem</h2>
<p>There is an interesting problem that arises in this algorithm.  What happens if a node is removed, de-allocated (through free() or similar), re-allocated (through malloc() or similar) and added back; all within the time in which another thread is paused?  If that other thread is about to perform a CAS using that pointer it could still succeed even though the state has changed!  This is what is known as the <a href="http://en.wikipedia.org/wiki/ABA_problem">ABA problem</a> in lock-free programming.</p>
<p>In garbage collected languages this isn't a problem.  Why?  Because the node's memory cannot be reclaimed for a new object until observing threads containing pointers to the structure have released them.</p>
<p>In C, however, we don't typically have the luxury of a garbage collector.  Historically, there have been a few approaches to work around this.  The original approach, by IBM, was to use a tag next to the pointer to be swapped.  They would increment that tag counter and use a double-word CAS.  While many 32-bit machines provide a 64-bit CAS, most 64-bit machines do not provide a 128-bit CAS.  This prevents it from being a truly useful solution for general use.</p>
<p>My concurrency library, libiris[4], currently does something similar to this.  By aligning the pointers properly you can use portions of the pointer itself for tagging.  This isn't scalable as you add workers, but it was a start.</p>
<p>But, ever the pragmatist that I am, I thought it was time to research the proper way to handle the situation.  Turns out there is a fantastic paper written in 2004 on a methodology called Hazard Pointers[2].  Hazard pointers are a way of notifying cooperative threads that you are removing some potentially unsafe structure.  Later, a reclamation step is performed which can free the memory once it is safe to do so.</p>
<p>To make these lock-free/wait-free algorithms ABA safe we can notify cooperative threads using these hazard pointers.  I've implemented the  hazard pointer methodology from the paper <a href="http://github.com/chergert/dukes_of_hazard/">here</a>.  We can include that and alter the algorithm slightly.</p>
<pre class="c"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">void</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">queue_enqueue<span style="color: #66cc66;">&#40;</span>Queue *q,</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">              gpointer data<span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Node *node, *tail, *next;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	node = g_slice_new<span style="color: #66cc66;">&#40;</span>Node<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	node-&gt;data = data;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	node-&gt;next = <span style="color: #000000; font-weight: bold;">NULL</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		tail = q-&gt;tail;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		HAZARD_SET<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, tail<span style="color: #66cc66;">&#41;</span>;              <span style="color: #808080; font-style: italic;">// Mark tail has hazardous</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>tail != q-&gt;tail<span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">// Check tail hasn't changed</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">continue</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		next = tail-&gt;next;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>tail != q-&gt;tail<span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">continue</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>next != <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			CAS<span style="color: #66cc66;">&#40;</span>&amp;q-&gt;tail, tail, next<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">continue</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>CAS<span style="color: #66cc66;">&#40;</span>&amp;tail-&gt;next, <span style="color: #000000; font-weight: bold;">null</span>, node<span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #000000; font-weight: bold;">break</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	CAS<span style="color: #66cc66;">&#40;</span>&amp;q-&gt;tail, tail, node<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">gpointer</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">queue_dequeue<span style="color: #66cc66;">&#40;</span>Queue *q<span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Node *tail, *next, *head;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		head = q-&gt;head;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		LF_HAZARD_SET<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, head<span style="color: #66cc66;">&#41;</span>;    <span style="color: #808080; font-style: italic;">// Mark head as hazardous</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>head != q-&gt;head<span style="color: #66cc66;">&#41;</span>       <span style="color: #808080; font-style: italic;">// Check head hasn't changed</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">continue</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		tail = q-&gt;tail;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		next = head-&gt;next;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		LF_HAZARD_SET<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, next<span style="color: #66cc66;">&#41;</span>;    <span style="color: #808080; font-style: italic;">// Mark next has hazardous</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>head != q-&gt;head<span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">continue</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>next == <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>; <span style="color: #808080; font-style: italic;">// Empty</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>head == tail<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			CAS<span style="color: #66cc66;">&#40;</span>&amp;q-&gt;tail, tail, next<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">continue</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		data = next-&gt;data;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>CAS<span style="color: #66cc66;">&#40;</span>&amp;q-&gt;head, head, next<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #000000; font-weight: bold;">break</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	LF_HAZARD_UNSET<span style="color: #66cc66;">&#40;</span>head<span style="color: #66cc66;">&#41;</span>;        <span style="color: #808080; font-style: italic;">// Retire head, and perform</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                                      <span style="color: #808080; font-style: italic;">// reclamation if needed.</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> data;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p>The paper discusses a few other algorithms too which I'd like to add to the repository.  Including a Set and LIFO Stack.  After which, I hope to do some merging and refactoring of this into libiris[5].  I want to make it easy for gtk+ applications to be fully asynchronous from within the main-loop.  Doing synchronous IO, or other blocking operations from the main-loop is unacceptable as far as I'm concerned.</p>
<p>--</p>
<p>[1] M.M. Michael and M.L. Scott, “Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms,” Proc. 15th Ann. ACM Symp. Principles of Distributed Computing, pp. 267-275, May 1996.<br />
[2] IEEE Transactions on Parallel and Distributed Systems, VOL. 15, NO. 6, June 2004 Page 491<br />
[3] <a href="http://github.com/chergert/dukes_of_hazard">http://github.com/chergert/dukes_of_hazard</a><br />
[4] <a href="http://git.dronelabs.com/iris">http://git.dronelabs.com/iris</a></p>
]]></content:encoded>
			<wfw:commentRss>http://audidude.com/?feed=rss2&amp;p=363</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>readline</title>
		<link>http://audidude.com/?p=356</link>
		<comments>http://audidude.com/?p=356#comments</comments>
		<pubDate>Mon, 09 Nov 2009 11:01:05 +0000</pubDate>
		<dc:creator>chergert</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://audidude.com/blog/?p=356</guid>
		<description><![CDATA[I made a crappy little abstraction on top of readline to make things simpler for myself.  Someone might find it useful.
#include &#34;egg-line.h&#34;// ...  EggLine *line = egg_line_new &#40;&#41;;  egg_line_set_prompt &#40;line, &#34;Linux-Router&#62; &#34;&#41;;  egg_line_set_entries &#40;line, completion_tree&#41;;  egg_line_run &#40;line&#41;;// ...&#160;
Code: http://github.com/chergert/egg-line/
Example: http://github.com/chergert/egg-line/blob/master/main.c
]]></description>
			<content:encoded><![CDATA[<p>I made a crappy little abstraction on top of readline to make things simpler for myself.  Someone might find it useful.</p>
<pre class="c"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339933;">#include &quot;egg-line.h&quot;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">// ...</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  EggLine *line = egg_line_new <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  egg_line_set_prompt <span style="color: #66cc66;">&#40;</span>line, <span style="color: #ff0000;">&quot;Linux-Router&gt; &quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  egg_line_set_entries <span style="color: #66cc66;">&#40;</span>line, completion_tree<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  egg_line_run <span style="color: #66cc66;">&#40;</span>line<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">// ...</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>Code: <a href="http://github.com/chergert/egg-line/">http://github.com/chergert/egg-line/</a><br />
Example: <a href="http://github.com/chergert/egg-line/blob/master/main.c">http://github.com/chergert/egg-line/blob/master/main.c</a></p>
]]></content:encoded>
			<wfw:commentRss>http://audidude.com/?feed=rss2&amp;p=356</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rapid Prototyping Tools</title>
		<link>http://audidude.com/?p=350</link>
		<comments>http://audidude.com/?p=350#comments</comments>
		<pubDate>Wed, 07 Oct 2009 06:58:42 +0000</pubDate>
		<dc:creator>chergert</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://audidude.com/blog/?p=350</guid>
		<description><![CDATA[It's nice to see people working on meta recently.  For example, Quickly.  After some long discussions with an architect at my last job, I hacked up something drastically different for myself.  It's a graphical tool to combine lots of meta pieces together from visual designs.
However, my available time keeps shrinking so I [...]]]></description>
			<content:encoded><![CDATA[<p>It's nice to see people working on meta recently.  For example, Quickly.  After some long discussions with an architect at my last job, I hacked up something drastically different for myself.  It's a graphical tool to combine lots of meta pieces together from visual designs.</p>
<p>However, my available time keeps shrinking so I thought I'd share it just to keep the meme going.  The idea was to write a meta-toolbox for myself that I can continually extend as I go and eventually write entire programs using it.</p>
<p>The gist is this.  Create nodes in the system. Tag them with information. Run the analyzer+generator against them.  The analyzer infers lots of things based on patterns I know and see in my head.  This allows for minimal time between cognition and implementation.</p>
<p>The input is simple as you'll see, the output is a ready to go project, runnable code in C, dbus support, threading or message passing, classes, properties, signals, methods, documentation, etc, etc.</p>
<p>Video is best watched fullscreen.</p>
<p><video src="http://x.dronelabs.com/chris/blog/screencasts/cosmos-1.ogv" width="450" controls="controls"><br />
Your browser doesn't support HTML5. Get the video <a href="http://x.dronelabs.com/chris/blog/screencasts/cosmos-1.ogv">here</a>.<br />
</video></p>
<p>My code isn't really in a shape to share as it isn't really useful unless your brain works exactly as mine, but you should be able to hack up your own in minimal time.</p>
]]></content:encoded>
			<wfw:commentRss>http://audidude.com/?feed=rss2&amp;p=350</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>GDateTime</title>
		<link>http://audidude.com/?p=342</link>
		<comments>http://audidude.com/?p=342#comments</comments>
		<pubDate>Sat, 03 Oct 2009 21:30:16 +0000</pubDate>
		<dc:creator>chergert</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://audidude.com/blog/?p=342</guid>
		<description><![CDATA[I got fed up with working around all the limitations with date and time in glib and C (time_t, struct timeval, struct tm, GTimeVal, GDate, etc) so I decided to write something new.
The result is GDateTime.  It handles dates and times from 1/1/1 to 12/31/9999 on 100-nanosecond intervals.  As requested by a few [...]]]></description>
			<content:encoded><![CDATA[<p>I got fed up with working around all the limitations with date and time in glib and C (time_t, struct timeval, struct tm, GTimeVal, GDate, etc) so I decided to write something new.</p>
<p>The result is GDateTime.  It handles dates and times from 1/1/1 to 12/31/9999 on 100-nanosecond intervals.  As requested by a few individuals, it is an opaque/boxed type.  String parsing is incomplete but the rest should be in good shape.</p>
<p>The glib branch is available <a href="http://git.dronelabs.com/glib/?h=datetime">here</a>.  Or for those with short attention spans, the header <a href="http://git.dronelabs.com/glib/tree/glib/gdatetime.h?h=datetime">gdatetime.h</a>.</p>
<p>You can even use GDateTimes as keys within a GHashTable using g_date_time_hash, g_date_time_equal.</p>
<p><code lineno="1">GHashTable *hash = g_hash_table_new_full (g_date_time_hash, g_date_time_equal, g_date_time_free, NULL);</code></p>
<p>I'm interested in feedback and patches.</p>
]]></content:encoded>
			<wfw:commentRss>http://audidude.com/?feed=rss2&amp;p=342</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>New Job</title>
		<link>http://audidude.com/?p=340</link>
		<comments>http://audidude.com/?p=340#comments</comments>
		<pubDate>Wed, 30 Sep 2009 02:01:50 +0000</pubDate>
		<dc:creator>chergert</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://audidude.com/blog/?p=340</guid>
		<description><![CDATA[I'm now working at VMWare.  I'm half way in the process of moving to San Francisco and should be up in the SoMa area full-time by next week.
]]></description>
			<content:encoded><![CDATA[<p>I'm now working at VMWare.  I'm half way in the process of moving to San Francisco and should be up in the SoMa area full-time by next week.</p>
]]></content:encoded>
			<wfw:commentRss>http://audidude.com/?feed=rss2&amp;p=340</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
