| 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 | #ifndef __ZMQ_IO_THREAD_HPP_INCLUDED__ |
| 31 | #define __ZMQ_IO_THREAD_HPP_INCLUDED__ |
| 32 | |
| 33 | #include "stdint.hpp" |
| 34 | #include "object.hpp" |
| 35 | #include "poller.hpp" |
| 36 | #include "i_poll_events.hpp" |
| 37 | #include "mailbox.hpp" |
| 38 | |
| 39 | namespace zmq |
| 40 | { |
| 41 | class ctx_t; |
| 42 | |
| 43 | // Generic part of the I/O thread. Polling-mechanism-specific features |
| 44 | // are implemented in separate "polling objects". |
| 45 | |
| 46 | class io_thread_t : public object_t, public i_poll_events |
| 47 | { |
| 48 | public: |
| 49 | io_thread_t (zmq::ctx_t *ctx_, uint32_t tid_); |
| 50 | |
| 51 | // Clean-up. If the thread was started, it's necessary to call 'stop' |
| 52 | // before invoking destructor. Otherwise the destructor would hang up. |
| 53 | ~io_thread_t (); |
| 54 | |
| 55 | // Launch the physical thread. |
| 56 | void start (); |
| 57 | |
| 58 | // Ask underlying thread to stop. |
| 59 | void stop (); |
| 60 | |
| 61 | // Returns mailbox associated with this I/O thread. |
| 62 | mailbox_t *get_mailbox (); |
| 63 | |
| 64 | // i_poll_events implementation. |
| 65 | void in_event (); |
| 66 | void out_event (); |
| 67 | void timer_event (int id_); |
| 68 | |
| 69 | // Used by io_objects to retrieve the associated poller object. |
| 70 | poller_t *get_poller (); |
| 71 | |
| 72 | // Command handlers. |
| 73 | void process_stop (); |
| 74 | |
| 75 | // Returns load experienced by the I/O thread. |
| 76 | int get_load (); |
| 77 | |
| 78 | private: |
| 79 | // I/O thread accesses incoming commands via this mailbox. |
| 80 | mailbox_t _mailbox; |
| 81 | |
| 82 | // Handle associated with mailbox' file descriptor. |
| 83 | poller_t::handle_t _mailbox_handle; |
| 84 | |
| 85 | // I/O multiplexing is performed using a poller object. |
| 86 | poller_t *_poller; |
| 87 | |
| 88 | ZMQ_NON_COPYABLE_NOR_MOVABLE (io_thread_t) |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | #endif |
| 93 | |