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