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_IOTHREAD_POLLER_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::thread_ctx_t &ctx_) : |
45 | worker_poller_base_t (ctx_), |
46 | retired (false) |
47 | { |
48 | } |
49 | |
50 | zmq::poll_t::~poll_t () |
51 | { |
52 | stop_worker (); |
53 | } |
54 | |
55 | zmq::poll_t::handle_t zmq::poll_t::add_fd (fd_t fd_, i_poll_events *events_) |
56 | { |
57 | check_thread (); |
58 | zmq_assert (fd_ != retired_fd); |
59 | |
60 | // If the file descriptor table is too small expand it. |
61 | fd_table_t::size_type sz = fd_table.size (); |
62 | if (sz <= (fd_table_t::size_type) fd_) { |
63 | fd_table.resize (fd_ + 1); |
64 | while (sz != (fd_table_t::size_type) (fd_ + 1)) { |
65 | fd_table[sz].index = retired_fd; |
66 | ++sz; |
67 | } |
68 | } |
69 | |
70 | pollfd pfd = {fd_, 0, 0}; |
71 | pollset.push_back (pfd); |
72 | zmq_assert (fd_table[fd_].index == retired_fd); |
73 | |
74 | fd_table[fd_].index = pollset.size () - 1; |
75 | fd_table[fd_].events = events_; |
76 | |
77 | // Increase the load metric of the thread. |
78 | adjust_load (1); |
79 | |
80 | return fd_; |
81 | } |
82 | |
83 | void zmq::poll_t::rm_fd (handle_t handle_) |
84 | { |
85 | check_thread (); |
86 | fd_t index = fd_table[handle_].index; |
87 | zmq_assert (index != retired_fd); |
88 | |
89 | // Mark the fd as unused. |
90 | pollset[index].fd = retired_fd; |
91 | fd_table[handle_].index = retired_fd; |
92 | retired = true; |
93 | |
94 | // Decrease the load metric of the thread. |
95 | adjust_load (-1); |
96 | } |
97 | |
98 | void zmq::poll_t::set_pollin (handle_t handle_) |
99 | { |
100 | check_thread (); |
101 | fd_t index = fd_table[handle_].index; |
102 | pollset[index].events |= POLLIN; |
103 | } |
104 | |
105 | void zmq::poll_t::reset_pollin (handle_t handle_) |
106 | { |
107 | check_thread (); |
108 | fd_t index = fd_table[handle_].index; |
109 | pollset[index].events &= ~((short) POLLIN); |
110 | } |
111 | |
112 | void zmq::poll_t::set_pollout (handle_t handle_) |
113 | { |
114 | check_thread (); |
115 | fd_t index = fd_table[handle_].index; |
116 | pollset[index].events |= POLLOUT; |
117 | } |
118 | |
119 | void zmq::poll_t::reset_pollout (handle_t handle_) |
120 | { |
121 | check_thread (); |
122 | fd_t index = fd_table[handle_].index; |
123 | pollset[index].events &= ~((short) POLLOUT); |
124 | } |
125 | |
126 | void zmq::poll_t::stop () |
127 | { |
128 | check_thread (); |
129 | // no-op... thread is stopped when no more fds or timers are registered |
130 | } |
131 | |
132 | int zmq::poll_t::max_fds () |
133 | { |
134 | return -1; |
135 | } |
136 | |
137 | void zmq::poll_t::loop () |
138 | { |
139 | while (true) { |
140 | // Execute any due timers. |
141 | int timeout = (int) execute_timers (); |
142 | |
143 | cleanup_retired (); |
144 | |
145 | if (pollset.empty ()) { |
146 | zmq_assert (get_load () == 0); |
147 | |
148 | if (timeout == 0) |
149 | break; |
150 | |
151 | // TODO sleep for timeout |
152 | continue; |
153 | } |
154 | |
155 | // Wait for events. |
156 | int rc = poll (&pollset[0], static_cast<nfds_t> (pollset.size ()), |
157 | timeout ? timeout : -1); |
158 | if (rc == -1) { |
159 | errno_assert (errno == EINTR); |
160 | continue; |
161 | } |
162 | |
163 | // If there are no events (i.e. it's a timeout) there's no point |
164 | // in checking the pollset. |
165 | if (rc == 0) |
166 | continue; |
167 | |
168 | for (pollset_t::size_type i = 0; i != pollset.size (); i++) { |
169 | zmq_assert (!(pollset[i].revents & POLLNVAL)); |
170 | if (pollset[i].fd == retired_fd) |
171 | continue; |
172 | if (pollset[i].revents & (POLLERR | POLLHUP)) |
173 | fd_table[pollset[i].fd].events->in_event (); |
174 | if (pollset[i].fd == retired_fd) |
175 | continue; |
176 | if (pollset[i].revents & POLLOUT) |
177 | fd_table[pollset[i].fd].events->out_event (); |
178 | if (pollset[i].fd == retired_fd) |
179 | continue; |
180 | if (pollset[i].revents & POLLIN) |
181 | fd_table[pollset[i].fd].events->in_event (); |
182 | } |
183 | } |
184 | } |
185 | |
186 | void zmq::poll_t::cleanup_retired () |
187 | { |
188 | // Clean up the pollset and update the fd_table accordingly. |
189 | if (retired) { |
190 | pollset_t::size_type i = 0; |
191 | while (i < pollset.size ()) { |
192 | if (pollset[i].fd == retired_fd) |
193 | pollset.erase (pollset.begin () + i); |
194 | else { |
195 | fd_table[pollset[i].fd].index = i; |
196 | i++; |
197 | } |
198 | } |
199 | retired = false; |
200 | } |
201 | } |
202 | |
203 | |
204 | #endif |
205 | |