-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.h
More file actions
56 lines (47 loc) · 1.17 KB
/
timer.h
File metadata and controls
56 lines (47 loc) · 1.17 KB
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
54
55
56
#pragma once
#include "requestData.h"
#include "3rd_party/nocopyable.hpp"
#include "3rd_party/mutexLock.hpp"
#include <unistd.h>
#include <memory>
#include <queue>
#include <deque>
class RequestData;
class TimerNode
{
typedef std::shared_ptr<RequestData> SP_ReqData;
private:
bool deleted;
size_t expired_time;
SP_ReqData request_data;
public:
TimerNode(SP_ReqData _request_data, int timeout);
~TimerNode();
void update(int timeout);
bool isvalid();
void clearReq();
void setDeleted();
bool isDeleted() const;
size_t getExpTime() const;
};
struct timerCmp
{
bool operator()(std::shared_ptr<TimerNode> &a, std::shared_ptr<TimerNode> &b) const
{
return a->getExpTime() > b->getExpTime();
}
};
class TimerManager
{
typedef std::shared_ptr<RequestData> SP_ReqData;
typedef std::shared_ptr<TimerNode> SP_TimerNode;
private:
std::priority_queue<SP_TimerNode, std::deque<SP_TimerNode>, timerCmp> TimerNodeQueue;
MutexLock lock;
public:
TimerManager();
~TimerManager();
void addTimer(SP_ReqData request_data, int timeout);
void addTimer(SP_TimerNode timer_node);
void handle_expired_event();
};