fix folly::FunctionScheduler.cancelFunctionAndWait() hanging issue
Summary:
When
- only one function is scheduled in FunctionScheduler; and
- the function is running while cancelFunctionAndWait() is being called;
FunctionScheduler.cancelFunctionAndWait() will hang forever. The root cause is that the condition in cancelFunctionAndWait() is incorrect:
```
runningCondvar_.wait(l, [currentFunction, this]() {
return currentFunction != currentFunction_;
});
```
because currentFunction will not be changed if only one function is in the scheduler.
The fix here is to
- clear currentFunction as nullptr. This also makes the internal behaviors of cancelFunction() and cancelFunctionAndWait() consistent.
- introduces additional variable to indicate the state of cancelling current function. After running the function, the background thread will detect cancellation of current function and clear the variable.
- cancelFunctionAndWait() condition variable will wait for the variable to be cleared.
Similarly, cancelAllFunctionsAndWait() also suffers from the same issue.
Unit tests are added to reproduce the issue.
Reviewed By: yfeldblum
Differential Revision:
D5271664
fbshipit-source-id:
acb223304d3eab23129907ce9ff5e57e55f1e909