-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathKAsyncWaker.cpp
More file actions
48 lines (41 loc) · 1.36 KB
/
Copy pathKAsyncWaker.cpp
File metadata and controls
48 lines (41 loc) · 1.36 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
#include "KAsyncWaker.h"
#include "KEventLoop.h"
#include "webserver/poller/KChannel.h"
#include "webserver/utils/KAsyncLogger.h"
#include <errno.h>
#include <unistd.h>
using namespace kback;
AsyncWaker::AsyncWaker(EventLoop *loop)
: wakerfd_(::eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC)), loop_(loop),
wakerchannel_(std::make_unique<Channel>(loop, wakerfd_)) {
if (wakerfd_ < 0) {
KBACK_LOG_SYSERR("Failed in eventfd");
abort();
}
wakerchannel_->setReadCallback([this](Timestamp) { handleRead(); });
wakerchannel_->enableReading();
}
AsyncWaker::~AsyncWaker() {
wakerchannel_->disableAll();
loop_->removeChannel(wakerchannel_.get());
::close(wakerfd_);
}
void AsyncWaker::handleRead() {
loop_->assertInLoopThread();
// 和wakeup函数对应,wakeup函数实际上是写事件,handleRead为对应的读事件
uint64_t one = 1;
ssize_t n = ::read(wakerfd_, &one, sizeof one);
// 判断读取的字节是不是 one对应的字节数
if (n != sizeof one) {
KBACK_LOG_ERROR("AsyncWaker::handleRead() reads %zd bytes instead of 8",
n);
}
}
void AsyncWaker::wakeup() {
uint64_t one = 1;
ssize_t n = ::write(wakerfd_, &one, sizeof one);
// 判断写入的字节是不是 one对应的字节数
if (n != sizeof one && errno != EAGAIN) {
KBACK_LOG_ERROR("AsyncWaker::wakeup() writes %zd bytes instead of 8", n);
}
}