| 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 | #if defined ZMQ_IOTHREAD_POLLER_USE_EPOLL |
| 32 | #include "epoll.hpp" |
| 33 | |
| 34 | #if !defined ZMQ_HAVE_WINDOWS |
| 35 | #include <unistd.h> |
| 36 | #endif |
| 37 | |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <signal.h> |
| 41 | #include <algorithm> |
| 42 | #include <new> |
| 43 | |
| 44 | #include "macros.hpp" |
| 45 | #include "err.hpp" |
| 46 | #include "config.hpp" |
| 47 | #include "i_poll_events.hpp" |
| 48 | |
| 49 | #ifdef ZMQ_HAVE_WINDOWS |
| 50 | const zmq::epoll_t::epoll_fd_t zmq::epoll_t::epoll_retired_fd = |
| 51 | INVALID_HANDLE_VALUE; |
| 52 | #endif |
| 53 | |
| 54 | zmq::epoll_t::epoll_t (const zmq::thread_ctx_t &ctx_) : |
| 55 | worker_poller_base_t (ctx_) |
| 56 | { |
| 57 | #ifdef ZMQ_IOTHREAD_POLLER_USE_EPOLL_CLOEXEC |
| 58 | // Setting this option result in sane behaviour when exec() functions |
| 59 | // are used. Old sockets are closed and don't block TCP ports, avoid |
| 60 | // leaks, etc. |
| 61 | _epoll_fd = epoll_create1 (EPOLL_CLOEXEC); |
| 62 | #else |
| 63 | _epoll_fd = epoll_create (1); |
| 64 | #endif |
| 65 | errno_assert (_epoll_fd != epoll_retired_fd); |
| 66 | } |
| 67 | |
| 68 | zmq::epoll_t::~epoll_t () |
| 69 | { |
| 70 | // Wait till the worker thread exits. |
| 71 | stop_worker (); |
| 72 | |
| 73 | #ifdef ZMQ_HAVE_WINDOWS |
| 74 | epoll_close (_epoll_fd); |
| 75 | #else |
| 76 | close (_epoll_fd); |
| 77 | #endif |
| 78 | for (retired_t::iterator it = _retired.begin (), end = _retired.end (); |
| 79 | it != end; ++it) { |
| 80 | LIBZMQ_DELETE (*it); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | zmq::epoll_t::handle_t zmq::epoll_t::add_fd (fd_t fd_, i_poll_events *events_) |
| 85 | { |
| 86 | check_thread (); |
| 87 | poll_entry_t *pe = new (std::nothrow) poll_entry_t; |
| 88 | alloc_assert (pe); |
| 89 | |
| 90 | // The memset is not actually needed. It's here to prevent debugging |
| 91 | // tools to complain about using uninitialised memory. |
| 92 | memset (pe, 0, sizeof (poll_entry_t)); |
| 93 | |
| 94 | pe->fd = fd_; |
| 95 | pe->ev.events = 0; |
| 96 | pe->ev.data.ptr = pe; |
| 97 | pe->events = events_; |
| 98 | |
| 99 | int rc = epoll_ctl (_epoll_fd, EPOLL_CTL_ADD, fd_, &pe->ev); |
| 100 | errno_assert (rc != -1); |
| 101 | |
| 102 | // Increase the load metric of the thread. |
| 103 | adjust_load (1); |
| 104 | |
| 105 | return pe; |
| 106 | } |
| 107 | |
| 108 | void zmq::epoll_t::rm_fd (handle_t handle_) |
| 109 | { |
| 110 | check_thread (); |
| 111 | poll_entry_t *pe = static_cast<poll_entry_t *> (handle_); |
| 112 | int rc = epoll_ctl (_epoll_fd, EPOLL_CTL_DEL, pe->fd, &pe->ev); |
| 113 | errno_assert (rc != -1); |
| 114 | pe->fd = retired_fd; |
| 115 | _retired.push_back (pe); |
| 116 | |
| 117 | // Decrease the load metric of the thread. |
| 118 | adjust_load (-1); |
| 119 | } |
| 120 | |
| 121 | void zmq::epoll_t::set_pollin (handle_t handle_) |
| 122 | { |
| 123 | check_thread (); |
| 124 | poll_entry_t *pe = static_cast<poll_entry_t *> (handle_); |
| 125 | pe->ev.events |= EPOLLIN; |
| 126 | int rc = epoll_ctl (_epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev); |
| 127 | errno_assert (rc != -1); |
| 128 | } |
| 129 | |
| 130 | void zmq::epoll_t::reset_pollin (handle_t handle_) |
| 131 | { |
| 132 | check_thread (); |
| 133 | poll_entry_t *pe = static_cast<poll_entry_t *> (handle_); |
| 134 | pe->ev.events &= ~(static_cast<short> (EPOLLIN)); |
| 135 | int rc = epoll_ctl (_epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev); |
| 136 | errno_assert (rc != -1); |
| 137 | } |
| 138 | |
| 139 | void zmq::epoll_t::set_pollout (handle_t handle_) |
| 140 | { |
| 141 | check_thread (); |
| 142 | poll_entry_t *pe = static_cast<poll_entry_t *> (handle_); |
| 143 | pe->ev.events |= EPOLLOUT; |
| 144 | int rc = epoll_ctl (_epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev); |
| 145 | errno_assert (rc != -1); |
| 146 | } |
| 147 | |
| 148 | void zmq::epoll_t::reset_pollout (handle_t handle_) |
| 149 | { |
| 150 | check_thread (); |
| 151 | poll_entry_t *pe = static_cast<poll_entry_t *> (handle_); |
| 152 | pe->ev.events &= ~(static_cast<short> (EPOLLOUT)); |
| 153 | int rc = epoll_ctl (_epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev); |
| 154 | errno_assert (rc != -1); |
| 155 | } |
| 156 | |
| 157 | void zmq::epoll_t::stop () |
| 158 | { |
| 159 | check_thread (); |
| 160 | } |
| 161 | |
| 162 | int zmq::epoll_t::max_fds () |
| 163 | { |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | void zmq::epoll_t::loop () |
| 168 | { |
| 169 | epoll_event ev_buf[max_io_events]; |
| 170 | |
| 171 | while (true) { |
| 172 | // Execute any due timers. |
| 173 | int timeout = static_cast<int> (execute_timers ()); |
| 174 | |
| 175 | if (get_load () == 0) { |
| 176 | if (timeout == 0) |
| 177 | break; |
| 178 | |
| 179 | // TODO sleep for timeout |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | // Wait for events. |
| 184 | int n = epoll_wait (_epoll_fd, &ev_buf[0], max_io_events, |
| 185 | timeout ? timeout : -1); |
| 186 | if (n == -1) { |
| 187 | errno_assert (errno == EINTR); |
| 188 | continue; |
| 189 | } |
| 190 | |
| 191 | for (int i = 0; i < n; i++) { |
| 192 | poll_entry_t *pe = |
| 193 | (static_cast<poll_entry_t *> (ev_buf[i].data.ptr)); |
| 194 | |
| 195 | if (pe->fd == retired_fd) |
| 196 | continue; |
| 197 | if (ev_buf[i].events & (EPOLLERR | EPOLLHUP)) |
| 198 | pe->events->in_event (); |
| 199 | if (pe->fd == retired_fd) |
| 200 | continue; |
| 201 | if (ev_buf[i].events & EPOLLOUT) |
| 202 | pe->events->out_event (); |
| 203 | if (pe->fd == retired_fd) |
| 204 | continue; |
| 205 | if (ev_buf[i].events & EPOLLIN) |
| 206 | pe->events->in_event (); |
| 207 | } |
| 208 | |
| 209 | // Destroy retired event sources. |
| 210 | for (retired_t::iterator it = _retired.begin (), end = _retired.end (); |
| 211 | it != end; ++it) { |
| 212 | LIBZMQ_DELETE (*it); |
| 213 | } |
| 214 | _retired.clear (); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | #endif |
| 219 | |