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 "macros.hpp" |
32 | #include "reaper.hpp" |
33 | #include "socket_base.hpp" |
34 | #include "err.hpp" |
35 | |
36 | zmq::reaper_t::reaper_t (class ctx_t *ctx_, uint32_t tid_) : |
37 | object_t (ctx_, tid_), |
38 | _mailbox_handle (static_cast<poller_t::handle_t> (NULL)), |
39 | _poller (NULL), |
40 | _sockets (0), |
41 | _terminating (false) |
42 | { |
43 | if (!_mailbox.valid ()) |
44 | return; |
45 | |
46 | _poller = new (std::nothrow) poller_t (*ctx_); |
47 | alloc_assert (_poller); |
48 | |
49 | if (_mailbox.get_fd () != retired_fd) { |
50 | _mailbox_handle = _poller->add_fd (_mailbox.get_fd (), this); |
51 | _poller->set_pollin (_mailbox_handle); |
52 | } |
53 | |
54 | #ifdef HAVE_FORK |
55 | _pid = getpid (); |
56 | #endif |
57 | } |
58 | |
59 | zmq::reaper_t::~reaper_t () |
60 | { |
61 | LIBZMQ_DELETE (_poller); |
62 | } |
63 | |
64 | zmq::mailbox_t *zmq::reaper_t::get_mailbox () |
65 | { |
66 | return &_mailbox; |
67 | } |
68 | |
69 | void zmq::reaper_t::start () |
70 | { |
71 | zmq_assert (_mailbox.valid ()); |
72 | |
73 | // Start the thread. |
74 | _poller->start ("Reaper" ); |
75 | } |
76 | |
77 | void zmq::reaper_t::stop () |
78 | { |
79 | if (get_mailbox ()->valid ()) { |
80 | send_stop (); |
81 | } |
82 | } |
83 | |
84 | void zmq::reaper_t::in_event () |
85 | { |
86 | while (true) { |
87 | #ifdef HAVE_FORK |
88 | if (unlikely (_pid != getpid ())) { |
89 | //printf("zmq::reaper_t::in_event return in child process %d\n", (int)getpid()); |
90 | return; |
91 | } |
92 | #endif |
93 | |
94 | // Get the next command. If there is none, exit. |
95 | command_t cmd; |
96 | int rc = _mailbox.recv (&cmd, 0); |
97 | if (rc != 0 && errno == EINTR) |
98 | continue; |
99 | if (rc != 0 && errno == EAGAIN) |
100 | break; |
101 | errno_assert (rc == 0); |
102 | |
103 | // Process the command. |
104 | cmd.destination->process_command (cmd); |
105 | } |
106 | } |
107 | |
108 | void zmq::reaper_t::out_event () |
109 | { |
110 | zmq_assert (false); |
111 | } |
112 | |
113 | void zmq::reaper_t::timer_event (int) |
114 | { |
115 | zmq_assert (false); |
116 | } |
117 | |
118 | void zmq::reaper_t::process_stop () |
119 | { |
120 | _terminating = true; |
121 | |
122 | // If there are no sockets being reaped finish immediately. |
123 | if (!_sockets) { |
124 | send_done (); |
125 | _poller->rm_fd (_mailbox_handle); |
126 | _poller->stop (); |
127 | } |
128 | } |
129 | |
130 | void zmq::reaper_t::process_reap (socket_base_t *socket_) |
131 | { |
132 | // Add the socket to the poller. |
133 | socket_->start_reaping (_poller); |
134 | |
135 | ++_sockets; |
136 | } |
137 | |
138 | void zmq::reaper_t::process_reaped () |
139 | { |
140 | --_sockets; |
141 | |
142 | // If reaped was already asked to terminate and there are no more sockets, |
143 | // finish immediately. |
144 | if (!_sockets && _terminating) { |
145 | send_done (); |
146 | _poller->rm_fd (_mailbox_handle); |
147 | _poller->stop (); |
148 | } |
149 | } |
150 | |