Repository navigation
Expand file tree
/
Copy pathTimerThread.h
More file actions
184 lines (164 loc) · 5.4 KB
/
Copy pathTimerThread.h
File metadata and controls
184 lines (164 loc) · 5.4 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
//-----------------------------------------------------------------------------
//
// TimerThread.h
//
// Timer for scheduling future events
//
// Copyright (c) 2017 h3ctrl <h3ctrl@gmail.com>
//
// SOFTWARE NOTICE AND LICENSE
//
// This file is part of OpenZWave.
//
// OpenZWave is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// OpenZWave is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
#ifndef _TIMERTHREAD_H_
#define _TIMERTHREAD_H_
#if __cplusplus >= 201103L || __APPLE__ || _MSC_VER
#include <functional>
using std::bind;
using std::function;
#else
#include <tr1/functional>
using std::tr1::bind;
using std::tr1::function;
#endif
#include "Defs.h"
#include "platform/Event.h"
#include "platform/Mutex.h"
#include "platform/TimeStamp.h"
namespace OpenZWave
{
class Driver;
namespace Internal
{
class Timer;
/** \brief The TimerThread class makes it possible to schedule events to happen
* at a certain time in the future.
*/
class OPENZWAVE_EXPORT TimerThread
{
friend class Timer;
//-----------------------------------------------------------------------------
// Timer based actions
//-----------------------------------------------------------------------------
public:
/** A timer callback function. */
typedef function<void(uint32)> TimerCallback;
/**
* Constructor.
*/
TimerThread(Driver *_driver);
/**
* Destructor.
*/
~TimerThread();
struct TimerEventEntry
{
Timer *instance;
Internal::Platform::TimeStamp timestamp;
TimerCallback callback;
uint32 id;
};
/**
* Main entry point for the timer thread. Wrapper around TimerThreadProc.
* \param _exitEvent Exit event indicating the thread should exit
* \param _context A TimerThread object
*/
static void TimerThreadEntryPoint(Internal::Platform::Event* _exitEvent, void* _context);
private:
//Driver* m_driver;
/**
* Schedule an event.
* \param _milliseconds The number of milliseconds before the event should happen
* \param _callback The function to be called when the time is reached
* \param _instance The Timer SubClass where this Event should be executed from
*/
TimerEventEntry* TimerSetEvent(int32 _milliseconds, TimerCallback _callback, Timer *_instance, uint32 id);
/**
* Remove a Event
*
*/
void TimerDelEvent(TimerEventEntry *);
/**
* Main class entry point for the timer thread. Contains the main timer loop.
* \param _exitEvent Exit event indicating the thread should exit
*/
void TimerThreadProc(Internal::Platform::Event* _exitEvent);
/** A list of upcoming timer events */
list<TimerEventEntry *> m_timerEventList;
Internal::Platform::Event* m_timerEvent; // Event to signal new timed action requested
Internal::Platform::Mutex* m_timerMutex; // Serialize access to class members
int32 m_timerTimeout; // Time in milliseconds to wait until next event
};
/**
* \brief Timer SubClass for automatically registering/unregistering Timer Callbacks
* if the instance goes out of scope
*
*/
class OPENZWAVE_EXPORT Timer
{
public:
/**
* \brief Constructor with the _driver this instance is associated with
* \param _driver The Driver that this instance is associated with
*/
Timer(Driver *_driver);
/**
* \brief Default Constructor
*/
Timer();
/**
* \brief Destructor
*/
~Timer();
/**
* \brief Schedule an event.
* \param _milliseconds The number of milliseconds before the event should happen
* \param _callback The function to be called when the time is reached
* \param _id The ID of the Timer
*/
TimerThread::TimerEventEntry* TimerSetEvent(int32 _milliseconds, TimerThread::TimerCallback _callback, uint32 id);
/**
* \brief Delete All Events registered to this instance
*/
void TimerDelEvents();
/**
* \brief Delete a Specific Event Registered to this instance
* \param te The TimerEventEntry Struct that was returned when Setting a Event
*/
void TimerDelEvent(TimerThread::TimerEventEntry *te);
/**
* \brief Delete a Specific Event Registered to this instance
* \param id The ID of the Timer To Delete
*/
void TimerDelEvent(uint32 id);
/**
* \brief Register the Driver Associated with this Instance
* \param _driver The Driver
*/
void SetDriver(Driver *_driver);
/**
* \brief Called From the TimerThread Class to execute a callback
* \param te The TimerEventEntry structure for the callback to execute
*/
void TimerFireEvent(TimerThread::TimerEventEntry *te);
private:
Driver* m_driver;
list<TimerThread::TimerEventEntry *> m_timerEventList;
};
} // namespace Internal
} // namespace OpenZWave
#endif // _TIMERTHREAD_H_