Add some assertions

This commit is contained in:
Eelco Dolstra 2014-07-24 00:16:06 +02:00
parent d3c61d83be
commit 1eb0af7ed5

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <thread> #include <thread>
#include <atomic>
#include <poll.h> #include <poll.h>
#include <sys/types.h> #include <sys/types.h>
@ -14,10 +15,12 @@ class MonitorFdHup
{ {
private: private:
std::thread thread; std::thread thread;
std::atomic_bool quit;
public: public:
MonitorFdHup(int fd) MonitorFdHup(int fd)
{ {
quit = false;
thread = std::thread([&]() { thread = std::thread([&]() {
/* Wait indefinitely until a POLLHUP occurs. */ /* Wait indefinitely until a POLLHUP occurs. */
struct pollfd fds[1]; struct pollfd fds[1];
@ -25,8 +28,10 @@ public:
fds[0].events = 0; fds[0].events = 0;
if (poll(fds, 1, -1) == -1) { if (poll(fds, 1, -1) == -1) {
if (errno != EINTR) abort(); // can't happen if (errno != EINTR) abort(); // can't happen
assert(quit);
return; // destructor is asking us to exit return; // destructor is asking us to exit
} }
assert(fds[0].revents & POLLHUP);
/* We got POLLHUP, so send an INT signal to the main thread. */ /* We got POLLHUP, so send an INT signal to the main thread. */
kill(getpid(), SIGINT); kill(getpid(), SIGINT);
}); });
@ -34,6 +39,7 @@ public:
~MonitorFdHup() ~MonitorFdHup()
{ {
quit = true;
pthread_kill(thread.native_handle(), SIGINT); pthread_kill(thread.native_handle(), SIGINT);
thread.join(); thread.join();
} }