PollGroupEpoll.hxx 2.44 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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.
 */

#ifndef MPD_EVENT_POLLGROUP_EPOLL_HXX
#define MPD_EVENT_POLLGROUP_EPOLL_HXX

23
#include "util/Compiler.h"
24
#include "system/EpollFD.hxx"
25 26 27 28

#include <array>
#include <algorithm>

29
class PollResultEpoll
30
{
31
	friend class PollGroupEpoll;
32 33

	std::array<epoll_event, 16> events;
34
	size_t n_events = 0;
35

36
public:
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	size_t GetSize() const noexcept {
		return n_events;
	}

	unsigned GetEvents(size_t i) const noexcept {
		return events[i].events;
	}

	void *GetObject(size_t i) const noexcept {
		return events[i].data.ptr;
	}

	void Reset() noexcept {
		n_events = 0;
	}
52

53
	void Clear(void *obj) noexcept {
54
		for (size_t i = 0; i < n_events; ++i)
55 56 57 58 59
			if (events[i].data.ptr == obj)
				events[i].events = 0;
	}
};

60
class PollGroupEpoll
61
{
62
	EpollFD epoll;
63

64 65
	PollGroupEpoll(PollGroupEpoll &) = delete;
	PollGroupEpoll &operator=(PollGroupEpoll &) = delete;
66 67 68 69 70 71
public:
	static constexpr unsigned READ = EPOLLIN;
	static constexpr unsigned WRITE = EPOLLOUT;
	static constexpr unsigned ERROR = EPOLLERR;
	static constexpr unsigned HANGUP = EPOLLHUP;

72
	PollGroupEpoll() = default;
73

74
	void ReadEvents(PollResultEpoll &result, int timeout_ms) noexcept {
75 76 77 78 79
		int ret = epoll.Wait(result.events.data(), result.events.size(),
				     timeout_ms);
		result.n_events = std::max(0, ret);
	}

80
	bool Add(int fd, unsigned events, void *obj) noexcept {
81 82 83
		return epoll.Add(fd, events, obj);
	}

84
	bool Modify(int fd, unsigned events, void *obj) noexcept {
85 86 87
		return epoll.Modify(fd, events, obj);
	}

88
	bool Remove(int fd) noexcept {
89 90 91
		return epoll.Remove(fd);
	}

92
	bool Abandon(gcc_unused int fd) noexcept {
93 94 95 96 97 98 99
		// Nothing to do in this implementation.
		// Closed descriptors are automatically unregistered.
		return true;
	}
};

#endif