1/*
2 Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3
4 This file is part of libzmq, the ZeroMQ core engine in C++.
5
6 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#ifndef __ZMQ_POLL_HPP_INCLUDED__
31#define __ZMQ_POLL_HPP_INCLUDED__
32
33// poller.hpp decides which polling mechanism to use.
34#include "poller.hpp"
35#if defined ZMQ_IOTHREAD_POLLER_USE_POLL
36
37#if defined ZMQ_HAVE_WINDOWS
38#error \
39 "poll is broken on Windows for the purpose of the I/O thread poller, use select instead; "\
40 "see https://github.com/zeromq/libzmq/issues/3107"
41#endif
42
43#include <poll.h>
44#include <stddef.h>
45#include <vector>
46
47#include "ctx.hpp"
48#include "fd.hpp"
49#include "thread.hpp"
50#include "poller_base.hpp"
51
52namespace zmq
53{
54struct i_poll_events;
55
56// Implements socket polling mechanism using the POSIX.1-2001
57// poll() system call.
58
59class poll_t : public worker_poller_base_t
60{
61 public:
62 typedef fd_t handle_t;
63
64 poll_t (const thread_ctx_t &ctx_);
65 ~poll_t ();
66
67 // "poller" concept.
68 // These methods may only be called from an event callback; add_fd may also be called before start.
69 handle_t add_fd (fd_t fd_, zmq::i_poll_events *events_);
70 void rm_fd (handle_t handle_);
71 void set_pollin (handle_t handle_);
72 void reset_pollin (handle_t handle_);
73 void set_pollout (handle_t handle_);
74 void reset_pollout (handle_t handle_);
75 void stop ();
76
77 static int max_fds ();
78
79 private:
80 // Main event loop.
81 virtual void loop ();
82
83 void cleanup_retired ();
84
85 struct fd_entry_t
86 {
87 fd_t index;
88 zmq::i_poll_events *events;
89 };
90
91 // This table stores data for registered descriptors.
92 typedef std::vector<fd_entry_t> fd_table_t;
93 fd_table_t fd_table;
94
95 // Pollset to pass to the poll function.
96 typedef std::vector<pollfd> pollset_t;
97 pollset_t pollset;
98
99 // If true, there's at least one retired event source.
100 bool retired;
101
102 ZMQ_NON_COPYABLE_NOR_MOVABLE (poll_t)
103};
104
105typedef poll_t poller_t;
106}
107
108#endif
109
110#endif
111