libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
epoll.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "precompiled.hpp"
31 #include "epoll.hpp"
32 #if defined ZMQ_USE_EPOLL
33 
34 #include <sys/epoll.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <algorithm>
39 #include <new>
40 
41 #include "macros.hpp"
42 #include "epoll.hpp"
43 #include "err.hpp"
44 #include "config.hpp"
45 #include "i_poll_events.hpp"
46 
47 zmq::epoll_t::epoll_t (const zmq::ctx_t &ctx_) :
48  ctx(ctx_),
49  stopping (false)
50 {
51  epoll_fd = epoll_create (1);
52  errno_assert (epoll_fd != -1);
53 }
54 
55 zmq::epoll_t::~epoll_t ()
56 {
57  // Wait till the worker thread exits.
58  worker.stop ();
59 
60  close (epoll_fd);
61  for (retired_t::iterator it = retired.begin (); it != retired.end (); ++it) {
62  LIBZMQ_DELETE(*it);
63  }
64 }
65 
66 zmq::epoll_t::handle_t zmq::epoll_t::add_fd (fd_t fd_, i_poll_events *events_)
67 {
68  poll_entry_t *pe = new (std::nothrow) poll_entry_t;
69  alloc_assert (pe);
70 
71  // The memset is not actually needed. It's here to prevent debugging
72  // tools to complain about using uninitialised memory.
73  memset (pe, 0, sizeof (poll_entry_t));
74 
75  pe->fd = fd_;
76  pe->ev.events = 0;
77  pe->ev.data.ptr = pe;
78  pe->events = events_;
79 
80  int rc = epoll_ctl (epoll_fd, EPOLL_CTL_ADD, fd_, &pe->ev);
81  errno_assert (rc != -1);
82 
83  // Increase the load metric of the thread.
84  adjust_load (1);
85 
86  return pe;
87 }
88 
89 void zmq::epoll_t::rm_fd (handle_t handle_)
90 {
91  poll_entry_t *pe = (poll_entry_t*) handle_;
92  int rc = epoll_ctl (epoll_fd, EPOLL_CTL_DEL, pe->fd, &pe->ev);
93  errno_assert (rc != -1);
94  pe->fd = retired_fd;
95  retired.push_back (pe);
96 
97  // Decrease the load metric of the thread.
98  adjust_load (-1);
99 }
100 
101 void zmq::epoll_t::set_pollin (handle_t handle_)
102 {
103  poll_entry_t *pe = (poll_entry_t*) handle_;
104  pe->ev.events |= EPOLLIN;
105  int rc = epoll_ctl (epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev);
106  errno_assert (rc != -1);
107 }
108 
109 void zmq::epoll_t::reset_pollin (handle_t handle_)
110 {
111  poll_entry_t *pe = (poll_entry_t*) handle_;
112  pe->ev.events &= ~((short) EPOLLIN);
113  int rc = epoll_ctl (epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev);
114  errno_assert (rc != -1);
115 }
116 
117 void zmq::epoll_t::set_pollout (handle_t handle_)
118 {
119  poll_entry_t *pe = (poll_entry_t*) handle_;
120  pe->ev.events |= EPOLLOUT;
121  int rc = epoll_ctl (epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev);
122  errno_assert (rc != -1);
123 }
124 
125 void zmq::epoll_t::reset_pollout (handle_t handle_)
126 {
127  poll_entry_t *pe = (poll_entry_t*) handle_;
128  pe->ev.events &= ~((short) EPOLLOUT);
129  int rc = epoll_ctl (epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev);
130  errno_assert (rc != -1);
131 }
132 
133 void zmq::epoll_t::start ()
134 {
135  ctx.start_thread (worker, worker_routine, this);
136 }
137 
138 void zmq::epoll_t::stop ()
139 {
140  stopping = true;
141 }
142 
143 int zmq::epoll_t::max_fds ()
144 {
145  return -1;
146 }
147 
148 void zmq::epoll_t::loop ()
149 {
150  epoll_event ev_buf [max_io_events];
151 
152  while (!stopping) {
153 
154  // Execute any due timers.
155  int timeout = (int) execute_timers ();
156 
157  // Wait for events.
158  int n = epoll_wait (epoll_fd, &ev_buf [0], max_io_events,
159  timeout ? timeout : -1);
160  if (n == -1) {
161  errno_assert (errno == EINTR);
162  continue;
163  }
164 
165  for (int i = 0; i < n; i ++) {
166  poll_entry_t *pe = ((poll_entry_t*) ev_buf [i].data.ptr);
167 
168  if (pe->fd == retired_fd)
169  continue;
170  if (ev_buf [i].events & (EPOLLERR | EPOLLHUP))
171  pe->events->in_event ();
172  if (pe->fd == retired_fd)
173  continue;
174  if (ev_buf [i].events & EPOLLOUT)
175  pe->events->out_event ();
176  if (pe->fd == retired_fd)
177  continue;
178  if (ev_buf [i].events & EPOLLIN)
179  pe->events->in_event ();
180  }
181 
182  // Destroy retired event sources.
183  for (retired_t::iterator it = retired.begin (); it != retired.end (); ++it) {
184  LIBZMQ_DELETE(*it);
185  }
186  retired.clear ();
187  }
188 }
189 
190 void zmq::epoll_t::worker_routine (void *arg_)
191 {
192  ((epoll_t*) arg_)->loop ();
193 }
194 
195 #endif
#define LIBZMQ_DELETE(p_object)
Definition: macros.hpp:7
int fd_t
Definition: fd.hpp:50
static void worker(void *s)
#define alloc_assert(x)
Definition: err.hpp:159
#define errno_assert(x)
Definition: err.hpp:129
Definition: command.hpp:80