folly/Bits.h (BitIterator): avoid -Wsign-compare error
[folly.git] / folly / wangle / futures / detail / ThreadWheelTimekeeper.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "ThreadWheelTimekeeper.h"
17
18 #include <folly/experimental/Singleton.h>
19 #include <folly/wangle/futures/Future.h>
20 #include <future>
21
22 namespace folly { namespace wangle { namespace detail {
23
24 namespace {
25   Singleton<ThreadWheelTimekeeper> timekeeperSingleton_;
26
27   // Our Callback object for HHWheelTimer
28   struct WTCallback : public folly::HHWheelTimer::Callback {
29     // Only allow creation by this factory, to ensure heap allocation.
30     static WTCallback* create() {
31       // optimization opportunity: memory pool
32       return new WTCallback();
33     }
34
35     Future<void> getFuture() {
36       return promise_.getFuture();
37     }
38
39    protected:
40     Promise<void> promise_;
41
42     explicit WTCallback() {
43       promise_.setInterruptHandler(
44         std::bind(&WTCallback::interruptHandler, this));
45     }
46
47     void timeoutExpired() noexcept override {
48       promise_.setValue();
49       delete this;
50     }
51
52     void interruptHandler() {
53       cancelTimeout();
54       delete this;
55     }
56   };
57
58 } // namespace
59
60
61 ThreadWheelTimekeeper::ThreadWheelTimekeeper() :
62   thread_([this]{ eventBase_.loopForever(); }),
63   wheelTimer_(new HHWheelTimer(&eventBase_, std::chrono::milliseconds(1)))
64 {
65   eventBase_.waitUntilRunning();
66   eventBase_.runInEventBaseThread([this]{
67     // 15 characters max
68     eventBase_.setName("FutureTimekeepr");
69   });
70 }
71
72 ThreadWheelTimekeeper::~ThreadWheelTimekeeper() {
73   eventBase_.runInEventBaseThread([this]{
74     wheelTimer_->cancelAll();
75   });
76   eventBase_.terminateLoopSoon();
77   thread_.join();
78 }
79
80 Future<void> ThreadWheelTimekeeper::after(Duration dur) {
81   auto cob = WTCallback::create();
82   auto f = cob->getFuture();
83   eventBase_.runInEventBaseThread([=]{
84     wheelTimer_->scheduleTimeout(cob, dur);
85   });
86   return f;
87 }
88
89 Timekeeper* getTimekeeperSingleton() {
90   return timekeeperSingleton_.get_fast();
91 }
92
93 }}}