nix-super/src/libutil/monitor-fd.hh

43 lines
728 B
C++
Raw Normal View History

#pragma once
#include <thread>
2014-07-24 01:16:06 +03:00
#include <atomic>
2014-12-14 02:51:14 +02:00
#include <cstdlib>
#include <poll.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
namespace nix {
class MonitorFdHup
{
private:
std::thread thread;
public:
MonitorFdHup(int fd)
{
thread = std::thread([fd]() {
/* Wait indefinitely until a POLLHUP occurs. */
struct pollfd fds[1];
fds[0].fd = fd;
fds[0].events = 0;
if (poll(fds, 1, -1) == -1) abort(); // can't happen
2014-07-24 01:16:06 +03:00
assert(fds[0].revents & POLLHUP);
2017-01-25 14:37:02 +02:00
triggerInterrupt();
});
};
~MonitorFdHup()
{
pthread_cancel(thread.native_handle());
thread.join();
}
};
}