io_thread.cpp 2.98 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2015 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 31
#include <new>

32
#include "macros.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
33 34 35
#include "io_thread.hpp"
#include "platform.hpp"
#include "err.hpp"
36
#include "ctx.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
37

Martin Sustrik's avatar
Martin Sustrik committed
38 39
zmq::io_thread_t::io_thread_t (ctx_t *ctx_, uint32_t tid_) :
    object_t (ctx_, tid_)
Martin Sustrik's avatar
Martin Sustrik committed
40
{
41
    poller = new (std::nothrow) poller_t (*ctx_);
42
    alloc_assert (poller);
Martin Sustrik's avatar
Martin Sustrik committed
43

44 45
    mailbox_handle = poller->add_fd (mailbox.get_fd (), this);
    poller->set_pollin (mailbox_handle);
Martin Sustrik's avatar
Martin Sustrik committed
46 47
}

Martin Sustrik's avatar
Martin Sustrik committed
48
zmq::io_thread_t::~io_thread_t ()
Martin Sustrik's avatar
Martin Sustrik committed
49
{
50
    LIBZMQ_DELETE(poller);
Martin Sustrik's avatar
Martin Sustrik committed
51 52
}

Martin Sustrik's avatar
Martin Sustrik committed
53
void zmq::io_thread_t::start ()
Martin Sustrik's avatar
Martin Sustrik committed
54 55 56 57 58
{
    //  Start the underlying I/O thread.
    poller->start ();
}

Martin Sustrik's avatar
Martin Sustrik committed
59
void zmq::io_thread_t::stop ()
Martin Sustrik's avatar
Martin Sustrik committed
60 61 62 63
{
    send_stop ();
}

64
zmq::mailbox_t *zmq::io_thread_t::get_mailbox ()
Martin Sustrik's avatar
Martin Sustrik committed
65
{
66
    return &mailbox;
Martin Sustrik's avatar
Martin Sustrik committed
67 68
}

Martin Sustrik's avatar
Martin Sustrik committed
69
int zmq::io_thread_t::get_load ()
Martin Sustrik's avatar
Martin Sustrik committed
70 71 72 73
{
    return poller->get_load ();
}

Martin Sustrik's avatar
Martin Sustrik committed
74
void zmq::io_thread_t::in_event ()
Martin Sustrik's avatar
Martin Sustrik committed
75
{
76 77 78
    //  TODO: Do we want to limit number of commands I/O thread can
    //  process in a single go?

79 80 81 82 83 84 85
    command_t cmd;
    int rc = mailbox.recv (&cmd, 0);

    while (rc == 0 || errno == EINTR) {
        if (rc == 0)
            cmd.destination->process_command (cmd);
        rc = mailbox.recv (&cmd, 0);
Martin Sustrik's avatar
Martin Sustrik committed
86
    }
87 88

    errno_assert (rc != 0 && errno == EAGAIN);
Martin Sustrik's avatar
Martin Sustrik committed
89 90
}

Martin Sustrik's avatar
Martin Sustrik committed
91
void zmq::io_thread_t::out_event ()
Martin Sustrik's avatar
Martin Sustrik committed
92 93
{
    //  We are never polling for POLLOUT here. This function is never called.
Martin Sustrik's avatar
Martin Sustrik committed
94
    zmq_assert (false);
Martin Sustrik's avatar
Martin Sustrik committed
95 96
}

97
void zmq::io_thread_t::timer_event (int)
Martin Sustrik's avatar
Martin Sustrik committed
98 99
{
    //  No timers here. This function is never called.
Martin Sustrik's avatar
Martin Sustrik committed
100
    zmq_assert (false);
Martin Sustrik's avatar
Martin Sustrik committed
101 102
}

103
zmq::poller_t *zmq::io_thread_t::get_poller ()
Martin Sustrik's avatar
Martin Sustrik committed
104
{
Martin Sustrik's avatar
Martin Sustrik committed
105
    zmq_assert (poller);
Martin Sustrik's avatar
Martin Sustrik committed
106 107 108
    return poller;
}

Martin Sustrik's avatar
Martin Sustrik committed
109
void zmq::io_thread_t::process_stop ()
Martin Sustrik's avatar
Martin Sustrik committed
110
{
111
    poller->rm_fd (mailbox_handle);
Martin Sustrik's avatar
Martin Sustrik committed
112 113
    poller->stop ();
}