trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
TimingWheel.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <trantor/net/EventLoop.h>
19#include <trantor/exports.h>
20#include <map>
21#include <mutex>
22#include <deque>
23#include <vector>
24#include <set>
25#include <unordered_map>
26#include <unordered_set>
27#include <atomic>
28#include <assert.h>
29
30#define TIMING_BUCKET_NUM_PER_WHEEL 100
31#define TIMING_TICK_INTERVAL 1.0
32
33namespace trantor
34{
35using EntryPtr = std::shared_ptr<void>;
36
37using EntryBucket = std::unordered_set<EntryPtr>;
38using BucketQueue = std::deque<EntryBucket>;
39
45class TRANTOR_EXPORT TimingWheel
46{
47 public:
48 class CallbackEntry
49 {
50 public:
51 CallbackEntry(std::function<void()> cb) : cb_(std::move(cb))
52 {
53 }
54 ~CallbackEntry()
55 {
56 cb_();
57 }
58
59 private:
60 std::function<void()> cb_;
61 };
62
78 size_t maxTimeout,
79 float ticksInterval = TIMING_TICK_INTERVAL,
80 size_t bucketsNumPerWheel = TIMING_BUCKET_NUM_PER_WHEEL);
81
82 void insertEntry(size_t delay, EntryPtr entryPtr);
83
84 void insertEntryInloop(size_t delay, EntryPtr entryPtr);
85
86 EventLoop *getLoop()
87 {
88 return loop_;
89 }
90
92
93 private:
94 std::vector<BucketQueue> wheels_;
95
96 std::atomic<size_t> ticksCounter_{0};
97
98 trantor::TimerId timerId_;
99 trantor::EventLoop *loop_;
100
101 float ticksInterval_;
102 size_t wheelsNum_;
103 size_t bucketsNumPerWheel_;
104};
105} // namespace trantor
As the name implies, this class represents an event loop that runs in a particular thread....
Definition EventLoop.h:56
This class implements a timer strategy with high performance and low accuracy. This is usually used i...
Definition TimingWheel.h:46
TimingWheel(trantor::EventLoop *loop, size_t maxTimeout, float ticksInterval=TIMING_TICK_INTERVAL, size_t bucketsNumPerWheel=TIMING_BUCKET_NUM_PER_WHEEL)
Construct a new timing wheel instance.
Definition EventLoop.h:34