poll.cpp 4.56 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-2011 250bpm s.r.o.
3
    Copyright (c) 2007-2009 iMatix Corporation
4
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
5 6 7 8

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
9
    the terms of the GNU Lesser General Public License as published by
Martin Sustrik's avatar
Martin Sustrik committed
10 11 12 13 14 15
    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
16
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
17

18
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
19 20 21
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

22 23
#include "poll.hpp"
#if defined ZMQ_USE_POLL
Martin Sustrik's avatar
Martin Sustrik committed
24 25 26 27 28 29 30 31 32 33 34

#include <sys/types.h>
#include <sys/time.h>
#include <poll.h>
#include <algorithm>

#include "poll.hpp"
#include "err.hpp"
#include "config.hpp"
#include "i_poll_events.hpp"

Martin Sustrik's avatar
Martin Sustrik committed
35
zmq::poll_t::poll_t () :
Martin Sustrik's avatar
Martin Sustrik committed
36 37 38 39 40
    retired (false),
    stopping (false)
{
}

41 42
zmq::poll_t::~poll_t ()
{
43
    worker.stop ();
44 45
}

46
zmq::poll_t::handle_t zmq::poll_t::add_fd (fd_t fd_, i_poll_events *events_)
Martin Sustrik's avatar
Martin Sustrik committed
47
{
48 49 50 51 52 53 54 55 56 57
    //  If the file descriptor table is too small expand it.
    fd_table_t::size_type sz = fd_table.size ();
    if (sz <= (fd_table_t::size_type) fd_) {
        fd_table.resize (fd_ + 1);
        while (sz != (fd_table_t::size_type) (fd_ + 1)) {
            fd_table [sz].index = retired_fd;
            ++sz;
        }
    }

Martin Sustrik's avatar
Martin Sustrik committed
58 59
    pollfd pfd = {fd_, 0, 0};
    pollset.push_back (pfd);
60
    zmq_assert (fd_table [fd_].index == retired_fd);
Martin Sustrik's avatar
Martin Sustrik committed
61 62 63 64 65

    fd_table [fd_].index = pollset.size() - 1;
    fd_table [fd_].events = events_;

    //  Increase the load metric of the thread.
66
    adjust_load (1);
Martin Sustrik's avatar
Martin Sustrik committed
67

68
    return fd_;
Martin Sustrik's avatar
Martin Sustrik committed
69 70
}

Martin Sustrik's avatar
Martin Sustrik committed
71
void zmq::poll_t::rm_fd (handle_t handle_)
Martin Sustrik's avatar
Martin Sustrik committed
72
{
73
    fd_t index = fd_table [handle_].index;
74
    zmq_assert (index != retired_fd);
Martin Sustrik's avatar
Martin Sustrik committed
75 76 77

    //  Mark the fd as unused.
    pollset [index].fd = retired_fd;
78
    fd_table [handle_].index = retired_fd;
Martin Sustrik's avatar
Martin Sustrik committed
79 80 81
    retired = true;

    //  Decrease the load metric of the thread.
82
    adjust_load (-1);
Martin Sustrik's avatar
Martin Sustrik committed
83 84
}

Martin Sustrik's avatar
Martin Sustrik committed
85
void zmq::poll_t::set_pollin (handle_t handle_)
Martin Sustrik's avatar
Martin Sustrik committed
86
{
87
    int index = fd_table [handle_].index;
Martin Sustrik's avatar
Martin Sustrik committed
88 89 90
    pollset [index].events |= POLLIN;
}

Martin Sustrik's avatar
Martin Sustrik committed
91
void zmq::poll_t::reset_pollin (handle_t handle_)
Martin Sustrik's avatar
Martin Sustrik committed
92
{
93
    int index = fd_table [handle_].index;
Martin Sustrik's avatar
Martin Sustrik committed
94 95 96
    pollset [index].events &= ~((short) POLLIN);
}

Martin Sustrik's avatar
Martin Sustrik committed
97
void zmq::poll_t::set_pollout (handle_t handle_)
Martin Sustrik's avatar
Martin Sustrik committed
98
{
99
    int index = fd_table [handle_].index;
Martin Sustrik's avatar
Martin Sustrik committed
100 101 102
    pollset [index].events |= POLLOUT;
}

Martin Sustrik's avatar
Martin Sustrik committed
103
void zmq::poll_t::reset_pollout (handle_t handle_)
Martin Sustrik's avatar
Martin Sustrik committed
104
{
105
    int index = fd_table [handle_].index;
Martin Sustrik's avatar
Martin Sustrik committed
106 107 108
    pollset [index].events &= ~((short) POLLOUT);
}

Martin Sustrik's avatar
Martin Sustrik committed
109
void zmq::poll_t::start ()
Martin Sustrik's avatar
Martin Sustrik committed
110 111 112 113
{
    worker.start (worker_routine, this);
}

Martin Sustrik's avatar
Martin Sustrik committed
114
void zmq::poll_t::stop ()
Martin Sustrik's avatar
Martin Sustrik committed
115 116 117 118
{
    stopping = true;
}

Martin Sustrik's avatar
Martin Sustrik committed
119
void zmq::poll_t::loop ()
Martin Sustrik's avatar
Martin Sustrik committed
120 121 122
{
    while (!stopping) {

123
        //  Execute any due timers.
124
        int timeout = (int) execute_timers ();
125

Martin Sustrik's avatar
Martin Sustrik committed
126
        //  Wait for events.
127
        int rc = poll (&pollset [0], pollset.size (), timeout ? timeout : -1);
Martin Sustrik's avatar
Martin Sustrik committed
128 129 130 131 132
        if (rc == -1 && errno == EINTR)
            continue;
        errno_assert (rc != -1);


133 134 135
        //  If there are no events (i.e. it's a timeout) there's no point
        //  in checking the pollset.
        if (rc == 0)
Martin Sustrik's avatar
Martin Sustrik committed
136 137
            continue;

138
        for (pollset_t::size_type i = 0; i != pollset.size (); i++) {
Martin Sustrik's avatar
Martin Sustrik committed
139

140 141
            zmq_assert (!(pollset [i].revents & POLLNVAL));
            if (pollset [i].fd == retired_fd)
Martin Sustrik's avatar
Martin Sustrik committed
142
               continue;
143 144 145
            if (pollset [i].revents & (POLLERR | POLLHUP))
                fd_table [pollset [i].fd].events->in_event ();
            if (pollset [i].fd == retired_fd)
Martin Sustrik's avatar
Martin Sustrik committed
146
               continue;
147 148 149
            if (pollset [i].revents & POLLOUT)
                fd_table [pollset [i].fd].events->out_event ();
            if (pollset [i].fd == retired_fd)
Martin Sustrik's avatar
Martin Sustrik committed
150
               continue;
151 152
            if (pollset [i].revents & POLLIN)
                fd_table [pollset [i].fd].events->in_event ();
Martin Sustrik's avatar
Martin Sustrik committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        }

        //  Clean up the pollset and update the fd_table accordingly.
        if (retired) {
            pollset_t::size_type i = 0;
            while (i < pollset.size ()) {
                if (pollset [i].fd == retired_fd)
                    pollset.erase (pollset.begin () + i);
                else {
                    fd_table [pollset [i].fd].index = i;
                    i ++;
                }
            }
            retired = false;
        }
    }
}

Martin Sustrik's avatar
Martin Sustrik committed
171
void zmq::poll_t::worker_routine (void *arg_)
Martin Sustrik's avatar
Martin Sustrik committed
172 173 174 175 176
{
    ((poll_t*) arg_)->loop ();
}

#endif