<?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>luminance &#187; concurrency</title>
	<atom:link href="http://www.luminance.org/tag/concurrency/feed" rel="self" type="application/rss+xml" />
	<link>http://www.luminance.org/blog</link>
	<description>Programming and Game Development - Kevin Gadd&#039;s Blog</description>
	<lastBuildDate>Sun, 02 Oct 2011 00:15:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Threaded Renderer</title>
		<link>http://www.luminance.org/blog/gruedorf/2009/07/20/threaded-renderer</link>
		<comments>http://www.luminance.org/blog/gruedorf/2009/07/20/threaded-renderer#comments</comments>
		<pubDate>Tue, 21 Jul 2009 05:07:44 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Gruedorf]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=585</guid>
		<description><![CDATA[Lots of changes since last week, but the majority of my efforts were focused on overhauling my approach to rendering. My basic approach is primarily inspired by Christer Ericson&#8217;s post about the approach he uses for ordering draw calls. Some other useful sources of information were Tom Forsyth&#8217;s post about the cost of renderstate changes [...]]]></description>
			<content:encoded><![CDATA[<p>Lots of changes since last week, but the majority of my efforts were focused on overhauling my approach to rendering.</p>
<div class="video"><object width="640" height="385" data="http://www.youtube-nocookie.com/v/stiPgEW6AJI&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;hd=1" type="application/x-shockwave-flash"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube-nocookie.com/v/stiPgEW6AJI&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;hd=1" /><param name="allowfullscreen" value="true" /></object></div>
<p>My basic approach is primarily inspired by <a href="http://realtimecollisiondetection.net/blog/?p=86">Christer Ericson&#8217;s post about the approach he uses for ordering draw calls</a>. Some other useful sources of information were <a href="http://home.comcast.net/~tom_forsyth/blog.wiki.html#[[Renderstate%20change%20costs]]">Tom Forsyth&#8217;s post about the cost of renderstate changes</a> and <a href="http://www.guerrilla-games.com/publications/dr_kz2_rsx_dev07.pdf">Guerilla&#8217;s presentation on Killzone 2&#8242;s renderer from DEVELOP 2007</a>. The Killzone 2 presentation is especially worthwhile since it describes the way they take advantage of concurrency techniques in their renderer.</p>
<p>Until now, the rendering code in my game has been almost entirely immediate-mode; various objects in the game world like tiles and entities had Draw methods that would utilize the game&#8217;s GraphicsDevice and SpriteBatch to render themselves, changing render states as needed and generating dynamic geometry where necessary (like for water).</p>
<p><span id="more-585"></span></p>
<p>Of course, if every object changes render states, you quickly run into a situation where you&#8217;re changing states tens of thousands of times per frame, which doesn&#8217;t perform very well. As a result, I ended up having to add some basic support for grouping together identical render states. In the end, I often had to perform multiple passes over the scene in order to minimize state changes, and the addition of multiple onscreen &#8216;layers&#8217; meant that I often had to make half a dozen passes over the same collections (like Level.Entities and Level.Geometry) in order to render things in the correct order without too many state changes. My new renderer clearly needed to provide an inexpensive way to reduce state changes without complicating my code.</p>
<p>In addition to the issue of state changes, however, was the simple fact that rendering was too expensive. In particular, rendering all the onscreen tiles for the 6+ layers in the level could often add up to 5%+ of my CPU time, due to the relative inefficiency of the XNA SpriteBatch class when trying to draw thousands of bitmaps at various locations on the screen. The use of SpriteBatch also meant that I couldn&#8217;t effectively sort tiles by texture, because using a custom blending configuration with SpriteBatch requires you to disable its texture sorting support. (My tiles and sprites have to be premultiplied so they look correct when scaled, unfortunately.) As a result, my new renderer needed to minimize the cost of individual drawing operations, and it also needed to give me a way to utilize material/texture sorting with custom blending configurations.</p>
<p>One final detail that factored into the design was concurrency: Between the cost of actually rendering the game world, and the cost of waiting for vertical sync, the main thread was spending as much as 40% of its time performing rendering, which made it much harder to maintain a smooth, consistent 60fps framerate. In order to minimize the amount of time the main thread spent blocked during rendering, my new design needed to make it possible to move rendering work off the main thread without introducing thread safety issues &#8211; putting a lock around all my game state wasn&#8217;t going to cut it.</p>
<hr />The design I ended up with roughly looks like this:</p>
<p>Every frame, at the beginning of my Draw method I construct a new Render.Frame object to represent the frame that&#8217;s being rendered.</p>
<p>The Render.Frame object contains a list of Render.Batch objects; each Batch has an associated Layer and Material. Layers are simple integers that allow me to explicitly order batches, so that I can create batches in any given order, and even create multiple batches at once. This also allows me to perform a single sort of the Batches array before sending the batches off to the video card, in order to minimize state changes.</p>
<p>Any given Batch contains a list of structures representing &#8216;Draw Calls&#8217;. In some cases, a draw call maps directly to a hardware drawing operation &#8211; like DrawUserPrimitives &#8211; but in other cases, it maps to something more granular. For example, a BitmapBatch contains BitmapDrawCalls, where each draw call represents a single bitmap, much like the arguments you pass to SpriteBatch&#8217;s Draw method. This allows me to sort individual draw calls based on their parameters to minimize state changes within a batch.</p>
<p>Finally, the Material objects associated with a given Batch are a superset of the XNA&#8217;s Effect class &#8211; they include a VertexDeclaration, Effect, and optional delegates for configuring other rendering state like the current blending function or stencil state. Grouping all these parameters together in one object allows me to sort batches cheaply by comparing material instances, but still gives me some level of granularity since I can change the shader parameters of an active Effect within a batch, for example to support rendering multiple textures inside a single BitmapBatch.</p>
<p>In addition to the more obvious performance advantages of this approach, like the ability to sort by material, one other advantage is less obvious: Since a Frame contains information on all the drawing operations that need to be performed, but doesn&#8217;t depend on any of my game state, I can safely hand that object to another thread and have that thread perform the drawing operations. This lets me move a significant portion of my rendering off the main thread, and begin performing my next Update while the previous Draw completes, without needing to add any locking or complex synchronization.</p>
<hr />It&#8217;s not all great, though. The biggest downsides to this approach are twofold:</p>
<p>First, I basically have to reimplement everything from scratch &#8211; SpriteBatch and SpriteFont are both completely impossible to extend, so I have to reimplement them in order to render text and bitmaps with this approach, and the same goes for any other rendering code based on directly manipulating a GraphicsDevice.</p>
<p>Second, this approach is inherently more dependent on the garbage collector, since most of the types must be classes by necessity. If I want to run well on the 360, this means I need to make use of pooling and other techniques to avoid frequent allocations during frames. I&#8217;m also no longer able to reuse a single scratch buffer when generating geometry, so every piece of geometry I render needs to have its own buffer &#8211; more allocations.</p>
<p>So far I&#8217;m pretty pleased with this approach. There&#8217;s more work to be done &#8211; for example, I don&#8217;t have pooling implemented so my game collects extremely often on the 360 &#8211; but a large portion of my game is now running on this new rendering architecture, and my average framerate has already improved slightly from moving work off the main thread, despite the fact that I haven&#8217;t spent any time on optimizations.</p>
<hr />The biggest challenge that remains is porting all of my old GraphicsDevice-oriented rendering code over to using batches. Here&#8217;s a before and after example:</p>
<pre>        public void RenderTileLayer (int index) {
            var layer = RuntimeLevel.Layers[index];
            RuntimeLayer.ItemInfo itemInfo = null;

            BeginSpriteBatch(BlendModes.AlphaPremultiplied);

            using (var e = layer.GetItemsFromBounds(Camera.Bounds))
            while (e.GetNext(out itemInfo)) {
                RenderTile(itemInfo.Item, SpriteBatch, Camera.ViewportPosition, Camera.Zoom, AnimationTimeProvider.Ticks);
            }
        }

        private void RenderTile (RuntimeTile tile, SpriteBatch spriteBatch, Vector2 viewportPosition, float zoom, long time) {
            var info = tile.TileInfo;
            var pos = (tile.Bounds.TopLeft - viewportPosition) * zoom;
            spriteBatch.Draw(info.Texture, pos, info.Rectangle, Colors.White, 0.0f, new Vector2(0, 0), zoom, strip.GetSpriteEffect(), 0.0f);
        }</pre>
<p>You can see here that my approach for rendering tiles is pretty simple: Get all the tiles within the screen&#8217;s boundaries, and render them one by one using a SpriteBatch. I&#8217;m able to reduce the number of state changes since I know that every tile within a given layer shares the same state, but I still need to change state once per layer (using the BeginSpriteBatch function, which calls SpriteBatch.Begin and sets up my render state). I also have to go out of my way to render each layer in the right order, which means calling RenderLayer in multiple places so that certain tiles appear below entities while other tiles appear above entities.</p>
<p>The new implementation looks like this:</p>
<pre>        public void RenderTileLayer (int index) {
            var layer = RuntimeLevel.Layers[index];
            RuntimeLayer.ItemInfo itemInfo = null;

            int drawLayer = (index &lt;= 2) ? DrawLayers.Background : DrawLayers.Foreground;

            using (var bitmapBatch = new BitmapBatch(PendingFrame, drawLayer + index, Materials.Bitmap[BlendModes.AlphaPremultiplied]))
            using (var e = layer.GetItemsFromBounds(Camera.Bounds))
            while (e.GetNext(out itemInfo)) {
                RenderTile(itemInfo.Item, bitmapBatch);
            }
        }

        public void RenderTile (RuntimeTile tile, Render.BitmapBatch batch) {
            var info = tile.TileInfo;

            var drawCall = new Render.BitmapDrawCall(info.Texture, tile.Bounds.TopLeft, info.Bounds);
            drawCall.Mirror(info.Strip.Mirroring.X, info.Strip.Mirroring.Y);

            batch.Add(drawCall);
        }</pre>
<p>One thing you&#8217;ll notice is that RenderTile now has considerably fewer arguments. Since I had to reimplement SpriteBatch from scratch, this gave me the opportunity to integrate some of my rendering calculations, like zooming and viewport positioning, directly into the shader. As a result, I don&#8217;t need to pass those values around as parameters anymore; I simply set them as a shader parameter every time they change. Using an EffectPool for all my shaders also means that I only need to set those parameters once, instead of having to update all my individual shaders with the correct values.</p>
<p>One other difference here is the need to explicitly choose a layer for the tiles to render on when I create a BitmapBatch. I have a small set of constants (named DrawLayers) that I use to roughly organize my layers, but I also add values to those constants so that I can organize batches within those layers. That way, I can be certain that if I draw three sets of tiles on the same layer, they are always drawn in the same order relative to each other.</p>
<p>I also have to explicitly pass in a Material object for the BitmapBatch, instead of just passing a blend mode to the BeginSpriteBatch function. This isn&#8217;t a significant change, but it does mean that I now have to explicitly manage my materials &#8211; as a result, my game now has a LoadMaterials function that runs at startup and creates all the various permutations of parameters I need and stores them so that I can grab them at runtime.</p>
<p>You may also notice that there are no explicit drawing operations in here anywhere; I&#8217;m just creating a BitmapBatch and adding draw calls to it. Essentially, what&#8217;s going on here is that creating a batch automatically attaches it to the Frame being built. When the batch is Disposed (by the using block, in this case), the Frame is notified that the contents of the Batch (its draw calls) are ready and it stores them for later. The use of IDisposable to represent &#8216;readiness&#8217; instead of disposal is a little weird, but it&#8217;s convenient in that it gives me relatively automatic batch management. This also means that if I wanted to, I could create lots of batches at once and fill them with draw calls on multiple threads, since the Frame has a straightforward way to determine whether all of its batches are ready yet.</p>
<hr />Below you can see a short video of how the renderer groups the game up into batches. If you look carefully, you may also notice that particles are being rendered behind all the batches, since they aren&#8217;t yet integrated into the renderer.</p>
<div class="video"><object width="640" height="385" data="http://www.youtube-nocookie.com/v/NtlDmjVpIXg&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;hd=1" type="application/x-shockwave-flash"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube-nocookie.com/v/NtlDmjVpIXg&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;hd=1" /><param name="allowfullscreen" value="true" /></object></div>
]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/blog/gruedorf/2009/07/20/threaded-renderer/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Threaded EndDraw in XNA (Crouching WaitOne, Hidden Lock)</title>
		<link>http://www.luminance.org/blog/gruedorf/2009/07/01/threaded-enddraw-in-xna</link>
		<comments>http://www.luminance.org/blog/gruedorf/2009/07/01/threaded-enddraw-in-xna#comments</comments>
		<pubDate>Wed, 01 Jul 2009 23:21:29 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Gruedorf]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[threading]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=526</guid>
		<description><![CDATA[After finishing up the materials for my Level Up 2009 entry, today I spent a little while trying out an idea I had recently: One of the problems with using vertical sync in a video game is that it eats into the available CPU time for performing game updates. The way vsync is implemented in [...]]]></description>
			<content:encoded><![CDATA[<p>After finishing up the materials for <a href="http://software.intel.com/en-us/contests/levelup2009/entry_detail.php?entryid=132339">my Level Up 2009 entry</a>, today I spent a little while trying out an idea I had recently:</p>
<p>One of the problems with using vertical sync in a video game is that it eats into the available CPU time for performing game updates. The way vsync is implemented in most graphics APIs, it causes your Present/EndDraw/SwapBuffers call to block until the card enters vertical blank and the frame is shown to the user. While this is ideal from a correctness perspective, it&#8217;s a tremendous waste since it means you can end up sitting there for up to 16 milliseconds, waiting for vertical blank. If your game spends lots of time doing both updating and drawing, all that time could be spent performing updates instead. Ouch.</p>
<p>Currently, my game spends about as much time drawing as it does updating. A significant portion of the time spent drawing (20-30%) is within the EndDraw function. Turning off vertical sync drops the amount of time spent in EndDraw considerably, but introduces tearing. So, as a potential solution, why not call EndDraw on a background thread? While the thread waits for vertical blank, I can begin performing the next frame&#8217;s Update, and in the event that I finish updating before the previous frame is visible, I simply wait for that previous EndDraw call before beginning to paint the next frame. In the optimal case, this means I can come much closer to the best possible framerate without introducing tearing, and in the worst case, the cost of rendering an individual frame is only *slightly* increased by the use of thread synchronization. The fact that I&#8217;m only doing EndDraw on another thread means that I don&#8217;t have to worry about protecting my game data with locks and other synchronization techniques, since the GraphicsDevice doesn&#8217;t use any of my game data when performing the EndDraw operation.</p>
<p>So, to test this out, I overrode my Game class&#8217;s BeginDraw and EndDraw methods. This turns out to be all we have to do to change the way drawing is performed, because the XNA Framework developers were kind enough to make both of these methods virtual.</p>
<pre>        protected override bool BeginDraw () {
            _DrawCompleteEvent.WaitOne();
            _DrawCompleteEvent.Reset();
            return base.BeginDraw();
        }

        protected override void EndDraw () {
            _DrawRequiredEvent.Set();
        }</pre>
<p>Of course, at this point, the two events used here are never set, so this code won&#8217;t work. Thus, we add a background thread to perform our painting:</p>
<pre>        AutoResetEvent _DrawRequiredEvent = new AutoResetEvent(false);
        ManualResetEvent _DrawCompleteEvent = new ManualResetEvent(true);

        public Game () {
            ...

            _DrawThread = new Thread(DrawThreadFunc);
            _DrawThread.IsBackground = true;
            _DrawThread.Start();
        }

        protected void DrawThreadFunc () {
            while (true) {
                _DrawRequiredEvent.WaitOne();
                base.EndDraw();
                _DrawCompleteEvent.Set();
            }
        }</pre>
<p>Fairly simple thread programming here: We create a thread, and set IsBackground to true so that it will stop as soon as the main thread exits. The thread spends all of its time waiting for a &#8216;required draw&#8217; signal, and then performs an EndDraw call. Once the call is complete, it sets another signal to inform the Game class that the previous draw has finished and it&#8217;s safe to perform a BeginDraw call (this lets us make sure that we never use the GraphicsDevice on the main thread while the background thread is performing an EndDraw).</p>
<p>Once this is all done, I start up my game, and&#8230; the framerate isn&#8217;t any different. Huh? What&#8217;s more, my frame profiler indicates that Update is now taking <strong>ten times</strong> as long as it used to, while Draw isn&#8217;t any faster. Huh???</p>
<p>In situations like this, it&#8217;s always good to consult a profiler to see if you&#8217;re missing something important:</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/07/profile_01.png"><img class="aligncenter size-full wp-image-527" title="profile_01" src="http://www.luminance.org/wp-content/uploads/2009/07/profile_01.png" alt="profile_01" width="550" height="255" /></a></p>
<p>So, we can see that the DrawThread is definitely doing its job &#8211; it calls EndDraw, then waits for a signal asking it to draw again. Both are taking about as much time as we&#8217;d expect. But why is Update taking so long&#8230;?</p>
<p style="text-align: center;"><a href="http://www.luminance.org/wp-content/uploads/2009/07/profile_02.png"><img class="aligncenter size-full wp-image-528" title="profile_02" src="http://www.luminance.org/wp-content/uploads/2009/07/profile_02.png" alt="profile_02" width="499" height="132" /></a></p>
<p style="text-align: left;">&#8230; oh. Oops.</p>
<p>So it turns out that I was using GraphicsDevice.Viewport.Width and GraphicsDevice.Viewport.Height in my camera code. Accessing the Viewport property caused the XNA framework to call into Direct3D to retrieve the viewport, which acquired the exact same lock being used by EndDraw, causing my main thread to stall until the draw completed. <strong>WHOOPS</strong>.</p>
<p>This is especially embarassing since the viewport size never changes anyway, so I could have just stored the width/height into constants. After doing just that and starting the game again, the profile looks more like you&#8217;d expect:</p>
<p><a href="http://www.luminance.org/wp-content/uploads/2009/07/profile_03.png"><img class="aligncenter size-full wp-image-529" title="profile_03" src="http://www.luminance.org/wp-content/uploads/2009/07/profile_03.png" alt="profile_03" width="562" height="254" /></a></p>
<p>What&#8217;s more, this is actually an improvement: With vertical sync <strong>enabled</strong>, this results in a significant reduction in the amount of time spent inside the BeginDraw/Draw/EndDraw functions on the main thread, which means there&#8217;s more time left to perform Updates. This means that I can maintain a solid, smooth 60fps easier on dual-core/hyperthreaded machines.</p>
<p>Even with vertical sync <strong>disabled</strong>, this is still an improvement, though not as significant &#8211; apparently other things are happening inside EndDraw (not a big surprise), so by shifting that work off onto a second thread, I&#8217;m still gaining some time to spend performing the next update. When I disable the built in framerate balancer, this brings my framerate from ~350fps up to ~380fps. Not bad for a couple dozen lines of code!</p>
<p>Of course, it&#8217;s worth pointing out that the XNA Framework documentation doesn&#8217;t make any promises here, so it&#8217;s possible that this technique is unsafe. When it comes to concurrency, it&#8217;s very easy to do the wrong thing and get away with it &#8211; as you might have noticed here, I was doing something utterly stupid and unsafe in my Update function, and I got away with it because the DirectX developers had the foresight to put a lock in the right place. If they hadn&#8217;t, my game might have corrupted state from accessing the GraphicsDevice on two threads, and crashed intermittently.</p>
<p>Regardless, this is a handy technique &#8211; once I&#8217;ve had the chance to do lots of testing on various PC configurations (and the XBox 360), I&#8217;ll probably be using it in my game when I ship.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/blog/gruedorf/2009/07/01/threaded-enddraw-in-xna/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Asynchronous programming for XNA games with Squared.Task</title>
		<link>http://www.luminance.org/blog/code/2009/06/13/asynchronous-programming-for-xna-games-with-squaredtask</link>
		<comments>http://www.luminance.org/blog/code/2009/06/13/asynchronous-programming-for-xna-games-with-squaredtask#comments</comments>
		<pubDate>Sat, 13 Jun 2009 08:27:54 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[tasks]]></category>
		<category><![CDATA[threading]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=465</guid>
		<description><![CDATA[A recent post on Shawn Hargreaves&#8217; blog reminded me that I never got around to sharing my technique for interacting with the XNA Guide API and doing other asynchronous operations during my game&#8217;s execution. It&#8217;s a simple application of some of the cooperative-threading techniques I&#8217;ve blogged about previously, and makes what would otherwise be a [...]]]></description>
			<content:encoded><![CDATA[<p>A <a href="http://blogs.msdn.com/shawnhar/archive/2009/06/10/pumping-the-guide.aspx">recent post on Shawn Hargreaves&#8217; blog</a> reminded me that I never got around to sharing my technique for interacting with the XNA Guide API and doing other asynchronous operations during my game&#8217;s execution. It&#8217;s a simple application of some of <a href="http://www.luminance.org/gruedorf/2009/05/03/concurrency-and-generics">the cooperative-threading techniques I&#8217;ve blogged about previously</a>, and makes what would otherwise be a somewhat painful exercise relatively simple in practice. So, for those who are working on XNA games or just curious, I&#8217;ll go into detail on the technique here and <a href="http://www.luminance.org/code/2009/06/13/asynchronous-programming-for-xna-games-with-squaredtask#urls">share the source code</a> with you!</p>
<p>The basic goal here is to be able to write game code in an imperative, sequential manner, without having to deal with locks, callbacks, polling, or race conditions. You rarely achieve perfection when dealing with concurrency, but even getting within sight of perfection can be worth the effort if it means you don&#8217;t have to spend time pulling your hair out trying to reproduce a rare threading bug.</p>
<p>In my game, any operation that can be performed off the main thread is done without blocking the main thread. Loading content, loading files, saving games, loading games, interacting with the guide, etc. This means that I can seamlessly load things in the background while the game is running without any noticeable stuttering. Normally, you&#8217;d need to do lots of juggling to handle this correctly &#8211; locking, threads, explicit synchronization &#8211; but thanks to cooperative threading, I don&#8217;t have to deal with any of that. The techniques I describe in this post are the same basic techniques I&#8217;m using for my game. Hopefully, that means they should work for yours too (though unfortunately that means if there are bugs in them, you&#8217;ve probably just found a bug in my game&#8230; whoops.)</p>
<p>Unless you&#8217;re working on an XNA game that uses networking (which sadly, I am not, so I can&#8217;t speak to the difficulties there), the biggest concurrency related issues you&#8217;re likely to deal with are twofold:</p>
<ul>
<li>Interacting with the XNA Guide APIs</li>
<li>Performing I/O</li>
</ul>
<p>By the end of this article I&#8217;ll have shown you how you can tackle these problems in your XNA games without having to deal directly with threads or synchronization, in a simple, imperative manner that works equally well on the PC and XBox 360.</p>
<p><img class="aligncenter size-medium wp-image-483" title="ss_02" src="http://www.luminance.org/wp-content/uploads/2009/06/ss_02-300x233.png" alt="ss_02" width="300" height="233" /></p>
<p>The Guide APIs are almost all designed in a manner that requires use of either threads, callbacks/continuations, or polling, because they come as Begin/End pairs that require management of ordering and state beyond a single frame. This is for the most part a necessity, as detailed in <a href="http://blogs.msdn.com/shawnhar/archive/2009/06/10/pumping-the-guide.aspx">the blog post I mentioned above</a>. So instead of trying to find a workaround, the best course of action is to simplify the process of working with those APIs &#8211; if possible, without introducing new issues or restricting functionality.</p>
<p>I/O is a more interesting problem; even when writing desktop applications, you often perform large I/O operations that have the potential to block, and in those cases, a well-written application uses concurrency techniques &#8211; threading, callbacks, etc &#8211; to avoid &#8216;hanging&#8217; and frustrating the user. However, on almost any modern desktop machine, you can typically get away with tiny I/O operations if done correctly &#8211; a good example is Firefox 3, which still regularly performs short disk operations on its UI thread, which can cause it to hang if your machine is under extreme I/O load, but works fine in almost every other situation.</p>
<p>The XBox 360 makes I/O particularly challenging because you can&#8217;t rely on latency and throughput characteristics that you might be used to on a desktop machine: Not only do you have to deal with the downsides inherent in hard disks, but you also have to deal with the possibility of a player using a memory card, and even worse, the possiblity of the available storage devices <strong>changing</strong> while your game is running (due to memory card insertion/removal, etc). This means that doing I/O in the main thread is pretty much a non-starter. You&#8217;re stuck: It&#8217;s concurrency time.</p>
<p>Luckily, cooperative threading provides a great solution for tackling both of these issues: It lets you reason about your problems in a manner you&#8217;re familiar with, and solve them by writing imperative code that still behaves correctly in tough situations, at the expense of some slight overhead and minor changes to your game code. You don&#8217;t have to build a tremendously complex state machine (since the C# compiler can be convinced to do the heavy lifting for us), and you don&#8217;t have to worry about locking and synchronization since all your code is guaranteed to run sequentially in the same thread, at the appropriate time.</p>
<p><span id="more-465"></span>So, for the initial scaffolding. As you might have guessed, we&#8217;ll be trying out the <a href="http://code.google.com/p/fracture/source/browse/#svn/trunk/Squared/TaskLib">cooperative threading framework from my open source library, Squared.Task.</a> This library is inspired by the techniques used in <a href="http://twistedmatrix.com/trac/">Twisted</a> and <a href="http://thespeedbump.livejournal.com/63798.html">imvu.task</a>. The library is designed to perform well under tight constraints, so you can use it just fine on the XBox 360 without any garbage creation* or thread contention issues.</p>
<p>The first thing we&#8217;re going to need is a way to manage our cooperative threads. We&#8217;ll do this by creating a TaskScheduler owned by our Game object, that will run our &#8216;Tasks&#8217; (cooperative threads) on the same thread as the rest of our game logic, and asking it to Step all pending tasks every time the Game&#8217;s Update method runs:</p>
<pre>    public class TaskExampleGame : Microsoft.Xna.Framework.Game {
        Squared.Task.TaskScheduler taskScheduler;
        ...

        public TaskExampleGame () {
            ...
            taskScheduler = new Squared.Task.TaskScheduler();
        }

        ...

        protected override void Update (GameTime gameTime) {
            ...

            taskScheduler.Step();
        }</pre>
<p>Pretty trivial so far. Creating a TaskScheduler is extremely cheap and quick, so you can do it right in your Game&#8217;s constructor; any particular expensive resources it requires are created on-demand. Calling the Step method every Update ensures that any waiting tasks are processed on a regular basis, as long as your game is updating at a solid framerate &#8211; which you always want it to be. If you don&#8217;t have any tasks pending, Step is effectively free, so when you&#8217;re not using the scheduler you don&#8217;t have to think about it.</p>
<p>Of course, this is still boilerplate. What we really want to do is actually get something done! So, let&#8217;s come up with a contrived example: We&#8217;ll make the A button increment an onscreen counter, the B button read the counter from a file on the player&#8217;s storage device, and the X button write the counter to that same file. This means we have to handle storage device selection, error conditions, and disk I/O &#8211; and we want to do it without causing any stuttering or hanging!</p>
<p>To begin with, we need the counter, and some basic scaffolding so that our example at least slightly resembles a well-behaved game &#8211; we&#8217;ll make sure to display the counter on screen so you can actually tell the example is working, and suspend our handling of user input if the Guide is open so that things don&#8217;t happen mysteriously in the background while you&#8217;re reading a message somebody sent you on MSN. I&#8217;m going to omit this scaffolding since you can see it in the <a href="#urls">source code (included at the bottom)</a>, and it&#8217;s tremendously boring.<img class="aligncenter size-medium wp-image-474" title="ss_01" src="http://www.luminance.org/wp-content/uploads/2009/06/ss_01-300x233.png" alt="ss_01" width="300" height="233" /></p>
<p>So, now we can hold the A button to increase the counter, and it just keeps going up! Great fun, really &#8211; just add a rope to burn and we&#8217;re ready for an IGF submission. Anyway, let&#8217;s get to the meat of it: We want to write the counter out to a file when the player hits the X button. To begin with, let&#8217;s just spike a basic implementation:</p>
<pre>        void SaveCounterToDiskViaCallbacks () {
            Guide.BeginShowStorageDeviceSelector((asyncResult) =&gt; {
                var storageDevice = Guide.EndShowStorageDeviceSelector(asyncResult);

                System.Threading.ThreadPool.QueueUserWorkItem((_) =&gt; {
                    var container = storageDevice.OpenContainer("TaskExample");

                    var file = System.IO.File.Open(
                        System.IO.Path.Combine(container.Path, "counter.bin"),
                        System.IO.FileMode.OpenOrCreate,
                        System.IO.FileAccess.Write,
                        System.IO.FileShare.None
                    );

                    byte[] bytes;
                    lock (CounterLock)
                        bytes = BitConverter.GetBytes(Counter);

                    file.Write(bytes, 0, bytes.Length);

                    file.Close();
                    container.Dispose();
                });
            }, null);
        }</pre>
<p>Even this trivial example has lots of problems &#8211; not only am I completely ignoring error handling, but there are plenty of other issues that make this approach fail to scale up to large projects. At least it works, right?</p>
<p>Next, let&#8217;s spike a callback-based implementation of reading the counter:</p>
<pre>        void LoadCounterFromDiskViaCallbacks () {
            Guide.BeginShowStorageDeviceSelector((asyncResult) =&gt; {
                var storageDevice = Guide.EndShowStorageDeviceSelector(asyncResult);

                System.Threading.ThreadPool.QueueUserWorkItem((_) =&gt; {
                    var container = storageDevice.OpenContainer("TaskExample");

                    var filePath = System.IO.Path.Combine(container.Path, "counter.bin");

                    if (!System.IO.File.Exists(filePath))
                        return;

                    var file = System.IO.File.Open(
                        filePath,
                        System.IO.FileMode.Open,
                        System.IO.FileAccess.Read,
                        System.IO.FileShare.None
                    );

                    var bytes = new byte[4];
                    file.Read(bytes, 0, 4);

                    int newCounter = BitConverter.ToInt32(bytes, 0);
                    lock (CounterLock)
                        Counter = newCounter;

                    file.Close();
                    container.Dispose();
                });
            }, null);
        }</pre>
<p>If you look carefully, you might even see a bit of error handling in there. Now that we all know what a trivial stall-free implementation looks like, we can try out our sample and see that it&#8217;s fully functional. In most cases on a PC, it will work more or less perfectly, since I/O is lightning fast, despite the many bugs and errors experienced .NET programmers can find in it. One obvious problem is the fact that it basically does everything in one threadpool work item, because anything other than that is painful. This means that you have to put locks around basically any data and objects you use within the implementation, which gets complicated quickly. Even worse, being in the thread pool means that if we need to call a function that can only be run from the main thread, we have to marshal back and forth manually.</p>
<p>If the above spikes confused you, you can find explanatory comments in the <a href="#urls">downloadable source code</a>. Let&#8217;s continue.</p>
<p>Now, to flex our mental muscles, let&#8217;s try and build a relatively ideal implementation of this example using our task scheduler. We&#8217;ll even handle errors and inform the user about them. As a result, you&#8217;ll see some code that mysteriously resembles scaffolding&#8230; Let&#8217;s get started.</p>
<p>To begin, we want to create tasks for our load and save operations. Let&#8217;s just write them, and worry about the functions they call later.</p>
<pre>        IEnumerator&lt;object&gt; SaveCounterToDisk (int counterValue) {
                var storageDeviceFuture = SelectStorageDevice();
                yield return storageDeviceFuture;

                var storageContainerFuture = OpenStorageContainer(
                    storageDeviceFuture.Result,
                    "TaskExample"
                );
                yield return storageContainerFuture;

                using (var container = storageContainerFuture.Result) {

                    var filePath = System.IO.Path.Combine(container.Path, "counter.bin");

                    var fileFuture = GetFileAdapter(
                        filePath,
                        System.IO.FileMode.OpenOrCreate,
                        System.IO.FileAccess.Write
                    );

                    yield return fileFuture;

                    using (var file = fileFuture.Result) {

                        var bytes = BitConverter.GetBytes(counterValue);
                        var writeFuture = file.Write(bytes, 0, bytes.Length);

                        yield return writeFuture;
                    }
                }
            }
        }</pre>
<p>So, let&#8217;s address the most confusing parts of this first before we continue on to the load function:</p>
<p>The function has to return an IEnumerator&lt;object&gt; so that the task system will recognize it as a task, and it can yield any waitable object out as a result. Unfortunately, we can&#8217;t use a strong type here because the compiler will only allow iterator functions to return IEnumerator or IEnumerable.</p>
<p>One of the patterns you see is retrieving a future from a function that begins an operation, and then yielding the future to wait for it to acquire a result. We don&#8217;t have to do these two operations in sequence, so if we wished, we could acquire futures for multiple operations in sequence before yielding on all of them at once &#8211; easy parallelization.</p>
<p>Once a future has a result, we&#8217;re free to retrieve it (using the Result property) or simply discard it. A future&#8217;s result will either be a value of the type you expect (the result of the operation), or an exception (caused by the operation failing). If the future&#8217;s result is an exception, attempting to retrieve it automatically causes it to be rethrown within a FutureException, preserving the original stack trace. This means that code using futures doesn&#8217;t ignore unexpected exceptions &#8211; it bubbles them up, just like single-threaded imperative code.</p>
<p>Another bonus here is that a discarded future is still monitored by the task system as a result of waiting on it &#8211; if you discard a future you&#8217;ve waited on, and the future contained an unhandled exception, it is automatically bubbled up through your task, causing resources to be cleaned up and allowing a higher up function or task to handle it. This means that futures with out a result we care about can simply be discarded without sacrificing correctness.</p>
<p>You may also wish to note that we pass the original value of the counter into the task to preserve it. This means that if the counter continues going up after the save begins, the original value will still be saved. No locking or state consistency issues to worry about; the compiler handles storing that argument for us while our task is running.</p>
<p>Most of the logic you&#8217;re familiar with from the original callback-based implementations has been replaced with simple functions that return futures. I&#8217;ll show those after the load function, which follows:</p>
<pre>        IEnumerator&lt;object&gt; LoadCounterFromDisk () {
            var storageDeviceFuture = SelectStorageDevice();
            yield return storageDeviceFuture;

            var storageContainerFuture = OpenStorageContainer(
                storageDeviceFuture.Result,
                "TaskExample"
            );
            yield return storageContainerFuture;

            using (var container = storageContainerFuture.Result) {

                var filePath = System.IO.Path.Combine(container.Path, "counter.bin");

                var fileFuture = GetFileAdapter(
                    filePath,
                    System.IO.FileMode.Open,
                    System.IO.FileAccess.Read
                );

                yield return fileFuture;

                if (fileFuture.Failed)
                    yield break;

                using (var file = fileFuture.Result) {

                    var bytes = new byte[4];
                    var readFuture = file.Read(bytes, 0, bytes.Length);

                    yield return readFuture;

                    Counter = BitConverter.ToInt32(bytes, 0);
                }
            }
        }</pre>
<p>Not much new here. One thing of note: If the GetFileAdapter call failed, we just abort the task immediately. The using blocks clean up all the resources we were using for us automatically, which means we don&#8217;t attempt to read from a nonexistent file. If we wanted, we could show an error dialog to the player here. In contrast to this, we don&#8217;t bother checking the number of bytes that were read, even though readFuture contains them. If I were a serious game developer instead of a blogger writing an example, I&#8217;d check the result and show an error if it&#8217;s anything other than four bytes (or perhaps just throw an exception).</p>
<p>Hopefully you&#8217;re all following me so far. For completeness, I&#8217;ll now show the wrapper functions used above:</p>
<pre>        Squared.Task.Future&lt;StorageDevice&gt; SelectStorageDevice () {
            var f = new Squared.Task.Future&lt;StorageDevice&gt;();

            Guide.BeginShowStorageDeviceSelector((asyncResult) =&gt; {
                try {
                    var result = Guide.EndShowStorageDeviceSelector(asyncResult);
                    f.SetResult(result, null);
                } catch (Exception ex) {
                    f.SetResult(null, ex);
                }
            }, null);

            return f;
        }

        Squared.Task.Future&lt;StorageContainer&gt; OpenStorageContainer (StorageDevice device, string titleName) {
            return Squared.Task.Future.RunInThread(
                () =&gt; device.OpenContainer(titleName)
            );
        }

        Squared.Task.Future&lt;Squared.Task.IO.FileDataAdapter&gt; GetFileAdapter (string fileName, System.IO.FileMode mode, System.IO.FileAccess access) {
            return Squared.Task.Future.RunInThread(
                () =&gt; new Squared.Task.IO.FileDataAdapter(
                    fileName,
                    mode,
                    access,
                    System.IO.FileShare.None
                )
            );
        }</pre>
<p>These wrappers are no more complicated than the original imperative code, with the exception of the try block in the Guide wrapper (necessary to propagate any errors out through the Future).</p>
<p>One advantage you might not notice is that since the Guide wrapper issues the original Begin call directly, it will bubble right up into the caller without any delay, allowing you to handle it immediately without waiting, while errors that occur later on will be stored into the Future and bubbled up into the caller.</p>
<p>We invoke OpenContainer on a thread to avoid it blocking the main thread. Those of you who are sticklers for correctness may have noticed that this isn&#8217;t 100% correct, since there&#8217;s no guarantee that this function is thread-safe. We&#8217;re relatively okay since we&#8217;re certain it won&#8217;t be used on any other threads at the same time, thanks to the ordering guarantees provided by the task system, but there&#8217;s still a slight potential for badness here. Caveat emptor. If you feel nervous, just call it directly from your task instead &#8211; no thread safety issues there.</p>
<p>One somewhat major difference is that I&#8217;m now constructing a FileDataAdapter object instead of opening a file using the standard System API. A FileDataAdapter is a utility class provided by my framework that simplifies the task of performing asynchronous I/O at a low level. It implements a generic interface that allows manipulating other data sources, like sockets and streams, in the same manner. For those seeking a higher-level interface, there are utility classes for performing WriteLine, ReadLine, and similar operations on an adapter &#8211; without needing a thread! Just because you want to write concurrent code doesn&#8217;t mean you have to suffer. <img src='http://www.luminance.org/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To run these tasks, the code you write isn&#8217;t much more complicated than calling the original functions:</p>
<pre>    taskScheduler.Start(
        SaveCounterToDisk(Counter),
        Squared.Task.TaskExecutionPolicy.RunAsBackgroundTask
    );</pre>
<p>The reason for the &#8216;RunAsBackgroundTask&#8217; argument is non-obvious, so I&#8217;ll explain: One of the features of the task system is lifetime management. Starting a task returns a Future that will be completed when the given task is done running. If you wish, you can Dispose the future, and the task will be cancelled. RunAsBackgroundTask is a special execution policy that tells the TaskScheduler that not only do you have no desire to manage the lifetime of the task, but you want it to automatically handle any errors that occur in the task.</p>
<p>Of course, the default error handler for background tasks knows nothing about XNA. So let&#8217;s write one that does, and use the Guide to show the error message to the user:</p>
<pre>        public TaskExampleGame () {
            ...
            taskScheduler.ErrorHandler = (error) =&gt; {
                taskScheduler.Start(HandleTaskError(error));
                return true;
            };
        }

        IEnumerator&lt;object&gt; HandleTaskError (Exception error) {
            while (error.InnerException != null)
                error = error.InnerException;

            string message = error.Message;

            if (message.Length &gt; 255)
                message = message.Substring(0, 255);

            while (Guide.IsVisible)
                yield return new Squared.Task.WaitForNextStep();

            var f = new Squared.Task.SignalFuture();
            Guide.BeginShowMessageBox(
                "An unexpected error occurred",
                message,
                new string[] { "Okay <img src='http://www.luminance.org/wordpress/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> " },
                0, MessageBoxIcon.Error,
                (asyncResult) =&gt; {
                    f.SetResult(null, null);
                }, null
            );

            yield return f;
        }</pre>
<p>A fairly simple error handler that schedules a new task to show a message box to alert the user about the error. You may notice I apply a simple polling technique here to avoid the need for any complex queueing or locking &#8211; if the error handler task discovers the guide is running, it simply suspends itself until the next step, allowing the guide to close. Obviously, you don&#8217;t want to be doing this in thousands of tasks every step if you want to scale, but for a task like this, it&#8217;s more than adequate.</p>
<p>You&#8217;ll notice that most of the trivial error cases work perfectly in this case. I even left one obvious case unhandled so you could easily trigger a failure &#8211; just cancel the storage device selection screen (though you&#8217;ll only see it if you have more than one), and you&#8217;ll see &#8216;NullReferenceException&#8217; pop up. What about opening the guide while selecting a storage device? Handled just fine.</p>
<p>Now that we&#8217;ve got our example up and running, let&#8217;s add a little polish. We&#8217;ll add a helpful message to explain how the example works, and put a bouncing smiley face on the screen that moves smoothly based on the current time, so we can see if the game hangs by watching the smiley. We&#8217;ll also add a simple status display so that we can see what the example is doing, and avoid queueing up dozens of copies of an operation all stalled waiting for the same resource (like a storage device). I&#8217;ll omit the code for these changes since it&#8217;s mostly uninteresting, but as before, you can see it in the <a href="#urls">downloadable source code</a>.</p>
<p><img class="aligncenter size-medium wp-image-483" title="ss_02" src="http://www.luminance.org/wp-content/uploads/2009/06/ss_02-300x233.png" alt="ss_02" width="300" height="233" /></p>
<p><a name="urls"></a><br />
Now that we&#8217;ve walked through the process of building this example (and gotten tremendously bored), here&#8217;s the source code and binaries so you can try it out on your Windows PC or XBox 360:</p>
<p><a href="http://luminance.org/xna/TaskExampleBinaries.zip">http://luminance.org/xna/TaskExampleBinaries.zip</a></p>
<p><a href="http://luminance.org/xna/TaskExampleSource.zip">http://luminance.org/xna/TaskExampleSource.zip</a></p>
<p>You&#8217;ll need <a href="http://creators.xna.com/en-US/news/xnagamestudio3.1">XNA Game Studio 3.1</a> installed, of course. You&#8217;ll need the latest version of my TaskLib and Util libraries, along with the XBox 360 versions of them (mostly identical), available on Google Code:</p>
<p><a href="http://code.google.com/p/fracture/source/browse/#svn/trunk/Squared">http://code.google.com/p/fracture/source/browse/#svn/trunk/Squared</a></p>
<p>The XBox 360 versions live in the &#8217;360&#8242; folder while the Windows versions live in their own named folders. You can either build them and add them as references, or add the projects themselves to the example as references. For my game, I&#8217;ve simply checked out the project as a subversion external so that it&#8217;s easy to keep it up to date, but my game doesn&#8217;t suddenly break if it changes.</p>
<p>I hope you&#8217;ve found this useful. This example, like all the source code for my libraries, is open source. I encourage you to share it, modify it, experiment, and maybe even use it in your own games. Please don&#8217;t hesitate to email me or leave a comment if you have questions, suggestions, or think you&#8217;ve found a mistake.</p>
<p><img class="aligncenter size-full wp-image-485" title="works-on-my-machine-starburst" src="http://www.luminance.org/wp-content/uploads/2009/06/works-on-my-machine-starburst.png" alt="works-on-my-machine-starburst" width="200" height="193" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/blog/code/2009/06/13/asynchronous-programming-for-xna-games-with-squaredtask/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Concurrency and generics</title>
		<link>http://www.luminance.org/blog/gruedorf/2009/05/03/concurrency-and-generics</link>
		<comments>http://www.luminance.org/blog/gruedorf/2009/05/03/concurrency-and-generics#comments</comments>
		<pubDate>Sun, 03 May 2009 08:59:21 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Gruedorf]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[csharp]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=358</guid>
		<description><![CDATA[Most of my work this week was on making some improvements to some of my libraries. I did some work on the game too, but nothing worth writing about. The main change I made was to one of the core classes I use for concurrency: Future. The future class is a container that can store [...]]]></description>
			<content:encoded><![CDATA[<p>Most of my work this week was on making some improvements to some of my libraries. I did some work on the game too, but nothing worth writing about. <img src='http://www.luminance.org/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The main change I made was to one of the core classes I use for concurrency: Future. The future class is a container that can store a single value, and can only be written to once. You can attach one or more listeners to it that will get invoked once it has a value. The primary advantage of using this class is that it lets you easily perform asynchronous operations without having to deal with locking and most types of race conditions, due to the fact that it&#8217;s write-once.</p>
<p>Having a type like Future is extremely useful when dealing with things like disk IO or network operations. But it also turns out to be useful in games &#8211; you can use Futures to represent long-lived operations like getting a user&#8217;s selection from a menu, finding a path through a level, or loading a texture from disk. Future is also the foundation of the Task primitive in my cooperative multithreading library.</p>
<p>The original interface for the Future class didn&#8217;t make use of .NET generics. This meant that it was easy to write general code atop futures &#8211; you could write a simple function that would take a list of futures as inputs and return a single future as a result that would become completed once all the inputs were completed, for example. The downside is that you basically didn&#8217;t get any type safety, and values were always boxed &#8211; a future that contained an integer had to store it as an object, and any time you wanted to retrieve a value from a future, you had to cast it.</p>
<p>This week I decided to try and refactor my code such that Future could utilize .NET generics but still be usable with simple utility functions.</p>
<p>The solution was to create a baseline interface that all Futures implement, and write all the utility functions and libraries using that baseline interface, but expose fully typed generic Futures as return values from functions. Here&#8217;s a before and after example:</p>
<pre>        public static Future WaitForAll (params Future[] futures) {
            return WaitForX(futures, 1);
        }

        public static Future RunInThread (Func&lt;object&gt; workItem) {
            var thunk = new FuncRunInThreadThunk {
                WorkItem = workItem,
            };
            ThreadPool.QueueUserWorkItem(FutureHelpers.RunInThreadHelper, thunk);
            return thunk.Future;
        }</pre>
<p>As you can see, the original code lacks type information, so you have to infer it from usage and do casting everywhere. The upside is that it&#8217;s general, but you end up paying for it when you make mistakes about types and have to add explicit casts everywhere.</p>
<pre>        public static IFuture WaitForAll (params IFuture[] futures) {
            return WaitForX(futures, 1);
        }

        public static Future&lt;U&gt; RunInThread&lt;U&gt; (Func&lt;U&gt; workItem) {
            var thunk = new FuncRunInThreadThunk&lt;U&gt; {
                WorkItem = workItem,
            };
            ThreadPool.QueueUserWorkItem(FutureHelpers.RunInThreadHelper, thunk);
            return thunk.Future;
        }</pre>
<p>In the refactored code, the type information is preserved when going through a utility function like RunInThread &#8211; if the function being run in a thread returns a float, the resulting future will also contain a float. This eliminates casting but still preserves generality &#8211; since a Future&lt;float&gt; implements IFuture just like a Future&lt;int&gt;, you can use that underlying type when writing utility functions, like WaitForAll (though one downside is that WaitForAll is forced to return an IFuture instead of a future of a specific type, since there&#8217;s no easy way to guarantee that all the futures are of a given type.)</p>
<p>Performing this refactoring took lots of individual steps &#8211; first, I had to change various code to use IFuture instead of Future, and make minor tweaks in order to keep things functioning and keep tests passing. In some cases I still needed to use Future (one obvious example &#8211; you can&#8217;t construct an interface, so in that case the real type needed to be used). The largest chunk of work there was updating the handlers for futures &#8211; previously, a future listener looked like this:</p>
<pre>var handler = (OnComplete) (future, result, error) =&gt; { Console.WriteLine("Future {0} completed with result {1}/error {2}", future, result, error); };</pre>
<p>The three arguments were mostly an artifact from previous refactorings &#8211; originally the Future argument wasn&#8217;t there at all. The existence of the result argument was a problem, because it had to be the same type as the future &#8211; a Future&lt;float&gt; needed an OnComplete listener that took a float result, which made it difficult to construct generic listeners.</p>
<p>In order to make things simpler, I removed the result and error parameters. Including the future still made it possible to get at the result and error, but allowed the listener to be general by accepting an IFuture instead of a strictly-typed Future&lt;T&gt;. This was important for being able to wire futures up to each other (The WaitForAll function is a good example of this, in that it connects multiple futures to a single result future.)</p>
<p>Once I had updated all the existing listener code, the final step was to add a type argument to Future, and add type information to various library functions.</p>
<p>Here&#8217;s an example of the results &#8211; if you were writing a Task to do asynchronous reads from a socket or a file, one line at a time, the code to do the read might look like this:</p>
<pre>Future f = input.ReadLine();
yield return f;
string line = (f.Result as string);</pre>
<p>The ReadLine method of the input stream returns a Future, which will be completed with a string when a line has been successfully read from the stream. The Task <strong>yield</strong>s on the Future to suspend execution until it&#8217;s been completed with a result. Once it&#8217;s completed, execution resumes, and we pull the resulting line out of the future. It&#8217;s obvious that there are some problems with this approach. With the new generic type, the cast is eliminated and ReadLine has a clear return type:</p>
<pre>Future&lt;string&gt; f = input.ReadLine();
yield return f;
string line = f.Result;</pre>
<p>And if you put C# 3.0&#8242;s language features to work, the end result is almost free of duplication:</p>
<pre>var f = input.ReadLine();
yield return f;
var line = f.Result;</pre>
<p>This is one example where having bidirectional iterators (like in Python) would be beneficial. Python&#8217;s yield expression has a result, so you can &#8216;send&#8217; an input into an iterator every time you advance it. As a result, this code written using a library like <a href="http://thespeedbump.livejournal.com/63798.html">imvu.task</a> would look like so:</p>
<pre>line = yield input.readLine()</pre>
<p>The advantage is pretty obvious &#8211; the entire operation is in one line and the meaning is more clear.</p>
<p>Still, this solution is pretty good for C#. You get most of the advantages you could want: Simple code, type safety, thread safety, rock-solid error handling, and scalability. (One of the advantages of using the Future type is that it ensures that you always get either a result or an error. This means that any code that uses a Future to retrieve a result will have exceptions rethrown when an asynchronous operation fails &#8211; no need to manage error flags and add lots of try/catch blocks.)</p>
<p>While doing all this refactoring, my large set of unit tests came in handy &#8211; they caught a number of bugs I introduced early, and revealed an existing bug that was months old. As a result, I ended up with better error handling, better type safety, and a more robust set of unit tests. On the other hand, I still don&#8217;t have any unit tests for my platformer, so maybe I should take another shot at figuring out how to write some for it. (:</p>
<p>If you&#8217;re interested in seeing how Future and Task work, you can see the source code and some working examples here:</p>
<p><a href="http://code.google.com/p/fracture/source/browse/#svn/trunk/Squared/TaskLib">http://code.google.com/p/fracture/source/browse/#svn/trunk/Squared/TaskLib</a></p>
<p>I encourage you to check out the repository and try the MUD Server and Telnet Chat examples. They&#8217;re not real-world applications, but they&#8217;re easy to understand and demonstrate how using Futures and Tasks simplifies concurrency. The MUD even has gameplay! <img src='http://www.luminance.org/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/blog/gruedorf/2009/05/03/concurrency-and-generics/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

