io_thread.cpp 2.76 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 "../bindings/c/zmq.h"
Martin Sustrik's avatar
Martin Sustrik committed
23 24 25 26 27 28

#include "io_thread.hpp"
#include "command.hpp"
#include "platform.hpp"
#include "err.hpp"
#include "command.hpp"
29
#include "dispatcher.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
30 31
#include "simple_semaphore.hpp"

32 33
zmq::io_thread_t::io_thread_t (dispatcher_t *dispatcher_, int thread_slot_,
      int flags_) :
34
    object_t (dispatcher_, thread_slot_)
Martin Sustrik's avatar
Martin Sustrik committed
35
{
36
    poller = new (std::nothrow) poller_t;
Martin Sustrik's avatar
Martin Sustrik committed
37
    zmq_assert (poller);
Martin Sustrik's avatar
Martin Sustrik committed
38 39 40 41 42

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

Martin Sustrik's avatar
Martin Sustrik committed
43
zmq::io_thread_t::~io_thread_t ()
Martin Sustrik's avatar
Martin Sustrik committed
44 45 46 47
{
    delete poller;
}

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

Martin Sustrik's avatar
Martin Sustrik committed
54
void zmq::io_thread_t::stop ()
Martin Sustrik's avatar
Martin Sustrik committed
55 56 57 58
{
    send_stop ();
}

Martin Sustrik's avatar
Martin Sustrik committed
59
zmq::i_signaler *zmq::io_thread_t::get_signaler ()
Martin Sustrik's avatar
Martin Sustrik committed
60 61 62 63
{
    return &signaler;
}

Martin Sustrik's avatar
Martin Sustrik committed
64
int zmq::io_thread_t::get_load ()
Martin Sustrik's avatar
Martin Sustrik committed
65 66 67 68
{
    return poller->get_load ();
}

Martin Sustrik's avatar
Martin Sustrik committed
69
void zmq::io_thread_t::in_event ()
Martin Sustrik's avatar
Martin Sustrik committed
70 71
{
    //  Find out which threads are sending us commands.
72
    uint64_t signals = signaler.check ();
Martin Sustrik's avatar
Martin Sustrik committed
73
    zmq_assert (signals);
Martin Sustrik's avatar
Martin Sustrik committed
74 75 76 77 78 79

    //  Iterate through all the threads in the process and find out
    //  which of them sent us commands.
    int slot_count = thread_slot_count ();
    for (int source_thread_slot = 0;
          source_thread_slot != slot_count; source_thread_slot++) {
80
        if (signals & (uint64_t (1) << source_thread_slot)) {
Martin Sustrik's avatar
Martin Sustrik committed
81 82 83

            //  Read all the commands from particular thread.
            command_t cmd;
84
            while (dispatcher->read (source_thread_slot, thread_slot, &cmd))
Martin Sustrik's avatar
Martin Sustrik committed
85 86 87 88 89
                cmd.destination->process_command (cmd);
        }
    }
}

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

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

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

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