Repository navigation
Expand file tree
/
Copy pathtimer_module.cpp
More file actions
332 lines (288 loc) · 11.5 KB
/
Copy pathtimer_module.cpp
File metadata and controls
332 lines (288 loc) · 11.5 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
Copyright (c) 2009-2026 WangTingMan <75142601@qq.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "timer_module.h"
#include "abstract_task.h"
#include "executable_task.h"
#include "log_util.h"
#include "framework_manager.h"
#include "framework_event.h"
#include "framework/internal/platform.h"
#include <chrono>
#include <limits>
#ifdef DEBUG_TIMER_MODULE
#define LogTimerDebug LogUtilInfo
#else
#define LogTimerDebug LogUtilIgnore
#endif
namespace framework
{
class timer_module_timer_task : public abstract_task
{
public:
timer_module_timer_task()
{
set_task_type(task_type::timer_module_task);
}
};
timer_module::timer_module()
{
set_name( s_timer_module_name );
set_module_type( abstract_module::module_type::concurrently_executing );
}
int64_t timer_module::get_system_booting_time()
{
std::chrono::milliseconds sysUpTime =
std::chrono::duration_cast< std::chrono::milliseconds >(
std::chrono::high_resolution_clock::now() -
std::chrono::high_resolution_clock::time_point() );
return sysUpTime.count();
}
std::string timer_module::to_booting_time_stamp( int64_t a_booting_time )
{
int64_t up_miliseconds = a_booting_time;
std::chrono::milliseconds sys_up_time = std::chrono::milliseconds( up_miliseconds );
int up_seconds = static_cast< int >(
std::chrono::duration_cast< std::chrono::seconds >( sys_up_time ).count() );
up_miliseconds -= up_seconds * 1000;
int up_minutes = up_seconds / 60;
up_seconds -= up_minutes * 60;
int up_hours = up_minutes / 60;
up_minutes -= up_hours * 60;
char buffer[100];
snprintf( buffer, sizeof buffer,
"[%02d:%02d:%02d.%03d]", up_hours,
up_minutes, up_seconds, static_cast<int>( up_miliseconds ) );
return buffer;
}
void timer_module::timer_timeout_callback()
{
std::shared_ptr<timer_module_timer_task> task;
task = std::make_shared<timer_module_timer_task>();
task->set_source_module( abstract_module::s_timer_module_name );
task->set_target_module( abstract_module::s_timer_module_name );
task->set_position( source_here );
framework_manager::get_instance().get_thread_manager().post_task( task, source_here );
}
void timer_module::initialize()
{
set_power_status( abstract_module::powering_status::power_on );
}
void timer_module::deinitialize()
{
set_power_status( abstract_module::powering_status::power_off );
}
void timer_module::handle_task( std::shared_ptr<abstract_task> a_task )
{
if( a_task->get_task_type() != task_type::timer_module_task )
{
return;
}
handle_timer_expired();
}
void timer_module::handle_event( std::shared_ptr<framework_event> a_event )
{
switch( a_event->m_event_type )
{
case event_type::power_on:
set_power_status( abstract_module::powering_status::power_on );
break;
case event_type::power_off:
// Other module needs to cancel the registered timer.
set_power_status( abstract_module::powering_status::power_off );
break;
case event_type::power_status_changed:
break;
case event_type::derived_type:
break;
default:
LogUtilError() << "event type ignored: " << static_cast< uint16_t >( a_event->m_event_type );
break;
}
}
uint32_t timer_module::register_timer
(
timer_control_block::timeout_callback a_expire_callback,
std::chrono::milliseconds a_interval,
uint32_t a_trigger_times,
std::string a_handle_module
)
{
return register_timer( a_expire_callback, a_interval, "", a_trigger_times, a_handle_module );
}
uint32_t timer_module::register_timer
(
timer_control_block::timeout_callback a_expire_callback,
std::chrono::milliseconds a_interval,
std::string a_timer_name,
uint32_t a_trigger_times,
std::string a_handle_module
)
{
std::shared_ptr<timer_control_block> timer = std::make_shared<timer_control_block>();
timer->set_timeout_callback( a_expire_callback );
timer->set_interval( static_cast< uint32_t >( a_interval.count() ) );
timer->set_trigger_times( a_trigger_times );
timer->set_timer_id( get_available_timer_id() );
timer->set_timer_name( a_timer_name );
timer->set_handle_module( a_handle_module );
timer->start_schedule();
LogTimerDebug() << "Create timer " << a_timer_name << " done. First trigger is " << a_interval
<< " later. On timepoint: " << to_booting_time_stamp( timer->get_time_to_execute() );
std::unique_lock<std::recursive_mutex> locker( m_mutex );
uint64_t nextFire = m_timers.empty() ? std::numeric_limits<uint64_t>::max() :
m_timers.front()->get_time_to_execute();
m_timers.push_front( timer );
m_timers.sort( []( std::shared_ptr<timer_control_block> const& a_left,
std::shared_ptr<timer_control_block> const& a_right )->bool
{
return a_left->get_time_to_execute() < a_right->get_time_to_execute();
} );
auto front_time_to_execute = m_timers.front()->get_time_to_execute();
if( front_time_to_execute < nextFire )
{
make_schedule_task_if_need( front_time_to_execute );
}
return timer->get_timer_id();
}
void timer_module::reset_timer
(
uint32_t a_id,
std::chrono::milliseconds a_interval
)
{
std::unique_lock<std::recursive_mutex> locker( m_mutex );
for( auto it = m_timers.begin(); it != m_timers.end(); ++it )
{
if( ( *it )->get_timer_id() == a_id )
{
( *it )->set_interval( a_interval.count() );
return;
}
}
}
void timer_module::undregister_timer( uint32_t a_timer_id )
{
std::unique_lock<std::recursive_mutex> locker( m_mutex );
for( auto it = m_timers.begin(); it != m_timers.end(); ++it )
{
if( ( *it )->get_timer_id() == a_timer_id )
{
m_timers.erase( it );
return;
}
}
}
void timer_module::handle_timer_expired()
{
std::unique_lock<std::recursive_mutex> locker( m_mutex );
for( auto it = m_timers.begin(); it != m_timers.end(); ++it )
{
std::shared_ptr<timer_control_block>& _timer = *it;
int64_t curTime = get_system_booting_time();
int64_t executeTime = _timer->get_time_to_execute();
int64_t diff = curTime - executeTime;
if( diff < -10 )
{
// Seem like I'm wake up early
break;
}
if( diff > 100 )
{
LogUtilWarning() << "timer need to be fire ealier. timer: " << _timer->get_timer_name()
<< ", diff = " << diff << ", curtime: " << to_booting_time_stamp( curTime ) << ", execute time: "
<< to_booting_time_stamp( executeTime );
}
int interval = _timer->get_interval();
std::shared_ptr<executable_task> task;
auto fun = _timer->get_timeout_callback();
uint32_t timer_id = _timer->get_timer_id();
std::string timer_name = _timer->get_timer_name();
uint32_t remain_trigger_times = _timer->get_remain_trigger_timers();
LogTimerDebug() << "timer: " << _timer->get_timer_name() << " remains " << remain_trigger_times
<< ", current execute time: " << to_booting_time_stamp( executeTime ) << ", current time: " << to_booting_time_stamp( curTime );
task = std::make_shared<executable_task>( [fun, timer_id, timer_name, executeTime, interval, remain_trigger_times]()
{
int64_t curTime = get_system_booting_time();
if( curTime - executeTime > 300 && remain_trigger_times > 0 )
{
/**
* If the there are many task in thread pool need to execute, then the repeat timer
* may execute many times at the same time. eg. if we have a debug break point here,
* then other thread is running and will run the timer so this case may be happen.
* So we need add a check here to avoid that. That is, for instance we have registered
* a repeating timer with interval is 1 seconds. And the add a debug break pointer here,
* then the timer thread will add 1, 2, 3, 4, 5, 6, 7 second timer event in thread pool.
* But all these timer event will execute at 7 second. So that unexpected case happen. We
* need avoid this. Here checked time is 300 milliseconds.
*/
return false;
}
LogTimerDebug() << "trigger timer: " << timer_name << ", remain " << remain_trigger_times;
fun( timer_id, timer_name );
return false;
} );
std::string handle_module = _timer->get_handle_module();
if( !handle_module.empty() )
{
// We will post this time out callback to the target module
task->set_target_module( handle_module );
}
else
{
// We will find a thread to execute the time out callback. In this case
// the user must be careful about the thread safe problem.
task->set_target_module( s_task_runner_module_name );
}
task->set_source_module( get_name() );
framework_manager::get_instance().get_thread_manager().post_task( task, source_here );
_timer->timer_triggered();
remain_trigger_times = _timer->get_remain_trigger_timers();
LogTimerDebug() << "Now, timer: " << _timer->get_timer_name() << " remains " << remain_trigger_times;
}
if( m_timers.empty() )
{
return;
}
m_timers.sort( []( std::shared_ptr<timer_control_block> const& a_left,
std::shared_ptr<timer_control_block> const& a_right )->bool
{
return a_left->get_time_to_execute() < a_right->get_time_to_execute();
} );
auto front_time_to_execute = m_timers.front()->get_time_to_execute();
auto diff = front_time_to_execute - get_system_booting_time();
if( diff < 0 )
{
/**
* If the front time to execute is ealier that current time, then we just add 10 ms
* to wait.
*/
front_time_to_execute = get_system_booting_time() + 10;
}
make_schedule_task_if_need( front_time_to_execute );
}
void timer_module::make_schedule_task_if_need( int64_t a_front_time_to_execute )
{
auto next_timeout = a_front_time_to_execute - get_system_booting_time();
if (next_timeout < 1)
{
next_timeout = 5;
}
set_timer( next_timeout );
}
}