poll.hpp 3.3 KB

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