os/boardsupport/emulator/emulatorbsp/specific/timer.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/boardsupport/emulator/emulatorbsp/specific/timer.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,176 @@
     1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// wins\specific\timer.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include "variant.h"
    1.22 +
    1.23 +#define KWinsTimerPanicCategory "WinsTimer"
    1.24 +
    1.25 +enum TWinsTimerPanic
    1.26 +	{
    1.27 +	EReleaseSemaphoreError = 1,
    1.28 +	ETimeSetEventError = 2
    1.29 +	};
    1.30 +
    1.31 +void PanicFromWinsTimer(TWinsTimerPanic aReason)
    1.32 +	{
    1.33 +	Kern::Fault(KWinsTimerPanicCategory, aReason);
    1.34 +	}
    1.35 +
    1.36 +
    1.37 +
    1.38 +inline Int64 FileTime2Milliseconds(const FILETIME& aFileTime)
    1.39 +	{return ((Int64(aFileTime.dwHighDateTime)<<32) + aFileTime.dwLowDateTime)/10000;}
    1.40 +
    1.41 +
    1.42 +void CALLBACK WinsTimer::Tick(UINT, UINT, DWORD aPtr, DWORD, DWORD) 
    1.43 +	{
    1.44 +
    1.45 +	WinsTimer& timer=*(WinsTimer*)aPtr;
    1.46 +	if (!ReleaseSemaphore(timer.iSemaphore,1,NULL))
    1.47 +		{
    1.48 +		__ASSERT_ALWAYS((GetLastError() == ERROR_TOO_MANY_POSTS), PanicFromWinsTimer(EReleaseSemaphoreError));
    1.49 +		timeKillEvent(timer.iTimer);
    1.50 +		// If WinsTimer::EventThread() has run since the above call to
    1.51 +		// ReleaseSemaphore() (this has been observed to happen while 
    1.52 +		// within the call to timeKillEvent()) then the semaphore may be 
    1.53 +		// signallable again. If it is, restart the timer otherwise wait 
    1.54 +		// for the event thread to restart it.
    1.55 +		if (ReleaseSemaphore(timer.iSemaphore,1,NULL))
    1.56 +			timer.Enable();
    1.57 +		else
    1.58 +			timer.iSuspend = timer.iMaxLagTicks;
    1.59 +		}
    1.60 +	}
    1.61 +
    1.62 +DWORD WINAPI WinsTimer::Thread(LPVOID aPtr)
    1.63 +	{
    1.64 +	static_cast<WinsTimer*>(aPtr)->EventThread(*(NTimerQ*)NTimerQ::TimerAddress());
    1.65 +	return 0;
    1.66 +	}
    1.67 +
    1.68 +void WinsTimer::EventThread(NTimerQ& aTimerQ)
    1.69 +	{
    1.70 +	for (;;)
    1.71 +		{
    1.72 +		WaitForSingleObject(iSemaphore,INFINITE);
    1.73 +		if (iSuspend > 0)
    1.74 +			{
    1.75 +			// Emulator interrupted/suspended for too long, MM-callback has been suspended
    1.76 +			//
    1.77 +			// Rather than try and catch up now, just discard the lost ticks - this improves
    1.78 +			// system behaviour particularly when debugging at the expense of losing sync
    1.79 +			// between the Windows clock and the emulator RTC.
    1.80 +			//
    1.81 +			while (--iSuspend > 0)
    1.82 +				WaitForSingleObject(iSemaphore, 0);	// absorb all signals
    1.83 +			//
    1.84 +			// un-nobble once we are debugging
    1.85 +			if (iNobbleNanos && IsDebuggerPresent())
    1.86 +				iNobbleNanos = 0;
    1.87 +			//
    1.88 +			// now restart the timer callbacks
    1.89 +			Enable();
    1.90 +			//
    1.91 +			// don't deliver a tick until next callback
    1.92 +			continue;
    1.93 +			}
    1.94 +		if (iNobbleNanos && iIdleThread != NKern::CurrentThread())
    1.95 +			Kern::NanoWait(iNobbleNanos);
    1.96 +		StartOfInterrupt();
    1.97 +		iTime += iPeriod;
    1.98 +		if (!iStandby)
    1.99 +			aTimerQ.Tick();
   1.100 +		EndOfInterrupt();
   1.101 +		}
   1.102 +	}
   1.103 +
   1.104 +
   1.105 +WinsTimer::WinsTimer()
   1.106 +	:iPeriod(0),iNobbleNanos(0),iMaxLagTicks(0),iSemaphore(NULL),iSuspend(0),iTime(0),iIdleThread(0)
   1.107 +	{}
   1.108 +
   1.109 +void WinsTimer::Init(TUint aPeriod)
   1.110 +	{
   1.111 +	// calculate the y2k offset in seconds from Win32 'zero' FILETIME
   1.112 +	// This initially synchronizes EPOC time with Windows time
   1.113 +	const SYSTEMTIME KSystemTimeY2K = {2000,1,0,1,0,0,0,0};
   1.114 +	FILETIME y2k, now;
   1.115 +	SystemTimeToFileTime(&KSystemTimeY2K,&y2k);
   1.116 +	GetSystemTimeAsFileTime(&now);
   1.117 +	iTime = FileTime2Milliseconds(now) - FileTime2Milliseconds(y2k);
   1.118 +
   1.119 +	TIMECAPS caps;
   1.120 +	timeGetDevCaps(&caps,sizeof(caps));
   1.121 +
   1.122 +	iPeriod = min(caps.wPeriodMax, max(caps.wPeriodMin, aPeriod));
   1.123 +	TUint resolution = max(caps.wPeriodMin, iPeriod/2);
   1.124 +	iMaxLagTicks = EMaxLag / iPeriod;
   1.125 +
   1.126 +	// limit 'catch-up' for when Win32 gets too busy for us to fire timer events
   1.127 +	// making this too large causes delays when resuming from debug
   1.128 +	iSemaphore = CreateSemaphoreA(NULL, 0, iMaxLagTicks, NULL);
   1.129 +
   1.130 +	timeBeginPeriod(resolution);
   1.131 +
   1.132 +	CreateWin32Thread(EThreadEvent, &WinsTimer::Thread, this, ETrue);
   1.133 +	}
   1.134 +
   1.135 +void WinsTimer::Enable()
   1.136 +	{
   1.137 +	iTimer = timeSetEvent(iPeriod,0,&WinsTimer::Tick,DWORD(this),TIME_PERIODIC);
   1.138 +	__ASSERT_ALWAYS(iTimer != NULL, PanicFromWinsTimer(ETimeSetEventError));
   1.139 +	}
   1.140 +
   1.141 +TInt WinsTimer::SystemTime() const
   1.142 +//
   1.143 +// Return the time in seconds since y2k
   1.144 +//
   1.145 +	{
   1.146 +	TInt irq = NKern::DisableAllInterrupts();
   1.147 +	Int64 time = iTime;
   1.148 +	NKern::RestoreInterrupts(irq);
   1.149 +	if (time < 0)
   1.150 +		time -= 999;	// we want rounding to -infinity for the division
   1.151 +	return TInt(time/1000);
   1.152 +	}
   1.153 +
   1.154 +void WinsTimer::SetSystemTime(TInt aTime)
   1.155 +//
   1.156 +// Set the time in seconds since y2k
   1.157 +//
   1.158 +	{
   1.159 +	Int64 time=aTime;
   1.160 +	time*=1000;
   1.161 +	TInt irq = NKern::DisableAllInterrupts();
   1.162 +	iTime = time;
   1.163 +	NKern::RestoreInterrupts(irq);
   1.164 +	}
   1.165 +
   1.166 +void WinsTimer::Standby()
   1.167 +	{
   1.168 +	iStandby = ETrue;
   1.169 +	}
   1.170 +
   1.171 +void WinsTimer::Wakeup()
   1.172 +	{
   1.173 +	iStandby = EFalse;
   1.174 +	// Busy wait for the next timer interrupt 
   1.175 +	volatile Int64* t = &iTime;
   1.176 +	Int64 time = *t;
   1.177 +	while (time == *t)
   1.178 +		{}
   1.179 +	}
   1.180 \ No newline at end of file