poller_base.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_POLLER_BASE_HPP_INCLUDED__
  25. #define __ZMQ_POLLER_BASE_HPP_INCLUDED__
  26. #include <map>
  27. #include "clock.hpp"
  28. #include "atomic_counter.hpp"
  29. #include "ctx.hpp"
  30. namespace zmq
  31. {
  32. struct i_poll_events;
  33. // A build of libzmq must provide an implementation of the poller_t concept. By
  34. // convention, this is done via a typedef.
  35. //
  36. // At the time of writing, the following implementations of the poller_t
  37. // concept exist: zmq::devpoll_t, zmq::epoll_t, zmq::kqueue_t, zmq::poll_t,
  38. // zmq::pollset_t, zmq::select_t
  39. //
  40. // An implementation of the poller_t concept must provide the following public
  41. // methods:
  42. // Returns load of the poller.
  43. // int get_load() const;
  44. //
  45. // Add a timeout to expire in timeout_ milliseconds. After the
  46. // expiration, timer_event on sink_ object will be called with
  47. // argument set to id_.
  48. // void add_timer(int timeout_, zmq::i_poll_events *sink_, int id_);
  49. //
  50. // Cancel the timer created by sink_ object with ID equal to id_.
  51. // void cancel_timer(zmq::i_poll_events *sink_, int id_);
  52. //
  53. // Adds a fd to the poller. Initially, no events are activated. These must
  54. // be activated by the set_* methods using the returned handle_.
  55. // handle_t add_fd(fd_t fd_, zmq::i_poll_events *events_);
  56. //
  57. // Deactivates any events that may be active for the given handle_, and
  58. // removes the fd associated with the given handle_.
  59. // void rm_fd(handle_t handle_);
  60. //
  61. // The set_* and reset_* methods activate resp. deactivate polling for
  62. // input/output readiness on the respective handle_, such that the
  63. // in_event/out_event methods on the associated zmq::i_poll_events object
  64. // will be called.
  65. // Note: while handle_t and fd_t may be the same type, and may even have the
  66. // same values for some implementation, this may not be assumed in general.
  67. // The methods may only be called with the handle returned by add_fd.
  68. // void set_pollin(handle_t handle_);
  69. // void reset_pollin(handle_t handle_);
  70. // void set_pollout(handle_t handle_);//
  71. // void reset_pollout(handle_t handle_);
  72. //
  73. // Starts operation of the poller. See below for details.
  74. // void start();
  75. //
  76. // Request termination of the poller.
  77. // TODO: might be removed in the future, as it has no effect.
  78. // void stop();
  79. //
  80. // Returns the maximum number of fds that can be added to an instance of the
  81. // poller at the same time, or -1 if there is no such fixed limit.
  82. // static int max_fds();
  83. //
  84. // Most of the methods may only be called from a zmq::i_poll_events callback
  85. // function when invoked by the poller (and, therefore, typically from the
  86. // poller's worker thread), with the following exceptions:
  87. // - get_load may be called from outside
  88. // - add_fd and add_timer may be called from outside before start
  89. // - start may be called from outside once
  90. //
  91. // After a poller is started, it waits for the registered events (input/output
  92. // readiness, timeout) to happen, and calls the respective functions on the
  93. // zmq::i_poll_events object. It terminates when no further registrations (fds
  94. // or timers) exist.
  95. //
  96. // Before start, add_fd must have been called at least once. Behavior may be
  97. // undefined otherwise.
  98. //
  99. // If the poller is implemented by a single worker thread (the
  100. // worker_poller_base_t base class may be used to implement such a poller),
  101. // no synchronization is required for the data structures modified by
  102. // add_fd, rm_fd, add_timer, cancel_timer, (re)set_poll(in|out). However,
  103. // reentrancy must be considered, e.g. when one of the functions modifies
  104. // a container that is being iterated by the poller.
  105. // A class that can be used as abase class for implementations of the poller
  106. // concept.
  107. //
  108. // For documentation of the public methods, see the description of the poller_t
  109. // concept.
  110. class poller_base_t
  111. {
  112. public:
  113. poller_base_t () ZMQ_DEFAULT;
  114. virtual ~poller_base_t ();
  115. // Methods from the poller concept.
  116. int get_load () const;
  117. void add_timer (int timeout_, zmq::i_poll_events *sink_, int id_);
  118. void cancel_timer (zmq::i_poll_events *sink_, int id_);
  119. protected:
  120. // Called by individual poller implementations to manage the load.
  121. void adjust_load (int amount_);
  122. // Executes any timers that are due. Returns number of milliseconds
  123. // to wait to match the next timer or 0 meaning "no timers".
  124. uint64_t execute_timers ();
  125. private:
  126. // Clock instance private to this I/O thread.
  127. clock_t _clock;
  128. // List of active timers.
  129. struct timer_info_t
  130. {
  131. zmq::i_poll_events *sink;
  132. int id;
  133. };
  134. typedef std::multimap<uint64_t, timer_info_t> timers_t;
  135. timers_t _timers;
  136. // Load of the poller. Currently the number of file descriptors
  137. // registered.
  138. atomic_counter_t _load;
  139. ZMQ_NON_COPYABLE_NOR_MOVABLE (poller_base_t)
  140. };
  141. // Base class for a poller with a single worker thread.
  142. class worker_poller_base_t : public poller_base_t
  143. {
  144. public:
  145. worker_poller_base_t (const thread_ctx_t &ctx_);
  146. // Methods from the poller concept.
  147. void start (const char *name = NULL);
  148. protected:
  149. // Checks whether the currently executing thread is the worker thread
  150. // via an assertion.
  151. // Should be called by the add_fd, removed_fd, set_*, reset_* functions
  152. // to ensure correct usage.
  153. void check_thread () const;
  154. // Stops the worker thread. Should be called from the destructor of the
  155. // leaf class.
  156. void stop_worker ();
  157. private:
  158. // Main worker thread routine.
  159. static void worker_routine (void *arg_);
  160. virtual void loop () = 0;
  161. // Reference to ZMQ context.
  162. const thread_ctx_t &_ctx;
  163. // Handle of the physical thread doing the I/O work.
  164. thread_t _worker;
  165. };
  166. }
  167. #endif