11# include <sys/types.h>
12# include <sys/event.h>
27 void runThread(
int watchFd,
int notifyFd);
31 MonitorFdHup(MonitorFdHup &&) =
delete;
32 MonitorFdHup(
const MonitorFdHup &) =
delete;
33 MonitorFdHup & operator=(MonitorFdHup &&) =
delete;
34 MonitorFdHup & operator=(
const MonitorFdHup &) =
delete;
39 notifyPipe.writeSide.close();
60inline void MonitorFdHup::runThread(
int watchFd,
int notifyFd)
62 int kqResult = kqueue();
64 throw SysError(
"MonitorFdHup kqueue");
66 AutoCloseFD kq{kqResult};
68 std::array<struct kevent, 2> kevs;
77 EV_SET(&kevs[0], watchFd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0,
nullptr);
78 EV_SET(&kevs[1], notifyFd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0,
nullptr);
80 int result = kevent(kq.get(), kevs.data(), kevs.size(),
nullptr, 0,
nullptr);
82 throw SysError(
"MonitorFdHup kevent add");
87 int numEvents = kevent(kq.get(),
nullptr, 0, &event, 1,
nullptr);
89 throw SysError(
"MonitorFdHup kevent watch");
92 if (numEvents > 0 && (event.flags & EV_EOF)) {
93 if (event.ident == uintptr_t(watchFd)) {
94 unix::triggerInterrupt();
102inline void MonitorFdHup::runThread(
int watchFd,
int notifyFd)
105 struct pollfd fds[2];
108 fds[1].fd = notifyFd;
111 auto count = poll(fds, 2, -1);
113 if (errno == EINTR || errno == EAGAIN) {
116 throw SysError(
"in MonitorFdHup poll()");
120 if (fds[0].revents & POLLHUP) {
121 unix::triggerInterrupt();
125 if (fds[1].revents & POLLHUP) {
133inline MonitorFdHup::MonitorFdHup(
int fd)
136 int notifyFd = notifyPipe.readSide.get();
137 thread = std::thread([
this, fd, notifyFd]() { this->runThread(fd, notifyFd); });
Definition file-descriptor.hh:331
Definition system-error.hh:283
File descriptor operations for almost arbitrary file descriptors.
Errors from the operating system: SystemError, and its subclasses for the native error codes,...