| 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 | |
| 34 | zmq::mailbox_t::mailbox_t () |
| 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.check_read (); |
| 40 | zmq_assert (!ok); |
| 41 | _active = false; |
| 42 | } |
| 43 | |
| 44 | zmq::mailbox_t::~mailbox_t () |
| 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 | |
| 54 | zmq::fd_t zmq::mailbox_t::get_fd () const |
| 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 | } |
| 102 | |
| 103 | bool zmq::mailbox_t::valid () const |
| 104 | { |
| 105 | return _signaler.valid (); |
| 106 | } |
| 107 | |