sl@0
|
1 |
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// wins\specific\timer.cpp
|
sl@0
|
15 |
//
|
sl@0
|
16 |
//
|
sl@0
|
17 |
|
sl@0
|
18 |
#include "variant.h"
|
sl@0
|
19 |
|
sl@0
|
20 |
#define KWinsTimerPanicCategory "WinsTimer"
|
sl@0
|
21 |
|
sl@0
|
22 |
enum TWinsTimerPanic
|
sl@0
|
23 |
{
|
sl@0
|
24 |
EReleaseSemaphoreError = 1,
|
sl@0
|
25 |
ETimeSetEventError = 2
|
sl@0
|
26 |
};
|
sl@0
|
27 |
|
sl@0
|
28 |
void PanicFromWinsTimer(TWinsTimerPanic aReason)
|
sl@0
|
29 |
{
|
sl@0
|
30 |
Kern::Fault(KWinsTimerPanicCategory, aReason);
|
sl@0
|
31 |
}
|
sl@0
|
32 |
|
sl@0
|
33 |
|
sl@0
|
34 |
|
sl@0
|
35 |
inline Int64 FileTime2Milliseconds(const FILETIME& aFileTime)
|
sl@0
|
36 |
{return ((Int64(aFileTime.dwHighDateTime)<<32) + aFileTime.dwLowDateTime)/10000;}
|
sl@0
|
37 |
|
sl@0
|
38 |
|
sl@0
|
39 |
void CALLBACK WinsTimer::Tick(UINT, UINT, DWORD aPtr, DWORD, DWORD)
|
sl@0
|
40 |
{
|
sl@0
|
41 |
|
sl@0
|
42 |
WinsTimer& timer=*(WinsTimer*)aPtr;
|
sl@0
|
43 |
if (!ReleaseSemaphore(timer.iSemaphore,1,NULL))
|
sl@0
|
44 |
{
|
sl@0
|
45 |
__ASSERT_ALWAYS((GetLastError() == ERROR_TOO_MANY_POSTS), PanicFromWinsTimer(EReleaseSemaphoreError));
|
sl@0
|
46 |
timeKillEvent(timer.iTimer);
|
sl@0
|
47 |
// If WinsTimer::EventThread() has run since the above call to
|
sl@0
|
48 |
// ReleaseSemaphore() (this has been observed to happen while
|
sl@0
|
49 |
// within the call to timeKillEvent()) then the semaphore may be
|
sl@0
|
50 |
// signallable again. If it is, restart the timer otherwise wait
|
sl@0
|
51 |
// for the event thread to restart it.
|
sl@0
|
52 |
if (ReleaseSemaphore(timer.iSemaphore,1,NULL))
|
sl@0
|
53 |
timer.Enable();
|
sl@0
|
54 |
else
|
sl@0
|
55 |
timer.iSuspend = timer.iMaxLagTicks;
|
sl@0
|
56 |
}
|
sl@0
|
57 |
}
|
sl@0
|
58 |
|
sl@0
|
59 |
DWORD WINAPI WinsTimer::Thread(LPVOID aPtr)
|
sl@0
|
60 |
{
|
sl@0
|
61 |
static_cast<WinsTimer*>(aPtr)->EventThread(*(NTimerQ*)NTimerQ::TimerAddress());
|
sl@0
|
62 |
return 0;
|
sl@0
|
63 |
}
|
sl@0
|
64 |
|
sl@0
|
65 |
void WinsTimer::EventThread(NTimerQ& aTimerQ)
|
sl@0
|
66 |
{
|
sl@0
|
67 |
for (;;)
|
sl@0
|
68 |
{
|
sl@0
|
69 |
WaitForSingleObject(iSemaphore,INFINITE);
|
sl@0
|
70 |
if (iSuspend > 0)
|
sl@0
|
71 |
{
|
sl@0
|
72 |
// Emulator interrupted/suspended for too long, MM-callback has been suspended
|
sl@0
|
73 |
//
|
sl@0
|
74 |
// Rather than try and catch up now, just discard the lost ticks - this improves
|
sl@0
|
75 |
// system behaviour particularly when debugging at the expense of losing sync
|
sl@0
|
76 |
// between the Windows clock and the emulator RTC.
|
sl@0
|
77 |
//
|
sl@0
|
78 |
while (--iSuspend > 0)
|
sl@0
|
79 |
WaitForSingleObject(iSemaphore, 0); // absorb all signals
|
sl@0
|
80 |
//
|
sl@0
|
81 |
// un-nobble once we are debugging
|
sl@0
|
82 |
if (iNobbleNanos && IsDebuggerPresent())
|
sl@0
|
83 |
iNobbleNanos = 0;
|
sl@0
|
84 |
//
|
sl@0
|
85 |
// now restart the timer callbacks
|
sl@0
|
86 |
Enable();
|
sl@0
|
87 |
//
|
sl@0
|
88 |
// don't deliver a tick until next callback
|
sl@0
|
89 |
continue;
|
sl@0
|
90 |
}
|
sl@0
|
91 |
if (iNobbleNanos && iIdleThread != NKern::CurrentThread())
|
sl@0
|
92 |
Kern::NanoWait(iNobbleNanos);
|
sl@0
|
93 |
StartOfInterrupt();
|
sl@0
|
94 |
iTime += iPeriod;
|
sl@0
|
95 |
if (!iStandby)
|
sl@0
|
96 |
aTimerQ.Tick();
|
sl@0
|
97 |
EndOfInterrupt();
|
sl@0
|
98 |
}
|
sl@0
|
99 |
}
|
sl@0
|
100 |
|
sl@0
|
101 |
|
sl@0
|
102 |
WinsTimer::WinsTimer()
|
sl@0
|
103 |
:iPeriod(0),iNobbleNanos(0),iMaxLagTicks(0),iSemaphore(NULL),iSuspend(0),iTime(0),iIdleThread(0)
|
sl@0
|
104 |
{}
|
sl@0
|
105 |
|
sl@0
|
106 |
void WinsTimer::Init(TUint aPeriod)
|
sl@0
|
107 |
{
|
sl@0
|
108 |
// calculate the y2k offset in seconds from Win32 'zero' FILETIME
|
sl@0
|
109 |
// This initially synchronizes EPOC time with Windows time
|
sl@0
|
110 |
const SYSTEMTIME KSystemTimeY2K = {2000,1,0,1,0,0,0,0};
|
sl@0
|
111 |
FILETIME y2k, now;
|
sl@0
|
112 |
SystemTimeToFileTime(&KSystemTimeY2K,&y2k);
|
sl@0
|
113 |
GetSystemTimeAsFileTime(&now);
|
sl@0
|
114 |
iTime = FileTime2Milliseconds(now) - FileTime2Milliseconds(y2k);
|
sl@0
|
115 |
|
sl@0
|
116 |
TIMECAPS caps;
|
sl@0
|
117 |
timeGetDevCaps(&caps,sizeof(caps));
|
sl@0
|
118 |
|
sl@0
|
119 |
iPeriod = min(caps.wPeriodMax, max(caps.wPeriodMin, aPeriod));
|
sl@0
|
120 |
TUint resolution = max(caps.wPeriodMin, iPeriod/2);
|
sl@0
|
121 |
iMaxLagTicks = EMaxLag / iPeriod;
|
sl@0
|
122 |
|
sl@0
|
123 |
// limit 'catch-up' for when Win32 gets too busy for us to fire timer events
|
sl@0
|
124 |
// making this too large causes delays when resuming from debug
|
sl@0
|
125 |
iSemaphore = CreateSemaphoreA(NULL, 0, iMaxLagTicks, NULL);
|
sl@0
|
126 |
|
sl@0
|
127 |
timeBeginPeriod(resolution);
|
sl@0
|
128 |
|
sl@0
|
129 |
CreateWin32Thread(EThreadEvent, &WinsTimer::Thread, this, ETrue);
|
sl@0
|
130 |
}
|
sl@0
|
131 |
|
sl@0
|
132 |
void WinsTimer::Enable()
|
sl@0
|
133 |
{
|
sl@0
|
134 |
iTimer = timeSetEvent(iPeriod,0,&WinsTimer::Tick,DWORD(this),TIME_PERIODIC);
|
sl@0
|
135 |
__ASSERT_ALWAYS(iTimer != NULL, PanicFromWinsTimer(ETimeSetEventError));
|
sl@0
|
136 |
}
|
sl@0
|
137 |
|
sl@0
|
138 |
TInt WinsTimer::SystemTime() const
|
sl@0
|
139 |
//
|
sl@0
|
140 |
// Return the time in seconds since y2k
|
sl@0
|
141 |
//
|
sl@0
|
142 |
{
|
sl@0
|
143 |
TInt irq = NKern::DisableAllInterrupts();
|
sl@0
|
144 |
Int64 time = iTime;
|
sl@0
|
145 |
NKern::RestoreInterrupts(irq);
|
sl@0
|
146 |
if (time < 0)
|
sl@0
|
147 |
time -= 999; // we want rounding to -infinity for the division
|
sl@0
|
148 |
return TInt(time/1000);
|
sl@0
|
149 |
}
|
sl@0
|
150 |
|
sl@0
|
151 |
void WinsTimer::SetSystemTime(TInt aTime)
|
sl@0
|
152 |
//
|
sl@0
|
153 |
// Set the time in seconds since y2k
|
sl@0
|
154 |
//
|
sl@0
|
155 |
{
|
sl@0
|
156 |
Int64 time=aTime;
|
sl@0
|
157 |
time*=1000;
|
sl@0
|
158 |
TInt irq = NKern::DisableAllInterrupts();
|
sl@0
|
159 |
iTime = time;
|
sl@0
|
160 |
NKern::RestoreInterrupts(irq);
|
sl@0
|
161 |
}
|
sl@0
|
162 |
|
sl@0
|
163 |
void WinsTimer::Standby()
|
sl@0
|
164 |
{
|
sl@0
|
165 |
iStandby = ETrue;
|
sl@0
|
166 |
}
|
sl@0
|
167 |
|
sl@0
|
168 |
void WinsTimer::Wakeup()
|
sl@0
|
169 |
{
|
sl@0
|
170 |
iStandby = EFalse;
|
sl@0
|
171 |
// Busy wait for the next timer interrupt
|
sl@0
|
172 |
volatile Int64* t = &iTime;
|
sl@0
|
173 |
Int64 time = *t;
|
sl@0
|
174 |
while (time == *t)
|
sl@0
|
175 |
{}
|
sl@0
|
176 |
} |