libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
io_thread.cpp
Go to the documentation of this file.
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 <new>
32 
33 #include "macros.hpp"
34 #include "io_thread.hpp"
35 #include "platform.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 {
42  poller = new (std::nothrow) poller_t (*ctx_);
44 
45  mailbox_handle = poller->add_fd (mailbox.get_fd (), this);
46  poller->set_pollin (mailbox_handle);
47 }
48 
50 {
52 }
53 
55 {
56  // Start the underlying I/O thread.
57  poller->start ();
58 }
59 
61 {
62  send_stop ();
63 }
64 
66 {
67  return &mailbox;
68 }
69 
71 {
72  return poller->get_load ();
73 }
74 
76 {
77  // TODO: Do we want to limit number of commands I/O thread can
78  // process in a single go?
79 
80  command_t cmd;
81  int rc = mailbox.recv (&cmd, 0);
82 
83  while (rc == 0 || errno == EINTR) {
84  if (rc == 0)
85  cmd.destination->process_command (cmd);
86  rc = mailbox.recv (&cmd, 0);
87  }
88 
89  errno_assert (rc != 0 && errno == EAGAIN);
90 }
91 
93 {
94  // We are never polling for POLLOUT here. This function is never called.
95  zmq_assert (false);
96 }
97 
99 {
100  // No timers here. This function is never called.
101  zmq_assert (false);
102 }
103 
105 {
106  zmq_assert (poller);
107  return poller;
108 }
109 
111 {
112  poller->rm_fd (mailbox_handle);
113  poller->stop ();
114 }
#define LIBZMQ_DELETE(p_object)
Definition: macros.hpp:7
poller_t::handle_t mailbox_handle
Definition: io_thread.hpp:88
void process_command(zmq::command_t &cmd_)
Definition: object.cpp:73
mailbox_t mailbox
Definition: io_thread.hpp:85
fd_t get_fd() const
Definition: mailbox.cpp:54
#define zmq_assert(x)
Definition: err.hpp:119
int recv(command_t *cmd_, int timeout_)
Definition: mailbox.cpp:69
poller_t * get_poller()
Definition: io_thread.cpp:104
zmq::object_t * destination
Definition: command.hpp:53
poller_t * poller
Definition: io_thread.hpp:91
void send_stop()
Definition: object.cpp:194
#define alloc_assert(x)
Definition: err.hpp:159
#define errno_assert(x)
Definition: err.hpp:129
void timer_event(int id_)
Definition: io_thread.cpp:98
io_thread_t(zmq::ctx_t *ctx_, uint32_t tid_)
Definition: io_thread.cpp:39
mailbox_t * get_mailbox()
Definition: io_thread.cpp:65