libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
mailbox.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 "mailbox.hpp"
32 #include "err.hpp"
33 
35 {
36  // Get the pipe into passive state. That way, if the users starts by
37  // polling on the associated file descriptor it will get woken up when
38  // new command is posted.
39  const bool ok = cpipe.read (NULL);
40  zmq_assert (!ok);
41  active = false;
42 }
43 
45 {
46  // TODO: Retrieve and deallocate commands inside the cpipe.
47 
48  // Work around problem that other threads might still be in our
49  // send() method, by waiting on the mutex before disappearing.
50  sync.lock ();
51  sync.unlock ();
52 }
53 
55 {
56  return signaler.get_fd ();
57 }
58 
59 void zmq::mailbox_t::send (const command_t &cmd_)
60 {
61  sync.lock ();
62  cpipe.write (cmd_, false);
63  const bool ok = cpipe.flush ();
64  sync.unlock ();
65  if (!ok)
66  signaler.send ();
67 }
68 
69 int zmq::mailbox_t::recv (command_t *cmd_, int timeout_)
70 {
71  // Try to get the command straight away.
72  if (active) {
73  if (cpipe.read (cmd_))
74  return 0;
75 
76  // If there are no more commands available, switch into passive state.
77  active = false;
78  }
79 
80  // Wait for signal from the command sender.
81  int rc = signaler.wait (timeout_);
82  if (rc == -1) {
83  errno_assert (errno == EAGAIN || errno == EINTR);
84  return -1;
85  }
86 
87  // Receive the signal.
88  rc = signaler.recv_failable ();
89  if (rc == -1) {
90  errno_assert (errno == EAGAIN);
91  return -1;
92  }
93 
94  // Switch into active state.
95  active = true;
96 
97  // Get a command.
98  const bool ok = cpipe.read (cmd_);
99  zmq_assert (ok);
100  return 0;
101 }
int recv_failable()
Definition: signaler.cpp:313
cpipe_t cpipe
Definition: mailbox.hpp:69
fd_t get_fd() const
Definition: mailbox.cpp:54
#define zmq_assert(x)
Definition: err.hpp:119
int wait(int timeout_)
Definition: signaler.cpp:207
fd_t get_fd() const
Definition: signaler.cpp:166
mutex_t sync
Definition: mailbox.hpp:78
bool flush()
Definition: ypipe.hpp:112
void send(const command_t &cmd_)
Definition: mailbox.cpp:59
int recv(command_t *cmd_, int timeout_)
Definition: mailbox.cpp:69
void write(const T &value_, bool incomplete_)
Definition: ypipe.hpp:83
signaler_t signaler
Definition: mailbox.hpp:72
void unlock()
Definition: mutex.hpp:136
void lock()
Definition: mutex.hpp:120
#define errno_assert(x)
Definition: err.hpp:129
bool read(T *value_)
Definition: ypipe.hpp:163