mailbox.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "precompiled.hpp"
4 #include "mailbox.hpp"
5 #include "err.hpp"
6 
7 zmq::mailbox_t::mailbox_t ()
8 {
9  // Get the pipe into passive state. That way, if the users starts by
10  // polling on the associated file descriptor it will get woken up when
11  // new command is posted.
12  const bool ok = _cpipe.check_read ();
13  zmq_assert (!ok);
14  _active = false;
15 }
16 
17 zmq::mailbox_t::~mailbox_t ()
18 {
19  // TODO: Retrieve and deallocate commands inside the _cpipe.
20 
21  // Work around problem that other threads might still be in our
22  // send() method, by waiting on the mutex before disappearing.
23  _sync.lock ();
24  _sync.unlock ();
25 }
26 
28 {
29  return _signaler.get_fd ();
30 }
31 
32 void zmq::mailbox_t::send (const command_t &cmd_)
33 {
34  _sync.lock ();
35  _cpipe.write (cmd_, false);
36  const bool ok = _cpipe.flush ();
37  _sync.unlock ();
38  if (!ok)
39  _signaler.send ();
40 }
41 
42 int zmq::mailbox_t::recv (command_t *cmd_, int timeout_)
43 {
44  // Try to get the command straight away.
45  if (_active) {
46  if (_cpipe.read (cmd_))
47  return 0;
48 
49  // If there are no more commands available, switch into passive state.
50  _active = false;
51  }
52 
53  // Wait for signal from the command sender.
54  int rc = _signaler.wait (timeout_);
55  if (rc == -1) {
56  errno_assert (errno == EAGAIN || errno == EINTR);
57  return -1;
58  }
59 
60  // Receive the signal.
61  rc = _signaler.recv_failable ();
62  if (rc == -1) {
64  return -1;
65  }
66 
67  // Switch into active state.
68  _active = true;
69 
70  // Get a command.
71  const bool ok = _cpipe.read (cmd_);
72  zmq_assert (ok);
73  return 0;
74 }
75 
76 bool zmq::mailbox_t::valid () const
77 {
78  return _signaler.valid ();
79 }
EINTR
#define EINTR
Definition: errno.hpp:7
EAGAIN
#define EAGAIN
Definition: errno.hpp:14
precompiled.hpp
zmq_assert
#define zmq_assert(x)
Definition: err.hpp:102
errno
int errno
send
void send(fd_t fd_, const char(&data_)[N])
Definition: test_security_curve.cpp:209
zmq::fd_t
int fd_t
Definition: zmq.hpp:287
get_fd
SETUP_TEARDOWN_TESTCONTEXT fd_t get_fd(void *socket_)
Definition: test_poller.cpp:17
ok
ROSCPP_DECL bool ok()
errno_assert
#define errno_assert(x)
Definition: err.hpp:113
mailbox.hpp
err.hpp


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:55