2013-08-08 21:50:07 +02:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2013-08-08 21:50:07 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "Loop.hxx"
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2013-10-15 09:38:12 +02:00
|
|
|
#include "system/Clock.hxx"
|
2013-08-07 22:16:59 +02:00
|
|
|
#include "TimeoutMonitor.hxx"
|
|
|
|
#include "SocketMonitor.hxx"
|
|
|
|
#include "IdleMonitor.hxx"
|
2014-01-04 14:56:02 +01:00
|
|
|
#include "DeferredMonitor.hxx"
|
2013-08-07 22:16:59 +02:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2014-02-04 23:56:24 +01:00
|
|
|
EventLoop::EventLoop()
|
2013-08-07 22:16:59 +02:00
|
|
|
:SocketMonitor(*this),
|
2013-10-15 09:38:12 +02:00
|
|
|
now_ms(::MonotonicClockMS()),
|
2014-01-05 01:37:22 +01:00
|
|
|
quit(false), busy(true),
|
2014-01-09 17:50:29 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
virgin(true),
|
|
|
|
#endif
|
2013-08-10 14:01:55 +02:00
|
|
|
thread(ThreadId::Null())
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
|
|
|
SocketMonitor::Open(wake_fd.Get());
|
|
|
|
SocketMonitor::Schedule(SocketMonitor::READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
EventLoop::~EventLoop()
|
|
|
|
{
|
|
|
|
assert(idle.empty());
|
|
|
|
assert(timers.empty());
|
2014-01-06 21:57:19 +01:00
|
|
|
|
|
|
|
/* this is necessary to get a well-defined destruction
|
|
|
|
order */
|
|
|
|
SocketMonitor::Cancel();
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventLoop::Break()
|
|
|
|
{
|
2014-01-04 15:58:51 +01:00
|
|
|
quit = true;
|
|
|
|
wake_fd.Write();
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
2013-11-28 11:37:23 +01:00
|
|
|
bool
|
|
|
|
EventLoop::Abandon(int _fd, SocketMonitor &m)
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
2014-01-09 17:50:29 +01:00
|
|
|
assert(IsInsideOrVirgin());
|
2014-01-05 01:40:50 +01:00
|
|
|
|
2013-11-28 11:37:23 +01:00
|
|
|
poll_result.Clear(&m);
|
|
|
|
return poll_group.Abandon(_fd);
|
2013-11-06 18:20:51 +01:00
|
|
|
}
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2013-11-06 18:20:51 +01:00
|
|
|
bool
|
|
|
|
EventLoop::RemoveFD(int _fd, SocketMonitor &m)
|
|
|
|
{
|
2014-01-05 01:40:50 +01:00
|
|
|
assert(IsInsideOrNull());
|
|
|
|
|
2013-11-28 11:37:23 +01:00
|
|
|
poll_result.Clear(&m);
|
|
|
|
return poll_group.Remove(_fd);
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventLoop::AddIdle(IdleMonitor &i)
|
|
|
|
{
|
2014-01-09 17:50:29 +01:00
|
|
|
assert(IsInsideOrVirgin());
|
2013-08-07 22:16:59 +02:00
|
|
|
assert(std::find(idle.begin(), idle.end(), &i) == idle.end());
|
|
|
|
|
|
|
|
idle.push_back(&i);
|
2014-01-05 01:38:51 +01:00
|
|
|
again = true;
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventLoop::RemoveIdle(IdleMonitor &i)
|
|
|
|
{
|
2014-01-09 17:50:29 +01:00
|
|
|
assert(IsInsideOrVirgin());
|
2014-01-05 01:40:50 +01:00
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
auto it = std::find(idle.begin(), idle.end(), &i);
|
|
|
|
assert(it != idle.end());
|
|
|
|
|
|
|
|
idle.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventLoop::AddTimer(TimeoutMonitor &t, unsigned ms)
|
|
|
|
{
|
2014-01-09 20:55:43 +01:00
|
|
|
/* can't use IsInsideOrVirgin() here because libavahi-client
|
|
|
|
modifies the timeout during avahi_client_free() */
|
|
|
|
assert(IsInsideOrNull());
|
2014-01-05 01:40:50 +01:00
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
timers.insert(TimerRecord(t, now_ms + ms));
|
2014-01-05 01:38:51 +01:00
|
|
|
again = true;
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventLoop::CancelTimer(TimeoutMonitor &t)
|
|
|
|
{
|
2014-01-05 01:40:50 +01:00
|
|
|
assert(IsInsideOrNull());
|
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
for (auto i = timers.begin(), end = timers.end(); i != end; ++i) {
|
|
|
|
if (&i->timer == &t) {
|
|
|
|
timers.erase(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 21:50:07 +02:00
|
|
|
void
|
|
|
|
EventLoop::Run()
|
|
|
|
{
|
2013-08-10 09:00:04 +02:00
|
|
|
assert(thread.IsNull());
|
2014-01-09 17:50:29 +01:00
|
|
|
assert(virgin);
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
virgin = false;
|
|
|
|
#endif
|
|
|
|
|
2013-08-10 09:00:04 +02:00
|
|
|
thread = ThreadId::GetCurrent();
|
2013-08-07 22:49:46 +02:00
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
assert(!quit);
|
2014-01-05 01:37:22 +01:00
|
|
|
assert(busy);
|
2013-08-07 22:16:59 +02:00
|
|
|
|
|
|
|
do {
|
2013-10-15 09:38:12 +02:00
|
|
|
now_ms = ::MonotonicClockMS();
|
2014-01-05 01:38:51 +01:00
|
|
|
again = false;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
|
|
|
/* invoke timers */
|
|
|
|
|
|
|
|
int timeout_ms;
|
|
|
|
while (true) {
|
|
|
|
auto i = timers.begin();
|
|
|
|
if (i == timers.end()) {
|
|
|
|
timeout_ms = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout_ms = i->due_ms - now_ms;
|
|
|
|
if (timeout_ms > 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
TimeoutMonitor &m = i->timer;
|
|
|
|
timers.erase(i);
|
|
|
|
|
|
|
|
m.Run();
|
|
|
|
|
|
|
|
if (quit)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* invoke idle */
|
|
|
|
|
|
|
|
while (!idle.empty()) {
|
|
|
|
IdleMonitor &m = *idle.front();
|
|
|
|
idle.pop_front();
|
|
|
|
m.Run();
|
|
|
|
|
|
|
|
if (quit)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-05 01:37:22 +01:00
|
|
|
/* try to handle DeferredMonitors without WakeFD
|
|
|
|
overhead */
|
|
|
|
mutex.lock();
|
|
|
|
HandleDeferred();
|
|
|
|
busy = false;
|
|
|
|
mutex.unlock();
|
|
|
|
|
2014-01-05 01:38:51 +01:00
|
|
|
if (again)
|
2013-08-07 22:16:59 +02:00
|
|
|
/* re-evaluate timers because one of the
|
|
|
|
IdleMonitors may have added a new
|
|
|
|
timeout */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* wait for new event */
|
|
|
|
|
2013-11-28 11:37:23 +01:00
|
|
|
poll_group.ReadEvents(poll_result, timeout_ms);
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2013-10-15 09:38:12 +02:00
|
|
|
now_ms = ::MonotonicClockMS();
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2014-01-05 01:37:22 +01:00
|
|
|
mutex.lock();
|
|
|
|
busy = true;
|
|
|
|
mutex.unlock();
|
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
/* invoke sockets */
|
2013-11-28 11:37:23 +01:00
|
|
|
for (int i = 0; i < poll_result.GetSize(); ++i) {
|
|
|
|
auto events = poll_result.GetEvents(i);
|
|
|
|
if (events != 0) {
|
2013-08-07 22:16:59 +02:00
|
|
|
if (quit)
|
|
|
|
break;
|
2014-01-04 17:05:01 +01:00
|
|
|
|
|
|
|
auto m = (SocketMonitor *)poll_result.GetObject(i);
|
|
|
|
m->Dispatch(events);
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-28 11:37:23 +01:00
|
|
|
poll_result.Reset();
|
2013-11-27 12:04:38 +01:00
|
|
|
|
2013-11-28 11:37:23 +01:00
|
|
|
} while (!quit);
|
2013-08-07 22:49:46 +02:00
|
|
|
|
2014-01-05 01:40:50 +01:00
|
|
|
#ifndef NDEBUG
|
2014-01-05 01:37:22 +01:00
|
|
|
assert(busy);
|
2013-08-10 09:00:04 +02:00
|
|
|
assert(thread.IsInside());
|
2014-01-05 01:40:50 +01:00
|
|
|
thread = ThreadId::Null();
|
|
|
|
#endif
|
2013-08-08 21:50:07 +02:00
|
|
|
}
|
|
|
|
|
2014-01-04 14:56:02 +01:00
|
|
|
void
|
|
|
|
EventLoop::AddDeferred(DeferredMonitor &d)
|
|
|
|
{
|
|
|
|
mutex.lock();
|
|
|
|
if (d.pending) {
|
|
|
|
mutex.unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(std::find(deferred.begin(),
|
|
|
|
deferred.end(), &d) == deferred.end());
|
|
|
|
|
2014-01-05 01:50:25 +01:00
|
|
|
/* we don't need to wake up the EventLoop if another
|
|
|
|
DeferredMonitor has already done it */
|
2014-01-05 01:37:22 +01:00
|
|
|
const bool must_wake = !busy && deferred.empty();
|
2014-01-05 01:50:25 +01:00
|
|
|
|
2014-01-04 14:56:02 +01:00
|
|
|
d.pending = true;
|
|
|
|
deferred.push_back(&d);
|
2014-01-05 01:37:22 +01:00
|
|
|
again = true;
|
2014-01-04 14:56:02 +01:00
|
|
|
mutex.unlock();
|
|
|
|
|
2014-01-05 01:50:25 +01:00
|
|
|
if (must_wake)
|
|
|
|
wake_fd.Write();
|
2014-01-04 14:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventLoop::RemoveDeferred(DeferredMonitor &d)
|
|
|
|
{
|
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
|
|
|
|
if (!d.pending) {
|
|
|
|
assert(std::find(deferred.begin(),
|
|
|
|
deferred.end(), &d) == deferred.end());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d.pending = false;
|
|
|
|
|
|
|
|
auto i = std::find(deferred.begin(), deferred.end(), &d);
|
|
|
|
assert(i != deferred.end());
|
|
|
|
|
|
|
|
deferred.erase(i);
|
|
|
|
}
|
|
|
|
|
2014-01-05 01:32:59 +01:00
|
|
|
void
|
|
|
|
EventLoop::HandleDeferred()
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
2014-01-04 14:56:02 +01:00
|
|
|
while (!deferred.empty() && !quit) {
|
|
|
|
DeferredMonitor &m = *deferred.front();
|
|
|
|
assert(m.pending);
|
|
|
|
|
|
|
|
deferred.pop_front();
|
|
|
|
m.pending = false;
|
|
|
|
|
|
|
|
mutex.unlock();
|
|
|
|
m.RunDeferred();
|
|
|
|
mutex.lock();
|
|
|
|
}
|
2014-01-05 01:32:59 +01:00
|
|
|
}
|
2014-01-04 14:56:02 +01:00
|
|
|
|
2014-01-05 01:32:59 +01:00
|
|
|
bool
|
|
|
|
EventLoop::OnSocketReady(gcc_unused unsigned flags)
|
|
|
|
{
|
2014-01-05 01:40:50 +01:00
|
|
|
assert(IsInside());
|
|
|
|
|
2014-01-05 01:32:59 +01:00
|
|
|
wake_fd.Read();
|
|
|
|
|
|
|
|
mutex.lock();
|
|
|
|
HandleDeferred();
|
2013-08-07 22:16:59 +02:00
|
|
|
mutex.unlock();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|