poller_base.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "precompiled.hpp"
4 #include "poller_base.hpp"
5 #include "i_poll_events.hpp"
6 #include "err.hpp"
7 
9 {
10  // Make sure there is no more load on the shutdown.
11  zmq_assert (get_load () == 0);
12 }
13 
15 {
16  return _load.get ();
17 }
18 
20 {
21  if (amount_ > 0)
22  _load.add (amount_);
23  else if (amount_ < 0)
24  _load.sub (-amount_);
25 }
26 
27 void zmq::poller_base_t::add_timer (int timeout_, i_poll_events *sink_, int id_)
28 {
29  uint64_t expiration = _clock.now_ms () + timeout_;
30  timer_info_t info = {sink_, id_};
31  _timers.insert (timers_t::value_type (expiration, info));
32 }
33 
35 {
36  // Complexity of this operation is O(n). We assume it is rarely used.
37  for (timers_t::iterator it = _timers.begin (), end = _timers.end ();
38  it != end; ++it)
39  if (it->second.sink == sink_ && it->second.id == id_) {
40  _timers.erase (it);
41  return;
42  }
43 
44  // We should generally never get here. Calling 'cancel_timer ()' on
45  // an already expired or canceled timer (or even worse - on a timer which
46  // never existed, supplying bad sink_ and/or id_ values) does not make any
47  // sense.
48  // But in some edge cases this might happen. As described in issue #3645
49  // `timer_event ()` call from `execute_timers ()` might call `cancel_timer ()`
50  // on already canceled (deleted) timer.
51  // As soon as that is resolved an 'assert (false)' should be put here.
52 }
53 
55 {
56  // Fast track.
57  if (_timers.empty ())
58  return 0;
59 
60  // Get the current time.
61  const uint64_t current = _clock.now_ms ();
62 
63  // Execute the timers that are already due.
64  uint64_t res = 0;
65  timer_info_t timer_temp;
66  timers_t::iterator it;
67 
68  do {
69  it = _timers.begin ();
70 
71  // If we have to wait to execute the item, same will be true for
72  // all the following items because multimap is sorted. Thus we can
73  // stop checking the subsequent timers.
74  if (it->first > current) {
75  res = it->first - current;
76  break;
77  }
78 
79  // Save and remove the timer because timer_event() call might delete
80  // exactly this timer and then the iterator will be invalid.
81  timer_temp = it->second;
82  _timers.erase (it);
83 
84  // Trigger the timer.
85  timer_temp.sink->timer_event (timer_temp.id);
86 
87  } while (!_timers.empty ());
88 
89  // Return the time to wait for the next timer (at least 1ms), or 0, if
90  // there are no more timers.
91  return res;
92 }
93 
95  _ctx (ctx_)
96 {
97 }
98 
100 {
101  _worker.stop ();
102 }
103 
105 {
106  zmq_assert (get_load () > 0);
107  _ctx.start_thread (_worker, worker_routine, this, name_);
108 }
109 
111 {
112 #ifndef NDEBUG
113  zmq_assert (!_worker.get_started () || _worker.is_current_thread ());
114 #endif
115 }
116 
118 {
119  (static_cast<worker_poller_base_t *> (arg_))->loop ();
120 }
i_poll_events.hpp
end
GLuint GLuint end
Definition: glcorearb.h:2858
precompiled.hpp
zmq_assert
#define zmq_assert(x)
Definition: err.hpp:102
zmq::worker_poller_base_t::worker_poller_base_t
worker_poller_base_t(const thread_ctx_t &ctx_)
Definition: poller_base.cpp:94
zmq::i_poll_events
Definition: i_poll_events.hpp:13
zmq::poller_base_t::timer_info_t::id
int id
Definition: poller_base.hpp:122
zmq::poller_base_t::adjust_load
void adjust_load(int amount_)
Definition: poller_base.cpp:19
zmq::worker_poller_base_t
Definition: poller_base.hpp:135
zmq::i_poll_events::timer_event
virtual void timer_event(int id_)=0
name_
string name_
Definition: googletest.cc:182
zmq::poller_base_t::timer_info_t
Definition: poller_base.hpp:119
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::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::worker_poller_base_t::check_thread
void check_thread() const
Definition: poller_base.cpp:110
value_type
zend_class_entry * value_type
Definition: php/ext/google/protobuf/message.c:2546
zmq::worker_poller_base_t::start
void start(const char *name=NULL)
Definition: poller_base.cpp:104
err.hpp
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
it
MapIter it
Definition: php/ext/google/protobuf/map.c:205
zmq::poller_base_t::add_timer
void add_timer(int timeout_, zmq::i_poll_events *sink_, int id_)
Definition: poller_base.cpp:27
poller_base.hpp


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