Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
platform_timer_asio_fallback.cpp
Go to the documentation of this file.
3
4#include <fc/fwd_impl.hpp>
5#include <fc/log/logger_config.hpp> //set_os_thread_name()
6
7#include <boost/asio.hpp>
8
9#include <mutex>
10#include <thread>
11
12namespace sysio { namespace chain {
13
14//a thread is shared for all instances
15static std::mutex timer_ref_mutex;
16static unsigned refcount;
17static std::thread checktime_thread;
18static std::unique_ptr<boost::asio::io_service> checktime_ios;
19
20struct platform_timer::impl {
21 std::unique_ptr<boost::asio::high_resolution_timer> timer;
22};
23
25 static_assert(sizeof(impl) <= fwd_size);
26
27 std::lock_guard guard(timer_ref_mutex);
28
29 if(refcount++ == 0) {
30 std::promise<void> p;
31 checktime_thread = std::thread([&p]() {
32 fc::set_os_thread_name("checktime");
33 checktime_ios = std::make_unique<boost::asio::io_service>();
34 boost::asio::io_service::work work(*checktime_ios);
35 p.set_value();
36
37 checktime_ios->run();
38 });
39 p.get_future().get();
40 }
41
42 my->timer = std::make_unique<boost::asio::high_resolution_timer>(*checktime_ios);
43
44 //compute_and_print_timer_accuracy(*this);
45}
46
48 stop();
49 if(std::lock_guard guard(timer_ref_mutex); --refcount == 0) {
50 checktime_ios->stop();
51 checktime_thread.join();
52 checktime_ios.reset();
53 }
54}
55
57 if(tp == fc::time_point::maximum()) {
58 expired = 0;
59 return;
60 }
62 if(x.count() <= 0)
63 expired = 1;
64 else {
65#if 0
66 std::promise<void> p;
67 checktime_ios->post([&p,this]() {
68 expired = 0;
69 p.set_value();
70 });
71 p.get_future().get();
72#endif
73 expired = 0;
74 my->timer->expires_after(std::chrono::microseconds(x.count()));
75 my->timer->async_wait([this](const boost::system::error_code& ec) {
76 if(ec)
77 return;
78 expired = 1;
79 call_expiration_callback();
80 });
81 }
82}
83
85 if(expired)
86 return;
87
88 my->timer->cancel();
89 expired = 1;
90}
91
92}}
const mie::Vuint & p
Definition bn.cpp:27
constexpr int64_t count() const
Definition time.hpp:26
static time_point now()
Definition time.cpp:14
constexpr const microseconds & time_since_epoch() const
Definition time.hpp:52
static constexpr time_point maximum()
Definition time.hpp:46
void set_os_thread_name(const string &name)
std::unique_ptr< boost::asio::high_resolution_timer > timer