00001 #ifndef TIMEOUT_HH
00002 #define TIMEOUT_HH
00003
00004
00005
00006
00007
00008
00009
00010 #include <time.h>
00011 #include <list>
00012 #include <functional>
00013 #include <string>
00014 #include <mutex>
00015 #include <vector>
00016 #include <map>
00017 #include <unordered_map>
00018
00022 class Timeout
00023 {
00024 public:
00028 struct timeoutspec
00029 {
00030 #if 0
00031 timeoutspec();
00032 timeoutspec( const timeoutspec & other );
00033 Timeout::timeoutspec & operator=( const Timeout::timeoutspec & other );
00034 #endif
00035 std::string desc;
00036 void* tag;
00037 std::function<void()> function;
00038 uint64_t tmo_tod_us;
00039 uint64_t period_us;
00040 int missed_periods;
00041 int check;
00042 };
00043
00048 explicit Timeout(int max_tmos = 100);
00049
00060 void add_periodic(const char* desc, void* tag, std::function<void()>& function
00061 , uint64_t period_us
00062 , uint64_t start_us = 0);
00063
00073 void add_periodic(const char* desc, void* tag, std::function<void()>& function
00074 , int rel_ms);
00075
00084 void add_periodic(const char* desc
00085 , uint64_t period_us
00086 , uint64_t start_us = 0);
00087
00097 void add_relative(const char* desc, void* tag, std::function<void()>& function
00098 , int rel_ms);
00099
00107 void add_relative(std::string desc, int rel_ms);
00108
00115 void copy_in_timeout(const char* desc, uint64_t period_us, uint64_t start_us = 0);
00116
00117
00124 bool cancel_timeout(void* tag, std::string desc);
00125
00134 int get_next_expired_timeout(std::string& desc, void** tag, std::function<void()>& function
00135 , uint64_t* tmo_tod_us);
00136
00141 void get_next_timeout_delay(int64_t* delay_us);
00142
00147 int get_next_timeout_msdly();
00148
00153 bool is_consistent();
00154
00158 void list_active_time();
00159
00160 private:
00161
00162 std::mutex lock_mutex_;
00163 std::vector<timeoutspec> tmospecs_;
00164 std::list<size_t> free_;
00165 std::multimap<uint64_t, size_t> active_time_;
00166 std::unordered_multimap<std::string, size_t> active_desc_;
00167
00168 void timeoutlist_init();
00169
00170 int tmo_is_before_ts(timeoutspec& tmo
00171 , const timespec& ts);
00172
00173 int get_clear_next_expired_timeout(timeoutspec& tmo
00174 , uint64_t tod_now_us);
00175
00176 void copy_in_timeout(timeoutspec& tmo);
00177 };
00178
00179 #endif // TIMEOUT_HH