libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
poll.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 "poll.hpp"
32 #if defined ZMQ_USE_POLL
33 
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <poll.h>
37 #include <algorithm>
38 
39 #include "poll.hpp"
40 #include "err.hpp"
41 #include "config.hpp"
42 #include "i_poll_events.hpp"
43 
44 zmq::poll_t::poll_t (const zmq::ctx_t &ctx_) :
45  ctx(ctx_),
46  retired (false),
47  stopping (false)
48 {
49 }
50 
51 zmq::poll_t::~poll_t ()
52 {
53  worker.stop ();
54 }
55 
56 zmq::poll_t::handle_t zmq::poll_t::add_fd (fd_t fd_, i_poll_events *events_)
57 {
58  // If the file descriptor table is too small expand it.
59  fd_table_t::size_type sz = fd_table.size ();
60  if (sz <= (fd_table_t::size_type) fd_) {
61  fd_table.resize (fd_ + 1);
62  while (sz != (fd_table_t::size_type) (fd_ + 1)) {
63  fd_table [sz].index = retired_fd;
64  ++sz;
65  }
66  }
67 
68  pollfd pfd = {fd_, 0, 0};
69  pollset.push_back (pfd);
70  zmq_assert (fd_table [fd_].index == retired_fd);
71 
72  fd_table [fd_].index = pollset.size() - 1;
73  fd_table [fd_].events = events_;
74 
75  // Increase the load metric of the thread.
76  adjust_load (1);
77 
78  return fd_;
79 }
80 
81 void zmq::poll_t::rm_fd (handle_t handle_)
82 {
83  fd_t index = fd_table [handle_].index;
84  zmq_assert (index != retired_fd);
85 
86  // Mark the fd as unused.
87  pollset [index].fd = retired_fd;
88  fd_table [handle_].index = retired_fd;
89  retired = true;
90 
91  // Decrease the load metric of the thread.
92  adjust_load (-1);
93 }
94 
95 void zmq::poll_t::set_pollin (handle_t handle_)
96 {
97  int index = fd_table [handle_].index;
98  pollset [index].events |= POLLIN;
99 }
100 
101 void zmq::poll_t::reset_pollin (handle_t handle_)
102 {
103  int index = fd_table [handle_].index;
104  pollset [index].events &= ~((short) POLLIN);
105 }
106 
107 void zmq::poll_t::set_pollout (handle_t handle_)
108 {
109  int index = fd_table [handle_].index;
110  pollset [index].events |= POLLOUT;
111 }
112 
113 void zmq::poll_t::reset_pollout (handle_t handle_)
114 {
115  int index = fd_table [handle_].index;
116  pollset [index].events &= ~((short) POLLOUT);
117 }
118 
119 void zmq::poll_t::start ()
120 {
121  ctx.start_thread (worker, worker_routine, this);
122 }
123 
124 void zmq::poll_t::stop ()
125 {
126  stopping = true;
127 }
128 
129 int zmq::poll_t::max_fds ()
130 {
131  return -1;
132 }
133 
134 void zmq::poll_t::loop ()
135 {
136  while (!stopping) {
137 
138  // Execute any due timers.
139  int timeout = (int) execute_timers ();
140 
141  // Wait for events.
142  int rc = poll (&pollset [0], pollset.size (), timeout ? timeout : -1);
143  if (rc == -1) {
144  errno_assert (errno == EINTR);
145  continue;
146  }
147 
148  // If there are no events (i.e. it's a timeout) there's no point
149  // in checking the pollset.
150  if (rc == 0)
151  continue;
152 
153  for (pollset_t::size_type i = 0; i != pollset.size (); i++) {
154 
155  zmq_assert (!(pollset [i].revents & POLLNVAL));
156  if (pollset [i].fd == retired_fd)
157  continue;
158  if (pollset [i].revents & (POLLERR | POLLHUP))
159  fd_table [pollset [i].fd].events->in_event ();
160  if (pollset [i].fd == retired_fd)
161  continue;
162  if (pollset [i].revents & POLLOUT)
163  fd_table [pollset [i].fd].events->out_event ();
164  if (pollset [i].fd == retired_fd)
165  continue;
166  if (pollset [i].revents & POLLIN)
167  fd_table [pollset [i].fd].events->in_event ();
168  }
169 
170  // Clean up the pollset and update the fd_table accordingly.
171  if (retired) {
172  pollset_t::size_type i = 0;
173  while (i < pollset.size ()) {
174  if (pollset [i].fd == retired_fd)
175  pollset.erase (pollset.begin () + i);
176  else {
177  fd_table [pollset [i].fd].index = i;
178  i ++;
179  }
180  }
181  retired = false;
182  }
183  }
184 }
185 
186 void zmq::poll_t::worker_routine (void *arg_)
187 {
188  ((poll_t*) arg_)->loop ();
189 }
190 
191 #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