libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
devpoll.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 "devpoll.hpp"
32 #if defined ZMQ_USE_DEVPOLL
33 
34 #include <sys/devpoll.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/ioctl.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <limits.h>
42 #include <algorithm>
43 
44 #include "devpoll.hpp"
45 #include "err.hpp"
46 #include "config.hpp"
47 #include "i_poll_events.hpp"
48 
49 zmq::devpoll_t::devpoll_t (const zmq::ctx_t &ctx_) :
50  ctx(ctx_),
51  stopping (false)
52 {
53  devpoll_fd = open ("/dev/poll", O_RDWR);
54  errno_assert (devpoll_fd != -1);
55 }
56 
57 zmq::devpoll_t::~devpoll_t ()
58 {
59  worker.stop ();
60  close (devpoll_fd);
61 }
62 
63 void zmq::devpoll_t::devpoll_ctl (fd_t fd_, short events_)
64 {
65  struct pollfd pfd = {fd_, events_, 0};
66  ssize_t rc = write (devpoll_fd, &pfd, sizeof pfd);
67  zmq_assert (rc == sizeof pfd);
68 }
69 
70 zmq::devpoll_t::handle_t zmq::devpoll_t::add_fd (fd_t fd_,
71  i_poll_events *reactor_)
72 {
73  // If the file descriptor table is too small expand it.
74  fd_table_t::size_type sz = fd_table.size ();
75  if (sz <= (fd_table_t::size_type) fd_) {
76  fd_table.resize (fd_ + 1);
77  while (sz != (fd_table_t::size_type) (fd_ + 1)) {
78  fd_table [sz].valid = false;
79  ++sz;
80  }
81  }
82 
83  zmq_assert (!fd_table [fd_].valid);
84 
85  fd_table [fd_].events = 0;
86  fd_table [fd_].reactor = reactor_;
87  fd_table [fd_].valid = true;
88  fd_table [fd_].accepted = false;
89 
90  devpoll_ctl (fd_, 0);
91  pending_list.push_back (fd_);
92 
93  // Increase the load metric of the thread.
94  adjust_load (1);
95 
96  return fd_;
97 }
98 
99 void zmq::devpoll_t::rm_fd (handle_t handle_)
100 {
101  zmq_assert (fd_table [handle_].valid);
102 
103  devpoll_ctl (handle_, POLLREMOVE);
104  fd_table [handle_].valid = false;
105 
106  // Decrease the load metric of the thread.
107  adjust_load (-1);
108 }
109 
110 void zmq::devpoll_t::set_pollin (handle_t handle_)
111 {
112  devpoll_ctl (handle_, POLLREMOVE);
113  fd_table [handle_].events |= POLLIN;
114  devpoll_ctl (handle_, fd_table [handle_].events);
115 }
116 
117 void zmq::devpoll_t::reset_pollin (handle_t handle_)
118 {
119  devpoll_ctl (handle_, POLLREMOVE);
120  fd_table [handle_].events &= ~((short) POLLIN);
121  devpoll_ctl (handle_, fd_table [handle_].events);
122 }
123 
124 void zmq::devpoll_t::set_pollout (handle_t handle_)
125 {
126  devpoll_ctl (handle_, POLLREMOVE);
127  fd_table [handle_].events |= POLLOUT;
128  devpoll_ctl (handle_, fd_table [handle_].events);
129 }
130 
131 void zmq::devpoll_t::reset_pollout (handle_t handle_)
132 {
133  devpoll_ctl (handle_, POLLREMOVE);
134  fd_table [handle_].events &= ~((short) POLLOUT);
135  devpoll_ctl (handle_, fd_table [handle_].events);
136 }
137 
138 void zmq::devpoll_t::start ()
139 {
140  ctx.start_thread (worker, worker_routine, this);
141 }
142 
143 void zmq::devpoll_t::stop ()
144 {
145  stopping = true;
146 }
147 
148 int zmq::devpoll_t::max_fds ()
149 {
150  return -1;
151 }
152 
153 void zmq::devpoll_t::loop ()
154 {
155  while (!stopping) {
156 
157  struct pollfd ev_buf [max_io_events];
158  struct dvpoll poll_req;
159 
160  for (pending_list_t::size_type i = 0; i < pending_list.size (); i ++)
161  fd_table [pending_list [i]].accepted = true;
162  pending_list.clear ();
163 
164  // Execute any due timers.
165  int timeout = (int) execute_timers ();
166 
167  // Wait for events.
168  // On Solaris, we can retrieve no more then (OPEN_MAX - 1) events.
169  poll_req.dp_fds = &ev_buf [0];
170 #if defined ZMQ_HAVE_SOLARIS
171  poll_req.dp_nfds = std::min ((int) max_io_events, OPEN_MAX - 1);
172 #else
173  poll_req.dp_nfds = max_io_events;
174 #endif
175  poll_req.dp_timeout = timeout ? timeout : -1;
176  int n = ioctl (devpoll_fd, DP_POLL, &poll_req);
177  if (n == -1 && errno == EINTR)
178  continue;
179  errno_assert (n != -1);
180 
181  for (int i = 0; i < n; i ++) {
182 
183  fd_entry_t *fd_ptr = &fd_table [ev_buf [i].fd];
184  if (!fd_ptr->valid || !fd_ptr->accepted)
185  continue;
186  if (ev_buf [i].revents & (POLLERR | POLLHUP))
187  fd_ptr->reactor->in_event ();
188  if (!fd_ptr->valid || !fd_ptr->accepted)
189  continue;
190  if (ev_buf [i].revents & POLLOUT)
191  fd_ptr->reactor->out_event ();
192  if (!fd_ptr->valid || !fd_ptr->accepted)
193  continue;
194  if (ev_buf [i].revents & POLLIN)
195  fd_ptr->reactor->in_event ();
196  }
197  }
198 }
199 
200 void zmq::devpoll_t::worker_routine (void *arg_)
201 {
202  ((devpoll_t*) arg_)->loop ();
203 }
204 
205 #endif
int fd_t
Definition: fd.hpp:50
#define zmq_assert(x)
Definition: err.hpp:119
static void worker(void *s)
#define errno_assert(x)
Definition: err.hpp:129
Definition: command.hpp:80