scclib
Stable Cloud Computing C++ Library
poller.cc
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <util/poller.h>
32 #include <sys/epoll.h>
33 #include <system_error>
34 #include <sstream>
35 #include <util/safe_clib.h>
36 
42 using namespace scc::util;
43 
44 Poller::Poller()
45 {
46  m_fd = epoll_create1(0);
47 }
48 
49 Poller::~Poller()
50 {
51  safe_close(m_fd);
52 }
53 
54 void Poller::set(int fd, int flags)
55 {
56  epoll_event ev;
57  int eflags = 0;
58 
59  if (flags & Poller::input)
60  {
61  eflags |= EPOLLIN;
62  }
63  if (flags & Poller::output)
64  {
65  eflags |= EPOLLOUT;
66  }
67  if (flags & Poller::read_hup)
68  {
69  eflags |= EPOLLRDHUP;
70  }
71  if (flags & Poller::priority)
72  {
73  eflags |= EPOLLPRI;
74  }
75 
76  if (eflags == 0)
77  {
78  throw std::runtime_error("No poll events specified");
79  }
80 
81  ev.events = eflags;
82  ev.data.fd = fd;
83 
84  if (m_polls.find(fd) == m_polls.end())
85  {
86  if (epoll_ctl(m_fd, EPOLL_CTL_ADD, fd, &ev) == -1)
87  {
88  throw std::system_error(errno, std::system_category());
89  }
90  }
91  else
92  {
93  if (epoll_ctl(m_fd, EPOLL_CTL_MOD, fd, &ev) == -1)
94  {
95  throw std::system_error(errno, std::system_category());
96  }
97  }
98  m_polls[fd] = eflags;
99 }
100 
101 void Poller::remove(int fd)
102 {
103  if (m_polls.find(fd) != m_polls.end())
104  {
105  if (epoll_ctl(m_fd, EPOLL_CTL_DEL, fd, NULL) == -1)
106  {
107  throw std::system_error(errno, std::system_category());
108  }
109  m_polls.erase(fd);
110  }
111 }
112 
113 void Poller::wait(int timeout_ms)
114 {
115  epoll_event evs[m_polls.size()];
116  int n;
117  do
118  {
119  n = epoll_wait(m_fd, evs, m_polls.size(), timeout_ms);
120  }
121  while (n == -1 && errno == EINTR);
122  if (n == -1)
123  {
124  std::stringstream st;
125  st << "epoll_wait()";
126  throw std::system_error(errno, std::system_category(), st.str());
127  }
128  m_events.clear();
129  for (int i = 0; i < n; i++)
130  {
131  if (m_polls.find(evs[i].data.fd) == m_polls.end())
132  {
133  throw std::runtime_error("invalid poll event returned");
134  }
135  m_events[evs[i].data.fd] = converteflags(evs[i].events);
136  }
137 }
138 
139 int Poller::converteflags(int flags)
140 {
141  int r = 0;
142 
143  if (flags & EPOLLIN)
144  {
145  r |= Poller::input;
146  }
147  if (flags & EPOLLOUT)
148  {
149  r |= Poller::output;
150  }
151  if (flags & EPOLLRDHUP)
152  {
153  r |= Poller::read_hup;
154  }
155  if (flags & EPOLLPRI)
156  {
157  r |= Poller::priority;
158  }
159  if (flags & EPOLLHUP)
160  {
161  r |= Poller::hup;
162  }
163  if (flags & EPOLLERR)
164  {
165  r |= Poller::error;
166  }
167 
168  return r;
169 }
170 
171 int Poller::event(int fd)
172 {
173  auto e = m_events.find(fd);
174  if (e == m_events.end())
175  {
176  return 0;
177  }
178  return e->second;
179 }
180 
void set(int, int)
Add a file desriptor to poller.
Definition: poller.cc:54
void remove(int)
Remove a file descriptor from poller.
Definition: poller.cc:101
@ priority
There is some exceptional condition on the file descriptor. E.g. There is out-of-band data on a TCP s...
Definition: poller.h:80
@ error
Error condition.
Definition: poller.h:82
@ output
File descriptor is available for write operations.
Definition: poller.h:78
@ hup
Hang up. The local or peer connection has been closed.
Definition: poller.h:81
@ read_hup
For stream sockets, peer connection (or local connection) has been shut down.
Definition: poller.h:79
@ input
File descriptor is available for read operations.
Definition: poller.h:77
void wait()
Wait forever for an event.
Definition: poller.h:110
int event(int)
Return flags which were polled for this file descriptor.
Definition: poller.cc:171
int safe_close(int fd)
Signal safe close.
Definition: safe_clib.cc:95
Linux kernel i/o event notification (poller).
Signal-safe C library wrapper.