Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
platform_timer_posix.cpp
Go to the documentation of this file.
3
4#include <fc/time.hpp>
5#include <fc/fwd_impl.hpp>
7
8#include <mutex>
9
10#include <signal.h>
11#include <time.h>
12
13namespace sysio { namespace chain {
14
15static_assert(std::atomic_bool::is_always_lock_free, "Only lock-free atomics AS-safe.");
16
18 timer_t timerid;
19
20 static void sig_handler(int, siginfo_t* si, void*) {
21 platform_timer* self = (platform_timer*)si->si_value.sival_ptr;
22 self->expired = 1;
23 self->call_expiration_callback();
24 }
25};
26
28 static_assert(sizeof(impl) <= fwd_size);
29
30 static bool initialized;
31 static std::mutex initalized_mutex;
32
33 if(std::lock_guard guard(initalized_mutex); !initialized) {
34 struct sigaction act;
35 sigemptyset(&act.sa_mask);
36 act.sa_sigaction = impl::sig_handler;
37 act.sa_flags = SA_SIGINFO | SA_RESTART;
38 FC_ASSERT(sigaction(SIGRTMIN, &act, NULL) == 0, "failed to aquire SIGRTMIN signal");
39 initialized = true;
40 }
41
42 struct sigevent se;
43 se.sigev_notify = SIGEV_SIGNAL;
44 se.sigev_signo = SIGRTMIN;
45 se.sigev_value.sival_ptr = (void*)this;
46
47 FC_ASSERT(timer_create(CLOCK_REALTIME, &se, &my->timerid) == 0, "failed to create timer");
48
50}
51
53 timer_delete(my->timerid);
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 time_t secs = x.count() / 1000000;
66 long nsec = (x.count() - (secs*1000000)) * 1000;
67 struct itimerspec enable = {{0, 0}, {secs, nsec}};
68 expired = 0;
69 if(timer_settime(my->timerid, 0, &enable, NULL) != 0)
70 expired = 1;
71 }
72}
73
75 if(expired)
76 return;
77 struct itimerspec disable = {{0, 0}, {0, 0}};
78 timer_settime(my->timerid, 0, &disable, NULL);
79 expired = 1;
80}
81
82}}
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
Defines exception's used by fc.
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
void compute_and_print_timer_accuracy(platform_timer &t)
@ self
the connection is to itself
Definition protocol.hpp:48
static void sig_handler(int, siginfo_t *si, void *)