os/kernelhwsrv/kernel/eka/nkernsmp/nk_timer.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\nk_timer.cpp
sl@0
    15
// Fast Millisecond Timer Implementation
sl@0
    16
// This file is just a template - you'd be mad not to machine code this
sl@0
    17
// 
sl@0
    18
//
sl@0
    19
sl@0
    20
#include "nk_priv.h"
sl@0
    21
sl@0
    22
#define i_NTimer_iState			i8888.iHState1
sl@0
    23
#define i_NTimer_iCompleteInDfc	i8888.iHState2
sl@0
    24
sl@0
    25
const TInt KTimerQDfcPriority=6;
sl@0
    26
sl@0
    27
GLDEF_D NTimerQ TheTimerQ;
sl@0
    28
sl@0
    29
extern "C" void send_irq_ipi(TSubScheduler*);
sl@0
    30
sl@0
    31
#ifndef __MSTIM_MACHINE_CODED__
sl@0
    32
#ifdef _DEBUG
sl@0
    33
#define __DEBUG_CALLBACK(n)	{if (iDebugFn) (*iDebugFn)(iDebugPtr,n);}
sl@0
    34
#else
sl@0
    35
#define __DEBUG_CALLBACK(n)
sl@0
    36
#endif
sl@0
    37
sl@0
    38
/** Construct a nanokernel timer tied to a specified thread or group
sl@0
    39
	
sl@0
    40
sl@0
    41
	@param	aTied Pointer to the thread/group to which the timer should be tied
sl@0
    42
	@param	aFunction Pointer to the function to call on timer expiry
sl@0
    43
	@param	aPtr Parameter to pass to the expiry handler
sl@0
    44
	
sl@0
    45
	@pre	Any context
sl@0
    46
sl@0
    47
	@publishedPartner
sl@0
    48
	@prototype
sl@0
    49
 */
sl@0
    50
EXPORT_C NTimer::NTimer(NSchedulable* aTied, NTimerFn aFunction, TAny* aPtr)
sl@0
    51
	{
sl@0
    52
	iPtr = aPtr;
sl@0
    53
	iFn = aFunction;
sl@0
    54
	iHType = EEventHandlerNTimer;
sl@0
    55
//	i8888.iHState1 = EIdle;		done by NEventHandler constructor
sl@0
    56
	if (aTied)
sl@0
    57
		{
sl@0
    58
		SetTied(aTied);
sl@0
    59
		}
sl@0
    60
	}
sl@0
    61
sl@0
    62
sl@0
    63
/** Construct a nanokernel timer which mutates into and runs as a DFC on expiry
sl@0
    64
	The DFC queue is not specified at object construction time, but must be set
sl@0
    65
	using NTimer::SetDfcQ() before the timer is used.
sl@0
    66
sl@0
    67
	@param	aFunction	Pointer to the function to call on timer expiry
sl@0
    68
	@param	aPtr		Parameter to pass to the expiry handler
sl@0
    69
	@param	aPriority	Priority of DFC within the queue (0 to 7, where 7 is highest)
sl@0
    70
	
sl@0
    71
	@pre	Any context
sl@0
    72
sl@0
    73
	@publishedPartner
sl@0
    74
	@prototype
sl@0
    75
 */
sl@0
    76
EXPORT_C NTimer::NTimer(TDfcFn aFunction, TAny* aPtr, TInt aPriority)
sl@0
    77
	{
sl@0
    78
	iPtr = aPtr;
sl@0
    79
	iFn = aFunction;
sl@0
    80
	iTied = 0;
sl@0
    81
	iHType = (TUint8)aPriority;
sl@0
    82
//	i8888.iHState0 = 0;			done by NEventHandler constructor
sl@0
    83
//	i8888.iHState1 = EIdle;		done by NEventHandler constructor
sl@0
    84
//	i8888.iHState2 = 0;			done by NEventHandler constructor
sl@0
    85
	}
sl@0
    86
sl@0
    87
sl@0
    88
/** Construct a nanokernel timer which mutates into and runs as a DFC on expiry
sl@0
    89
sl@0
    90
	@param	aFunction	Pointer to the function to call on timer expiry
sl@0
    91
	@param	aPtr		Parameter to pass to the expiry handler
sl@0
    92
	@param	aDfcQ		Pointer to DFC queue which this timer should use
sl@0
    93
	@param	aPriority	Priority of DFC within the queue (0 to 7, where 7 is highest)
sl@0
    94
	
sl@0
    95
	@pre	Any context
sl@0
    96
sl@0
    97
	@publishedPartner
sl@0
    98
	@prototype
sl@0
    99
 */
sl@0
   100
EXPORT_C NTimer::NTimer(TDfcFn aFunction, TAny* aPtr, TDfcQue* aDfcQ, TInt aPriority)
sl@0
   101
	{
sl@0
   102
	iPtr = aPtr;
sl@0
   103
	iFn = aFunction;
sl@0
   104
	iDfcQ = aDfcQ;
sl@0
   105
	iHType = (TUint8)aPriority;
sl@0
   106
//	i8888.iHState0 = 0;			done by NEventHandler constructor
sl@0
   107
//	i8888.iHState1 = EIdle;		done by NEventHandler constructor
sl@0
   108
//	i8888.iHState2 = 0;			done by NEventHandler constructor
sl@0
   109
	}
sl@0
   110
sl@0
   111
sl@0
   112
/** Set the DFC queue to be used by an NTimer constructed using a TDfcFn
sl@0
   113
sl@0
   114
	@param	aDfcQ		Pointer to DFC queue which this timer should use
sl@0
   115
sl@0
   116
	@pre	Timer cannot be in use
sl@0
   117
	@pre	Any context
sl@0
   118
sl@0
   119
	@publishedPartner
sl@0
   120
	@prototype
sl@0
   121
 */
sl@0
   122
EXPORT_C void NTimer::SetDfcQ(TDfcQue* aDfcQ)
sl@0
   123
	{
sl@0
   124
	__NK_ASSERT_ALWAYS(aDfcQ!=0);
sl@0
   125
	__NK_ASSERT_ALWAYS(iHType < KNumDfcPriorities);
sl@0
   126
	__NK_ASSERT_ALWAYS(i8816.iHState16==EIdle);
sl@0
   127
	iDfcQ = aDfcQ;
sl@0
   128
	}
