event/Loop: add flag "dead"

This commit is contained in:
Max Kellermann 2018-01-29 22:52:13 +01:00
parent 6ea2cb3644
commit ce68701c0c
2 changed files with 18 additions and 2 deletions

View File

@ -25,7 +25,9 @@
#include "util/ScopeExit.hxx" #include "util/ScopeExit.hxx"
EventLoop::EventLoop(ThreadId _thread) EventLoop::EventLoop(ThreadId _thread)
:SocketMonitor(*this), quit(false), thread(_thread) :SocketMonitor(*this),
quit(false), dead(false),
thread(_thread)
{ {
SocketMonitor::Open(SocketDescriptor(wake_fd.Get())); SocketMonitor::Open(SocketDescriptor(wake_fd.Get()));
} }
@ -142,10 +144,14 @@ EventLoop::Run() noexcept
assert(IsInside()); assert(IsInside());
assert(!quit); assert(!quit);
assert(!dead);
assert(busy); assert(busy);
SocketMonitor::Schedule(SocketMonitor::READ); SocketMonitor::Schedule(SocketMonitor::READ);
AtScopeExit(this) { SocketMonitor::Cancel(); }; AtScopeExit(this) {
dead = true;
SocketMonitor::Cancel();
};
do { do {
now = std::chrono::steady_clock::now(); now = std::chrono::steady_clock::now();
@ -210,6 +216,7 @@ EventLoop::Run() noexcept
} while (!quit); } while (!quit);
#ifndef NDEBUG #ifndef NDEBUG
assert(!dead);
assert(busy); assert(busy);
assert(thread.IsInside()); assert(thread.IsInside());
#endif #endif

View File

@ -88,6 +88,11 @@ class EventLoop final : SocketMonitor
std::atomic_bool quit; std::atomic_bool quit;
/**
* If this is true, then Run() has returned.
*/
std::atomic_bool dead;
/** /**
* True when the object has been modified and another check is * True when the object has been modified and another check is
* necessary before going to sleep via PollGroup::ReadEvents(). * necessary before going to sleep via PollGroup::ReadEvents().
@ -199,6 +204,10 @@ private:
bool OnSocketReady(unsigned flags) noexcept override; bool OnSocketReady(unsigned flags) noexcept override;
public: public:
gcc_pure
bool IsDead() const noexcept {
return dead;
}
/** /**
* Are we currently running inside this EventLoop's thread? * Are we currently running inside this EventLoop's thread?