| 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 "kqueue.hpp" |
| 32 | #if defined ZMQ_IOTHREAD_POLLER_USE_KQUEUE |
| 33 | |
| 34 | #include <sys/time.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/event.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <unistd.h> |
| 39 | #include <algorithm> |
| 40 | #include <new> |
| 41 | |
| 42 | #include "macros.hpp" |
| 43 | #include "kqueue.hpp" |
| 44 | #include "err.hpp" |
| 45 | #include "config.hpp" |
| 46 | #include "i_poll_events.hpp" |
| 47 | #include "likely.hpp" |
| 48 | |
| 49 | // NetBSD defines (struct kevent).udata as intptr_t, everyone else |
| 50 | // as void *. |
| 51 | #if defined ZMQ_HAVE_NETBSD |
| 52 | #define kevent_udata_t intptr_t |
| 53 | #else |
| 54 | #define kevent_udata_t void * |
| 55 | #endif |
| 56 | |
| 57 | zmq::kqueue_t::kqueue_t (const zmq::thread_ctx_t &ctx_) : |
| 58 | worker_poller_base_t (ctx_) |
| 59 | { |
| 60 | // Create event queue |
| 61 | kqueue_fd = kqueue (); |
| 62 | errno_assert (kqueue_fd != -1); |
| 63 | #ifdef HAVE_FORK |
| 64 | pid = getpid (); |
| 65 | #endif |
| 66 | } |
| 67 | |
| 68 | zmq::kqueue_t::~kqueue_t () |
| 69 | { |
| 70 | stop_worker (); |
| 71 | close (kqueue_fd); |
| 72 | } |
| 73 | |
| 74 | void zmq::kqueue_t::kevent_add (fd_t fd_, short filter_, void *udata_) |
| 75 | { |
| 76 | check_thread (); |
| 77 | struct kevent ev; |
| 78 | |
| 79 | EV_SET (&ev, fd_, filter_, EV_ADD, 0, 0, (kevent_udata_t) udata_); |
| 80 | int rc = kevent (kqueue_fd, &ev, 1, NULL, 0, NULL); |
| 81 | errno_assert (rc != -1); |
| 82 | } |
| 83 | |
| 84 | void zmq::kqueue_t::kevent_delete (fd_t fd_, short filter_) |
| 85 | { |
| 86 | struct kevent ev; |
| 87 | |
| 88 | EV_SET (&ev, fd_, filter_, EV_DELETE, 0, 0, 0); |
| 89 | int rc = kevent (kqueue_fd, &ev, 1, NULL, 0, NULL); |
| 90 | errno_assert (rc != -1); |
| 91 | } |
| 92 | |
| 93 | zmq::kqueue_t::handle_t zmq::kqueue_t::add_fd (fd_t fd_, |
| 94 | i_poll_events *reactor_) |
| 95 | { |
| 96 | check_thread (); |
| 97 | poll_entry_t *pe = new (std::nothrow) poll_entry_t; |
| 98 | alloc_assert (pe); |
| 99 | |
| 100 | pe->fd = fd_; |
| 101 | pe->flag_pollin = 0; |
| 102 | pe->flag_pollout = 0; |
| 103 | pe->reactor = reactor_; |
| 104 | |
| 105 | adjust_load (1); |
| 106 | |
| 107 | return pe; |
| 108 | } |
| 109 | |
| 110 | void zmq::kqueue_t::rm_fd (handle_t handle_) |
| 111 | { |
| 112 | check_thread (); |
| 113 | poll_entry_t *pe = (poll_entry_t *) handle_; |
| 114 | if (pe->flag_pollin) |
| 115 | kevent_delete (pe->fd, EVFILT_READ); |
| 116 | if (pe->flag_pollout) |
| 117 | kevent_delete (pe->fd, EVFILT_WRITE); |
| 118 | pe->fd = retired_fd; |
| 119 | retired.push_back (pe); |
| 120 | |
| 121 | adjust_load (-1); |
| 122 | } |
| 123 | |
| 124 | void zmq::kqueue_t::set_pollin (handle_t handle_) |
| 125 | { |
| 126 | check_thread (); |
| 127 | poll_entry_t *pe = (poll_entry_t *) handle_; |
| 128 | if (likely (!pe->flag_pollin)) { |
| 129 | pe->flag_pollin = true; |
| 130 | kevent_add (pe->fd, EVFILT_READ, pe); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void zmq::kqueue_t::reset_pollin (handle_t handle_) |
| 135 | { |
| 136 | check_thread (); |
| 137 | poll_entry_t *pe = (poll_entry_t *) handle_; |
| 138 | if (likely (pe->flag_pollin)) { |
| 139 | pe->flag_pollin = false; |
| 140 | kevent_delete (pe->fd, EVFILT_READ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void zmq::kqueue_t::set_pollout (handle_t handle_) |
| 145 | { |
| 146 | check_thread (); |
| 147 | poll_entry_t *pe = (poll_entry_t *) handle_; |
| 148 | if (likely (!pe->flag_pollout)) { |
| 149 | pe->flag_pollout = true; |
| 150 | kevent_add (pe->fd, EVFILT_WRITE, pe); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void zmq::kqueue_t::reset_pollout (handle_t handle_) |
| 155 | { |
| 156 | check_thread (); |
| 157 | poll_entry_t *pe = (poll_entry_t *) handle_; |
| 158 | if (likely (pe->flag_pollout)) { |
| 159 | pe->flag_pollout = false; |
| 160 | kevent_delete (pe->fd, EVFILT_WRITE); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void zmq::kqueue_t::stop () |
| 165 | { |
| 166 | } |
| 167 | |
| 168 | int zmq::kqueue_t::max_fds () |
| 169 | { |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | void zmq::kqueue_t::loop () |
| 174 | { |
| 175 | while (true) { |
| 176 | // Execute any due timers. |
| 177 | int timeout = (int) execute_timers (); |
| 178 | |
| 179 | if (get_load () == 0) { |
| 180 | if (timeout == 0) |
| 181 | break; |
| 182 | |
| 183 | // TODO sleep for timeout |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | // Wait for events. |
| 188 | struct kevent ev_buf[max_io_events]; |
| 189 | timespec ts = {timeout / 1000, (timeout % 1000) * 1000000}; |
| 190 | int n = kevent (kqueue_fd, NULL, 0, &ev_buf[0], max_io_events, |
| 191 | timeout ? &ts : NULL); |
| 192 | #ifdef HAVE_FORK |
| 193 | if (unlikely (pid != getpid ())) { |
| 194 | //printf("zmq::kqueue_t::loop aborting on forked child %d\n", (int)getpid()); |
| 195 | // simply exit the loop in a forked process. |
| 196 | return; |
| 197 | } |
| 198 | #endif |
| 199 | if (n == -1) { |
| 200 | errno_assert (errno == EINTR); |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | for (int i = 0; i < n; i++) { |
| 205 | poll_entry_t *pe = (poll_entry_t *) ev_buf[i].udata; |
| 206 | |
| 207 | if (pe->fd == retired_fd) |
| 208 | continue; |
| 209 | if (ev_buf[i].flags & EV_EOF) |
| 210 | pe->reactor->in_event (); |
| 211 | if (pe->fd == retired_fd) |
| 212 | continue; |
| 213 | if (ev_buf[i].filter == EVFILT_WRITE) |
| 214 | pe->reactor->out_event (); |
| 215 | if (pe->fd == retired_fd) |
| 216 | continue; |
| 217 | if (ev_buf[i].filter == EVFILT_READ) |
| 218 | pe->reactor->in_event (); |
| 219 | } |
| 220 | |
| 221 | // Destroy retired event sources. |
| 222 | for (retired_t::iterator it = retired.begin (); it != retired.end (); |
| 223 | ++it) { |
| 224 | LIBZMQ_DELETE (*it); |
| 225 | } |
| 226 | retired.clear (); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | #endif |
| 231 | |