io_thread.cpp 2.3 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
#include <new>

Martin Sustrik's avatar
Martin Sustrik committed
22 23 24
#include "io_thread.hpp"
#include "platform.hpp"
#include "err.hpp"
25
#include "ctx.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
26

Martin Sustrik's avatar
Martin Sustrik committed
27 28
zmq::io_thread_t::io_thread_t (ctx_t *ctx_, uint32_t tid_) :
    object_t (ctx_, tid_)
Martin Sustrik's avatar
Martin Sustrik committed
29
{
30
    poller = new (std::nothrow) poller_t;
31
    alloc_assert (poller);
Martin Sustrik's avatar
Martin Sustrik committed
32

33 34
    mailbox_handle = poller->add_fd (mailbox.get_fd (), this);
    poller->set_pollin (mailbox_handle);
Martin Sustrik's avatar
Martin Sustrik committed
35 36
}

Martin Sustrik's avatar
Martin Sustrik committed
37
zmq::io_thread_t::~io_thread_t ()
Martin Sustrik's avatar
Martin Sustrik committed
38 39 40 41
{
    delete poller;
}

Martin Sustrik's avatar
Martin Sustrik committed
42
void zmq::io_thread_t::start ()
Martin Sustrik's avatar
Martin Sustrik committed
43 44 45 46 47
{
    //  Start the underlying I/O thread.
    poller->start ();
}

Martin Sustrik's avatar
Martin Sustrik committed
48
void zmq::io_thread_t::stop ()
Martin Sustrik's avatar
Martin Sustrik committed
49 50 51 52
{
    send_stop ();
}

53
zmq::mailbox_t *zmq::io_thread_t::get_mailbox ()
Martin Sustrik's avatar
Martin Sustrik committed
54
{
55
    return &mailbox;
Martin Sustrik's avatar
Martin Sustrik committed
56 57
}

Martin Sustrik's avatar
Martin Sustrik committed
58
int zmq::io_thread_t::get_load ()
Martin Sustrik's avatar
Martin Sustrik committed
59 60 61 62
{
    return poller->get_load ();
}

Martin Sustrik's avatar
Martin Sustrik committed
63
void zmq::io_thread_t::in_event ()
Martin Sustrik's avatar
Martin Sustrik committed
64
{
65 66 67
    //  TODO: Do we want to limit number of commands I/O thread can
    //  process in a single go?

68 69 70 71 72 73 74
    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
75
    }
76 77

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

Martin Sustrik's avatar
Martin Sustrik committed
80
void zmq::io_thread_t::out_event ()
Martin Sustrik's avatar
Martin Sustrik committed
81 82
{
    //  We are never polling for POLLOUT here. This function is never called.
Martin Sustrik's avatar
Martin Sustrik committed
83
    zmq_assert (false);
Martin Sustrik's avatar
Martin Sustrik committed
84 85
}

86
void zmq::io_thread_t::timer_event (int)
Martin Sustrik's avatar
Martin Sustrik committed
87 88
{
    //  No timers here. This function is never called.
Martin Sustrik's avatar
Martin Sustrik committed
89
    zmq_assert (false);
Martin Sustrik's avatar
Martin Sustrik committed
90 91
}

92
zmq::poller_t *zmq::io_thread_t::get_poller ()
Martin Sustrik's avatar
Martin Sustrik committed
93
{
Martin Sustrik's avatar
Martin Sustrik committed
94
    zmq_assert (poller);
Martin Sustrik's avatar
Martin Sustrik committed
95 96 97
    return poller;
}

Martin Sustrik's avatar
Martin Sustrik committed
98
void zmq::io_thread_t::process_stop ()
Martin Sustrik's avatar
Martin Sustrik committed
99
{
100
    poller->rm_fd (mailbox_handle);
Martin Sustrik's avatar
Martin Sustrik committed
101 102
    poller->stop ();
}