sl@0
   129
sl@0
   130
sl@0
   131
/** Tie a nanokernel timer to a thread or group
sl@0
   132
sl@0
   133
	@param	aTied = pointer to thread or group to which IDFC should be tied
sl@0
   134
	@return	KErrNone if successful
sl@0
   135
	@return	KErrDied if thread has exited or group has been destroyed.
sl@0
   136
sl@0
   137
	@pre Call in thread context, interrupts enabled
sl@0
   138
	@pre Timer must not be queued or running
sl@0
   139
	@pre Timer must not already be tied
sl@0
   140
	@pre Must not be a mutating timer (constructed with TDfcFn)
sl@0
   141
sl@0
   142
	@publishedPartner
sl@0
   143
	@prototype
sl@0
   144
 */
sl@0
   145
EXPORT_C TInt NTimer::SetTied(NSchedulable* aTied)
sl@0
   146
	{
sl@0
   147
	__NK_ASSERT_ALWAYS(!IsMutating());
sl@0
   148
	__NK_ASSERT_ALWAYS(i8888.iHState1 == EIdle);
sl@0
   149
	__NK_ASSERT_ALWAYS(aTied && !iTied);
sl@0
   150
	NKern::Lock();
sl@0
   151
	TInt r = aTied->AddTiedEvent(this);
sl@0
   152
	__NK_ASSERT_ALWAYS(r==KErrNone || r==KErrDied);
sl@0
   153
	NKern::Unlock();
sl@0
   154
	return r;
sl@0
   155
	}
sl@0
   156
sl@0
   157
sl@0
   158
/** Destroy a nanokernel timer
sl@0
   159
sl@0
   160
	@pre Call in thread context, interrupts enabled, preemption enabled
sl@0
   161
	@pre Calling thread in critical section
sl@0
   162
	@pre No fast mutex held
sl@0
   163
sl@0
   164
	@publishedPartner
sl@0
   165
	@prototype
sl@0
   166
 */
sl@0
   167
EXPORT_C NTimer::~NTimer()
sl@0
   168
	{
sl@0
   169
	if (!IsMutating() && iTied)
sl@0
   170
		{
sl@0
   171
		NKern::Lock();
sl@0
   172
		// remove from tied thread/group
sl@0
   173
		NEventHandler::TiedLock.LockOnly();
sl@0
   174
		NSchedulable* tied = iTied;
sl@0
   175
		DoCancel(ECancelDestroy);
sl@0
   176
		if (tied)	// might have been dequeued by thread/group termination
sl@0
   177
			{
sl@0
   178
			tied->AcqSLock();
sl@0
   179
			if (iTiedLink.iNext)
sl@0
   180
				{
sl@0
   181
				iTiedLink.Deque();
sl@0
   182
				iTiedLink.iNext = 0;
sl@0
   183
				}
sl@0
   184
			iTied = 0;
sl@0
   185
			tied->RelSLock();
sl@0
   186
			}
sl@0
   187
		NEventHandler::TiedLock.UnlockOnly();
sl@0
   188
		NKern::Unlock();
sl@0
   189
		}
sl@0
   190
	else if (IsMutating() && iDfcQ)
sl@0
   191
		DoCancelMutating(ECancelDestroy);
sl@0
   192
	else
sl@0
   193
		DoCancel(ECancelDestroy);
sl@0
   194
	}
sl@0
   195
sl@0
   196
sl@0
   197
/** Starts a nanokernel timer in one-shot mode with ISR callback.
sl@0
   198
	
sl@0
   199
	Queues the timer to expire in the specified number of nanokernel ticks. The
sl@0
   200
	actual wait time will be at least that much and may be up to one tick more.
sl@0
   201
	The expiry handler will be called in ISR context.
sl@0
   202
	
sl@0
   203
	Note that NKern::TimerTicks() can be used to convert milliseconds to ticks.
sl@0
   204
sl@0
   205
	@param	aTime Timeout in nanokernel ticks
sl@0
   206
	
sl@0
   207
	@return	KErrNone if no error; KErrInUse if timer is already active.
sl@0
   208
	
sl@0
   209
	@pre	Any context
sl@0
   210
	
sl@0
   211
	@see    NKern::TimerTicks()
sl@0
   212
 */
sl@0
   213
EXPORT_C TInt NTimer::OneShot(TInt aTime)
sl@0
   214
	{
sl@0
   215
	return OneShot(aTime,FALSE);
sl@0
   216
	}
sl@0
   217
sl@0
   218
sl@0
   219
/** Starts a nanokernel timer in one-shot mode with ISR or DFC callback.
sl@0
   220
	
sl@0
   221
	Queues the timer to expire in the specified number of nanokernel ticks. The
sl@0
   222
	actual wait time will be at least that much and may be up to one tick more.
sl@0
   223
	For normal timers (constructed with NTimerFn) the expiry handler will be
sl@0
   224
	called in either ISR context or in the context of the nanokernel timer
sl@0
   225
	thread (DfcThread1). For mutating timers (constructed with TDfcFn) the
sl@0
   226
	expiry handler is called in the context of the thread running the relevant
sl@0
   227
	TDfcQue.
sl@0
   228
sl@0
   229
    Note that NKern::TimerTicks() can be used to convert milliseconds to ticks.
sl@0
   230
sl@0
   231
	@param	aTime Timeout in nanokernel ticks
sl@0
   232
	@param	aDfc TRUE if DFC callback required, FALSE if ISR callback required.
sl@0
   233
			Note that this parameter is ignored for mutating timers.
sl@0
   234
	
sl@0
   235
	@return	KErrNone if no error
sl@0
   236
	@return	KErrInUse if timer is already active.
sl@0
   237
	@return	KErrDied if tied thread/group has exited
sl@0
   238
	
sl@0
   239
	@pre	Any context
sl@0
   240
	
sl@0
   241
	@see    NKern::TimerTicks()
sl@0
   242
 */
sl@0
   243
