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 
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 
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
static constexpr unsigned WRITE
int Wait(epoll_event *events, int maxevents, int timeout)
Definition: EPollFD.hxx:52
static constexpr unsigned ERROR
A class that wraps Linux epoll.
Definition: EPollFD.hxx:37
static constexpr unsigned HANGUP
bool Add(int _fd, uint32_t events, void *ptr)
Definition: EPollFD.hxx:60
void * GetObject(int i) const
void Clear(void *obj)
bool Modify(int fd, unsigned events, void *obj)
static constexpr unsigned READ
void ReadEvents(PollResultEPoll &result, int timeout_ms)
bool Remove(int fd)
bool Add(int fd, unsigned events, void *obj)
bool Modify(int _fd, uint32_t events, void *ptr)
Definition: EPollFD.hxx:68
int GetSize() const
#define gcc_unused
Definition: Compiler.h:118
PollGroupEPoll()=default
bool Remove(int _fd)
Definition: EPollFD.hxx:76
bool Abandon(gcc_unused int fd)
unsigned GetEvents(int i) const