os/kernelhwsrv/kernel/eka/nkern/sched.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
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\sched.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
// TDfc member data
sl@0
    19
#define __INCLUDE_TDFC_DEFINES__
sl@0
    20
#define __INCLUDE_NTHREADBASE_DEFINES__
sl@0
    21
sl@0
    22
#include "nk_priv.h"
sl@0
    23
sl@0
    24
void ResumeDelayedThreads(TAny*)
sl@0
    25
	{
sl@0
    26
	TScheduler& s=TheScheduler;
sl@0
    27
	while (!s.iDelayedQ.IsEmpty())
sl@0
    28
		{
sl@0
    29
		NThreadBase& t = *(NThreadBase*)s.iDelayedQ.First()->Deque();
sl@0
    30
		t.i_ThrdAttr &= ~KThreadAttDelayed;
sl@0
    31
		if (t.iSuspendCount==0)
sl@0
    32
			t.DoReady();
sl@0
    33
		else
sl@0
    34
			t.iNState=NThreadBase::ESuspended;
sl@0
    35
		}
sl@0
    36
	}
sl@0
    37
sl@0
    38
// Don't need to zero initialise anything here since
sl@0
    39
// TheScheduler resides in .bss
sl@0
    40
TScheduler::TScheduler()
sl@0
    41
	:	TPriListBase(KNumPriorities), iKernCSLocked(1), iDelayDfc(ResumeDelayedThreads, NULL)
sl@0
    42
	{
sl@0
    43
	}
sl@0
    44
sl@0
    45
sl@0
    46
/** Return a pointer to the scheduler
sl@0
    47
	Intended for use by the crash debugger, not for general device driver use.
sl@0
    48
sl@0
    49
	@return	Pointer to the scheduler object
sl@0
    50
	@internalTechnology
sl@0
    51
 */
sl@0
    52
EXPORT_C TScheduler* TScheduler::Ptr()
sl@0
    53
	{
sl@0
    54
	return &TheScheduler;
sl@0
    55
	}
sl@0
    56
sl@0
    57
sl@0
    58
#ifndef __MSTIM_MACHINE_CODED__
sl@0
    59
// Enter and leave with interrupts off
sl@0
    60
void TScheduler::TimesliceTick()
sl@0
    61
	{
sl@0
    62
	NThreadBase* pC=iCurrentThread;
sl@0
    63
	if (pC->iTime>0 && --pC->iTime==0)
sl@0
    64
		RescheduleNeeded();
sl@0
    65
	}
sl@0
    66
#endif
sl@0
    67
sl@0
    68
#ifndef __SCHEDULER_MACHINE_CODED__
sl@0
    69
void TScheduler::Remove(NThreadBase* aThread)
sl@0
    70
	{
sl@0
    71
	__NK_ASSERT_DEBUG(!aThread->iHeldFastMutex);	// can't block while holding fast mutex
sl@0
    72
	aThread->iTime=aThread->iTimeslice;		// thread has blocked so it gets a fresh timeslice for next time
sl@0
    73
	TPriListBase::Remove(aThread);
sl@0
    74
	}
sl@0
    75
#endif
sl@0
    76
sl@0
    77
void TScheduler::RotateReadyList(TInt p)
sl@0
    78
//
sl@0
    79
// rotate the ready list for priority p
sl@0
    80
//
sl@0
    81
	{
sl@0
    82
	__NK_ASSERT_DEBUG(p>=0 && p<KNumPriorities);
sl@0
    83
	SDblQueLink* pQ=iQueue[p];
sl@0
    84
	if (pQ)
sl@0
    85
		{
sl@0
    86
		SDblQueLink* pN=pQ->iNext;
sl@0
    87
		if (pN!=pQ)
sl@0
    88
			{
sl@0
    89
			NThread* pT=(NThread*)pQ;
sl@0
    90
			pT->iTime=pT->iTimeslice;
sl@0
    91
			iQueue[p]=pN;
sl@0
    92
			if (pQ==iCurrentThread)
sl@0
    93
				RescheduleNeeded();
sl@0
    94
			}
sl@0
    95
		}
sl@0
    96
	}
sl@0
    97
sl@0
    98
/**	Converts a time interval in microseconds to thread timeslice ticks
sl@0
    99
sl@0
   100
	@param aMicroseconds time interval in microseconds.
sl@0
   101
	@return Number of thread timeslice ticks.  Non-integral results are rounded up.
sl@0
   102
sl@0
   103
 	@pre aMicroseconds should be nonnegative
sl@0
   104
	@pre any context
sl@0
   105
 */
sl@0
   106
EXPORT_C TInt NKern::TimesliceTicks(TUint32 aMicroseconds)
sl@0
   107
	{
sl@0
   108
	TUint32 msp = TheTimerQ.iTickPeriod;
sl@0
   109
	TUint32 ticks = (TUint32(aMicroseconds) + msp - 1) / msp;
sl@0
   110
	return ticks;
sl@0
   111
	}
sl@0
   112
sl@0
   113
sl@0
   114
/** Return the total CPU time so far used by the specified thread.
sl@0
   115
sl@0
   116
	@return The total CPU time in units of 1/NKern::CpuTimeMeasFreq().
sl@0
   117
*/
sl@0
   118
EXPORT_C TUint64 NKern::ThreadCpuTime(NThread* aThread)
sl@0
   119
	{
sl@0
   120
#ifdef MONITOR_THREAD_CPU_TIME
sl@0
   121
	NKern::Lock();
sl@0
   122
	TUint64 t = aThread->iTotalCpuTime;
sl@0
   123
	if (aThread == TheScheduler.iCurrentThread)
sl@0
   124
		t += TUint64(NKern::FastCounter() - aThread->iLastStartTime);
sl@0
   125
	NKern::Unlock();
sl@0
   126
	return t;
sl@0
   127
#else
sl@0
   128
	return 0;
sl@0
   129
#endif
sl@0
   130
	}