artdaq  v3_01_00
Timeout.hh
1 #ifndef TIMEOUT_HH
2 #define TIMEOUT_HH
3 /* This file (Timeout.h) was created by Ron Rechenmacher <ron@fnal.gov> on
4  Sep 28, 2009. "TERMS AND CONDITIONS" governing this file are in the README
5  or COPYING file. If you do not have such a file, one can be obtained by
6  contacting Ron or Fermi Lab in Batavia IL, 60510, phone: 630-840-3000.
7  $RCSfile: Timeout.h,v $
8  rev="$Revision: 1.9 $$Date: 2016/10/12 07:11:55 $";
9  */
10 #include <time.h> // struct timespec
11 #include <list>
12 #include <functional> // std::function
13 #include <string>
14 #include <mutex>
15 #include <vector>
16 #include <map>
17 #include <unordered_map>
18 
22 class Timeout
23 {
24 public:
28  struct timeoutspec
29  {
30 #if 0
31  timeoutspec();
32  timeoutspec( const timeoutspec & other );
33  Timeout::timeoutspec & operator=( const Timeout::timeoutspec & other );
34 #endif
35  std::string desc;
36  void* tag;
37  std::function<void()> function;
38  uint64_t tmo_tod_us;
39  uint64_t period_us;
41  int check;
42  };
43 
48  explicit Timeout(int max_tmos = 100);
49 
60  void add_periodic(const char* desc, void* tag, std::function<void()>& function
61  , uint64_t period_us
62  , uint64_t start_us = 0);
63 
73  void add_periodic(const char* desc, void* tag, std::function<void()>& function
74  , int rel_ms);
75 
84  void add_periodic(const char* desc
85  , uint64_t period_us
86  , uint64_t start_us = 0);
87 
97  void add_relative(const char* desc, void* tag, std::function<void()>& function
98  , int rel_ms);
99 
107  void add_relative(std::string desc, int rel_ms);
108 
115  void copy_in_timeout(const char* desc, uint64_t period_us, uint64_t start_us = 0);
116 
117  // maybe need to be able to cancel by id,and/or desc and/or Client/funptr
124  bool cancel_timeout(void* tag, std::string desc);
125 
134  int get_next_expired_timeout(std::string& desc, void** tag, std::function<void()>& function
135  , uint64_t* tmo_tod_us);
136 
141  void get_next_timeout_delay(int64_t* delay_us);
142 
148 
153  bool is_consistent();
154 
158  void list_active_time();
159 
160 private:
161 
162  std::mutex lock_mutex_;
163  std::vector<timeoutspec> tmospecs_;
164  std::list<size_t> free_; // list of tmospecs indexes
165  std::multimap<uint64_t, size_t> active_time_;
166  std::unordered_multimap<std::string, size_t> active_desc_;
167 
168  void timeoutlist_init();
169 
170  int tmo_is_before_ts(timeoutspec& tmo
171  , const timespec& ts);
172 
173  int get_clear_next_expired_timeout(timeoutspec& tmo
174  , uint64_t tod_now_us);
175 
176  void copy_in_timeout(timeoutspec& tmo);
177 };
178 
179 #endif // TIMEOUT_HH
void copy_in_timeout(const char *desc, uint64_t period_us, uint64_t start_us=0)
Add a timeout with the given parameters.
Definition: Timeout.cc:293
void add_relative(const char *desc, void *tag, std::function< void()> &function, int rel_ms)
Add a periodic timeout to the Timeout container.
Definition: Timeout.cc:112
void * tag
could be file descriptor (fd)
Definition: Timeout.hh:36
uint64_t tmo_tod_us
When the function should be executed (gettimeofday, microseconds)
Definition: Timeout.hh:38
The Timeout class performs registered actions at specified intervals.
Definition: Timeout.hh:22
int get_next_timeout_msdly()
Get the amount to wait for the next timeout to occur.
Definition: Timeout.cc:184
int check
Check the timeoutspec.
Definition: Timeout.hh:41
int missed_periods
Number of periods that passed while the function was executing.
Definition: Timeout.hh:40
void get_next_timeout_delay(int64_t *delay_us)
Get the amount to wait for the next timeout to occur.
Definition: Timeout.cc:163
uint64_t period_us
0 if not periodic
Definition: Timeout.hh:39
Specification for a Timeout function.
Definition: Timeout.hh:28
int get_next_expired_timeout(std::string &desc, void **tag, std::function< void()> &function, uint64_t *tmo_tod_us)
Get a timeout that has expired.
Definition: Timeout.cc:140
bool is_consistent()
Run a consistency check on all confiugured timeouts.
Definition: Timeout.cc:202
std::string desc
Description of the Timeout function.
Definition: Timeout.hh:35
bool cancel_timeout(void *tag, std::string desc)
Cancel the timeout having the given description and tag.
Definition: Timeout.cc:323
void add_periodic(const char *desc, void *tag, std::function< void()> &function, uint64_t period_us, uint64_t start_us=0)
Add a periodic timeout to the Timeout container.
Definition: Timeout.cc:67
Timeout(int max_tmos=100)
Construct a Timeout object.
Definition: Timeout.cc:58
void list_active_time()
TRACE all active timeouts.
Definition: Timeout.cc:362