mailbox.hpp 2.32 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
3 4 5 6

    This file is part of 0MQ.

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

    0MQ 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
14
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
15

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

20 21
#ifndef __ZMQ_MAILBOX_HPP_INCLUDED__
#define __ZMQ_MAILBOX_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
22

23 24
#include <stddef.h>

Martin Sustrik's avatar
Martin Sustrik committed
25
#include "platform.hpp"
26
#include "signaler.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
27
#include "fd.hpp"
28
#include "config.hpp"
29
#include "command.hpp"
30 31
#include "ypipe.hpp"
#include "mutex.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
32

Martin Sustrik's avatar
Martin Sustrik committed
33
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
34 35
{

36
    class mailbox_t
Martin Sustrik's avatar
Martin Sustrik committed
37 38 39
    {
    public:

40 41
        mailbox_t ();
        ~mailbox_t ();
Martin Sustrik's avatar
Martin Sustrik committed
42 43

        fd_t get_fd ();
44
        void send (const command_t &cmd_);
45
        int recv (command_t *cmd_, int timeout_);
46
        
47 48 49 50 51 52 53
#ifdef HAVE_FORK
        // close the file descriptors in the signaller. This is used in a forked
        // child process to close the file descriptors so that they do not interfere
        // with the context in the parent process.
        void forked() { signaler.forked(); }
#endif

Martin Sustrik's avatar
Martin Sustrik committed
54 55
    private:

56 57 58
        //  The pipe to store actual commands.
        typedef ypipe_t <command_t, command_pipe_granularity> cpipe_t;
        cpipe_t cpipe;
59

60 61
        //  Signaler to pass signals from writer thread to reader thread.
        signaler_t signaler;
62

63 64 65 66 67
        //  There's only one thread receiving from the mailbox, but there
        //  is arbitrary number of threads sending. Given that ypipe requires
        //  synchronised access on both of its endpoints, we have to synchronise
        //  the sending side.
        mutex_t sync;
68

69 70 71
        //  True if the underlying pipe is active, ie. when we are allowed to
        //  read commands from it.
        bool active;
72

73 74
        //  Disable copying of mailbox_t object.
        mailbox_t (const mailbox_t&);
75
        const mailbox_t &operator = (const mailbox_t&);
Martin Sustrik's avatar
Martin Sustrik committed
76 77 78 79 80
    };

}

#endif