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

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
Martin Sustrik's avatar
Martin Sustrik 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
Martin Sustrik's avatar
Martin Sustrik 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.
Martin Sustrik's avatar
Martin Sustrik committed
25

26
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30
#include "precompiled.hpp"
31
#include "mailbox.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
32 33
#include "err.hpp"

34
zmq::mailbox_t::mailbox_t ()
Martin Sustrik's avatar
Martin Sustrik committed
35
{
36 37 38
    //  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.
Martin Hurton's avatar
Martin Hurton committed
39
    const bool ok = cpipe.read (NULL);
40 41
    zmq_assert (!ok);
    active = false;
Martin Sustrik's avatar
Martin Sustrik committed
42 43
}

44
zmq::mailbox_t::~mailbox_t ()
Martin Sustrik's avatar
Martin Sustrik committed
45
{
46
    //  TODO: Retrieve and deallocate commands inside the cpipe.
47 48 49 50 51

    // Work around problem that other threads might still be in our
    // send() method, by waiting on the mutex before disappearing.
    sync.lock ();
    sync.unlock ();
Martin Sustrik's avatar
Martin Sustrik committed
52 53
}

Martin Hurton's avatar
Martin Hurton committed
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 62
    sync.lock ();
    cpipe.write (cmd_, false);
Martin Hurton's avatar
Martin Hurton committed
63
    const bool ok = cpipe.flush ();
64 65 66
    sync.unlock ();
    if (!ok)
        signaler.send ();
67 68
}

69
int zmq::mailbox_t::recv (command_t *cmd_, int timeout_)
70
{
71 72
    //  Try to get the command straight away.
    if (active) {
73
        if (cpipe.read (cmd_))
74 75 76 77
            return 0;

        //  If there are no more commands available, switch into passive state.
        active = false;
78
    }
79

80
    //  Wait for signal from the command sender.
81
    int rc = signaler.wait (timeout_);
82 83
    if (rc == -1) {
        errno_assert (errno == EAGAIN || errno == EINTR);
84
        return -1;
85 86 87
    }

    //  Receive the signal.
88 89 90 91 92
    rc = signaler.recv_failable ();
    if (rc == -1) {
        errno_assert (errno == EAGAIN);
        return -1;
    }
93

94
    //  Switch into active state.
95
    active = true;
Martin Sustrik's avatar
Martin Sustrik committed
96

97
    //  Get a command.
98
    const bool ok = cpipe.read (cmd_);
99
    zmq_assert (ok);
100 101
    return 0;
}