| 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 | |
| 32 | #include <new> |
| 33 | |
| 34 | #include "macros.hpp" |
| 35 | #include "io_thread.hpp" |
| 36 | #include "err.hpp" |
| 37 | #include "ctx.hpp" |
| 38 | |
| 39 | zmq::io_thread_t::io_thread_t (ctx_t *ctx_, uint32_t tid_) : |
| 40 | object_t (ctx_, tid_), |
| 41 | _mailbox_handle (static_cast<poller_t::handle_t> (NULL)) |
| 42 | { |
| 43 | _poller = new (std::nothrow) poller_t (*ctx_); |
| 44 | alloc_assert (_poller); |
| 45 | |
| 46 | if (_mailbox.get_fd () != retired_fd) { |
| 47 | _mailbox_handle = _poller->add_fd (_mailbox.get_fd (), this); |
| 48 | _poller->set_pollin (_mailbox_handle); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | zmq::io_thread_t::~io_thread_t () |
| 53 | { |
| 54 | LIBZMQ_DELETE (_poller); |
| 55 | } |
| 56 | |
| 57 | void zmq::io_thread_t::start () |
| 58 | { |
| 59 | char name[16] = "" ; |
| 60 | snprintf (name, sizeof (name), "IO/%u" , |
| 61 | get_tid () - zmq::ctx_t::reaper_tid - 1); |
| 62 | // Start the underlying I/O thread. |
| 63 | _poller->start (name); |
| 64 | } |
| 65 | |
| 66 | void zmq::io_thread_t::stop () |
| 67 | { |
| 68 | send_stop (); |
| 69 | } |
| 70 | |
| 71 | zmq::mailbox_t *zmq::io_thread_t::get_mailbox () |
| 72 | { |
| 73 | return &_mailbox; |
| 74 | } |
| 75 | |
| 76 | int zmq::io_thread_t::get_load () |
| 77 | { |
| 78 | return _poller->get_load (); |
| 79 | } |
| 80 | |
| 81 | void zmq::io_thread_t::in_event () |
| 82 | { |
| 83 | // TODO: Do we want to limit number of commands I/O thread can |
| 84 | // process in a single go? |
| 85 | |
| 86 | command_t cmd; |
| 87 | int rc = _mailbox.recv (&cmd, 0); |
| 88 | |
| 89 | while (rc == 0 || errno == EINTR) { |
| 90 | if (rc == 0) |
| 91 | cmd.destination->process_command (cmd); |
| 92 | rc = _mailbox.recv (&cmd, 0); |
| 93 | } |
| 94 | |
| 95 | errno_assert (rc != 0 && errno == EAGAIN); |
| 96 | } |
| 97 | |
| 98 | void zmq::io_thread_t::out_event () |
| 99 | { |
| 100 | // We are never polling for POLLOUT here. This function is never called. |
| 101 | zmq_assert (false); |
| 102 | } |
| 103 | |
| 104 | void zmq::io_thread_t::timer_event (int) |
| 105 | { |
| 106 | // No timers here. This function is never called. |
| 107 | zmq_assert (false); |
| 108 | } |
| 109 | |
| 110 | zmq::poller_t *zmq::io_thread_t::get_poller () |
| 111 | { |
| 112 | zmq_assert (_poller); |
| 113 | return _poller; |
| 114 | } |
| 115 | |
| 116 | void zmq::io_thread_t::process_stop () |
| 117 | { |
| 118 | zmq_assert (_mailbox_handle); |
| 119 | _poller->rm_fd (_mailbox_handle); |
| 120 | _poller->stop (); |
| 121 | } |
| 122 | |