Update contrib.
1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // wins\specific\timer.cpp
20 #define KWinsTimerPanicCategory "WinsTimer"
24 EReleaseSemaphoreError = 1,
25 ETimeSetEventError = 2
28 void PanicFromWinsTimer(TWinsTimerPanic aReason)
30 Kern::Fault(KWinsTimerPanicCategory, aReason);
35 inline Int64 FileTime2Milliseconds(const FILETIME& aFileTime)
36 {return ((Int64(aFileTime.dwHighDateTime)<<32) + aFileTime.dwLowDateTime)/10000;}
39 void CALLBACK WinsTimer::Tick(UINT, UINT, DWORD aPtr, DWORD, DWORD)
42 WinsTimer& timer=*(WinsTimer*)aPtr;
43 if (!ReleaseSemaphore(timer.iSemaphore,1,NULL))
45 __ASSERT_ALWAYS((GetLastError() == ERROR_TOO_MANY_POSTS), PanicFromWinsTimer(EReleaseSemaphoreError));
46 timeKillEvent(timer.iTimer);
47 // If WinsTimer::EventThread() has run since the above call to
48 // ReleaseSemaphore() (this has been observed to happen while
49 // within the call to timeKillEvent()) then the semaphore may be
50 // signallable again. If it is, restart the timer otherwise wait
51 // for the event thread to restart it.
52 if (ReleaseSemaphore(timer.iSemaphore,1,NULL))
55 timer.iSuspend = timer.iMaxLagTicks;
59 DWORD WINAPI WinsTimer::Thread(LPVOID aPtr)
61 static_cast<WinsTimer*>(aPtr)->EventThread(*(NTimerQ*)NTimerQ::TimerAddress());
65 void WinsTimer::EventThread(NTimerQ& aTimerQ)
69 WaitForSingleObject(iSemaphore,INFINITE);
72 // Emulator interrupted/suspended for too long, MM-callback has been suspended
74 // Rather than try and catch up now, just discard the lost ticks - this improves
75 // system behaviour particularly when debugging at the expense of losing sync
76 // between the Windows clock and the emulator RTC.
78 while (--iSuspend > 0)
79 WaitForSingleObject(iSemaphore, 0); // absorb all signals
81 // un-nobble once we are debugging
82 if (iNobbleNanos && IsDebuggerPresent())
85 // now restart the timer callbacks
88 // don't deliver a tick until next callback
91 if (iNobbleNanos && iIdleThread != NKern::CurrentThread())
92 Kern::NanoWait(iNobbleNanos);
102 WinsTimer::WinsTimer()
103 :iPeriod(0),iNobbleNanos(0),iMaxLagTicks(0),iSemaphore(NULL),iSuspend(0),iTime(0),iIdleThread(0)
106 void WinsTimer::Init(TUint aPeriod)
108 // calculate the y2k offset in seconds from Win32 'zero' FILETIME
109 // This initially synchronizes EPOC time with Windows time
110 const SYSTEMTIME KSystemTimeY2K = {2000,1,0,1,0,0,0,0};
112 SystemTimeToFileTime(&KSystemTimeY2K,&y2k);
113 GetSystemTimeAsFileTime(&now);
114 iTime = FileTime2Milliseconds(now) - FileTime2Milliseconds(y2k);
117 timeGetDevCaps(&caps,sizeof(caps));
119 iPeriod = min(caps.wPeriodMax, max(caps.wPeriodMin, aPeriod));
120 TUint resolution = max(caps.wPeriodMin, iPeriod/2);
121 iMaxLagTicks = EMaxLag / iPeriod;
123 // limit 'catch-up' for when Win32 gets too busy for us to fire timer events
124 // making this too large causes delays when resuming from debug
125 iSemaphore = CreateSemaphoreA(NULL, 0, iMaxLagTicks, NULL);
127 timeBeginPeriod(resolution);
129 CreateWin32Thread(EThreadEvent, &WinsTimer::Thread, this, ETrue);
132 void WinsTimer::Enable()
134 iTimer = timeSetEvent(iPeriod,0,&WinsTimer::Tick,DWORD(this),TIME_PERIODIC);
135 __ASSERT_ALWAYS(iTimer != NULL, PanicFromWinsTimer(ETimeSetEventError));
138 TInt WinsTimer::SystemTime() const
140 // Return the time in seconds since y2k
143 TInt irq = NKern::DisableAllInterrupts();
145 NKern::RestoreInterrupts(irq);
147 time -= 999; // we want rounding to -infinity for the division
148 return TInt(time/1000);
151 void WinsTimer::SetSystemTime(TInt aTime)
153 // Set the time in seconds since y2k
158 TInt irq = NKern::DisableAllInterrupts();
160 NKern::RestoreInterrupts(irq);
163 void WinsTimer::Standby()
168 void WinsTimer::Wakeup()
171 // Busy wait for the next timer interrupt
172 volatile Int64* t = &iTime;