(wangle) express current Core functionality with a state machine
authorHans Fugal <fugalh@fb.com>
Tue, 21 Oct 2014 17:24:06 +0000 (10:24 -0700)
committerdcsommer <dcsommer@fb.com>
Wed, 29 Oct 2014 23:03:04 +0000 (16:03 -0700)
commit1be516d1df75d5c33f9b064c288f1e8533f5c8c3
treee77fddaf6d46e5e4b0eb2cf75bb6eb9f247d6601
parent4c7a8438a608fe4e0107c61fda9c29746e558008
(wangle) express current Core functionality with a state machine

Summary:
This is a refactor of the current functionality to use a state machine (inheriting from `FSM`). This will make it easier to extend to do cancellation and Future collapsing. Performance is the same, maybe slightly faster (abt 1%). (I might be a little conservative with the atomics, it might be worth going through and reasoning about whether they all need to be atomic).

The state machine is two states, Waiting (no value), and Done (has a value). Transitioning to Done will execute the callback if it exists and we are active. Otherwise the callback will happen when it is set and active is true.

There is a subjective balancing act in place here, between making a state for every single mutable bit of information (which results in an explosion of states: hasValue X hasCallback X isActive X isCancelled …), and finding a sweet spot of expressivity. This isn't too far from the way Twitter did it, though we don't (yet) have all the states they have (and they don't have the concept of hot/cold futures). But I got there by way of replacing the `mutex_` with the state, after changing all those variables to atomics so they didn't need mutex protection (resulting in only `callback_` and `result_` needing it). I expect the state machine will morph as the rest of the functionality is added, but hopefully it will be easier to understand and keep correct (that's the idea anyway).

Test Plan: Tests still pass (and not by accident, I made several awesome mistakes along the way).

Reviewed By: davejwatson@fb.com

Subscribers: net-systems@, fugalh, exa, njormrod

FB internal diff: D1618240

Tasks: 4618297
folly/wangle/detail/Core.h