2014-07-23 17:21:00 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <thread>
|
2014-07-23 22:16:06 +00:00
|
|
|
#include <atomic>
|
2014-07-23 17:21:00 +00:00
|
|
|
|
2014-12-14 00:51:14 +00:00
|
|
|
#include <cstdlib>
|
2014-07-23 17:21:00 +00:00
|
|
|
#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)
|
|
|
|
{
|
2014-07-24 07:52:10 +00:00
|
|
|
thread = std::thread([fd]() {
|
2014-07-23 17:21:00 +00:00
|
|
|
/* Wait indefinitely until a POLLHUP occurs. */
|
|
|
|
struct pollfd fds[1];
|
|
|
|
fds[0].fd = fd;
|
|
|
|
fds[0].events = 0;
|
2014-07-24 09:47:51 +00:00
|
|
|
if (poll(fds, 1, -1) == -1) abort(); // can't happen
|
2014-07-23 22:16:06 +00:00
|
|
|
assert(fds[0].revents & POLLHUP);
|
2017-01-25 12:37:02 +00:00
|
|
|
triggerInterrupt();
|
2014-07-23 17:21:00 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
~MonitorFdHup()
|
|
|
|
{
|
2014-07-24 09:47:51 +00:00
|
|
|
pthread_cancel(thread.native_handle());
|
2014-07-23 17:21:00 +00:00
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|