EXPORT_C TInt NTimer::OneShot(TInt aTime, TBool aDfc)
sl@0
   244
	{
sl@0
   245
	__NK_ASSERT_DEBUG(aTime>=0);
sl@0
   246
	/** iFn could be set to NULL after NTimer::OneShot(TInt, TDfc&) call.
sl@0
   247
	Call-back mechanism cannot be changed in the life time of a timer. */
sl@0
   248
	__NK_ASSERT_DEBUG(iFn!=NULL);
sl@0
   249
sl@0
   250
	TInt irq = TheTimerQ.iTimerSpinLock.LockIrqSave();
sl@0
   251
	if (!IsValid())
sl@0
   252
		{
sl@0
   253
		TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   254
		return KErrDied;
sl@0
   255
		}
sl@0
   256
	TUint16 state = i8816.iHState16;
sl@0
   257
	if (IsNormal())
sl@0
   258
		state &= 0xFF;
sl@0
   259
	else
sl@0
   260
		aDfc = FALSE;	// mutating timers start as ISR completion
sl@0
   261
	if (state!=EIdle)
sl@0
   262
		{
sl@0
   263
		TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   264
		return KErrInUse;
sl@0
   265
		}
sl@0
   266
	mb();	// ensure that if we observe an idle state all accesses to the NTimer have also been observed
sl@0
   267
	i_NTimer_iCompleteInDfc=TUint8(aDfc?1:0);
sl@0
   268
	iTriggerTime=TheTimerQ.iMsCount+(TUint32)aTime;
sl@0
   269
	TheTimerQ.Add(this);
sl@0
   270
	TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   271
	return KErrNone;
sl@0
   272
	}
sl@0
   273
sl@0
   274
sl@0
   275
/** Starts a nanokernel timer in one-shot mode with callback in dfc thread that provided DFC belongs to.
sl@0
   276
	
sl@0
   277
	Queues the timer to expire in the specified number of nanokernel ticks. The
sl@0
   278
	actual wait time will be at least that much and may be up to one tick more.
sl@0
   279
	On expiry aDfc will be queued in ISR context.
sl@0
   280
sl@0
   281
    Note that NKern::TimerTicks() can be used to convert milliseconds to ticks.
sl@0
   282
sl@0
   283
	@param	aTime Timeout in nanokernel ticks
sl@0
   284
	@param	aDfc - Dfc to be queued when the timer expires.
sl@0
   285
	
sl@0
   286
	@return	KErrNone if no error
sl@0
   287
	@return	KErrInUse if timer is already active.
sl@0
   288
	@return	KErrDied if tied thread/group has exited
sl@0
   289
	
sl@0
   290
	@pre	Any context
sl@0
   291
	@pre	Must not be a mutating timer (constructed with TDfcFn)
sl@0
   292
	
sl@0
   293
	@see    NKern::TimerTicks()
sl@0
   294
 */
sl@0
   295
EXPORT_C TInt NTimer::OneShot(TInt aTime, TDfc& aDfc)
sl@0
   296
	{
sl@0
   297
	__NK_ASSERT_DEBUG(!IsMutating());
sl@0
   298
	__NK_ASSERT_DEBUG(aTime>=0);
sl@0
   299
	TInt irq = TheTimerQ.iTimerSpinLock.LockIrqSave();
sl@0
   300
	if (iHType != EEventHandlerNTimer)
sl@0
   301
		{
sl@0
   302
		TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   303
		return KErrDied;
sl@0
   304
		}
sl@0
   305
	if (i_NTimer_iState!=EIdle)
sl@0
   306
		{
sl@0
   307
		TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   308
		return KErrInUse;
sl@0
   309
		}
sl@0
   310
	mb();	// ensure that if we observe an idle state all accesses to the NTimer have also been observed
sl@0
   311
	i_NTimer_iCompleteInDfc = 0;
sl@0
   312
	iFn = NULL;
sl@0
   313
	iPtr = (TAny*) &aDfc;
sl@0
   314
	iTriggerTime=TheTimerQ.iMsCount+(TUint32)aTime;
sl@0
   315
	TheTimerQ.Add(this);
sl@0
   316
	TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   317
	return KErrNone;
sl@0
   318
	}
sl@0
   319
sl@0
   320
sl@0
   321
/** Starts a nanokernel timer in zero-drift periodic mode with ISR or DFC callback.
sl@0
   322
sl@0
   323
	Queues the timer to expire in the specified number of nanokernel ticks,
sl@0
   324
	measured from the time at which it last expired. This allows exact periodic
sl@0
   325
	timers to be implemented with no drift caused by delays in requeueing the
sl@0
   326
	timer.
sl@0
   327
sl@0
   328
	The expiry handler will be called in the same context as the previous timer
sl@0
   329
	expiry. Generally the way this is used is that NTimer::OneShot() is used to start 
sl@0
   330
	the first time interval and this specifies whether the callback is in ISR context 
sl@0
   331
	or in the context of the nanokernel timer thread (DfcThread1) or other Dfc thread.
sl@0
   332
	The expiry handler then uses NTimer::Again() to requeue the timer.
sl@0
   333
sl@0
   334
	@param	aTime Timeout in nanokernel ticks
sl@0
   335
sl@0
   336
	@return	KErrNone if no error
sl@0
   337
	@return	KErrInUse if timer is already active;
sl@0
   338
	@return	KErrArgument if the requested expiry time is in the past.
sl@0
   339
	@return	KErrDied if tied thread/group has exited
sl@0
   340
	        
sl@0
   341
	@pre	Any context
sl@0
   342
 */
sl@0
   343
EXPORT_C TInt NTimer::Again(TInt aTime)
sl@0
   344
//
sl@0
   345
// Wait aTime from last trigger time - used for periodic timers
sl@0
   346
//
sl@0
   347
	{
sl@0
   348
	__NK_ASSERT_DEBUG(aTime>0);
sl@0
   349
	TInt irq = TheTimerQ.iTimerSpinLock.LockIrqSave();
sl@0
   350
	if (!IsValid())
sl@0
   351
		{
sl@0
   352
		TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   353
		return KErrDied;
sl@0
   354
		}
sl@0
   355
	TUint16 state = i8816.iHState16;
sl@0
   356
	if (IsNormal())
sl@0
   357
		state &= 0xFF;
sl@0
   358
	if (state!=EIdle)
sl@0
   359
		{
sl@0
   360
		TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   361
		return KErrInUse;
sl@0
   362
		}
sl@0
   363
	mb();	// ensure that if we observe an idle state all accesses to the NTimer have also been observed
sl@0
   364
	TUint32 nextTick=TheTimerQ.iMsCount;
sl@0
   365
	TUint32 trigger=iTriggerTime+(TUint32)aTime;
sl@0
   366
	TUint32 d=trigger-nextTick;
sl@0
   367
	if (d>=0x80000000)
sl@0
   368
		{
sl@0
   369
		TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   370
		return KErrArgument;		// requested time is in the past
sl@0
   371
		}
sl@0
   372
	iTriggerTime=trigger;
sl@0
   373
	TheTimerQ.Add(this);
sl@0
   374
	TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   375
	return KErrNone;
sl@0
   376
	}
