jminardi/codetools — pull request #1 by mdickinson (2012-11-21 14:04:42+00:00)
Title: Update state machine; fix deadlock; remove busy wait.

Hi Jack,

Here's some example code showing one possible way to fix some of the current issues. Main ideas:

1. Encapsulate the state machine in a small portion of the code: nothing outside the marked section knows or cares about the state and state lock.

2. Rework the state machine to be aware of the possiblility of 'deferred' execution. Ioannis and I spent some time hashing out how this would work, and it got surprisingly complicated for a while. In the end, we were able to reduce the state to 3 boolean variables (for a total of 8 states, but one of those states never occurs). In this form the transitions are all really simple. It's instructive to draw the cube underlying the state machine.

3. Use a condition variable to avoid the busy wait. The condition variable notifies on any change of state, so that it's easy to listen to all changes of state and wait until you get some expected state.