os/kernelhwsrv/kernel/eka/nkern/win32/ncutilf.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kernel/eka/nkern/win32/ncutilf.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,80 @@
     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 the License "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 +// e32\nkern\win32\ncutilf.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include "nk_priv.h"
    1.22 +
    1.23 +// Win32 fast counter implementation
    1.24 +
    1.25 +static TBool FastCounterSupported = FALSE;
    1.26 +static TInt FastCounterShift;
    1.27 +static TInt FastCounterAdjustedFreq;
    1.28 +
    1.29 +/** Initialise the fast counter.
    1.30 +
    1.31 +    Check whether a high performance counter is available and determine its
    1.32 +    frequency.  Work out a scaling that allows a 32 bit counter value that wraps
    1.33 +    at most every 16 seconds.
    1.34 +*/
    1.35 +void FastCounterInit()
    1.36 +	{
    1.37 +	// Test support by getting count - QueryPerformanceFrequency can succeed
    1.38 +	// even if not supported
    1.39 +	LARGE_INTEGER count;
    1.40 +	count.QuadPart = 0;
    1.41 +	if (!QueryPerformanceCounter(&count) || count.QuadPart == 0)
    1.42 +		return;  // not supported
    1.43 +	
    1.44 +	LARGE_INTEGER freq;
    1.45 +	freq.QuadPart = 0;
    1.46 +	if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0)
    1.47 +		return;  // not supported
    1.48 +
    1.49 +	TInt mso = __e32_find_ms1_32(freq.LowPart);
    1.50 +	FastCounterShift = (mso > 27) ? mso - 27 : 0;
    1.51 +	
    1.52 +	FastCounterSupported = TRUE;
    1.53 +	FastCounterAdjustedFreq = freq.LowPart >> FastCounterShift;	
    1.54 +	}
    1.55 +    
    1.56 +/** Get the current value of the high performance counter.
    1.57 +
    1.58 +    If a high performance counter is not available, this uses the millisecond
    1.59 +    tick count instead.
    1.60 +*/
    1.61 +EXPORT_C TUint32 NKern::FastCounter()
    1.62 +	{
    1.63 +	if (FastCounterSupported)
    1.64 +		{
    1.65 +		LARGE_INTEGER count;
    1.66 +		count.QuadPart = 0;
    1.67 +		QueryPerformanceCounter(&count);
    1.68 +		return (TUint32)(count.QuadPart >> FastCounterShift);
    1.69 +		}
    1.70 +	else
    1.71 +		return NTickCount();
    1.72 +	}
    1.73 +
    1.74 +
    1.75 +/** Get the frequency of counter queried by NKern::FastCounter().
    1.76 +*/
    1.77 +EXPORT_C TInt NKern::FastCounterFrequency()
    1.78 +	{
    1.79 +	if (FastCounterSupported)
    1.80 +		return FastCounterAdjustedFreq;
    1.81 +	else
    1.82 +		return 1000000 / TickPeriod();
    1.83 +	}