hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
run_loop_handler.hpp
Go to the documentation of this file.
1 
5 #ifndef IROHA_RUN_LOOP_HANDLER_HPP
6 #define IROHA_RUN_LOOP_HANDLER_HPP
7 
8 #include <ciso646>
9 #include <condition_variable>
10 
11 #include <rxcpp/rx-lite.hpp>
12 
13 namespace iroha {
14  namespace schedulers {
15 
16  inline void handleEvents(rxcpp::composite_subscription &subscription,
17  rxcpp::schedulers::run_loop &run_loop) {
18  std::condition_variable wait_cv;
19 
20  run_loop.set_notify_earlier_wakeup(
21  [&wait_cv](const auto &) { wait_cv.notify_one(); });
22 
23  std::mutex wait_mutex;
24  std::unique_lock<std::mutex> lock(wait_mutex);
25  while (subscription.is_subscribed() or not run_loop.empty()) {
26  while (not run_loop.empty()
27  and run_loop.peek().when <= run_loop.now()) {
28  run_loop.dispatch();
29  }
30 
31  if (run_loop.empty()) {
32  wait_cv.wait(lock, [&run_loop, &subscription]() {
33  return not subscription.is_subscribed() or not run_loop.empty();
34  });
35  } else {
36  wait_cv.wait_until(lock, run_loop.peek().when);
37  }
38  }
39  }
40  } // namespace schedulers
41 } // namespace iroha
42 
43 #endif // IROHA_RUN_LOOP_HANDLER_HPP
Definition: block_query.hpp:15
void handleEvents(rxcpp::composite_subscription &subscription, rxcpp::schedulers::run_loop &run_loop)
Definition: run_loop_handler.hpp:16