proxygen
TimeoutQueue.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-present 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 
28 #pragma once
29 
30 #include <cstdint>
31 #include <functional>
32 
33 #include <boost/multi_index/indexed_by.hpp>
34 #include <boost/multi_index/member.hpp>
35 #include <boost/multi_index/ordered_index.hpp>
36 #include <boost/multi_index_container.hpp>
37 
38 namespace folly {
39 
40 class TimeoutQueue {
41  public:
42  typedef int64_t Id;
43  typedef std::function<void(Id, int64_t)> Callback;
44 
46 
52  Id add(int64_t now, int64_t delay, Callback callback);
53 
62  Id addRepeating(int64_t now, int64_t interval, Callback callback);
63 
68  bool erase(Id id);
69 
88  return runInternal(now, true);
89  }
91  return runInternal(now, false);
92  }
93 
97  int64_t nextExpiration() const;
98 
99  private:
100  int64_t runInternal(int64_t now, bool runOnce);
101  // noncopyable
102  TimeoutQueue(const TimeoutQueue&) = delete;
103  TimeoutQueue& operator=(const TimeoutQueue&) = delete;
104 
105  struct Event {
106  Id id;
109  Callback callback;
110  };
111 
112  typedef boost::multi_index_container<
113  Event,
114  boost::multi_index::indexed_by<
115  boost::multi_index::ordered_unique<
116  boost::multi_index::member<Event, Id, &Event::id>>,
117  boost::multi_index::ordered_non_unique<
118  boost::multi_index::member<Event, int64_t, &Event::expiration>>>>
120 
121  enum {
122  BY_ID = 0,
124  };
125 
128 };
129 
130 } // namespace folly
int64_t runOnce(int64_t now)
Definition: TimeoutQueue.h:87
int64_t runLoop(int64_t now)
Definition: TimeoutQueue.h:90
Id add(int64_t now, int64_t delay, Callback callback)
std::chrono::steady_clock::time_point now()
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
int64_t nextExpiration() const
std::function< void(Id, int64_t)> Callback
Definition: TimeoutQueue.h:43
boost::multi_index_container< Event, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::member< Event, Id,&Event::id > >, boost::multi_index::ordered_non_unique< boost::multi_index::member< Event, int64_t,&Event::expiration > > > > Set
Definition: TimeoutQueue.h:119
Id addRepeating(int64_t now, int64_t interval, Callback callback)
TimeoutQueue & operator=(const TimeoutQueue &)=delete
Event
Definition: Events.h:15
int64_t runInternal(int64_t now, bool runOnce)