Tweak pollTimedOutRPCs thread synchronization (#30355)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/30355
- Make processTimedOutFutures hold lock.
- Reduce unnecessary scan on future and future timeout maps.
- Reduce the scope of lock at a spot.
- Avoid repeatedly wake up if user set timeout = 0.
ghstack-source-id: 95409528
Test Plan:
# Unit tests
```
buck test mode/dev-nosan //caffe2/test:rpc_fork -- test_rpc_timeouts
buck-out/gen/caffe2/test/rpc_fork\#binary.par -r test_rpc_timeouts
```
```
buck test mode/dev-nosan //caffe2/test:rpc_fork_thrift -- test_rpc_timeouts
buck-out/gen/caffe2/test/rpc_fork_thrift\#binary.par -r test_rpc_timeouts
```
Differential Revision: D5516149
fbshipit-source-id: 4bb0bd59fa31d9bfaef9f07ac0126782da17f762