Rework the Future::Core state machine
authorHans Fugal <fugalh@fb.com>
Fri, 30 Jan 2015 23:23:24 +0000 (15:23 -0800)
committerwoo <woo@fb.com>
Mon, 2 Feb 2015 21:14:50 +0000 (13:14 -0800)
commit173044e4ea34f4bd3fe927359f73c5649a02436f
tree221352336c1509370baf7691e9669154cc361d10
parentba2435923724278b39ed9306803264874584e66f
Rework the Future::Core state machine

Summary:
There was a race reading `callback_` in `maybeCallback` and setting `callback_` in `setCallback`. This diff reworks the state machine to make this unpossible. To avoid the explosion of states due to the cross-product of has-interrupt-handler/has-been-interrupted/etc. I introduce a separate lock for setting interrupt handler and interruption, since this is primarily orthogonal. Other attributes (active, for example) are still atomic variables, and while somewhat tied into the state machine logically (e.g. transitioning from Armed to Done only happens when active) they are mostly independent, keeping the state machine simple (and probably faster).

I think it may even be possible to do some things cheaper. In some states, we may not need to protect the writing of `callback_` and `result_`. But we'd need to enforce some ordering so I'm not going to try to tackle that. But that could be some speedup if we can do it cheaply.

Test Plan:
Builds and all existing tests pass.

Reviewed By: rockyliu4@fb.com

Subscribers: yfeldblum, stepan, trunkagent, exa, folly-diffs@, jsedgwick

FB internal diff: D1807854

Tasks: 6087856

Signature: t1:1807854:1422656713:25b62706cd7952b2dde06dab08074f8030db456b
folly/futures/detail/Core.h