-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.cpp
More file actions
53 lines (47 loc) · 1.48 KB
/
Copy pathTimer.cpp
File metadata and controls
53 lines (47 loc) · 1.48 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
#include <muduo/Timer.h>
#include <muduo/Channel.h>
#include <muduo/EventLoop.h>
#include <muduo/TimerQueue.h>
#include <sys/timerfd.h>
#include <sys/timerfd.h>
#include <unistd.h>
using namespace muduo;
int CreateTimerfd() {
int fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
if (fd == -1) {
LOG_SYSFATAL << "Failed to create timerfd";
}
return fd;
}
Watcher::Watcher(TimerQueue* owner)
: owner_(owner)
#ifdef MUDUO_USE_MEMPOOL
, watcherChannel_(new (owner_->Owner()->GetMemoryPool()) Channel(owner_->Owner(), CreateTimerfd()))
#else
, watcherChannel_(std::make_unique<Channel>(owner_->Owner(), CreateTimerfd()))
#endif
{
watcherChannel_->EnableReading();
watcherChannel_->SetReadCallback(std::bind(&Watcher::HandleExpiredTimers, this));
// watcherChannel_->SetReadCallback([this](Channel::ReceiveTimePoint_t t) {
// this->HandleExpiredTimers();
// });
}
Watcher::~Watcher() noexcept {
watcherChannel_->disableAllEvents();
watcherChannel_->Remove();
close(watcherChannel_->FileDescriptor());
}
void Watcher::HandleExpiredTimers() {
owner_->Owner()->AssertInLoopThread();
ReadTimerfd();
owner_->HandleExpiredTimers();
}
void Watcher::ReadTimerfd() const {
uint64_t count = 0;
int ret = ::read(watcherChannel_->FileDescriptor(), &count, sizeof count);
if (ret != sizeof count) {
LOG_ERROR << "Watcher::ReadTimerfd reads " << ret << " bytes instead of 8";
}
// return count;
}