-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHCPPTimer.cpp
More file actions
96 lines (82 loc) · 2.13 KB
/
HCPPTimer.cpp
File metadata and controls
96 lines (82 loc) · 2.13 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/***************************************************************
* Name: HCPPTimer.cpp
* Purpose: 实现HCPPTimer接口
* Author: HYH (hyhsystem.cn)
* Created: 2026-04-08
* Copyright: HYH (hyhsystem.cn)
* License: MIT
**************************************************************/
#define HCPPTIMER_IMPLEMENTATION 1
#include "HCPPTimer.h"
#include <mutex>
namespace HCPPTimerGlobal
{
extern class TimerThread
{
HCPPThread *m_thread;
std::recursive_mutex m_lock;
public:
TimerThread();
~TimerThread();
HCPPThread* GetThread();
} g_timerthread;
}
HCPPTimer::HCPPTimer(HCPPObject *parent,std::chrono::microseconds interval,std::function<void(HCPPTimer *timer)> timeout,bool IsOneShot):HCPPObject(parent),m_interval(interval),m_timeout(timeout),m_IsOneShot(IsOneShot),m_end(std::chrono::steady_clock::time_point::min())
{
m_start=std::chrono::steady_clock::now();
m_end=m_start+m_interval;
SetParent(parent,true);
SetRunable();
}
HCPPTimer::~HCPPTimer()
{
SetRunable(false);
}
void HCPPTimer::timeout()
{
if(m_timeout!=NULL)
{
try
{
m_timeout(this);
}
catch(...)
{
}
}
}
bool HCPPTimer::SetParent(HCPPObject * parent,bool force_update)
{
//定时器必须要求顶层父对象为线程
if(parent==NULL || parent->GetTopHCPPObject()->GetType() != HCPPOBJECT_TYPE_THREAD)
{
parent=HCPPTimerGlobal::g_timerthread.GetThread();
}
return HCPPObject::SetParent(parent,force_update);
}
void HCPPTimer::DeleteTimer()
{
DeleteLatter();
}
void HCPPTimer::InvokeInit()
{
}
void HCPPTimer::InvokeUpdate()
{
if(m_end!=std::chrono::steady_clock::time_point::min() && std::chrono::steady_clock::now()>m_end)
{
//定时器超时
if(m_IsOneShot)
{
timeout();
m_end=std::chrono::steady_clock::time_point::min();
//稍后删除
DeleteLatter();
}
else
{
timeout();
m_end+=m_interval;
}
}
}