1/*
2 * Legal Notice
3 *
4 * This document and associated source code (the "Work") is a part of a
5 * benchmark specification maintained by the TPC.
6 *
7 * The TPC reserves all right, title, and interest to the Work as provided
8 * under U.S. and international laws, including without limitation all patent
9 * and trademark rights therein.
10 *
11 * No Warranty
12 *
13 * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION
14 * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE
15 * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER
16 * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
17 * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES,
18 * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR
19 * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF
20 * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE.
21 * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT,
22 * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT
23 * WITH REGARD TO THE WORK.
24 * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO
25 * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE
26 * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS
27 * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT,
28 * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY,
29 * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT
30 * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD
31 * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * Contributors
34 * - Doug Johnson
35 */
36
37/******************************************************************************
38 * Description: Template for a Timer Wheel. This allows for efficient
39 * handling of timers that have a known maximum period.
40 ******************************************************************************/
41
42#ifndef TIMER_WHEEL_H
43#define TIMER_WHEEL_H
44
45#include "utilities/EGenUtilities_stdafx.h"
46#include "utilities/MiscConsts.h"
47#include "Wheel.h"
48#include "WheelTime.h"
49#include "TimerWheelTimer.h"
50
51namespace TPCE {
52
53template <class T, class T2, INT32 Period, INT32 Resolution> class CTimerWheel {
54
55private:
56 CDateTime m_BaseTime;
57
58 CWheelTime m_LastTime;
59 CWheelTime m_CurrentTime;
60 CWheelTime m_NextTime;
61
62 TWheelConfig m_WheelConfig;
63
64 list<CTimerWheelTimer<T, T2> *> m_TimerWheel[(Period * (MsPerSecond / Resolution))];
65
66 INT32 m_NumberOfTimers;
67
68 INT32 ExpiryProcessing(void);
69 void ProcessTimerList(list<CTimerWheelTimer<T, T2> *> *pList);
70 INT32 SetNextTime(void);
71
72public:
73 static const INT32 NO_OUTSTANDING_TIMERS = -1;
74
75 CTimerWheel();
76 ~CTimerWheel(void);
77
78 bool Empty(void);
79 INT32 ProcessExpiredTimers(void);
80 INT32 StartTimer(double Offset, T2 *pExpiryObject, void (T2::*pExpiryFunction)(T *), T *pExpiryData);
81
82}; // class CTimerWheel
83
84template <class T, class T2, INT32 Period, INT32 Resolution>
85CTimerWheel<T, T2, Period, Resolution>::CTimerWheel()
86 : m_BaseTime(), m_LastTime(&m_WheelConfig, 0, 0), m_CurrentTime(&m_WheelConfig, 0, 0),
87 m_NextTime(&m_WheelConfig, MaxWheelCycles, (Period * (MsPerSecond / Resolution)) - 1),
88 m_WheelConfig((Period * (MsPerSecond / Resolution)), Resolution), m_NumberOfTimers(0) {
89 m_BaseTime.Set();
90}
91
92template <class T, class T2, INT32 Period, INT32 Resolution>
93CTimerWheel<T, T2, Period, Resolution>::~CTimerWheel(void) {
94 typename list<CTimerWheelTimer<T, T2> *>::iterator ExpiredTimer;
95
96 for (INT32 ii = 0; ii < (Period * (MsPerSecond / Resolution)); ii++) {
97 if (!m_TimerWheel[ii].empty()) {
98 ExpiredTimer = m_TimerWheel[ii].begin();
99 while (ExpiredTimer != m_TimerWheel[ii].end()) {
100 delete *ExpiredTimer;
101 m_NumberOfTimers--;
102 ExpiredTimer++;
103 }
104 m_TimerWheel[ii].clear();
105 }
106 }
107}
108
109template <class T, class T2, INT32 Period, INT32 Resolution> bool CTimerWheel<T, T2, Period, Resolution>::Empty(void) {
110 return (m_NumberOfTimers == 0 ? true : false);
111}
112
113template <class T, class T2, INT32 Period, INT32 Resolution>
114INT32 CTimerWheel<T, T2, Period, Resolution>::StartTimer(double Offset, T2 *pExpiryObject,
115 void (T2::*pExpiryFunction)(T *), T *pExpiryData) {
116 CDateTime Now;
117 CWheelTime RequestedTime(&m_WheelConfig, m_BaseTime, Now, (INT32)(Offset * (MsPerSecond / Resolution)));
118 CTimerWheelTimer<T, T2> *pNewTimer = new CTimerWheelTimer<T, T2>(pExpiryObject, pExpiryFunction, pExpiryData);
119
120 // Update current wheel position
121 m_CurrentTime.Set(m_BaseTime, Now);
122
123 // Since the current time has been updated, we should make sure
124 // any outstanding timers have been processed before proceeding.
125 ExpiryProcessing();
126
127 m_TimerWheel[RequestedTime.Index()].push_back(pNewTimer);
128 m_NumberOfTimers++;
129
130 if (RequestedTime < m_NextTime) {
131 m_NextTime = RequestedTime;
132 }
133
134 return (m_NextTime.Offset(m_CurrentTime));
135}
136
137template <class T, class T2, INT32 Period, INT32 Resolution>
138INT32 CTimerWheel<T, T2, Period, Resolution>::ProcessExpiredTimers(void) {
139 CDateTime Now;
140
141 // Update current wheel position
142 m_CurrentTime.Set(m_BaseTime, Now);
143
144 return (ExpiryProcessing());
145}
146
147template <class T, class T2, INT32 Period, INT32 Resolution>
148INT32 CTimerWheel<T, T2, Period, Resolution>::ExpiryProcessing(void) {
149 while (m_LastTime < m_CurrentTime) {
150 m_LastTime++;
151 if (!m_TimerWheel[m_LastTime.Index()].empty()) {
152 ProcessTimerList(&m_TimerWheel[m_LastTime.Index()]);
153 }
154 }
155 return (SetNextTime());
156}
157
158template <class T, class T2, INT32 Period, INT32 Resolution>
159void CTimerWheel<T, T2, Period, Resolution>::ProcessTimerList(list<CTimerWheelTimer<T, T2> *> *pList) {
160 typename list<CTimerWheelTimer<T, T2> *>::iterator ExpiredTimer;
161
162 ExpiredTimer = pList->begin();
163 while (ExpiredTimer != pList->end()) {
164 (((*ExpiredTimer)->m_pExpiryObject)->*((*ExpiredTimer)->m_pExpiryFunction))((*ExpiredTimer)->m_pExpiryData);
165 delete *ExpiredTimer;
166 m_NumberOfTimers--;
167 ExpiredTimer++;
168 }
169 pList->clear();
170}
171
172template <class T, class T2, INT32 Period, INT32 Resolution>
173INT32 CTimerWheel<T, T2, Period, Resolution>::SetNextTime(void) {
174 if (0 == m_NumberOfTimers) {
175 m_NextTime.Set(MaxWheelCycles, (Period * (MsPerSecond / Resolution)) - 1);
176 return (NO_OUTSTANDING_TIMERS);
177 } else {
178 m_NextTime = m_CurrentTime;
179 while (m_TimerWheel[m_NextTime.Index()].empty()) {
180 m_NextTime++;
181 }
182 return (m_NextTime.Offset(m_CurrentTime));
183 }
184}
185
186} // namespace TPCE
187
188#endif // TIMER_WHEEL_H
189