First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\nkern\sched.cpp
19 #define __INCLUDE_TDFC_DEFINES__
20 #define __INCLUDE_NTHREADBASE_DEFINES__
24 void ResumeDelayedThreads(TAny*)
26 TScheduler& s=TheScheduler;
27 while (!s.iDelayedQ.IsEmpty())
29 NThreadBase& t = *(NThreadBase*)s.iDelayedQ.First()->Deque();
30 t.i_ThrdAttr &= ~KThreadAttDelayed;
31 if (t.iSuspendCount==0)
34 t.iNState=NThreadBase::ESuspended;
38 // Don't need to zero initialise anything here since
39 // TheScheduler resides in .bss
40 TScheduler::TScheduler()
41 : TPriListBase(KNumPriorities), iKernCSLocked(1), iDelayDfc(ResumeDelayedThreads, NULL)
46 /** Return a pointer to the scheduler
47 Intended for use by the crash debugger, not for general device driver use.
49 @return Pointer to the scheduler object
52 EXPORT_C TScheduler* TScheduler::Ptr()
58 #ifndef __MSTIM_MACHINE_CODED__
59 // Enter and leave with interrupts off
60 void TScheduler::TimesliceTick()
62 NThreadBase* pC=iCurrentThread;
63 if (pC->iTime>0 && --pC->iTime==0)
68 #ifndef __SCHEDULER_MACHINE_CODED__
69 void TScheduler::Remove(NThreadBase* aThread)
71 __NK_ASSERT_DEBUG(!aThread->iHeldFastMutex); // can't block while holding fast mutex
72 aThread->iTime=aThread->iTimeslice; // thread has blocked so it gets a fresh timeslice for next time
73 TPriListBase::Remove(aThread);
77 void TScheduler::RotateReadyList(TInt p)
79 // rotate the ready list for priority p
82 __NK_ASSERT_DEBUG(p>=0 && p<KNumPriorities);
83 SDblQueLink* pQ=iQueue[p];
86 SDblQueLink* pN=pQ->iNext;
89 NThread* pT=(NThread*)pQ;
90 pT->iTime=pT->iTimeslice;
92 if (pQ==iCurrentThread)
98 /** Converts a time interval in microseconds to thread timeslice ticks
100 @param aMicroseconds time interval in microseconds.
101 @return Number of thread timeslice ticks. Non-integral results are rounded up.
103 @pre aMicroseconds should be nonnegative
106 EXPORT_C TInt NKern::TimesliceTicks(TUint32 aMicroseconds)
108 TUint32 msp = TheTimerQ.iTickPeriod;
109 TUint32 ticks = (TUint32(aMicroseconds) + msp - 1) / msp;
114 /** Return the total CPU time so far used by the specified thread.
116 @return The total CPU time in units of 1/NKern::CpuTimeMeasFreq().
118 EXPORT_C TUint64 NKern::ThreadCpuTime(NThread* aThread)
120 #ifdef MONITOR_THREAD_CPU_TIME
122 TUint64 t = aThread->iTotalCpuTime;
123 if (aThread == TheScheduler.iCurrentThread)
124 t += TUint64(NKern::FastCounter() - aThread->iLastStartTime);