sl@0
   377
sl@0
   378
sl@0
   379
/** Cancels a nanokernel timer.
sl@0
   380
sl@0
   381
	Removes this timer from the nanokernel timer queue. Does nothing if the
sl@0
   382
	timer is inactive or has already expired.
sl@0
   383
	Note that if the timer was queued and DFC callback requested it is possible
sl@0
   384
	for the expiry handler to run even after Cancel() has been called. This will
sl@0
   385
	occur in the case where DfcThread1 is preempted just before calling the
sl@0
   386
	expiry handler for this timer and the preempting thread/ISR/IDFC calls
sl@0
   387
	Cancel() on the timer.
sl@0
   388
sl@0
   389
	@pre	Any context for a non-mutating NTimer (constructed with NTimerFn)
sl@0
   390
	@pre	For mutating NTimer (constructed with TDfcFn), IDFC or thread context only.
sl@0
   391
	@return	TRUE if timer was actually cancelled
sl@0
   392
	@return	FALSE if timer was not cancelled - this could be because it was not
sl@0
   393
				active or because its expiry handler was already running on
sl@0
   394
				another CPU or in the timer DFC.
sl@0
   395
 */
sl@0
   396
EXPORT_C TBool NTimer::Cancel()
sl@0
   397
	{
sl@0
   398
	if (IsMutating() && iDfcQ)
sl@0
   399
		return DoCancelMutating(0);
sl@0
   400
	return DoCancel(0)!=EIdle;
sl@0
   401
	}
sl@0
   402
sl@0
   403
void NTimer::DoCancel0(TUint aState)
sl@0
   404
	{
sl@0
   405
	if (aState>ETransferring && aState<=EFinal)	// idle or transferring timers are not on a queue
sl@0
   406
		Deque();
sl@0
   407
	switch (aState)
sl@0
   408
		{
sl@0
   409
		case ETransferring:	// signal DFC to abort this iteration
sl@0
   410
			TheTimerQ.iTransferringCancelled=TRUE;
sl@0
   411
			break;
sl@0
   412
		case ECritical:		// signal DFC to abort this iteration
sl@0
   413
			TheTimerQ.iCriticalCancelled=TRUE;
sl@0
   414
			break;
sl@0
   415
		case EFinal:
sl@0
   416
			{
sl@0
   417
			// Need to clear bit in iPresent if both final queues now empty
sl@0
   418
			// NOTE: Timer might actually be on the completed queue rather than the final queue
sl@0
   419
			//		 but the check is harmless in any case.
sl@0
   420
			TInt i=iTriggerTime & NTimerQ::ETimerQMask;
sl@0
   421
			NTimerQ::STimerQ& q=TheTimerQ.iTickQ[i];
sl@0
   422
			if (q.iIntQ.IsEmpty() && q.iDfcQ.IsEmpty())
sl@0
   423
				TheTimerQ.iPresent &= ~(1<<i);
sl@0
   424
			break;
sl@0
   425
			}
sl@0
   426
		case EIdle:			// nothing to do
sl@0
   427
		case EHolding:		// just deque
sl@0
   428
		case EOrdered:		// just deque
sl@0
   429
			break;
sl@0
   430
		default:
sl@0
   431
			__NK_ASSERT_ALWAYS(0);
sl@0
   432
		}
sl@0
   433
	}
sl@0
   434
sl@0
   435
TUint NTimer::DoCancel(TUint aFlags)
sl@0
   436
	{
sl@0
   437
	NSchedulable* tied = 0;
sl@0
   438
	TInt irq = NKern::DisableAllInterrupts();
sl@0
   439
	TheTimerQ.iTimerSpinLock.LockOnly();
sl@0
   440
	TUint state = i_NTimer_iState;
sl@0
   441
	mb();
sl@0
   442
	if (IsNormal() && state>=EEventQ)
sl@0
   443
		{
sl@0
   444
		// It's on a CPU's event handler queue
sl@0
   445
		TInt cpu = state - EEventQ;
sl@0
   446
		if (cpu < TheScheduler.iNumCpus)
sl@0
   447
			{
sl@0
   448
			TSubScheduler* ss = TheSubSchedulers + cpu;
sl@0
   449
			ss->iEventHandlerLock.LockOnly();
sl@0
   450
			state = i_NTimer_iState;
sl@0
   451
			if (state != EIdle)
sl@0
   452
				{
sl@0
   453
				Deque();	// we got to it first
sl@0
   454
				tied = iTied;
sl@0
   455
				i_NTimer_iState = EIdle;
sl@0
   456
				}
sl@0
   457
			ss->iEventHandlerLock.UnlockOnly();
sl@0
   458
			goto end;
sl@0
   459
			}
sl@0
   460
		}
sl@0
   461
	DoCancel0(state);
sl@0
   462
	if (IsMutating())
sl@0
   463
		i8816.iHState16 = 0;
sl@0
   464
	else
sl@0
   465
		i_NTimer_iState=EIdle;
sl@0
   466
end:
sl@0
   467
	if (aFlags & ECancelDestroy)
sl@0
   468
		iHType = EEventHandlerDummy;
sl@0
   469
	TheTimerQ.iTimerSpinLock.UnlockOnly();
sl@0
   470
	if (tied)
sl@0
   471
		tied->EndTiedEvent();	// FIXME - Could be called in thread context
sl@0
   472
	NKern::RestoreInterrupts(irq);
sl@0
   473
	return state;
sl@0
   474
	}
sl@0
   475
sl@0
   476
