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

44 lines
809 B
C++
Raw Normal View History

#pragma once
#include <thread>
2014-07-24 00:16:06 +02:00
#include <atomic>
2014-12-09 12:16:27 +01:00
#include <stdlib.h>
#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 00:16:06 +02:00
assert(fds[0].revents & POLLHUP);
/* We got POLLHUP, so send an INT signal to the main thread. */
kill(getpid(), SIGINT);
});
};
~MonitorFdHup()
{
pthread_cancel(thread.native_handle());
thread.join();
}
};
}