-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerQueue.h
More file actions
69 lines (55 loc) · 1.79 KB
/
Copy pathTimerQueue.h
File metadata and controls
69 lines (55 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#if !defined(MUDUO_TIMER_QUEUE_H)
#define MUDUO_TIMER_QUEUE_H
#include <muduo/base/allocator/Allocatable.h>
#include <muduo/TimerType.h>
#include <chrono>
#include <memory>
#include <vector>
#include <atomic>
#include <functional>
#include <unordered_map>
namespace muduo {
using namespace detail;
class EventLoop; // forward declaration
class Watcher; // forward declaration
class Timer; // forward declaration
#ifdef MUDUO_USE_MEMPOOL
class TimerQueue : public base::detail::Allocatable {
#else
class TimerQueue {
#endif
using TimerId = std::atomic_int;
// static_assert(typeid(TimerQueue::TimerId::value_type) == typeid(detail::TimerId_t), "The type of timer-id must be the same");
// non-copyable & non-moveable
TimerQueue(const TimerQueue&) = delete;
TimerQueue operator=(const TimerQueue) = delete;
class TimerMinHeap;
public:
TimerQueue(EventLoop* owner);
~TimerQueue() noexcept;
/**
* thread-safe
*/
detail::TimerId_t AddTimer(const TimePoint_t& when, const Interval_t& interval, const TimeoutCb_t& cb);
void CancelTimer(const detail::TimerId_t id);
/**
* return the loop instance whose own the current channel
*/
EventLoop* Owner() { return owner_; }
const EventLoop* Owner() const { return owner_; }
void HandleExpiredTimers();
private:
void AddTimerInLoop(std::unique_ptr<Timer>& t_p);
void CancelTimerInLoop(const detail::TimerId_t id);
void ResetTimerfd();
using ExpiredTimerList = std::vector<std::unique_ptr<Timer>>;
ExpiredTimerList GetExpiredTimers();
private:
EventLoop* const owner_;
std::unique_ptr<Watcher> watcher_;
std::unique_ptr<TimerMinHeap> heap_;
TimerId nextTimerId_;
TimePoint_t latestTime_;
};
} // namespace muduo
#endif // MUDUO_TIMER_QUEUE_H