-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridge.cpp
More file actions
55 lines (46 loc) · 1.35 KB
/
Copy pathBridge.cpp
File metadata and controls
55 lines (46 loc) · 1.35 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
#include <muduo/Bridge.h>
#include <muduo/Channel.h>
#include <muduo/EventLoop.h>
#include <unistd.h>
#include <sys/eventfd.h>
using namespace muduo;
namespace muduo::detail {
int CreateEventFd() {
int ret = ::eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK);
if (ret == -1) {
LOG_SYSFATAL << "Failed to create eventfd";
}
return ret;
}
} // namespace muduo::detail
Bridge::Bridge(EventLoop* loop)
: owner_(loop)
#ifdef MUDUO_USE_MEMPOOL
, chan_(new (owner_->GetMemoryPool()) Channel(loop, CreateEventFd()))
#else
, chan_(std::make_unique<Channel>(loop, CreateEventFd()))
#endif
{
chan_->EnableReading();
chan_->SetReadCallback(std::bind(&Bridge::HandleWakeUpFdRead, this));
}
Bridge::~Bridge() noexcept {
chan_->disableAllEvents();
chan_->Remove();
::close(chan_->FileDescriptor());
}
void Bridge::WakeUp() const {
uint64_t buffer = 1;
auto n = ::write(chan_->FileDescriptor(), &buffer, sizeof buffer);
if (n != sizeof buffer) {
LOG_ERROR << "Bridge::WakeUp() writes " << n << " bytes instead of 8";
}
}
void Bridge::HandleWakeUpFdRead() const {
owner_->AssertInLoopThread();
uint64_t buffer = 1;
ssize_t n = ::read(chan_->FileDescriptor(), &buffer, sizeof buffer);
if (n != sizeof buffer) {
LOG_ERROR << "Bridge::HandleWakeUpFdRead() reads " << n << " bytes instead of 8";
}
}