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