-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctimer.h
86 lines (73 loc) · 2.63 KB
/
ctimer.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
/**
* $DateTime: 2015/08/23 13:00:12 $
* $Id: //depot/sircond/ctimer.h#6 $
* $Change: 2154 $
*
* Copyright (C) 2015 Swarga Research (http://www.swarga-research.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _CTIMER_H_
#define _CTIMER_H_
#include <list>
using std::list;
#include "ctask.h"
namespace sr
{
typedef uint32_t HTIMER; //!< HANDLE TO A TIMER OBJECT
const HTIMER INVALID_TIMER_HANDLE_VALUE = 0xffffffffu; //!< SENTINEL VALUE FOR AN INVALID TIMER HANDLE
typedef void (*TIMERCALLBACK)(void*); //!< SIGNATURE OF A TIMER CALLBACK FUNCTION
//!
//! \internal
//! \brief A periodic timer object.
//!
//! This timer object calls the specified function periodically
//! at the specified interval. It will continue to make these
//! calls until the timer object is destroyed.
//!
class TIMERREC
{
public:
HTIMER handle; //!< THE HANDLE (UNIQUE ID) OF THIS TIMER
uint32_t count; //!< COUNT OF CLOCK TICKS SINCE TIMER LAST FIRED
uint32_t interval; //!< CALL INTERVAL (MS)
void* instance; //!< APPLICATION-SUPPLIED INSTANCE DATA
TIMERCALLBACK callback; //!< POINTER TO A FUNCTION THAT WILL BE CALLED WHEN A TIMER EXPIRES
TIMERREC ();
TIMERREC (const TIMERREC& t);
};
//!
//! \brief A timer manager class.
//!
//! Manages a set of periodic timer objects.
//!
class CTimer : public sr::CTask
{
public:
CTimer ();
~CTimer ();
HTIMER Create (uint32_t interval, void* instance, TIMERCALLBACK callback);
void Restart (HTIMER h);
void Destroy (HTIMER h);
void OnRun ();
private:
std::mutex m_critsect; // SERIALIZES ACCESS TO THE TIMER LIST
list<TIMERREC> m_timers; // THE LIST OF ACTIVE TIMERS
uint32_t m_sleep_time; // TIME BETWEEN THREAD WAKEUPS
uint32_t m_sequence; // USED TO GENERATE TEMPORALLY UNIQUE TIMER HANDLE VALUES
void UpdateSleepTime (uint32_t new_interval);
};
}
#endif