event/Loop: add thread-safety assertions

This commit is contained in:
Max Kellermann 2014-01-05 01:40:50 +01:00
parent 4ddfc6e9a2
commit 6268955778
3 changed files with 30 additions and 1 deletions

View File

@ -26,7 +26,7 @@
void void
IdleMonitor::Cancel() IdleMonitor::Cancel()
{ {
assert(loop.IsInside()); assert(loop.IsInsideOrNull());
if (!IsActive()) if (!IsActive())
return; return;

View File

@ -57,6 +57,8 @@ EventLoop::Break()
bool bool
EventLoop::Abandon(int _fd, SocketMonitor &m) EventLoop::Abandon(int _fd, SocketMonitor &m)
{ {
assert(IsInside());
poll_result.Clear(&m); poll_result.Clear(&m);
return poll_group.Abandon(_fd); return poll_group.Abandon(_fd);
} }
@ -64,6 +66,8 @@ EventLoop::Abandon(int _fd, SocketMonitor &m)
bool bool
EventLoop::RemoveFD(int _fd, SocketMonitor &m) EventLoop::RemoveFD(int _fd, SocketMonitor &m)
{ {
assert(IsInsideOrNull());
poll_result.Clear(&m); poll_result.Clear(&m);
return poll_group.Remove(_fd); return poll_group.Remove(_fd);
} }
@ -71,6 +75,7 @@ EventLoop::RemoveFD(int _fd, SocketMonitor &m)
void void
EventLoop::AddIdle(IdleMonitor &i) EventLoop::AddIdle(IdleMonitor &i)
{ {
assert(IsInside());
assert(std::find(idle.begin(), idle.end(), &i) == idle.end()); assert(std::find(idle.begin(), idle.end(), &i) == idle.end());
idle.push_back(&i); idle.push_back(&i);
@ -79,6 +84,8 @@ EventLoop::AddIdle(IdleMonitor &i)
void void
EventLoop::RemoveIdle(IdleMonitor &i) EventLoop::RemoveIdle(IdleMonitor &i)
{ {
assert(IsInside());
auto it = std::find(idle.begin(), idle.end(), &i); auto it = std::find(idle.begin(), idle.end(), &i);
assert(it != idle.end()); assert(it != idle.end());
@ -88,12 +95,16 @@ EventLoop::RemoveIdle(IdleMonitor &i)
void void
EventLoop::AddTimer(TimeoutMonitor &t, unsigned ms) EventLoop::AddTimer(TimeoutMonitor &t, unsigned ms)
{ {
assert(IsInside());
timers.insert(TimerRecord(t, now_ms + ms)); timers.insert(TimerRecord(t, now_ms + ms));
} }
void void
EventLoop::CancelTimer(TimeoutMonitor &t) EventLoop::CancelTimer(TimeoutMonitor &t)
{ {
assert(IsInsideOrNull());
for (auto i = timers.begin(), end = timers.end(); i != end; ++i) { for (auto i = timers.begin(), end = timers.end(); i != end; ++i) {
if (&i->timer == &t) { if (&i->timer == &t) {
timers.erase(i); timers.erase(i);
@ -176,7 +187,10 @@ EventLoop::Run()
} while (!quit); } while (!quit);
#ifndef NDEBUG
assert(thread.IsInside()); assert(thread.IsInside());
thread = ThreadId::Null();
#endif
} }
void void
@ -236,6 +250,8 @@ EventLoop::HandleDeferred()
bool bool
EventLoop::OnSocketReady(gcc_unused unsigned flags) EventLoop::OnSocketReady(gcc_unused unsigned flags)
{ {
assert(IsInside());
wake_fd.Read(); wake_fd.Read();
mutex.lock(); mutex.lock();

View File

@ -102,6 +102,8 @@ public:
* A caching wrapper for MonotonicClockMS(). * A caching wrapper for MonotonicClockMS().
*/ */
unsigned GetTimeMS() const { unsigned GetTimeMS() const {
assert(IsInside());
return now_ms; return now_ms;
} }
@ -113,10 +115,14 @@ public:
void Break(); void Break();
bool AddFD(int _fd, unsigned flags, SocketMonitor &m) { bool AddFD(int _fd, unsigned flags, SocketMonitor &m) {
assert(thread.IsNull() || thread.IsInside());
return poll_group.Add(_fd, flags, &m); return poll_group.Add(_fd, flags, &m);
} }
bool ModifyFD(int _fd, unsigned flags, SocketMonitor &m) { bool ModifyFD(int _fd, unsigned flags, SocketMonitor &m) {
assert(IsInside());
return poll_group.Modify(_fd, flags, &m); return poll_group.Modify(_fd, flags, &m);
} }
@ -177,6 +183,13 @@ public:
return thread.IsInside(); return thread.IsInside();
} }
#ifndef NDEBUG
gcc_pure
bool IsInsideOrNull() const {
return thread.IsNull() || thread.IsInside();
}
#endif
}; };
#endif /* MAIN_NOTIFY_H */ #endif /* MAIN_NOTIFY_H */