mailbox_safe.cpp 3.84 KB
Newer Older
somdoron's avatar
somdoron committed
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
somdoron's avatar
somdoron committed
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
somdoron's avatar
somdoron committed
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
somdoron's avatar
somdoron committed
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
somdoron's avatar
somdoron committed
25 26 27 28 29

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30
#include "precompiled.hpp"
somdoron's avatar
somdoron committed
31
#include "mailbox_safe.hpp"
32
#include "clock.hpp"
somdoron's avatar
somdoron committed
33 34
#include "err.hpp"

35 36
#include <algorithm>

37
zmq::mailbox_safe_t::mailbox_safe_t (mutex_t *sync_) : _sync (sync_)
somdoron's avatar
somdoron committed
38 39 40 41
{
    //  Get the pipe into passive state. That way, if the users starts by
    //  polling on the associated file descriptor it will get woken up when
    //  new command is posted.
42
    const bool ok = _cpipe.check_read ();
somdoron's avatar
somdoron committed
43 44 45 46 47 48 49 50 51
    zmq_assert (!ok);
}

zmq::mailbox_safe_t::~mailbox_safe_t ()
{
    //  TODO: Retrieve and deallocate commands inside the cpipe.

    // Work around problem that other threads might still be in our
    // send() method, by waiting on the mutex before disappearing.
52 53
    _sync->lock ();
    _sync->unlock ();
somdoron's avatar
somdoron committed
54 55
}

56
void zmq::mailbox_safe_t::add_signaler (signaler_t *signaler_)
somdoron's avatar
somdoron committed
57
{
58
    _signalers.push_back (signaler_);
somdoron's avatar
somdoron committed
59 60
}

61
void zmq::mailbox_safe_t::remove_signaler (signaler_t *signaler_)
somdoron's avatar
somdoron committed
62 63
{
    // TODO: make a copy of array and signal outside the lock
64
    const std::vector<zmq::signaler_t *>::iterator end = _signalers.end ();
65
    std::vector<signaler_t *>::iterator it =
66
      std::find (_signalers.begin (), end, signaler_);
somdoron's avatar
somdoron committed
67

68
    if (it != end)
69
        _signalers.erase (it);
somdoron's avatar
somdoron committed
70 71
}

72 73
void zmq::mailbox_safe_t::clear_signalers ()
{
74
    _signalers.clear ();
75 76
}

somdoron's avatar
somdoron committed
77 78
void zmq::mailbox_safe_t::send (const command_t &cmd_)
{
79 80 81
    _sync->lock ();
    _cpipe.write (cmd_, false);
    const bool ok = _cpipe.flush ();
somdoron's avatar
somdoron committed
82 83

    if (!ok) {
84
        _cond_var.broadcast ();
85 86 87 88

        for (std::vector<signaler_t *>::iterator it = _signalers.begin (),
                                                 end = _signalers.end ();
             it != end; ++it) {
89
            (*it)->send ();
somdoron's avatar
somdoron committed
90 91 92
        }
    }

93
    _sync->unlock ();
somdoron's avatar
somdoron committed
94 95 96 97 98
}

int zmq::mailbox_safe_t::recv (command_t *cmd_, int timeout_)
{
    //  Try to get the command straight away.
99
    if (_cpipe.read (cmd_))
somdoron's avatar
somdoron committed
100
        return 0;
101

102 103 104 105 106
    //  If the timeout is zero, it will be quicker to release the lock, giving other a chance to send a command
    //  and immediately relock it.
    if (timeout_ == 0) {
        _sync->unlock ();
        _sync->lock ();
107
    } else {
108 109 110 111 112 113
        //  Wait for signal from the command sender.
        int rc = _cond_var.wait (_sync, timeout_);
        if (rc == -1) {
            errno_assert (errno == EAGAIN || errno == EINTR);
            return -1;
        }
somdoron's avatar
somdoron committed
114 115 116
    }

    //  Another thread may already fetch the command
117
    const bool ok = _cpipe.read (cmd_);
somdoron's avatar
somdoron committed
118 119 120 121 122 123 124 125

    if (!ok) {
        errno = EAGAIN;
        return -1;
    }

    return 0;
}