title: Threading Box2D with pthreads
body: <p>So I'm essentially trying to implement an AIR Native Extension that does the physics simulation in C with interfaces through Actionscript.</p>
  
  <p>I've gone through quite a few iterations which I'll list below for interest sake and I'm at what I think could be my final attempt at getting this working in a more performant way.</p>
  
  <p>Ultimately I'm looking for help in how I should be setting up a threading environment for running the simulation of Box2D on a separate thread and then polling for state in AS3.</p>
  
  <p><strong>Methods:</strong></p>
  
  <ol>
  <li><strong>Brute Force:</strong> </li>
  </ol>
  
  <p>In this method I simply call into C from AS3 and tell it to create a world and pass it some boxes to add to this world. Every frame in AS3, I call into C to tell the world to Step, then loop through all the bodies in the World, get their position and rotation, convert them to actionscript objects and put them in an actionscript array and then send that back to AS3. Once there I loop through the returning array and assign those position and rotation values to my sprites so they visually update.</p>
  
  <p>The results are actually quite decent with about 116 boxes being added before the framerate suffers. This is compared to 30 boxes in a pure AS3 implementation. Note that these stats are in Debug mode. In release mode, they both make it to about 120 boxes. There is little difference between the AS3 implementation and the Native Extension implementation.</p>
  
  <ol start="2">
  <li><strong>ByteArray Sharing</strong></li>
  </ol>
  
  <p>In order to improve performance I decided it would be a good idea to try and limit the amount of data being marshalled across C and AS3. ANE's support sharing a byte array's memory space and so I would send the ByteArray created in AS3 to C and have C simply update the ByteArray. This saves us from having to construct AS3 objects in C and pass them back. Every frame, AS3 simply needs to iterate through it's ByteArray and see what C has written into it and then assign those values to the sprites to set the visual state.</p>
  
  <p>The results here are sadly about the same. Improvements are only marginal.</p>
  
  <ol>
  <li><strong>Direct Object Setting From C</strong></li>
  </ol>
  
  <p>Another thing ANE's are capable of is setting the property of an object that lives in AS3. In this sense I aimed to eliminate the overhead of passing back data to AS3, the looping through the bodies to collect data in C and the looping through in AS3 to assign the values. I directly modified the Box2D code so that when it's values were changed it would write the new x, y, rotation values directly on the corresponding Sprite.</p>
  
  <p>The results are amazing at very low amounts of objects since the call to set these properties is well under a millisecond. The problem is that this scales linearly and around 90 or so objects, the overhead is too severe and things start to slow down.</p>
  
  <ol>
  <li><strong>Threading</strong></li>
  </ol>
  
  <p>At this point I was a bit stumped. There's overhead in marshalling data, there's a cost in C for iterating and constructing the data to return and there's a cost in AS3 for iterating to assign values to the sprites. </p>
  
  <p>Obviously there needs to be a trade-off so my current solution is the best I can come up with for now. </p>
  
  <p>On the AS3 side you call into C to create your world, call in to add a box to that world, and call in to tell C you want a refresh of your data. When boxes are created in AS3 they get a unique id and they are stored in a dictionary with the key being the id.</p>
  
  <p>On the C side, the world is created and a new pthread is spawned to do the Step. Essentially simulating the world on another thread. After it steps, it assembles all the data and writes it into a double array. Then it does so again and again and again. It just simulates forever basically on it's own thread.</p>
  
  <p>When we call in to C to add a new box, I need to create a new box and add it to that world. Since the world is Stepping this could cause problems which means I need to use mutexes I'm pretty sure.</p>
  
  <p>Same thing when we call to get the values refreshed in AIR, I'll want to do a memcpy from the array of doubles into my AS3 bytearray and then loop through the bytearray to set the values on the visual.</p>
  
  <p>The mutexes were giving me trouble so I basically implemented my own which you can see below... and laugh at :)</p>
  
  <p>However it does work, just not as fast as I would like it too. Around 90 we slow down again.</p>
  
  <p>Anyone have any thoughts or pointers? It'd be greatly appreciated!</p>
  
  <p><strong>C Code</strong></p>
  
  <p>The parser was acting up so i've pasted it here:
  <a href="http://pastebin.com/eBQGuGJX" rel="noreferrer">http://pastebin.com/eBQGuGJX</a></p>
  
  <p><strong>AS3 Code</strong></p>
  
  <p>Same thing with the parser. I've only included the relevant method dealing with every frame in AS3.
  <a href="http://pastebin.com/R1Qs2Tyt" rel="noreferrer">http://pastebin.com/R1Qs2Tyt</a></p>