/
timer.cpp
243 lines (218 loc) · 7.14 KB
/
timer.cpp
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
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* EPICS BASE Versions 3.13.7
* and higher are distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
\*************************************************************************/
/*
* Author Jeffrey O. Hill
* johill@lanl.gov
* 505 665 1831
*/
#include <typeinfo>
#include <string>
#include <stdexcept>
#include <stdio.h>
#define epicsExportSharedSymbols
#include "epicsGuard.h"
#include "timerPrivate.h"
#include "errlog.h"
#ifdef _MSC_VER
# pragma warning ( push )
# pragma warning ( disable:4660 )
#endif
template class tsFreeList < timer, 0x20 >;
#ifdef _MSC_VER
# pragma warning ( pop )
#endif
timer::timer ( timerQueue & queueIn ) :
queue ( queueIn ), curState ( stateLimbo ), pNotify ( 0 )
{
}
timer::~timer ()
{
this->cancel ();
}
void timer::destroy ()
{
timerQueue & queueTmp = this->queue;
this->~timer ();
queueTmp.timerFreeList.release ( this );
}
void timer::start ( epicsTimerNotify & notify, double delaySeconds )
{
this->start ( notify, epicsTime::getCurrent () + delaySeconds );
}
void timer::start ( epicsTimerNotify & notify, const epicsTime & expire )
{
epicsGuard < epicsMutex > locker ( this->queue.mutex );
this->privateStart ( notify, expire );
}
void timer::privateStart ( epicsTimerNotify & notify, const epicsTime & expire )
{
this->pNotify = & notify;
this->exp = expire - ( this->queue.notify.quantum () / 2.0 );
bool reschedualNeeded = false;
if ( this->curState == stateActive ) {
// above expire time and notify will override any restart parameters
// that may be returned from the timer expire callback
return;
}
else if ( this->curState == statePending ) {
this->queue.timerList.remove ( *this );
if ( this->queue.timerList.first() == this &&
this->queue.timerList.count() > 0 ) {
reschedualNeeded = true;
}
}
# ifdef DEBUG
unsigned preemptCount=0u;
# endif
//
// insert into the pending queue
//
// Finds proper time sorted location using a linear search.
//
// **** this should use a binary tree ????
//
tsDLIter < timer > pTmr = this->queue.timerList.lastIter ();
while ( true ) {
if ( ! pTmr.valid () ) {
//
// add to the beginning of the list
//
this->queue.timerList.push ( *this );
reschedualNeeded = true;
break;
}
if ( pTmr->exp <= this->exp ) {
//
// add after the item found that expires earlier
//
this->queue.timerList.insertAfter ( *this, *pTmr );
break;
}
# ifdef DEBUG
preemptCount++;
# endif
--pTmr;
}
this->curState = timer::statePending;
if ( reschedualNeeded ) {
this->queue.notify.reschedule ();
}
# if defined(DEBUG) && 0
this->show ( 10u );
this->queue.show ( 10u );
# endif
debugPrintf ( ("Start of \"%s\" with delay %f at %p preempting %u\n",
typeid ( this->notify ).name (),
expire - epicsTime::getCurrent (),
this, preemptCount ) );
}
void timer::cancel ()
{
bool reschedual = false;
bool wakeupCancelBlockingThreads = false;
{
epicsGuard < epicsMutex > locker ( this->queue.mutex );
this->pNotify = 0;
if ( this->curState == statePending ) {
this->queue.timerList.remove ( *this );
this->curState = stateLimbo;
if ( this->queue.timerList.first() == this &&
this->queue.timerList.count() > 0 ) {
reschedual = true;
}
}
else if ( this->curState == stateActive ) {
this->queue.cancelPending = true;
this->curState = timer::stateLimbo;
if ( this->queue.processThread != epicsThreadGetIdSelf() ) {
// make certain timer expire() does not run after cancel () returns,
// but dont require that lock is applied while calling expire()
while ( this->queue.cancelPending &&
this->queue.pExpireTmr == this ) {
epicsGuardRelease < epicsMutex > autoRelease ( locker );
this->queue.cancelBlockingEvent.wait ();
}
// in case other threads are waiting
wakeupCancelBlockingThreads = true;
}
}
}
if ( reschedual ) {
this->queue.notify.reschedule ();
}
if ( wakeupCancelBlockingThreads ) {
this->queue.cancelBlockingEvent.signal ();
}
}
epicsTimer::expireInfo timer::getExpireInfo () const
{
// taking a lock here guarantees that users will not
// see brief intervals when a timer isnt active because
// it is is canceled when start is called
epicsGuard < epicsMutex > locker ( this->queue.mutex );
if ( this->curState == statePending || this->curState == stateActive ) {
return expireInfo ( true, this->exp );
}
return expireInfo ( false, epicsTime() );
}
void timer::show ( unsigned int level ) const
{
epicsGuard < epicsMutex > locker ( this->queue.mutex );
double delay;
if ( this->curState == statePending || this->curState == stateActive ) {
try {
delay = this->exp - epicsTime::getCurrent();
}
catch ( ... ) {
delay = - DBL_MAX;
}
}
else {
delay = -DBL_MAX;
}
const char *pStateName;
if ( this->curState == statePending ) {
pStateName = "pending";
}
else if ( this->curState == stateActive ) {
pStateName = "active";
}
else if ( this->curState == stateLimbo ) {
pStateName = "limbo";
}
else {
pStateName = "corrupt";
}
printf ( "timer, state = %s, delay = %f\n",
pStateName, delay );
if ( level >= 1u && this->pNotify ) {
this->pNotify->show ( level - 1u );
}
}
void timer::operator delete ( void * )
{
// Visual C++ .net appears to require operator delete if
// placement operator delete is defined? I smell a ms rat
// because if I declare placement new and delete, but
// comment out the placement delete definition there are
// no undefined symbols.
errlogPrintf ( "%s:%d this compiler is confused about placement delete - memory was probably leaked",
__FILE__, __LINE__ );
}
void epicsTimerForC::operator delete ( void * )
{
// Visual C++ .net appears to require operator delete if
// placement operator delete is defined? I smell a ms rat
// because if I declare placement new and delete, but
// comment out the placement delete definition there are
// no undefined symbols.
errlogPrintf ( "%s:%d this compiler is confused about placement delete - memory was probably leaked",
__FILE__, __LINE__ );
}