MultiSocketMonitor.cxx 3.21 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 "MultiSocketMonitor.hxx"
21
#include "Loop.hxx"
22

23 24
#include <algorithm>

25 26 27 28
#ifdef USE_EPOLL
#include <errno.h>
#endif

29
#ifndef _WIN32
30 31 32
#include <poll.h>
#endif

33
MultiSocketMonitor::MultiSocketMonitor(EventLoop &_loop) noexcept
34 35
	:IdleMonitor(_loop),
	 timeout_event(_loop, BIND_THIS_METHOD(OnTimeout)) {
36 37
}

38
void
39
MultiSocketMonitor::Reset() noexcept
40
{
41
	assert(GetEventLoop().IsInside());
42 43

	fds.clear();
44 45 46
#ifdef USE_EPOLL
	always_ready_fds.clear();
#endif
47
	IdleMonitor::Cancel();
48
	timeout_event.Cancel();
49
	ready = refresh = false;
50 51
}

52 53 54 55 56 57 58
bool
MultiSocketMonitor::AddSocket(SocketDescriptor fd, unsigned events) noexcept
{
	fds.emplace_front(*this, fd);
	bool success = fds.front().Schedule(events);
	if (!success) {
		fds.pop_front();
59 60 61 62 63 64 65

#ifdef USE_EPOLL
		if (errno == EPERM)
			/* not supported by epoll (e.g. "/dev/null"):
			   add it to the "always ready" list */
			always_ready_fds.push_front({fd, events});
#endif
66 67 68 69 70
	}

	return success;
}

71
void
72
MultiSocketMonitor::ClearSocketList() noexcept
73
{
74
	assert(GetEventLoop().IsInside());
75 76

	fds.clear();
77 78 79
#ifdef USE_EPOLL
	always_ready_fds.clear();
#endif
80 81
}

82
#ifndef _WIN32
83 84

void
85
MultiSocketMonitor::ReplaceSocketList(pollfd *pfds, unsigned n) noexcept
86
{
87 88 89 90
#ifdef USE_EPOLL
	always_ready_fds.clear();
#endif

91 92
	pollfd *const end = pfds + n;

93
	UpdateSocketList([pfds, end](SocketDescriptor fd) -> unsigned {
94
			auto i = std::find_if(pfds, end, [fd](const struct pollfd &pfd){
95
					return pfd.fd == fd.Get();
96 97 98 99
				});
			if (i == end)
				return 0;

100
			return std::exchange(i->events, 0);
101 102 103 104
		});

	for (auto i = pfds; i != end; ++i)
		if (i->events != 0)
105
			AddSocket(SocketDescriptor(i->fd), i->events);
106 107 108 109
}

#endif

110
void
111
MultiSocketMonitor::Prepare() noexcept
112
{
113 114 115 116 117 118 119 120 121 122 123 124 125 126
	auto timeout = PrepareSockets();

#ifdef USE_EPOLL
	if (!always_ready_fds.empty()) {
		/* if there was at least one file descriptor not
		   supported by epoll, install a very short timeout
		   because we assume it's always ready */
		constexpr std::chrono::steady_clock::duration ready_timeout =
			std::chrono::milliseconds(1);
		if (timeout < timeout.zero() || timeout > ready_timeout)
			timeout = ready_timeout;
	}
#endif

127
	if (timeout >= timeout.zero())
128
		timeout_event.Schedule(timeout);
129
	else
130
		timeout_event.Cancel();
131 132 133 134

}

void
135
MultiSocketMonitor::OnIdle() noexcept
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
{
	if (ready) {
		ready = false;
		DispatchSockets();

		/* TODO: don't refresh always; require users to call
		   InvalidateSockets() */
		refresh = true;
	}

	if (refresh) {
		refresh = false;
		Prepare();
	}
}