os/kernelhwsrv/kernel/eka/nkern/win32/ncutilf.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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 the License "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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32\nkern\win32\ncutilf.cpp
    15 // 
    16 //
    17 
    18 #include "nk_priv.h"
    19 
    20 // Win32 fast counter implementation
    21 
    22 static TBool FastCounterSupported = FALSE;
    23 static TInt FastCounterShift;
    24 static TInt FastCounterAdjustedFreq;
    25 
    26 /** Initialise the fast counter.
    27 
    28     Check whether a high performance counter is available and determine its
    29     frequency.  Work out a scaling that allows a 32 bit counter value that wraps
    30     at most every 16 seconds.
    31 */
    32 void FastCounterInit()
    33 	{
    34 	// Test support by getting count - QueryPerformanceFrequency can succeed
    35 	// even if not supported
    36 	LARGE_INTEGER count;
    37 	count.QuadPart = 0;
    38 	if (!QueryPerformanceCounter(&count) || count.QuadPart == 0)
    39 		return;  // not supported
    40 	
    41 	LARGE_INTEGER freq;
    42 	freq.QuadPart = 0;
    43 	if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0)
    44 		return;  // not supported
    45 
    46 	TInt mso = __e32_find_ms1_32(freq.LowPart);
    47 	FastCounterShift = (mso > 27) ? mso - 27 : 0;
    48 	
    49 	FastCounterSupported = TRUE;
    50 	FastCounterAdjustedFreq = freq.LowPart >> FastCounterShift;	
    51 	}
    52     
    53 /** Get the current value of the high performance counter.
    54 
    55     If a high performance counter is not available, this uses the millisecond
    56     tick count instead.
    57 */
    58 EXPORT_C TUint32 NKern::FastCounter()
    59 	{
    60 	if (FastCounterSupported)
    61 		{
    62 		LARGE_INTEGER count;
    63 		count.QuadPart = 0;
    64 		QueryPerformanceCounter(&count);
    65 		return (TUint32)(count.QuadPart >> FastCounterShift);
    66 		}
    67 	else
    68 		return NTickCount();
    69 	}
    70 
    71 
    72 /** Get the frequency of counter queried by NKern::FastCounter().
    73 */
    74 EXPORT_C TInt NKern::FastCounterFrequency()
    75 	{
    76 	if (FastCounterSupported)
    77 		return FastCounterAdjustedFreq;
    78 	else
    79 		return 1000000 / TickPeriod();
    80 	}