mailbox.hpp 2.13 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-2011 250bpm s.r.o.
3
    Copyright (c) 2007-2009 iMatix Corporation
4
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
5 6 7 8

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
9
    the terms of the GNU Lesser General Public License as published by
Martin Sustrik's avatar
Martin Sustrik committed
10 11 12 13 14 15
    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
16
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
17

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

22 23
#ifndef __ZMQ_MAILBOX_HPP_INCLUDED__
#define __ZMQ_MAILBOX_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
24

25 26
#include <stddef.h>

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

Martin Sustrik's avatar
Martin Sustrik committed
35
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
36 37
{

38
    class mailbox_t
Martin Sustrik's avatar
Martin Sustrik committed
39 40 41
    {
    public:

42 43
        mailbox_t ();
        ~mailbox_t ();
Martin Sustrik's avatar
Martin Sustrik committed
44 45

        fd_t get_fd ();
46
        void send (const command_t &cmd_);
47
        int recv (command_t *cmd_, int timeout_);
48
        
Martin Sustrik's avatar
Martin Sustrik committed
49 50
    private:

51 52 53
        //  The pipe to store actual commands.
        typedef ypipe_t <command_t, command_pipe_granularity> cpipe_t;
        cpipe_t cpipe;
54

55 56
        //  Signaler to pass signals from writer thread to reader thread.
        signaler_t signaler;
57

58 59 60 61 62
        //  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;
63

64 65 66
        //  True if the underlying pipe is active, ie. when we are allowed to
        //  read commands from it.
        bool active;
67

68 69
        //  Disable copying of mailbox_t object.
        mailbox_t (const mailbox_t&);
70
        const mailbox_t &operator = (const mailbox_t&);
Martin Sustrik's avatar
Martin Sustrik committed
71 72 73 74 75
    };

}

#endif