-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathKAsyncWaker.cpp
More file actions
50 lines (44 loc) · 1.38 KB
/
Copy pathKAsyncWaker.cpp
File metadata and controls
50 lines (44 loc) · 1.38 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
#include "KAsyncWaker.h"
#include "../poller/KChannel.h"
#include "KEventLoop.h"
using namespace kback;
AsyncWaker::AsyncWaker(EventLoop *loop)
: wakerfd_(::eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC)), loop_(loop),
wakerchannel_(new Channel(loop, wakerfd_)) {
if (wakerfd_ < 0) {
#ifdef USE_STD_COUT
std::cout << "LOG_SYSERR: "
<< "Failed in eventfd" << std::endl;
#endif
abort();
}
wakerchannel_->setReadCallback(std::bind(&AsyncWaker::handleRead, this));
wakerchannel_->enableReading();
}
AsyncWaker::~AsyncWaker() { ::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) {
#ifdef USE_STD_COUT
std::cout << "LOG_ERROR: "
<< "AsyncWaker::handleRead() reads " << n
<< " bytes instead of 8";
#endif
}
}
void AsyncWaker::wakeup() {
uint64_t one = 1;
ssize_t n = ::write(wakerfd_, &one, sizeof one);
// 判断写入的字节是不是 one对应的字节数
if (n != sizeof one) {
#ifdef USE_STD_COUT
std::cout << "LOG_ERROR: "
<< "AsyncWaker::wakeup() writes " << n << " bytes instead of 8"
<< std::endl;
#endif
}
}