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


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57