TBool NTimer::DoCancelMutating(TUint aFlags)
sl@0
   477
	{
sl@0
   478
	CHECK_PRECONDITIONS(MASK_NOT_ISR,"NTimer::Cancel (mutating NTimer)");
sl@0
   479
	TSubScheduler& ss0 = SubScheduler();
sl@0
   480
	TBool wait = FALSE;
sl@0
   481
	TInt cpu = -1;
sl@0
   482
	TBool result = TRUE;
sl@0
   483
	TDfc* d = (TDfc*)this;
sl@0
   484
	NKern::Lock();
sl@0
   485
	TDfcQue* q = iDfcQ;
sl@0
   486
	NThreadBase* t = q->iThread;
sl@0
   487
	t->AcqSLock();
sl@0
   488
	TheTimerQ.iTimerSpinLock.LockIrq();
sl@0
   489
sl@0
   490
	// 0000->0000, XX00->ZZ00, xxYY->zzYY
sl@0
   491
	TUint state = d->CancelInitialStateChange();
sl@0
   492
	if (state & 0xFF00)
sl@0
   493
		{
sl@0
   494
		// someone else cancelling at the same time - just wait for them to finish
sl@0
   495
		// they can only be waiting for the cancel IPI
sl@0
   496
		result = FALSE;
sl@0
   497
		wait = TRUE;
sl@0
   498
		goto end;
sl@0
   499
		}
sl@0
   500
	if (state == 0)	// timer was not active
sl@0
   501
		{
sl@0
   502
		result = FALSE;
sl@0
   503
		goto end;
sl@0
   504
		}
sl@0
   505
	if (state>=ETransferring && state<=EFinal)
sl@0
   506
		{
sl@0
   507
		DoCancel0(state);
sl@0
   508
		// cancel is complete
sl@0
   509
		goto reset;
sl@0
   510
		}
sl@0
   511
	if (state==1)
sl@0
   512
		{
sl@0
   513
		// on DFC final queue
sl@0
   514
		q->Remove((TPriListLink*)this);
sl@0
   515
		goto reset;
sl@0
   516
		}
sl@0
   517
	// must be on IDFC queue - need to send cancel IPI
sl@0
   518
	__NK_ASSERT_ALWAYS((state>>5)==4);
sl@0
   519
	cpu = state & 0x1f;
sl@0
   520
	if (TUint(cpu) == ss0.iCpuNum)
sl@0
   521
		{
sl@0
   522
		// it's on this CPU's IDFC queue so just dequeue it and finish
sl@0
   523
		Deque();
sl@0
   524
		cpu = -1;
sl@0
   525
reset:
sl@0
   526
		d->ResetState();	// release semantics
sl@0
   527
		}
sl@0
   528
end:
sl@0
   529
	if (aFlags & ECancelDestroy)
sl@0
   530
		iHType = EEventHandlerDummy;
sl@0
   531
	TheTimerQ.iTimerSpinLock.UnlockIrq();
sl@0
   532
	t->RelSLock();
sl@0
   533
	if (cpu>=0)
sl@0
   534
		{
sl@0
   535
		TCancelIPI ipi;
sl@0
   536
		ipi.Send(d, cpu);
sl@0
   537
		ipi.WaitCompletion();
sl@0
   538
		wait = TRUE;
sl@0
   539
		}
sl@0
   540
	if (wait)
sl@0
   541
		{
sl@0
   542
		TUint n = 0x01000000;
sl@0
   543
		while ((i8816.iHState16>>8) & ss0.iCpuMask)
sl@0
   544
			{
sl@0
   545
			__chill();
sl@0
   546
			if (!--n)
sl@0
   547
				__crash();
sl@0
   548
			}
sl@0
   549
		}
sl@0
   550
	NKern::Unlock();
sl@0
   551
	return result;
sl@0
   552
	}
sl@0
   553
#endif
sl@0
   554
sl@0
   555
sl@0
   556
/** Obtains the address of the nanokernel timer queue object.
sl@0
   557
sl@0
   558
	Not intended for general use. Intended only for base ports in order to get
sl@0
   559
	the address used to call NTimerQ::Tick() with.
sl@0
   560
sl@0
   561
	@return	The address of the nanokernel timer queue object
sl@0
   562
	@pre	Any context
sl@0
   563
 */
sl@0
   564
EXPORT_C TAny* NTimerQ::TimerAddress()
sl@0
   565
	{
sl@0
   566
	return &TheTimerQ;
sl@0
   567
	}
sl@0
   568
sl@0
   569
NTimerQ::NTimerQ()
sl@0
   570
	:	iDfc(NTimerQ::DfcFn,this,NULL,KTimerQDfcPriority),
sl@0
   571
		iDfcCompleteCount(1),
sl@0
   572
		iTimerSpinLock(TSpinLock::EOrderNTimerQ)
sl@0
   573
	{
sl@0
   574
	// NOTE: All other members are initialised to zero since the single instance
sl@0
   575
	//		 of NTimerQ resides in .bss
sl@0
   576
	}
sl@0
   577
sl@0
   578
void NTimerQ::Init1(TInt aTickPeriod)
sl@0
   579
	{
sl@0
   580
	TheTimerQ.iTickPeriod=aTickPeriod;
sl@0
   581
	__KTRACE_OPT(KBOOT,DEBUGPRINT("NTimerQ::Init1 - period %d us",aTickPeriod));
sl@0
   582
	__KTRACE_OPT(KMEMTRACE, DEBUGPRINT("MT:P %d",aTickPeriod));
sl@0
   583
	}
sl@0
   584
sl@0
   585
void NTimerQ::Init3(TDfcQue* aDfcQ)
sl@0
   586
	{
sl@0
   587
	__KTRACE_OPT(KBOOT,DEBUGPRINT("NTimerQ::Init3 DFCQ at %08x",aDfcQ));
sl@0
   588
	TheTimerQ.iDfc.SetDfcQ(aDfcQ);
sl@0
   589
	}
sl@0
   590
sl@0
   591
#ifndef __MSTIM_MACHINE_CODED__
sl@0
   592
void NTimerQ::Add(NTimer* aTimer)
sl@0
   593
//
sl@0
   594
//	Internal function to add a timer to the queue.
sl@0
   595
//	Enter and return with timer queue spin lock held.
sl@0
   596
//
sl@0
   597
	{
sl@0
   598
	TInt t=TInt(aTimer->iTriggerTime-iMsCount);
sl@0
   599
	if (t<ENumTimerQueues)
sl@0
   600
		AddFinal(aTimer);
sl@0
   601
	else
sl@0
   602
		{
sl@0
   603
		// >=32ms to expiry, so put on holding queue
sl@0
   604
		aTimer->i_NTimer_iState=NTimer::EHolding;
sl@0
   605
		iHoldingQ.Add(aTimer);
sl@0
   606
		}
sl@0
   607
	}
