libzmq/src/io_thread.cpp

109 lines
2.4 KiB
C++
Raw Normal View History

2009-07-29 12:07:54 +02:00
/*
Copyright (c) 2007-2010 iMatix Corporation
2009-07-29 12:07:54 +02:00
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
2009-07-29 12:07:54 +02:00
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
GNU Lesser General Public License for more details.
2009-07-29 12:07:54 +02:00
You should have received a copy of the GNU Lesser General Public License
2009-07-29 12:07:54 +02:00
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2009-12-15 23:49:55 +01:00
#include <new>
#include "../include/zmq.h"
2009-07-29 12:07:54 +02:00
#include "io_thread.hpp"
#include "platform.hpp"
#include "err.hpp"
2010-05-05 14:24:54 +02:00
#include "ctx.hpp"
2009-07-29 12:07:54 +02:00
zmq::io_thread_t::io_thread_t (ctx_t *ctx_, uint32_t tid_) :
object_t (ctx_, tid_)
2009-07-29 12:07:54 +02:00
{
2009-12-15 23:49:55 +01:00
poller = new (std::nothrow) poller_t;
2009-08-03 11:30:13 +02:00
zmq_assert (poller);
2009-07-29 12:07:54 +02:00
signaler_handle = poller->add_fd (signaler.get_fd (), this);
poller->set_pollin (signaler_handle);
}
2009-08-03 11:30:13 +02:00
zmq::io_thread_t::~io_thread_t ()
2009-07-29 12:07:54 +02:00
{
delete poller;
}
2009-08-03 11:30:13 +02:00
void zmq::io_thread_t::start ()
2009-07-29 12:07:54 +02:00
{
// Start the underlying I/O thread.
poller->start ();
}
2009-08-03 11:30:13 +02:00
void zmq::io_thread_t::stop ()
2009-07-29 12:07:54 +02:00
{
send_stop ();
}
2010-04-29 17:31:57 +02:00
zmq::signaler_t *zmq::io_thread_t::get_signaler ()
2009-07-29 12:07:54 +02:00
{
return &signaler;
}
2009-08-03 11:30:13 +02:00
int zmq::io_thread_t::get_load ()
2009-07-29 12:07:54 +02:00
{
return poller->get_load ();
}
2009-08-03 11:30:13 +02:00
void zmq::io_thread_t::in_event ()
2009-07-29 12:07:54 +02:00
{
// TODO: Do we want to limit number of commands I/O thread can
// process in a single go?
while (true) {
// Get the next command. If there is none, exit.
command_t cmd;
int rc = signaler.recv (&cmd, false);
if (rc != 0 && errno == EINTR)
continue;
if (rc != 0 && errno == EAGAIN)
break;
errno_assert (rc == 0);
// Process the command.
cmd.destination->process_command (cmd);
2009-07-29 12:07:54 +02:00
}
}
2009-08-03 11:30:13 +02:00
void zmq::io_thread_t::out_event ()
2009-07-29 12:07:54 +02:00
{
// We are never polling for POLLOUT here. This function is never called.
2009-08-03 11:30:13 +02:00
zmq_assert (false);
2009-07-29 12:07:54 +02:00
}
void zmq::io_thread_t::timer_event (int id_)
2009-07-29 12:07:54 +02:00
{
// No timers here. This function is never called.
2009-08-03 11:30:13 +02:00
zmq_assert (false);
2009-07-29 12:07:54 +02:00
}
zmq::poller_t *zmq::io_thread_t::get_poller ()
2009-07-29 12:07:54 +02:00
{
2009-08-03 11:30:13 +02:00
zmq_assert (poller);
2009-07-29 12:07:54 +02:00
return poller;
}
2009-08-03 11:30:13 +02:00
void zmq::io_thread_t::process_stop ()
2009-07-29 12:07:54 +02:00
{
poller->rm_fd (signaler_handle);
poller->stop ();
}