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 <condition_variable>
9 
10 #include <rxcpp/rx.hpp>
11 
12 namespace iroha {
13  namespace schedulers {
14 
15  inline void handleEvents(rxcpp::composite_subscription &subscription,
16  rxcpp::schedulers::run_loop &run_loop) {
17  std::condition_variable wait_cv;
18 
19  run_loop.set_notify_earlier_wakeup(
20  [&wait_cv](const auto &) { wait_cv.notify_one(); });
21 
22  std::mutex wait_mutex;
23  std::unique_lock<std::mutex> lock(wait_mutex);
24  while (subscription.is_subscribed() or not run_loop.empty()) {
25  while (not run_loop.empty()
26  and run_loop.peek().when <= run_loop.now()) {
27  run_loop.dispatch();
28  }
29 
30  if (run_loop.empty()) {
31  wait_cv.wait(lock, [&run_loop, &subscription]() {
32  return not subscription.is_subscribed() or not run_loop.empty();
33  });
34  } else {
35  wait_cv.wait_until(lock, run_loop.peek().when);
36  }
37  }
38  }
39  } // namespace schedulers
40 } // namespace iroha
41 
42 #endif // IROHA_RUN_LOOP_HANDLER_HPP
Definition: block_query.hpp:14
void handleEvents(rxcpp::composite_subscription &subscription, rxcpp::schedulers::run_loop &run_loop)
Definition: run_loop_handler.hpp:15