io_thread.cpp 2.32 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2010 iMatix Corporation
Martin Sustrik's avatar
Martin Sustrik committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the Lesser GNU General Public License as published by
    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
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

20 21
#include <new>

22
#include "../include/zmq.h"
Martin Sustrik's avatar
Martin Sustrik committed
23 24 25 26

#include "io_thread.hpp"
#include "platform.hpp"
#include "err.hpp"
27
#include "ctx.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
28

29
zmq::io_thread_t::io_thread_t (ctx_t *ctx_,
30
      uint32_t thread_slot_) :
31
    object_t (ctx_, thread_slot_)
Martin Sustrik's avatar
Martin Sustrik committed
32
{
33
    poller = new (std::nothrow) poller_t;
Martin Sustrik's avatar
Martin Sustrik committed
34
    zmq_assert (poller);
Martin Sustrik's avatar
Martin Sustrik committed
35 36 37 38 39

    signaler_handle = poller->add_fd (signaler.get_fd (), this);
    poller->set_pollin (signaler_handle);
}

Martin Sustrik's avatar
Martin Sustrik committed
40
zmq::io_thread_t::~io_thread_t ()
Martin Sustrik's avatar
Martin Sustrik committed
41 42 43 44
{
    delete poller;
}

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

Martin Sustrik's avatar
Martin Sustrik committed
51
void zmq::io_thread_t::stop ()
Martin Sustrik's avatar
Martin Sustrik committed
52 53 54 55
{
    send_stop ();
}

56
zmq::signaler_t *zmq::io_thread_t::get_signaler ()
Martin Sustrik's avatar
Martin Sustrik committed
57 58 59 60
{
    return &signaler;
}

Martin Sustrik's avatar
Martin Sustrik committed
61
int zmq::io_thread_t::get_load ()
Martin Sustrik's avatar
Martin Sustrik committed
62 63 64 65
{
    return poller->get_load ();
}

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

71 72
    while (true) {

73 74 75
        //  Get the next command. If there is none, exit.
        command_t cmd;
        if (!signaler.recv (&cmd, false))
76 77
            break;

78 79
        //  Process the command.
        cmd.destination->process_command (cmd);
Martin Sustrik's avatar
Martin Sustrik committed
80 81 82
    }
}

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

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

95
zmq::poller_t *zmq::io_thread_t::get_poller ()
Martin Sustrik's avatar
Martin Sustrik committed
96
{
Martin Sustrik's avatar
Martin Sustrik committed
97
    zmq_assert (poller);
Martin Sustrik's avatar
Martin Sustrik committed
98 99 100
    return poller;
}

Martin Sustrik's avatar
Martin Sustrik committed
101
void zmq::io_thread_t::process_stop ()
Martin Sustrik's avatar
Martin Sustrik committed
102 103 104 105
{
    poller->rm_fd (signaler_handle);
    poller->stop ();
}