Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
platform_timer.hpp
Go to the documentation of this file.
1#pragma once
2#include <fc/time.hpp>
3#include <fc/fwd.hpp>
4#include <fc/scoped_exit.hpp>
5
7
8#include <atomic>
9
10#include <signal.h>
11
12namespace sysio { namespace chain {
13
17
18 void start(fc::time_point tp);
19 void stop();
20
21 /* Sets a callback for when timer expires. Be aware this could might fire from a signal handling context and/or
22 on any particular thread. Only a single callback can be registered at once; trying to register more will
23 result in an exception. Setting to nullptr disables any current set callback */
24 void set_expiration_callback(void(*func)(void*), void* user) {
25 bool expect_false = false;
26 while(!atomic_compare_exchange_strong(&_callback_variables_busy, &expect_false, true))
27 expect_false = false;
28 auto reset_busy = fc::make_scoped_exit([this]() {
29 _callback_variables_busy.store(false, std::memory_order_release);
30 });
31 SYS_ASSERT(!(func && _expiration_callback), misc_exception, "Setting a platform_timer callback when one already exists");
32
33 _expiration_callback = func;
34 _expiration_callback_data = user;
35 }
36
37 std::atomic_bool expired = true;
38
39private:
40 struct impl;
41 constexpr static size_t fwd_size = 8;
43
44 void call_expiration_callback() {
45 bool expect_false = false;
46 if(atomic_compare_exchange_strong(&_callback_variables_busy, &expect_false, true)) {
47 void(*cb)(void*) = _expiration_callback;
48 void* cb_data = _expiration_callback_data;
49 if(cb) {
50 cb(cb_data);
51 }
52 _callback_variables_busy.store(false, std::memory_order_release);
53 }
54 }
55
56 std::atomic_bool _callback_variables_busy = false;
57 void(*_expiration_callback)(void*) = nullptr;
58 void* _expiration_callback_data;
59};
60
61}}
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
Used to forward declare value types.
Definition fwd.hpp:11
scoped_exit< Callback > make_scoped_exit(Callback &&c)
void set_expiration_callback(void(*func)(void *), void *user)