-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.h
53 lines (48 loc) · 1.07 KB
/
timer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef __M_TIMER_H
#define __M_TIMER_H
#include <vector>
#include <sys/time.h>
#include <log.h>
#include <algorithm>
#include <functional>
#include <map>
#include <iostream>
#include <mutex>
#include <thread>
struct TimerNode{
int fd;
time_t time;
time_t time_out;
std::function<void()> time_out_cb;
bool operator<(const TimerNode& t){
return time < t.time;
}
};
class TimerHeap{
private:
std::vector<TimerNode> *timer_heap;
std::map<int, time_t> *time_log;
std::mutex mtx;
public:
TimerHeap();
~TimerHeap();
template<class F>
void add(int fd, int time_out, F&& cb){
TimerNode tn;
tn.fd = fd;
tn.time = time(nullptr);
tn.time_out = time_out;
tn.time_out_cb = cb;
{
std::lock_guard<std::mutex> locker(mtx);
timer_heap->push_back(tn);
std::push_heap(timer_heap->begin(), timer_heap->end());
(*time_log)[fd] = tn.time;
}
}
void remove(int fd);
void check();
void update(int fd);
time_t get_waittime();
};
#endif