-
Notifications
You must be signed in to change notification settings - Fork 166
/
ecal_timed_cb.h
138 lines (124 loc) 路 3.78 KB
/
ecal_timed_cb.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
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
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ========================= eCAL LICENSE =================================
*/
/**
* @file ecal_timed_cb.h
* @brief eCAL timer callback class (for internal use only !!)
**/
#pragma once
#include <atomic>
#include <chrono>
#include <functional>
#include <thread>
#include <assert.h>
#include "ecal_process.h"
namespace eCAL
{
class CTimedCB;
/**
* @brief Timer callback function type.
**/
typedef std::function<void(void)> TimerCallbackT;
/**
* @brief eCAL timer callback class.
*
* The CTimedCB class is used to realize simple time triggered callbacks.
**/
class CTimedCB
{
public:
/**
* @brief Constructor.
**/
CTimedCB() : m_stop(false), m_running(false) {}
/**
* @brief Constructor.
*
* @param timeout_ Timer callback loop time in ms.
* @param callback_ The callback function.
* @param delay_ Timer callback delay for first call in ms.
**/
CTimedCB(int timeout_, TimerCallbackT callback_, int delay_ = 0) : m_stop(false), m_running(false) { Start(timeout_, callback_, delay_); }
/**
* @brief Destructor.
**/
virtual ~CTimedCB() { Stop(); }
CTimedCB(const CTimedCB&) = delete;
CTimedCB& operator=(const CTimedCB&) = delete;
CTimedCB(CTimedCB&& rhs) = delete;
CTimedCB& operator=(CTimedCB&& rhs) = delete;
/**
* @brief Start the timer.
*
* @param timeout_ Timer callback loop time in ms.
* @param callback_ The callback function.
* @param delay_ Timer callback delay for first call in ms.
*
* @return True if timer could be started.
**/
bool Start(const int timeout_, TimerCallbackT callback_, const int delay_ = 0)
{
assert(m_running == false);
if (m_running) return(false);
if (timeout_ < 0) return(false);
m_stop = false;
m_thread = std::thread(&CTimedCB::Thread, this, callback_, timeout_, delay_);
m_running = true;
return(true);
}
/**
* @brief Stop the timer.
*
* @return True if timer could be stopped.
**/
bool Stop()
{
if (!m_running) return(false);
m_stop = true;
// Wait for the callback thread to finish
if (m_thread.joinable()) {
m_thread.join();
}
m_running = false;
return(true);
}
private:
void Thread(TimerCallbackT callback_, int timeout_, int delay_)
{
assert(callback_ != nullptr);
if (callback_ == nullptr) return;
if (delay_ > 0) eCAL::Process::SleepFor(std::chrono::milliseconds(delay_));
while (!m_stop)
{
auto start = std::chrono::steady_clock::now();
(callback_)();
if (timeout_ > 0)
{
auto now = std::chrono::steady_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count();
auto sleep_duration = timeout_ - elapsed_time;
eCAL::Process::SleepFor(std::chrono::milliseconds(sleep_duration));
}
}
m_stop = false;
}
std::atomic<bool> m_stop;
std::atomic<bool> m_running;
std::thread m_thread;
};
}