sl@0
   608
sl@0
   609
void NTimerQ::AddFinal(NTimer* aTimer)
sl@0
   610
//
sl@0
   611
//	Internal function to add a timer to the corresponding final queue.
sl@0
   612
//	Enter and return with timer queue spin lock held.
sl@0
   613
//
sl@0
   614
	{
sl@0
   615
	TInt i=aTimer->iTriggerTime & ETimerQMask;
sl@0
   616
	SDblQue* pQ;
sl@0
   617
	if (aTimer->i_NTimer_iCompleteInDfc)
sl@0
   618
		pQ=&iTickQ[i].iDfcQ;
sl@0
   619
	else
sl@0
   620
		pQ=&iTickQ[i].iIntQ;
sl@0
   621
	iPresent |= (1<<i);
sl@0
   622
	aTimer->i_NTimer_iState=NTimer::EFinal;
sl@0
   623
	pQ->Add(aTimer);
sl@0
   624
	}
sl@0
   625
sl@0
   626
void NTimerQ::DfcFn(TAny* aPtr)
sl@0
   627
	{
sl@0
   628
	((NTimerQ*)aPtr)->Dfc();
sl@0
   629
	}
sl@0
   630
sl@0
   631
void NTimerQ::Dfc()
sl@0
   632
//
sl@0
   633
// Do deferred timer queue processing and/or DFC completions
sl@0
   634
//
sl@0
   635
	{
sl@0
   636
	// First transfer entries on the Ordered queue to the Final queues
sl@0
   637
	FOREVER
sl@0
   638
		{
sl@0
   639
		iTimerSpinLock.LockIrq();
sl@0
   640
		if (iOrderedQ.IsEmpty())
sl@0
   641
			break;
sl@0
   642
		NTimer* pC=(NTimer*)iOrderedQ.First();
sl@0
   643
		TInt remain=pC->iTriggerTime-iMsCount;
sl@0
   644
		if (remain>=ENumTimerQueues)
sl@0
   645
			break;
sl@0
   646
sl@0
   647
		// If remaining time <32 ticks, add it to final queue;
sl@0
   648
		// also if remain < 0 we've 'missed it' so add to final queue.
sl@0
   649
		pC->Deque();
sl@0
   650
		AddFinal(pC);
sl@0
   651
		iTimerSpinLock.UnlockIrq();
sl@0
   652
		__DEBUG_CALLBACK(0);
sl@0
   653
		}
sl@0
   654
	iTimerSpinLock.UnlockIrq();
sl@0
   655
	__DEBUG_CALLBACK(1);
sl@0
   656
sl@0
   657
	// Next transfer entries on the Holding queue to the Ordered queue or final queue
sl@0
   658
	FOREVER
sl@0
   659
		{
sl@0
   660
		iTimerSpinLock.LockIrq();
sl@0
   661
		if (iHoldingQ.IsEmpty())
sl@0
   662
			break;
sl@0
   663
		NTimer* pC=(NTimer*)iHoldingQ.First();
sl@0
   664
		pC->Deque();
sl@0
   665
		pC->i_NTimer_iState=NTimer::ETransferring;
sl@0
   666
		iTransferringCancelled=FALSE;
sl@0
   667
		TUint32 trigger=pC->iTriggerTime;
sl@0
   668
		if (TInt(trigger-iMsCount)<ENumTimerQueues)
sl@0
   669
			{
sl@0
   670
			// <32ms remaining so put it on final queue
sl@0
   671
			AddFinal(pC);
sl@0
   672
			}
sl@0
   673
		else
sl@0
   674
			{
sl@0
   675
			FOREVER
sl@0
   676
				{
sl@0
   677
				iTimerSpinLock.UnlockIrq();
sl@0
   678
				__DEBUG_CALLBACK(2);
sl@0
   679
sl@0
   680
				// we now need to walk ordered queue to find correct position for pC
sl@0
   681
				SDblQueLink* anchor=&iOrderedQ.iA;
sl@0
   682
				iCriticalCancelled=FALSE;
sl@0
   683
				iTimerSpinLock.LockIrq();
sl@0
   684
				NTimer* pN=(NTimer*)iOrderedQ.First();
sl@0
   685
				while (pN!=anchor && !iTransferringCancelled)
sl@0
   686
					{
sl@0
   687
					if ((pN->iTriggerTime-trigger)<0x80000000u)
sl@0
   688
						break;	// insert before pN
sl@0
   689
					pN->i_NTimer_iState=NTimer::ECritical;
sl@0
   690
					iTimerSpinLock.UnlockIrq();
sl@0
   691
					__DEBUG_CALLBACK(3);
sl@0
   692
					iTimerSpinLock.LockIrq();
sl@0
   693
					if (iCriticalCancelled)
sl@0
   694
						break;
sl@0
   695
					pN->i_NTimer_iState=NTimer::EOrdered;
sl@0
   696
					pN=(NTimer*)pN->iNext;
sl@0
   697
					}
sl@0
   698
sl@0
   699
				if (iTransferringCancelled)
sl@0
   700
					break;		// this one has been cancelled, go on to next one
sl@0
   701
				if (!iCriticalCancelled)
sl@0
   702
					{
sl@0
   703
					pC->InsertBefore(pN);
sl@0
   704
					pC->i_NTimer_iState=NTimer::EOrdered;
sl@0
   705
					break;		// done this one
sl@0
   706
					}
sl@0
   707
				}
sl@0
   708
			}
sl@0
   709
		iTimerSpinLock.UnlockIrq();
sl@0
   710
		__DEBUG_CALLBACK(4);
sl@0
   711
		}
sl@0
   712
	iTimerSpinLock.UnlockIrq();
sl@0
   713
	__DEBUG_CALLBACK(5);
sl@0
   714
sl@0
   715
	// Finally do call backs for timers which requested DFC callback
sl@0
   716
	FOREVER
sl@0
   717
		{
sl@0
   718
		iTimerSpinLock.LockIrq();
sl@0
   719
		if (iCompletedQ.IsEmpty())
sl@0
   720
			break;
sl@0
   721
		NTimer* pC=(NTimer*)iCompletedQ.First();
sl@0
   722
		pC->Deque();
sl@0
   723
		pC->i_NTimer_iState=NTimer::EIdle;
sl@0
   724
		TAny* p=pC->iPtr;
sl@0
   725
		NTimerFn f=pC->iFn;
sl@0
   726
		iTimerSpinLock.UnlockIrq();
sl@0
   727
		__DEBUG_CALLBACK(7);
sl@0
   728
		(*f)(p);
sl@0
   729
		}
sl@0
   730
	iTimerSpinLock.UnlockIrq();
sl@0
   731
	__e32_atomic_add_rel32(&iDfcCompleteCount, 2);
sl@0
   732
	}
