forked from sttp/cppapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.cpp
More file actions
179 lines (146 loc) · 3.94 KB
/
Timer.cpp
File metadata and controls
179 lines (146 loc) · 3.94 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//******************************************************************************************************
// Timer.cpp - Gbtc
//
// Copyright © 2022, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 12/10/2022 - J. Ritchie Carroll
// Generated original version of source code.
//
//******************************************************************************************************
#include "Timer.h"
#include "ManualResetEvent.h"
using namespace std;
using namespace sttp;
const TimerPtr Timer::NullPtr = nullptr;
void Timer::TimerThread()
{
m_running = true;
do
{
try
{
const int32_t interval = m_interval;
if (interval > 0)
{
ManualResetEvent handle;
handle.Wait(interval);
}
}
catch (boost::thread_interrupted&)
{
m_running = false;
return;
}
if (m_running && m_callback != nullptr)
m_callback(m_this, m_userData);
}
while (m_autoReset && m_running);
m_running = false;
}
Timer::Timer(): Timer(1000, nullptr, false)
{
}
Timer::Timer(const int32_t interval, TimerElapsedCallback callback, const bool autoReset):
m_this(NullPtr),
m_timerThread(ThreadNullPtr),
m_interval(interval),
m_callback(std::move(callback)),
m_userData(nullptr),
m_autoReset(autoReset),
m_running(false)
{
}
Timer::~Timer() noexcept
{
try
{
Stop();
}
catch (...)
{
// ReSharper disable once CppRedundantControlFlowJump
return;
}
}
bool Timer::IsRunning() const
{
return m_running;
}
int32_t Timer::GetInterval() const
{
return m_interval;
}
void Timer::SetInterval(const int32_t value)
{
if (value == m_interval)
return;
const bool restart = m_running;
Stop();
m_interval = value;
if (restart)
Start();
}
TimerElapsedCallback Timer::GetCallback() const
{
return m_callback;
}
void Timer::SetCallback(TimerElapsedCallback value)
{
m_callback = std::move(value);
}
const void* Timer::GetUserData() const
{
return m_userData;
}
void Timer::SetUserData(void* value)
{
m_userData = value;
}
bool Timer::GetAutoReset() const
{
return m_autoReset;
}
void Timer::SetAutoReset(const bool value)
{
m_autoReset = value;
}
void Timer::Start()
{
if (m_callback == nullptr)
throw std::invalid_argument("Cannot start timer, no callback function has been defined.");
if (m_this == nullptr && !weak_from_this().expired())
m_this = shared_from_this();
if (m_running)
Stop();
m_timerThread = NewSharedPtr<sttp::Thread>([this] { TimerThread(); });
}
void Timer::Stop()
{
if (!m_running)
return;
m_running = false;
if (m_timerThread != nullptr)
{
const ThreadPtr timerThread = m_timerThread;
if (timerThread != nullptr)
{
timerThread->interrupt();
if (boost::this_thread::get_id() != timerThread->get_id())
timerThread->join();
}
}
m_timerThread.reset();
}