Loop.cxx 5.65 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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"
22 23 24
#include "TimeoutMonitor.hxx"
#include "SocketMonitor.hxx"
#include "IdleMonitor.hxx"
25
#include "DeferredMonitor.hxx"
26 27 28

#include <algorithm>

29
EventLoop::EventLoop()
30
	:SocketMonitor(*this)
31 32 33 34 35 36 37 38 39
{
	SocketMonitor::Open(wake_fd.Get());
	SocketMonitor::Schedule(SocketMonitor::READ);
}

EventLoop::~EventLoop()
{
	assert(idle.empty());
	assert(timers.empty());
40 41 42 43

	/* this is necessary to get a well-defined destruction
	   order */
	SocketMonitor::Cancel();
44 45 46 47 48
}

void
EventLoop::Break()
{
49 50
	quit = true;
	wake_fd.Write();
51 52
}

53 54
bool
EventLoop::Abandon(int _fd, SocketMonitor &m)
55
{
56
	assert(IsInsideOrVirgin());
57

58 59
	poll_result.Clear(&m);
	return poll_group.Abandon(_fd);
60
}
61

62 63 64
bool
EventLoop::RemoveFD(int _fd, SocketMonitor &m)
{
65 66
	assert(IsInsideOrNull());

67 68
	poll_result.Clear(&m);
	return poll_group.Remove(_fd);
69 70 71 72 73
}

void
EventLoop::AddIdle(IdleMonitor &i)
{
74
	assert(IsInsideOrVirgin());
75 76 77
	assert(std::find(idle.begin(), idle.end(), &i) == idle.end());

	idle.push_back(&i);
78
	again = true;
79 80 81 82 83
}

void
EventLoop::RemoveIdle(IdleMonitor &i)
{
84
	assert(IsInsideOrVirgin());
85

86 87 88 89 90 91 92
	auto it = std::find(idle.begin(), idle.end(), &i);
	assert(it != idle.end());

	idle.erase(it);
}

void
93
EventLoop::AddTimer(TimeoutMonitor &t, std::chrono::steady_clock::duration d)
94
{
95 96 97
	/* can't use IsInsideOrVirgin() here because libavahi-client
	   modifies the timeout during avahi_client_free() */
	assert(IsInsideOrNull());
98

99
	timers.insert(TimerRecord(t, now + d));
100
	again = true;
101 102 103 104 105
}

void
EventLoop::CancelTimer(TimeoutMonitor &t)
{
106 107
	assert(IsInsideOrNull());

108 109 110 111 112 113 114 115
	for (auto i = timers.begin(), end = timers.end(); i != end; ++i) {
		if (&i->timer == &t) {
			timers.erase(i);
			return;
		}
	}
}

116 117 118 119 120 121 122 123 124 125 126 127 128
/**
 * Convert the given timeout specification to a milliseconds integer,
 * to be used by functions like poll() and epoll_wait().  Any negative
 * value (= never times out) is translated to the magic value -1.
 */
static constexpr int
ExportTimeoutMS(std::chrono::steady_clock::duration timeout)
{
	return timeout >= timeout.zero()
		? int(std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count())
		: -1;
}

129 130 131
void
EventLoop::Run()
{
132
	assert(thread.IsNull());
133 134 135 136 137 138
	assert(virgin);

#ifndef NDEBUG
	virgin = false;
#endif

139
	thread = ThreadId::GetCurrent();
140

141
	assert(!quit);
142
	assert(busy);
143 144

	do {
145
		now = std::chrono::steady_clock::now();
146
		again = false;
147 148 149

		/* invoke timers */

150
		std::chrono::steady_clock::duration timeout;
151 152 153
		while (true) {
			auto i = timers.begin();
			if (i == timers.end()) {
154
				timeout = std::chrono::steady_clock::duration(-1);
155 156 157
				break;
			}

158 159
			timeout = i->due - now;
			if (timeout > timeout.zero())
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
				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;
		}

182 183 184 185 186
		/* try to handle DeferredMonitors without WakeFD
		   overhead */
		mutex.lock();
		HandleDeferred();
		busy = false;
187
		const bool _again = again;
188 189
		mutex.unlock();

190
		if (_again)
191 192 193 194 195 196 197
			/* re-evaluate timers because one of the
			   IdleMonitors may have added a new
			   timeout */
			continue;

		/* wait for new event */

198
		poll_group.ReadEvents(poll_result, ExportTimeoutMS(timeout));
199

200
		now = std::chrono::steady_clock::now();
201

202 203 204 205
		mutex.lock();
		busy = true;
		mutex.unlock();

206
		/* invoke sockets */
207 208 209
		for (int i = 0; i < poll_result.GetSize(); ++i) {
			auto events = poll_result.GetEvents(i);
			if (events != 0) {
210 211
				if (quit)
					break;
212 213 214

				auto m = (SocketMonitor *)poll_result.GetObject(i);
				m->Dispatch(events);
215 216 217
			}
		}

218
		poll_result.Reset();
219

220
	} while (!quit);
221

222
#ifndef NDEBUG
223
	assert(busy);
224
	assert(thread.IsInside());
225 226
	thread = ThreadId::Null();
#endif
227 228
}

229 230 231 232 233 234 235 236 237 238 239 240
void
EventLoop::AddDeferred(DeferredMonitor &d)
{
	mutex.lock();
	if (d.pending) {
		mutex.unlock();
		return;
	}

	assert(std::find(deferred.begin(),
			 deferred.end(), &d) == deferred.end());

241 242
	/* we don't need to wake up the EventLoop if another
	   DeferredMonitor has already done it */
243
	const bool must_wake = !busy && deferred.empty();
244

245 246
	d.pending = true;
	deferred.push_back(&d);
247
	again = true;
248 249
	mutex.unlock();

250 251
	if (must_wake)
		wake_fd.Write();
252 253 254 255 256
}

void
EventLoop::RemoveDeferred(DeferredMonitor &d)
{
257
	const std::lock_guard<Mutex> protect(mutex);
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

	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);
}

273 274
void
EventLoop::HandleDeferred()
275
{
276 277 278 279 280 281 282 283 284 285 286
	while (!deferred.empty() && !quit) {
		DeferredMonitor &m = *deferred.front();
		assert(m.pending);

		deferred.pop_front();
		m.pending = false;

		mutex.unlock();
		m.RunDeferred();
		mutex.lock();
	}
287
}
288

289 290 291
bool
EventLoop::OnSocketReady(gcc_unused unsigned flags)
{
292 293
	assert(IsInside());

294 295 296 297
	wake_fd.Read();

	mutex.lock();
	HandleDeferred();
298 299 300 301
	mutex.unlock();

	return true;
}