MPD: event/PollGroupEPoll.hxx Source File

MPD 0.20.15

PollGroupEPoll.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_EVENT_POLLGROUP_EPOLL_HXX
21 #define MPD_EVENT_POLLGROUP_EPOLL_HXX
22
23 #include "check.h"
24
25 #include "Compiler.h"
26 #include "system/EPollFD.hxx"
27
28 #include <array>
29 #include <algorithm>
30
31 class PollResultEPoll
32 {
33 friend class PollGroupEPoll;
34
35 std::array<epoll_event, 16> events;
36 int n_events;
37 public:
38 PollResultEPoll() : n_events(0) { }
39
40 int GetSize() const { return n_events; }
41 unsigned GetEvents(int i) const { return events[i].events; }
42 void *GetObject(int i) const { return events[i].data.ptr; }
43 void Reset() { n_events = 0; }
44
45 void Clear(void *obj) {
46 for (int i = 0; i < n_events; ++i)
47 if (events[i].data.ptr == obj)
48 events[i].events = 0;
49 }
50 };
51
52 class PollGroupEPoll
53 {
54 EPollFD epoll;
55
56 PollGroupEPoll(PollGroupEPoll &) = delete;
57 PollGroupEPoll &operator=(PollGroupEPoll &) = delete;
58 public:
59 static constexpr unsigned READ = EPOLLIN;
60 static constexpr unsigned WRITE = EPOLLOUT;
61 static constexpr unsigned ERROR = EPOLLERR;
62 static constexpr unsigned HANGUP = EPOLLHUP;
63
64 PollGroupEPoll() = default;
65
66 void ReadEvents(PollResultEPoll &result, int timeout_ms) {
67 int ret = epoll.Wait(result.events.data(), result.events.size(),
68 timeout_ms);
69 result.n_events = std::max(0, ret);
70 }
71
72 bool Add(int fd, unsigned events, void *obj) {
73 return epoll.Add(fd, events, obj);
74 }
75
76 bool Modify(int fd, unsigned events, void *obj) {
77 return epoll.Modify(fd, events, obj);
78 }
79
80 bool Remove(int fd) {
81 return epoll.Remove(fd);
82 }
83
84 bool Abandon(gcc_unused int fd) {
85 // Nothing to do in this implementation.
86 // Closed descriptors are automatically unregistered.
87 return true;
88 }
89 };
90
91 #endif
check.h
PollGroupEPoll::WRITE
static constexpr unsigned WRITE
Definition: PollGroupEPoll.hxx:60
EPollFD::Wait
int Wait(epoll_event *events, int maxevents, int timeout)
Definition: EPollFD.hxx:52
EPollFD.hxx
PollResultEPoll::PollResultEPoll
PollResultEPoll()
Definition: PollGroupEPoll.hxx:38
PollResultEPoll::Reset
void Reset()
Definition: PollGroupEPoll.hxx:43
PollGroupEPoll::ERROR
static constexpr unsigned ERROR
Definition: PollGroupEPoll.hxx:61
EPollFD
A class that wraps Linux epoll.
Definition: EPollFD.hxx:37
PollGroupEPoll::HANGUP
static constexpr unsigned HANGUP
Definition: PollGroupEPoll.hxx:62
EPollFD::Add
bool Add(int _fd, uint32_t events, void *ptr)
Definition: EPollFD.hxx:60
PollResultEPoll::GetObject
void * GetObject(int i) const
Definition: PollGroupEPoll.hxx:42
PollResultEPoll::Clear
void Clear(void *obj)
Definition: PollGroupEPoll.hxx:45
PollGroupEPoll
Definition: PollGroupEPoll.hxx:52
PollGroupEPoll::Modify
bool Modify(int fd, unsigned events, void *obj)
Definition: PollGroupEPoll.hxx:76
Compiler.h
PollGroupEPoll::READ
static constexpr unsigned READ
Definition: PollGroupEPoll.hxx:59
PollGroupEPoll::ReadEvents
void ReadEvents(PollResultEPoll &result, int timeout_ms)
Definition: PollGroupEPoll.hxx:66
PollGroupEPoll::Remove
bool Remove(int fd)
Definition: PollGroupEPoll.hxx:80
PollGroupEPoll::Add
bool Add(int fd, unsigned events, void *obj)
Definition: PollGroupEPoll.hxx:72
EPollFD::Modify
bool Modify(int _fd, uint32_t events, void *ptr)
Definition: EPollFD.hxx:68
PollResultEPoll
Definition: PollGroupEPoll.hxx:31
PollResultEPoll::GetSize
int GetSize() const
Definition: PollGroupEPoll.hxx:40
gcc_unused
#define gcc_unused
Definition: Compiler.h:118
PollGroupEPoll::PollGroupEPoll
PollGroupEPoll()=default
EPollFD::Remove
bool Remove(int _fd)
Definition: EPollFD.hxx:76
PollGroupEPoll::Abandon
bool Abandon(gcc_unused int fd)
Definition: PollGroupEPoll.hxx:84
PollResultEPoll::GetEvents
unsigned GetEvents(int i) const
Definition: PollGroupEPoll.hxx:41