sl@0
   733
sl@0
   734
sl@0
   735
/** Tick over the nanokernel timer queue.
sl@0
   736
	This function should be called by the base port in the system tick timer ISR.
sl@0
   737
	It should not be called at any other time.
sl@0
   738
	The value of 'this' to pass is the value returned by NTimerQ::TimerAddress().
sl@0
   739
sl@0
   740
	@see NTimerQ::TimerAddress()
sl@0
   741
 */
sl@0
   742
EXPORT_C void NTimerQ::Tick()
sl@0
   743
	{
sl@0
   744
	TInt irq = iTimerSpinLock.LockIrqSave();
sl@0
   745
	TInt i=iMsCount & ETimerQMask;
sl@0
   746
	iMsCount++;
sl@0
   747
	STimerQ* pQ=iTickQ+i;
sl@0
   748
	iPresent &= ~(1<<i);
sl@0
   749
	TBool doDfc=FALSE;
sl@0
   750
	if (!pQ->iDfcQ.IsEmpty())
sl@0
   751
		{
sl@0
   752
		// transfer DFC completions to completed queue and queue DFC
sl@0
   753
		iCompletedQ.MoveFrom(&pQ->iDfcQ);
sl@0
   754
		doDfc=TRUE;
sl@0
   755
		}
sl@0
   756
	if ((i&(ETimerQMask>>1))==0)
sl@0
   757
		{
sl@0
   758
		// Every 16 ticks we check if a DFC is required.
sl@0
   759
		// This allows a DFC latency of up to 16 ticks before timers are missed.
sl@0
   760
		if (!iHoldingQ.IsEmpty())
sl@0
   761
			{
sl@0
   762
			doDfc=TRUE;				// if holding queue nonempty, queue DFC to sort
sl@0
   763
			}
sl@0
   764
		else if (!iOrderedQ.IsEmpty())
sl@0
   765
			{
sl@0
   766
			// if first ordered queue entry expires in <32ms, queue the DFC to transfer
sl@0
   767
			NTimer* pC=(NTimer*)iOrderedQ.First();
sl@0
   768
			TUint x = pC->iTriggerTime - iMsCount;
sl@0
   769
			if (x < (TUint)ENumTimerQueues)
sl@0
   770
				{
sl@0
   771
				doDfc=TRUE;
sl@0
   772
				}
sl@0
   773
			}
sl@0
   774
		}
sl@0
   775
	if (!pQ->iIntQ.IsEmpty())
sl@0
   776
		{
sl@0
   777
		// transfer ISR completions to a temporary queue
sl@0
   778
		// careful here - other CPUs could dequeue timers!
sl@0
   779
		SDblQue q(&pQ->iIntQ,0);
sl@0
   780
		for (; !q.IsEmpty(); iTimerSpinLock.LockIrqSave())
sl@0
   781
			{
sl@0
   782
			NTimer* pC=(NTimer*)q.First();
sl@0
   783
			pC->Deque();
sl@0
   784
			if (pC->IsMutating())
sl@0
   785
				{
sl@0
   786
				pC->AddAsDFC();			//mutate NTimer into TDfc and Add() it
sl@0
   787
				iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   788
				continue;
sl@0
   789
				}
sl@0
   790
			if (!pC->iFn)
sl@0
   791
				{
sl@0
   792
				pC->i_NTimer_iState=NTimer::EIdle;
sl@0
   793
				iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   794
				((TDfc*)(pC->iPtr))->Add();
sl@0
   795
				continue;
sl@0
   796
				}
sl@0
   797
			NSchedulable* tied = pC->iTied;
sl@0
   798
			if (tied)
sl@0
   799
				{
sl@0
   800
				TInt cpu = tied->BeginTiedEvent();
sl@0
   801
				if (cpu != NKern::CurrentCpu())
sl@0
   802
					{
sl@0
   803
					pC->i_NTimer_iState = TUint8(NTimer::EEventQ + cpu);
sl@0
   804
					TSubScheduler* ss = TheSubSchedulers + cpu;
sl@0
   805
					TBool kick = ss->QueueEvent(pC);
sl@0
   806
					iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   807
					if (kick)
sl@0
   808
						send_irq_ipi(ss);
sl@0
   809
					continue;
sl@0
   810
					}
sl@0
   811
				}
sl@0
   812
			pC->i_NTimer_iState=NTimer::EIdle;
sl@0
   813
			TAny* p = pC->iPtr;
sl@0
   814
			NTimerFn f = pC->iFn;
sl@0
   815
			iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   816
			(*f)(p);
sl@0
   817
			if (tied)
sl@0
   818
				tied->EndTiedEvent();
sl@0
   819
			}
sl@0
   820
		}
sl@0
   821
	iTimerSpinLock.UnlockIrqRestore(irq);
sl@0
   822
	if (doDfc)
sl@0
   823
		iDfc.Add();
sl@0
   824
	}
sl@0
   825
sl@0
   826
sl@0
   827
/** Mutate an NTimer into a DFC and Add() it
sl@0
   828
sl@0
   829
If NTimer state is EFinal, change to DFC state 008n and add to endogenous IDFC
sl@0
   830
queue for this CPU.
sl@0
   831
sl@0
   832
Enter and return with IRQs disabled and timer spin lock held
sl@0
   833
No need to worry about Cancel()s since timer spin lock is held
sl@0
   834
Don't touch iHState0
sl@0
   835
sl@0
   836
@internalComponent
sl@0
   837
*/
sl@0
   838
void NTimer::AddAsDFC()
sl@0
   839
	{
sl@0
   840
	TSubScheduler& ss = SubScheduler();
sl@0
   841
	i8816.iHState16 = (TUint16)(0x80|ss.iCpuNum);
sl@0
   842
	ss.iDfcs.Add(this);
sl@0
   843
	ss.iDfcPendingFlag = 1;
sl@0
   844
	}
sl@0
   845
sl@0
   846
sl@0
   847
/** Check if a nanokernel timer is pending or not
sl@0
   848
sl@0
   849
	@return	TRUE if the timer is pending (OneShot() etc. would return KErrInUse)
sl@0
   850
	@return FALSE if the timer is idle (OneShot() etc. would succeed)
sl@0
   851
	@pre	Any context
sl@0
   852
sl@0
   853
	@publishedPartner
sl@0
   854
	@prototype
sl@0
   855
 */
sl@0
   856
EXPORT_C TBool NTimer::IsPending()
sl@0
   857
	{
sl@0
   858
	TUint16 state = i8816.iHState16;
sl@0
   859
	return state != EIdle;
sl@0
   860
	}
sl@0
   861
sl@0
   862
sl@0
   863
/** Return the number of ticks before the next nanokernel timer expiry.
sl@0
   864
	May on occasion return a pessimistic estimate (i.e. too low).
sl@0
   865
	Used by base port to disable the system tick interrupt when the system
sl@0
   866
	is idle.
sl@0
   867
sl@0
   868
	@return	The number of ticks before the next nanokernel timer expiry.
sl@0
   869
	
sl@0
   870
	@pre	Interrupts must be disabled.
sl@0
   871
	
sl@0
   872
	@post	Interrupts are disabled.
sl@0
   873
 */
sl@0
   874
EXPORT_C TInt NTimerQ::IdleTime()
sl@0
   875
	{
sl@0
   876
	CHECK_PRECONDITIONS(MASK_INTERRUPTS_DISABLED,"NTimerQ::IdleTime");	
sl@0
   877
	NTimerQ& m=TheTimerQ;
sl@0
   878
	TUint32 next=m.iMsCount;	// number of next tick
sl@0
   879
	TUint32 p=m.iPresent;
sl@0
   880
	TInt r=KMaxTInt;
sl@0
   881
	if (p)
sl@0
   882
		{
sl@0
   883
		// Final queues nonempty
sl@0
   884
		TInt nx=next&0x1f;				// number of next tick modulo 32
sl@0
   885
		p=(p>>nx)|(p<<(32-nx));			// rotate p right by nx (so lsb corresponds to next tick)
sl@0
   886
		r=__e32_find_ls1_32(p);	// find number of zeros before LS 1
sl@0
   887
		}
sl@0
   888
	if (!m.iHoldingQ.IsEmpty())
sl@0
   889
		{
sl@0
   890
		// Sort operation required - need to process next tick divisible by 16
sl@0
   891
		TInt nx=next&0x0f;				// number of next tick modulo 16
sl@0
   892
		TInt r2=nx?(16-nx):0;			// number of ticks before next divisible by 16
sl@0
   893
		if (r2<r)
sl@0
   894
			r=r2;
sl@0
   895
		}
sl@0
   896
	if (!m.iOrderedQ.IsEmpty())
sl@0
   897
		{
sl@0
   898
		// Timers present on ordered queue
sl@0
   899
		NTimer* pC=(NTimer*)m.iOrderedQ.First();
sl@0
   900
		TUint32 tt=pC->iTriggerTime;
sl@0
   901
		tt=(tt&~0x0f)-16;				// time at which transfer to final queue would occur
sl@0
   902
		TInt r3=(TInt)(tt-next);
sl@0
   903
		if (r3<r)
sl@0
   904
			r=r3;
sl@0
   905
		}
sl@0
   906
	return r;
sl@0
   907
	}
sl@0
   908
#endif
sl@0
   909
sl@0
   910
sl@0
   911
/** Advance the nanokernel timer queue by the specified number of ticks.
sl@0
   912
	It is assumed that no timers expire as a result of this.
sl@0
   913
	Used by base port when system comes out of idle mode after disabling the
sl@0
   914
	system tick interrupt to bring the timer queue up to date.
sl@0
   915
sl@0
   916
	@param	aTicks Number of ticks skipped due to tick suppression
sl@0
   917
sl@0
   918
	@pre	Interrupts must be disabled.
sl@0
   919
sl@0
   920
	@post	Interrupts are disabled.
sl@0
   921
 */
sl@0
   922
EXPORT_C void NTimerQ::Advance(TInt aTicks)
sl@0
   923
	{
sl@0
   924
	CHECK_PRECONDITIONS(MASK_INTERRUPTS_DISABLED,"NTimerQ::Advance");	
sl@0
   925
	TheTimerQ.iMsCount+=(TUint32)aTicks;
sl@0
   926
	}
sl@0
   927
sl@0
   928
sl@0
   929
/**	Returns the period of the nanokernel timer.
sl@0
   930
	@return Period in microseconds
sl@0
   931
	@pre any context
sl@0
   932
	@see NTimer
sl@0
   933
 */
sl@0
   934
EXPORT_C TInt NKern::TickPeriod()
sl@0
   935
	{
sl@0
   936
	return TheTimerQ.iTickPeriod;
sl@0
   937
	}
sl@0
   938
sl@0
   939
sl@0
   940
/**	Converts a time interval to timer ticks.
sl@0
   941
sl@0
   942
	@param aMilliseconds time interval in milliseconds.
sl@0
   943
	@return Number of nanokernel timer ticks.  Non-integral results are rounded up.
sl@0
   944
sl@0
   945
 	@pre aMilliseconds should be <=2147483 to avoid integer overflow.
sl@0
   946
	@pre any context
sl@0
   947
 */
sl@0
   948
EXPORT_C TInt NKern::TimerTicks(TInt aMilliseconds)
sl@0
   949
	{
sl@0
   950
	__ASSERT_WITH_MESSAGE_DEBUG(aMilliseconds<=2147483,"aMilliseconds should be <=2147483","NKern::TimerTicks");
sl@0
   951
	TUint32 msp=TheTimerQ.iTickPeriod;
sl@0
   952
	if (msp==1000)	// will be true except on pathological hardware
sl@0
   953
		return aMilliseconds;
sl@0
   954
	TUint32 us=(TUint32)aMilliseconds*1000;
sl@0
   955
	return (us+msp-1)/msp;
sl@0
   956
	}
sl@0
   957