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\ncsched.cpp
|
sl@0
|
15 |
//
|
sl@0
|
16 |
//
|
sl@0
|
17 |
|
sl@0
|
18 |
// NThreadBase member data
|
sl@0
|
19 |
#define __INCLUDE_NTHREADBASE_DEFINES__
|
sl@0
|
20 |
|
sl@0
|
21 |
#include <e32cmn.h>
|
sl@0
|
22 |
#include <e32cmn_private.h>
|
sl@0
|
23 |
#include "nk_priv.h"
|
sl@0
|
24 |
|
sl@0
|
25 |
#ifdef __EMI_SUPPORT__
|
sl@0
|
26 |
extern void EMI_AddTaskSwitchEvent(TAny* aPrevious, TAny* aNext);
|
sl@0
|
27 |
extern void EMI_CheckDfcTag(TAny* aNext);
|
sl@0
|
28 |
#endif
|
sl@0
|
29 |
typedef void (*ProcessHandler)(TAny* aAddressSpace);
|
sl@0
|
30 |
|
sl@0
|
31 |
static DWORD TlsIndex = TLS_OUT_OF_INDEXES;
|
sl@0
|
32 |
|
sl@0
|
33 |
static NThreadBase* SelectThread(TScheduler& aS)
|
sl@0
|
34 |
//
|
sl@0
|
35 |
// Select the next thread to run.
|
sl@0
|
36 |
// This is the heart of the rescheduling algorithm.
|
sl@0
|
37 |
//
|
sl@0
|
38 |
{
|
sl@0
|
39 |
NThreadBase* t = static_cast<NThreadBase*>(aS.First());
|
sl@0
|
40 |
__NK_ASSERT_DEBUG(t);
|
sl@0
|
41 |
#ifdef _DEBUG
|
sl@0
|
42 |
if (t->iHeldFastMutex)
|
sl@0
|
43 |
{
|
sl@0
|
44 |
__KTRACE_OPT(KSCHED2,DEBUGPRINT("Resched init->%T, Holding %M",t,t->iHeldFastMutex));
|
sl@0
|
45 |
}
|
sl@0
|
46 |
else
|
sl@0
|
47 |
{
|
sl@0
|
48 |
__KTRACE_OPT(KSCHED2,DEBUGPRINT("Resched init->%T",t));
|
sl@0
|
49 |
}
|
sl@0
|
50 |
#endif
|
sl@0
|
51 |
if (t->iTime == 0 && !t->Alone())
|
sl@0
|
52 |
{
|
sl@0
|
53 |
// round robin
|
sl@0
|
54 |
// get here if thread's timeslice has expired and there is another
|
sl@0
|
55 |
// thread ready at the same priority
|
sl@0
|
56 |
if (t->iHeldFastMutex)
|
sl@0
|
57 |
{
|
sl@0
|
58 |
// round-robin deferred due to fast mutex held
|
sl@0
|
59 |
t->iHeldFastMutex->iWaiting = 1;
|
sl@0
|
60 |
return t;
|
sl@0
|
61 |
}
|
sl@0
|
62 |
t->iTime = t->iTimeslice; // reset old thread time slice
|
sl@0
|
63 |
t = static_cast<NThreadBase*>(t->iNext); // next thread
|
sl@0
|
64 |
aS.iQueue[t->iPriority] = t; // make it first in list
|
sl@0
|
65 |
__KTRACE_OPT(KSCHED2,DEBUGPRINT("RoundRobin->%T",t));
|
sl@0
|
66 |
}
|
sl@0
|
67 |
if (t->iHeldFastMutex)
|
sl@0
|
68 |
{
|
sl@0
|
69 |
if (t->iHeldFastMutex == &aS.iLock)
|
sl@0
|
70 |
{
|
sl@0
|
71 |
// thread holds system lock: use it
|
sl@0
|
72 |
return t;
|
sl@0
|
73 |
}
|
sl@0
|
74 |
if ((t->i_ThrdAttr & KThreadAttImplicitSystemLock) != 0 && aS.iLock.iHoldingThread)
|
sl@0
|
75 |
t->iHeldFastMutex->iWaiting = 1;
|
sl@0
|
76 |
__NK_ASSERT_DEBUG((t->i_ThrdAttr & KThreadAttAddressSpace) == 0);
|
sl@0
|
77 |
/*
|
sl@0
|
78 |
Check for an address space change. Not implemented for Win32, but useful as
|
sl@0
|
79 |
documentaiton of the algorithm.
|
sl@0
|
80 |
|
sl@0
|
81 |
if ((t->i_ThrdAttr & KThreadAttAddressSpace) != 0 && t->iAddressSpace != aS.iAddressSpace)
|
sl@0
|
82 |
t->iHeldFastMutex->iWaiting = 1;
|
sl@0
|
83 |
*/
|
sl@0
|
84 |
}
|
sl@0
|
85 |
else if (t->iWaitFastMutex && t->iWaitFastMutex->iHoldingThread)
|
sl@0
|
86 |
{
|
sl@0
|
87 |
__KTRACE_OPT(KSCHED2,DEBUGPRINT("Resched inter->%T, Blocked on %M",t->iWaitFastMutex->iHoldingThread,t->iWaitFastMutex));
|
sl@0
|
88 |
t = t->iWaitFastMutex->iHoldingThread;
|
sl@0
|
89 |
}
|
sl@0
|
90 |
else if (t->i_ThrdAttr & KThreadAttImplicitSystemLock)
|
sl@0
|
91 |
{
|
sl@0
|
92 |
// implicit system lock required
|
sl@0
|
93 |
if (aS.iLock.iHoldingThread)
|
sl@0
|
94 |
{
|
sl@0
|
95 |
// system lock held, switch to that thread
|
sl@0
|
96 |
t = aS.iLock.iHoldingThread;
|
sl@0
|
97 |
__KTRACE_OPT(KSCHED2,DEBUGPRINT("Resched inter->%T (IMP SYS)",t));
|
sl@0
|
98 |
t->iHeldFastMutex->iWaiting = 1; // aS.iLock.iWaiting = 1;
|
sl@0
|
99 |
return t;
|
sl@0
|
100 |
}
|
sl@0
|
101 |
__NK_ASSERT_DEBUG((t->i_ThrdAttr & KThreadAttAddressSpace) == 0);
|
sl@0
|
102 |
/*
|
sl@0
|
103 |
Check for an address space change. Not implemented for Win32, but useful as
|
sl@0
|
104 |
documentaiton of the algorithm.
|
sl@0
|
105 |
|
sl@0
|
106 |
if ((t->i_ThrdAttr & KThreadAttAddressSpace) != 0 || t->iAddressSpace != aS.iAddressSpace)
|
sl@0
|
107 |
{
|
sl@0
|
108 |
// what do we do now?
|
sl@0
|
109 |
__NK_ASSERT_DEBUG(FALSE);
|
sl@0
|
110 |
}
|
sl@0
|
111 |
*/
|
sl@0
|
112 |
}
|
sl@0
|
113 |
return t;
|
sl@0
|
114 |
}
|
sl@0
|
115 |
|
sl@0
|
116 |
// from NThread
|
sl@0
|
117 |
#undef i_ThrdAttr
|
sl@0
|
118 |
|
sl@0
|
119 |
TBool NThread::WakeUp()
|
sl@0
|
120 |
//
|
sl@0
|
121 |
// Wake up the thread. What to do depends on whether we were preempted or voluntarily
|
sl@0
|
122 |
// rescheduled.
|
sl@0
|
123 |
//
|
sl@0
|
124 |
// Return TRUE if we need to immediately reschedule again because we had to unlock
|
sl@0
|
125 |
// the kernel but there are DFCs pending. In this case, the thread does not wake up.
|
sl@0
|
126 |
//
|
sl@0
|
127 |
// NB. kernel is locked
|
sl@0
|
128 |
//
|
sl@0
|
129 |
{
|
sl@0
|
130 |
switch (iWakeup)
|
sl@0
|
131 |
{
|
sl@0
|
132 |
default:
|
sl@0
|
133 |
FAULT();
|
sl@0
|
134 |
case EIdle:
|
sl@0
|
135 |
__NK_ASSERT_ALWAYS(TheScheduler.iCurrentThread == this);
|
sl@0
|
136 |
__NK_ASSERT_ALWAYS(SetEvent(iScheduleLock));
|
sl@0
|
137 |
break;
|
sl@0
|
138 |
case ERelease:
|
sl@0
|
139 |
TheScheduler.iCurrentThread = this;
|
sl@0
|
140 |
__NK_ASSERT_ALWAYS(SetEvent(iScheduleLock));
|
sl@0
|
141 |
break;
|
sl@0
|
142 |
case EResumeLocked:
|
sl@0
|
143 |
// The thread is Win32 suspended and must be resumed.
|
sl@0
|
144 |
//
|
sl@0
|
145 |
// A newly created thread does not need the kernel unlocked so we can
|
sl@0
|
146 |
// just resume the suspended thread
|
sl@0
|
147 |
//
|
sl@0
|
148 |
__KTRACE_OPT(KSCHED,DEBUGPRINT("Win32Resume->%T",this));
|
sl@0
|
149 |
iWakeup = ERelease;
|
sl@0
|
150 |
TheScheduler.iCurrentThread = this;
|
sl@0
|
151 |
if (TheScheduler.iProcessHandler)
|
sl@0
|
152 |
(*ProcessHandler(TheScheduler.iProcessHandler))(iAddressSpace); // new thread will need to have its static data updated
|
sl@0
|
153 |
__NK_ASSERT_ALWAYS(TInt(ResumeThread(iWinThread)) > 0); // check thread was previously suspended
|
sl@0
|
154 |
break;
|
sl@0
|
155 |
case EResumeDiverted:
|
sl@0
|
156 |
// The thread is Win32 suspended and must be resumed.
|
sl@0
|
157 |
//
|
sl@0
|
158 |
// The thread needs to be diverted, and does not need the kernel
|
sl@0
|
159 |
// unlocked.
|
sl@0
|
160 |
//
|
sl@0
|
161 |
// It's safe the divert the thread here because we called
|
sl@0
|
162 |
// IsSafeToPreempt() when we suspended it - otherwise the diversion
|
sl@0
|
163 |
// could get lost.
|
sl@0
|
164 |
//
|
sl@0
|
165 |
__KTRACE_OPT(KSCHED,DEBUGPRINT("Win32Resume->%T (Resuming diverted thread)",this));
|
sl@0
|
166 |
iWakeup = ERelease;
|
sl@0
|
167 |
ApplyDiversion();
|
sl@0
|
168 |
TheScheduler.iCurrentThread = this;
|
sl@0
|
169 |
__NK_ASSERT_ALWAYS(TInt(ResumeThread(iWinThread)) == 1);
|
sl@0
|
170 |
break;
|
sl@0
|
171 |
case EResume:
|
sl@0
|
172 |
// The thread is Win32 suspended and must be resumed.
|
sl@0
|
173 |
//
|
sl@0
|
174 |
// the complication here is that we have to unlock the kernel on behalf of the
|
sl@0
|
175 |
// pre-empted thread. This means that we have to check to see if there are more DFCs
|
sl@0
|
176 |
// pending or a reschedule required, as we unlock the kernel. That check is
|
sl@0
|
177 |
// carried out with interrupts disabled.
|
sl@0
|
178 |
//
|
sl@0
|
179 |
// If so, we go back around the loop in this thread context
|
sl@0
|
180 |
//
|
sl@0
|
181 |
// Otherwise, we unlock the kernel (having marked us as not-preempted),
|
sl@0
|
182 |
// enable interrupts and then resume the thread. If pre-emption occurs before the thread
|
sl@0
|
183 |
// is resumed, it is the new thread that is pre-empted, not the running thread, so we are guaranteed
|
sl@0
|
184 |
// to be able to call ResumeThread. If pre-emption occurs, and we are rescheduled to run before
|
sl@0
|
185 |
// that occurs, we will once again be running with the kernel locked and the other thread will
|
sl@0
|
186 |
// have been re-suspended by Win32: so all is well.
|
sl@0
|
187 |
//
|
sl@0
|
188 |
{
|
sl@0
|
189 |
__KTRACE_OPT(KSCHED,DEBUGPRINT("Win32Resume->%T",this));
|
sl@0
|
190 |
TInt irq = NKern::DisableAllInterrupts();
|
sl@0
|
191 |
if (TheScheduler.iDfcPendingFlag || TheScheduler.iRescheduleNeededFlag)
|
sl@0
|
192 |
{
|
sl@0
|
193 |
// we were interrrupted... back to the top
|
sl@0
|
194 |
TheScheduler.iRescheduleNeededFlag = TRUE; // ensure we do the reschedule
|
sl@0
|
195 |
return TRUE;
|
sl@0
|
196 |
}
|
sl@0
|
197 |
iWakeup = ERelease;
|
sl@0
|
198 |
TheScheduler.iCurrentThread = this;
|
sl@0
|
199 |
if (TheScheduler.iProcessHandler)
|
sl@0
|
200 |
(*ProcessHandler(TheScheduler.iProcessHandler))(iAddressSpace); // threads resumed after interrupt or locks need to have static data updated
|
sl@0
|
201 |
|
sl@0
|
202 |
if (iInKernel == 0 && iUserModeCallbacks != NULL)
|
sl@0
|
203 |
ApplyDiversion();
|
sl@0
|
204 |
else
|
sl@0
|
205 |
TheScheduler.iKernCSLocked = 0; // have to unlock the kernel on behalf of the new thread
|
sl@0
|
206 |
|
sl@0
|
207 |
TheScheduler.iCurrentThread = this;
|
sl@0
|
208 |
NKern::RestoreInterrupts(irq);
|
sl@0
|
209 |
__NK_ASSERT_ALWAYS(TInt(ResumeThread(iWinThread)) > 0); // check thread was previously suspended
|
sl@0
|
210 |
}
|
sl@0
|
211 |
break;
|
sl@0
|
212 |
}
|
sl@0
|
213 |
return FALSE;
|
sl@0
|
214 |
}
|
sl@0
|
215 |
|
sl@0
|
216 |
static void ThreadExit(NThread& aCurrent, NThread& aNext)
|
sl@0
|
217 |
//
|
sl@0
|
218 |
// The final context switch of a thread.
|
sl@0
|
219 |
// Wake up the next thread and then destroy this one's Win32 resources.
|
sl@0
|
220 |
//
|
sl@0
|
221 |
// Return without terminating if we need to immediately reschedule again because
|
sl@0
|
222 |
// we had to unlock the kernel but there are DFCs pending.
|
sl@0
|
223 |
//
|
sl@0
|
224 |
{
|
sl@0
|
225 |
// the thread is dead
|
sl@0
|
226 |
// extract win32 handles from dying NThread object before rescheduling
|
sl@0
|
227 |
HANDLE sl = aCurrent.iScheduleLock;
|
sl@0
|
228 |
HANDLE th = aCurrent.iWinThread;
|
sl@0
|
229 |
|
sl@0
|
230 |
// wake up the next thread
|
sl@0
|
231 |
if (aNext.WakeUp())
|
sl@0
|
232 |
return; // need to re-reschedule in this thread
|
sl@0
|
233 |
|
sl@0
|
234 |
// we are now a vanilla win32 thread, nKern no longer knows about us
|
sl@0
|
235 |
// release resources and exit cleanly
|
sl@0
|
236 |
CloseHandle(sl);
|
sl@0
|
237 |
CloseHandle(th);
|
sl@0
|
238 |
ExitThread(0); // does not return
|
sl@0
|
239 |
}
|
sl@0
|
240 |
|
sl@0
|
241 |
#ifdef MONITOR_THREAD_CPU_TIME
|
sl@0
|
242 |
static inline void UpdateThreadCpuTime(NThread& aCurrent, NThread& aNext)
|
sl@0
|
243 |
{
|
sl@0
|
244 |
TUint32 timestamp = NKern::FastCounter();
|
sl@0
|
245 |
if (aCurrent.iLastStartTime)
|
sl@0
|
246 |
aCurrent.iTotalCpuTime += timestamp - aCurrent.iLastStartTime;
|
sl@0
|
247 |
aNext.iLastStartTime = timestamp;
|
sl@0
|
248 |
}
|
sl@0
|
249 |
#else
|
sl@0
|
250 |
static inline void UpdateThreadCpuTime(NThread& /*aCurrent*/, NThread& /*aNext*/)
|
sl@0
|
251 |
{
|
sl@0
|
252 |
}
|
sl@0
|
253 |
#endif
|
sl@0
|
254 |
|
sl@0
|
255 |
static void SwitchThreads(NThread& aCurrent, NThread& aNext)
|
sl@0
|
256 |
//
|
sl@0
|
257 |
// The fundamental context switch - wake up the next thread and wait for reschedule
|
sl@0
|
258 |
// trivially is aNext.WakeUp(), Wait(aCurrent.iScheduleLock), but we may be able to
|
sl@0
|
259 |
// optimise the signal-and-wait
|
sl@0
|
260 |
//
|
sl@0
|
261 |
{
|
sl@0
|
262 |
UpdateThreadCpuTime(aCurrent, aNext);
|
sl@0
|
263 |
if (aCurrent.iNState == NThread::EDead)
|
sl@0
|
264 |
ThreadExit(aCurrent, aNext);
|
sl@0
|
265 |
else if (Win32AtomicSOAW && aNext.iWakeup==NThread::ERelease)
|
sl@0
|
266 |
{
|
sl@0
|
267 |
// special case optimization for normally blocked threads using atomic Win32 primitive
|
sl@0
|
268 |
TheScheduler.iCurrentThread = &aNext;
|
sl@0
|
269 |
DWORD result=SignalObjectAndWait(aNext.iScheduleLock,aCurrent.iScheduleLock, INFINITE, FALSE);
|
sl@0
|
270 |
if (result != WAIT_OBJECT_0)
|
sl@0
|
271 |
{
|
sl@0
|
272 |
__NK_ASSERT_ALWAYS(result == 0xFFFFFFFF);
|
sl@0
|
273 |
KPrintf("SignalObjectAndWait() failed with %d (%T->%T)",GetLastError(),&aCurrent,&aNext);
|
sl@0
|
274 |
FAULT();
|
sl@0
|
275 |
}
|
sl@0
|
276 |
}
|
sl@0
|
277 |
else
|
sl@0
|
278 |
{
|
sl@0
|
279 |
if (aNext.WakeUp())
|
sl@0
|
280 |
return; // need to re-reschedule in this thread
|
sl@0
|
281 |
__NK_ASSERT_ALWAYS(WaitForSingleObject(aCurrent.iScheduleLock, INFINITE) == WAIT_OBJECT_0);
|
sl@0
|
282 |
}
|
sl@0
|
283 |
}
|
sl@0
|
284 |
|
sl@0
|
285 |
void TScheduler::YieldTo(NThreadBase*)
|
sl@0
|
286 |
//
|
sl@0
|
287 |
// Directed context switch to the nominated thread.
|
sl@0
|
288 |
// Enter with kernel locked, exit with kernel unlocked but interrupts disabled.
|
sl@0
|
289 |
//
|
sl@0
|
290 |
{
|
sl@0
|
291 |
RescheduleNeeded();
|
sl@0
|
292 |
TScheduler::Reschedule();
|
sl@0
|
293 |
}
|
sl@0
|
294 |
|
sl@0
|
295 |
void TScheduler::Reschedule()
|
sl@0
|
296 |
//
|
sl@0
|
297 |
// Enter with kernel locked, exit with kernel unlocked, interrupts disabled.
|
sl@0
|
298 |
// If the thread is dead do not return, but terminate the thread.
|
sl@0
|
299 |
//
|
sl@0
|
300 |
{
|
sl@0
|
301 |
__NK_ASSERT_ALWAYS(TheScheduler.iKernCSLocked == 1);
|
sl@0
|
302 |
NThread& me = *static_cast<NThread*>(TheScheduler.iCurrentThread);
|
sl@0
|
303 |
for (;;)
|
sl@0
|
304 |
{
|
sl@0
|
305 |
NKern::DisableAllInterrupts();
|
sl@0
|
306 |
if (TheScheduler.iDfcPendingFlag)
|
sl@0
|
307 |
TheScheduler.QueueDfcs();
|
sl@0
|
308 |
if (!TheScheduler.iRescheduleNeededFlag)
|
sl@0
|
309 |
break;
|
sl@0
|
310 |
NKern::EnableAllInterrupts();
|
sl@0
|
311 |
TheScheduler.iRescheduleNeededFlag = FALSE;
|
sl@0
|
312 |
NThread* t = static_cast<NThread*>(SelectThread(TheScheduler));
|
sl@0
|
313 |
__KTRACE_OPT(KSCHED,DEBUGPRINT("Reschedule->%T (%08x%08x)",t,TheScheduler.iPresent[1],TheScheduler.iPresent[0]));
|
sl@0
|
314 |
#ifdef __EMI_SUPPORT__
|
sl@0
|
315 |
EMI_AddTaskSwitchEvent(&me,t);
|
sl@0
|
316 |
EMI_CheckDfcTag(t);
|
sl@0
|
317 |
#endif
|
sl@0
|
318 |
#ifdef BTRACE_CPU_USAGE
|
sl@0
|
319 |
if(TheScheduler.iCpuUsageFilter)
|
sl@0
|
320 |
TheScheduler.iBTraceHandler(BTRACE_HEADER_C(4,BTrace::ECpuUsage,BTrace::ENewThreadContext),0,(TUint32)t,0,0,0,0,0);
|
sl@0
|
321 |
#endif
|
sl@0
|
322 |
SwitchThreads(me, *t);
|
sl@0
|
323 |
|
sl@0
|
324 |
// we have just been scheduled to run... check for diversion/new Dfcs
|
sl@0
|
325 |
NThread::TDivert divert = me.iDivert;
|
sl@0
|
326 |
if (divert)
|
sl@0
|
327 |
{
|
sl@0
|
328 |
// diversion (e.g. force exit)
|
sl@0
|
329 |
me.iDivert = NULL;
|
sl@0
|
330 |
divert(); // does not return
|
sl@0
|
331 |
}
|
sl@0
|
332 |
}
|
sl@0
|
333 |
if (TheScheduler.iProcessHandler)
|
sl@0
|
334 |
(*ProcessHandler(TheScheduler.iProcessHandler))(me.iAddressSpace);
|
sl@0
|
335 |
// interrrupts are disabled, the kernel is still locked
|
sl@0
|
336 |
TheScheduler.iKernCSLocked = 0;
|
sl@0
|
337 |
}
|
sl@0
|
338 |
|
sl@0
|
339 |
/** Put the emulator into 'idle'.
|
sl@0
|
340 |
This is called by the idle thread when there is nothing else to do.
|
sl@0
|
341 |
|
sl@0
|
342 |
@internalTechnology
|
sl@0
|
343 |
*/
|
sl@0
|
344 |
EXPORT_C void NThread::Idle()
|
sl@0
|
345 |
//
|
sl@0
|
346 |
// Rather than spin, we go to sleep on the schedule lock. Preemption detects
|
sl@0
|
347 |
// this state (Win32Idling) and pokes the event rather than diverting the thread.
|
sl@0
|
348 |
//
|
sl@0
|
349 |
// enter and exit with kernel locked
|
sl@0
|
350 |
//
|
sl@0
|
351 |
{
|
sl@0
|
352 |
NThread& me = *static_cast<NThread*>(TheScheduler.iCurrentThread);
|
sl@0
|
353 |
me.iWakeup = EIdle;
|
sl@0
|
354 |
__NK_ASSERT_ALWAYS(WaitForSingleObject(me.iScheduleLock, INFINITE) == WAIT_OBJECT_0);
|
sl@0
|
355 |
// something happened, and we've been prodded by an interrupt
|
sl@0
|
356 |
// the kernel was locked by the interrupt, and now reschedule
|
sl@0
|
357 |
me.iWakeup = ERelease;
|
sl@0
|
358 |
TScheduler::Reschedule();
|
sl@0
|
359 |
NKern::EnableAllInterrupts();
|
sl@0
|
360 |
}
|
sl@0
|
361 |
|
sl@0
|
362 |
void SchedulerInit(NThread& aInit)
|
sl@0
|
363 |
//
|
sl@0
|
364 |
// Initialise the win32 nKern scheduler
|
sl@0
|
365 |
//
|
sl@0
|
366 |
{
|
sl@0
|
367 |
DWORD procaffin,sysaffin;
|
sl@0
|
368 |
if (GetProcessAffinityMask(GetCurrentProcess(),&procaffin,&sysaffin))
|
sl@0
|
369 |
{
|
sl@0
|
370 |
DWORD cpu;
|
sl@0
|
371 |
switch (Win32SingleCpu)
|
sl@0
|
372 |
{
|
sl@0
|
373 |
default:
|
sl@0
|
374 |
// bind the emulator to a nominated CPU on the host PC
|
sl@0
|
375 |
cpu = (1<<Win32SingleCpu);
|
sl@0
|
376 |
if (!(sysaffin & cpu))
|
sl@0
|
377 |
cpu = procaffin; // CPU selection invalid
|
sl@0
|
378 |
break;
|
sl@0
|
379 |
case NThread::ECpuSingle:
|
sl@0
|
380 |
// bind the emulator to a single CPU on the host PC, pick one
|
sl@0
|
381 |
cpu = procaffin ^ (procaffin & (procaffin-1));
|
sl@0
|
382 |
break;
|
sl@0
|
383 |
case NThread::ECpuAll:
|
sl@0
|
384 |
// run the emulator on all CPUs on the host PC
|
sl@0
|
385 |
cpu=sysaffin;
|
sl@0
|
386 |
break;
|
sl@0
|
387 |
}
|
sl@0
|
388 |
SetProcessAffinityMask(GetCurrentProcess(), cpu);
|
sl@0
|
389 |
}
|
sl@0
|
390 |
// identify if we can use the atomic SignalObjectAndWait API in Win32 for rescheduling
|
sl@0
|
391 |
Win32AtomicSOAW = (SignalObjectAndWait(aInit.iScheduleLock, aInit.iScheduleLock, INFINITE, FALSE) == WAIT_OBJECT_0);
|
sl@0
|
392 |
//
|
sl@0
|
393 |
// allocate the TLS used for thread identification, and set it for the init thread
|
sl@0
|
394 |
TlsIndex = TlsAlloc();
|
sl@0
|
395 |
__NK_ASSERT_ALWAYS(TlsIndex != TLS_OUT_OF_INDEXES);
|
sl@0
|
396 |
SchedulerRegister(aInit);
|
sl@0
|
397 |
//
|
sl@0
|
398 |
Interrupt.Init();
|
sl@0
|
399 |
|
sl@0
|
400 |
Win32FindNonPreemptibleFunctions();
|
sl@0
|
401 |
}
|
sl@0
|
402 |
|
sl@0
|
403 |
void SchedulerRegister(NThread& aSelf)
|
sl@0
|
404 |
{
|
sl@0
|
405 |
TlsSetValue(TlsIndex,&aSelf);
|
sl@0
|
406 |
}
|
sl@0
|
407 |
|
sl@0
|
408 |
NThread* SchedulerThread()
|
sl@0
|
409 |
{
|
sl@0
|
410 |
if (TlsIndex != TLS_OUT_OF_INDEXES)
|
sl@0
|
411 |
return static_cast<NThread*>(TlsGetValue(TlsIndex));
|
sl@0
|
412 |
else
|
sl@0
|
413 |
return NULL; // not yet initialised
|
sl@0
|
414 |
}
|
sl@0
|
415 |
|
sl@0
|
416 |
inline TBool IsScheduledThread()
|
sl@0
|
417 |
{
|
sl@0
|
418 |
return SchedulerThread() == TheScheduler.iCurrentThread;
|
sl@0
|
419 |
}
|
sl@0
|
420 |
|
sl@0
|
421 |
NThread& CheckedCurrentThread()
|
sl@0
|
422 |
{
|
sl@0
|
423 |
NThread* t = SchedulerThread();
|
sl@0
|
424 |
__NK_ASSERT_ALWAYS(t == TheScheduler.iCurrentThread);
|
sl@0
|
425 |
return *t;
|
sl@0
|
426 |
}
|
sl@0
|
427 |
|
sl@0
|
428 |
|
sl@0
|
429 |
/** Disable normal 'interrupts'.
|
sl@0
|
430 |
|
sl@0
|
431 |
@param aLevel Ignored
|
sl@0
|
432 |
@return Cookie to be passed into RestoreInterrupts()
|
sl@0
|
433 |
*/
|
sl@0
|
434 |
EXPORT_C TInt NKern::DisableInterrupts(TInt /*aLevel*/)
|
sl@0
|
435 |
{
|
sl@0
|
436 |
return Interrupt.Mask();
|
sl@0
|
437 |
}
|
sl@0
|
438 |
|
sl@0
|
439 |
|
sl@0
|
440 |
/** Disable all maskable 'interrupts'.
|
sl@0
|
441 |
|
sl@0
|
442 |
@return Cookie to be passed into RestoreInterrupts()
|
sl@0
|
443 |
*/
|
sl@0
|
444 |
EXPORT_C TInt NKern::DisableAllInterrupts()
|
sl@0
|
445 |
{
|
sl@0
|
446 |
return Interrupt.Mask();
|
sl@0
|
447 |
}
|
sl@0
|
448 |
|
sl@0
|
449 |
|
sl@0
|
450 |
/** Enable all maskable 'interrupts'
|
sl@0
|
451 |
|
sl@0
|
452 |
@internalComponent
|
sl@0
|
453 |
*/
|
sl@0
|
454 |
EXPORT_C void NKern::EnableAllInterrupts()
|
sl@0
|
455 |
{
|
sl@0
|
456 |
Interrupt.Restore(0);
|
sl@0
|
457 |
}
|
sl@0
|
458 |
|
sl@0
|
459 |
|
sl@0
|
460 |
/** Restore interrupt mask to state preceding a DisableInterrupts() call
|
sl@0
|
461 |
|
sl@0
|
462 |
@param aLevel Cookie returned by Disable(All)Interrupts()
|
sl@0
|
463 |
*/
|
sl@0
|
464 |
EXPORT_C void NKern::RestoreInterrupts(TInt aLevel)
|
sl@0
|
465 |
{
|
sl@0
|
466 |
Interrupt.Restore(aLevel);
|
sl@0
|
467 |
}
|
sl@0
|
468 |
|
sl@0
|
469 |
|
sl@0
|
470 |
/** Unlocks the kernel.
|
sl@0
|
471 |
|
sl@0
|
472 |
Decrements iKernCSLocked; if it becomes zero and IDFCs or a reschedule are
|
sl@0
|
473 |
pending, calls the scheduler to process them.
|
sl@0
|
474 |
|
sl@0
|
475 |
@pre Call either in a thread or an IDFC context.
|
sl@0
|
476 |
@pre Do not call from an ISR.
|
sl@0
|
477 |
@pre Do not call from bare Win32 threads.
|
sl@0
|
478 |
*/
|
sl@0
|
479 |
EXPORT_C void NKern::Unlock()
|
sl@0
|
480 |
//
|
sl@0
|
481 |
// using this coding sequence it is possible to call Reschedule unnecessarily
|
sl@0
|
482 |
// if we are preempted after testing the flags (lock is zero at this point).
|
sl@0
|
483 |
// However, in the common case this is much faster because 'disabling interrupts'
|
sl@0
|
484 |
// can be very expensive.
|
sl@0
|
485 |
//
|
sl@0
|
486 |
{
|
sl@0
|
487 |
CHECK_PRECONDITIONS(MASK_NOT_ISR,"NKern::Unlock");
|
sl@0
|
488 |
__ASSERT_WITH_MESSAGE_DEBUG(IsScheduledThread(),"Do not call from bare Win32 threads","NKern::Unlock"); // check that we are a scheduled thread
|
sl@0
|
489 |
__NK_ASSERT_ALWAYS(TheScheduler.iKernCSLocked > 0); // Can't unlock if it isn't locked!
|
sl@0
|
490 |
if (--TheScheduler.iKernCSLocked == 0)
|
sl@0
|
491 |
{
|
sl@0
|
492 |
if (TheScheduler.iRescheduleNeededFlag || TheScheduler.iDfcPendingFlag)
|
sl@0
|
493 |
{
|
sl@0
|
494 |
TheScheduler.iKernCSLocked = 1;
|
sl@0
|
495 |
TScheduler::Reschedule();
|
sl@0
|
496 |
NKern::EnableAllInterrupts();
|
sl@0
|
497 |
}
|
sl@0
|
498 |
}
|
sl@0
|
499 |
}
|
sl@0
|
500 |
|
sl@0
|
501 |
|
sl@0
|
502 |
/** Locks the kernel.
|
sl@0
|
503 |
|
sl@0
|
504 |
Increments iKernCSLocked, thereby deferring IDFCs and preemption.
|
sl@0
|
505 |
|
sl@0
|
506 |
@pre Call either in a thread or an IDFC context.
|
sl@0
|
507 |
@pre Do not call from an ISR.
|
sl@0
|
508 |
@pre Do not call from bare Win32 threads.
|
sl@0
|
509 |
*/
|
sl@0
|
510 |
EXPORT_C void NKern::Lock()
|
sl@0
|
511 |
{
|
sl@0
|
512 |
CHECK_PRECONDITIONS(MASK_NOT_ISR,"NKern::Lock");
|
sl@0
|
513 |
__ASSERT_WITH_MESSAGE_ALWAYS(IsScheduledThread(),"Do not call from bare Win32 threads","NKern::Lock"); // check that we are a scheduled thread
|
sl@0
|
514 |
++TheScheduler.iKernCSLocked;
|
sl@0
|
515 |
}
|
sl@0
|
516 |
|
sl@0
|
517 |
|
sl@0
|
518 |
/** Locks the kernel and returns a pointer to the current thread
|
sl@0
|
519 |
Increments iKernCSLocked, thereby deferring IDFCs and preemption.
|
sl@0
|
520 |
|
sl@0
|
521 |
@pre Call either in a thread or an IDFC context.
|
sl@0
|
522 |
@pre Do not call from an ISR.
|
sl@0
|
523 |
@pre Do not call from bare Win32 threads.
|
sl@0
|
524 |
*/
|
sl@0
|
525 |
EXPORT_C NThread* NKern::LockC()
|
sl@0
|
526 |
{
|
sl@0
|
527 |
CHECK_PRECONDITIONS(MASK_NOT_ISR,"NKern::Lock");
|
sl@0
|
528 |
__ASSERT_WITH_MESSAGE_ALWAYS(IsScheduledThread(),"Do not call from bare Win32 threads","NKern::Lock"); // check that we are a scheduled thread
|
sl@0
|
529 |
++TheScheduler.iKernCSLocked;
|
sl@0
|
530 |
return (NThread*)TheScheduler.iCurrentThread;
|
sl@0
|
531 |
}
|
sl@0
|
532 |
|
sl@0
|
533 |
|
sl@0
|
534 |
/** Allows IDFCs and rescheduling if they are pending.
|
sl@0
|
535 |
|
sl@0
|
536 |
If IDFCs or a reschedule are pending and iKernCSLocked is exactly equal to 1
|
sl@0
|
537 |
calls the scheduler to process the IDFCs and possibly reschedule.
|
sl@0
|
538 |
|
sl@0
|
539 |
@return Nonzero if a reschedule actually occurred, zero if not.
|
sl@0
|
540 |
|
sl@0
|
541 |
@pre Call either in a thread or an IDFC context.
|
sl@0
|
542 |
@pre Do not call from an ISR.
|
sl@0
|
543 |
@pre Do not call from bare Win32 threads.
|
sl@0
|
544 |
*/
|
sl@0
|
545 |
EXPORT_C TInt NKern::PreemptionPoint()
|
sl@0
|
546 |
{
|
sl@0
|
547 |
CHECK_PRECONDITIONS(MASK_NOT_ISR,"NKern::PreemptionPoint");
|
sl@0
|
548 |
__ASSERT_WITH_MESSAGE_DEBUG(IsScheduledThread(),"Do not call from bare Win32 threads","NKern::PreemptionPoint"); // check that we are a scheduled thread
|
sl@0
|
549 |
if (TheScheduler.iKernCSLocked == 1 &&
|
sl@0
|
550 |
(TheScheduler.iRescheduleNeededFlag || TheScheduler.iDfcPendingFlag))
|
sl@0
|
551 |
{
|
sl@0
|
552 |
TScheduler::Reschedule();
|
sl@0
|
553 |
TheScheduler.iKernCSLocked = 1;
|
sl@0
|
554 |
NKern::EnableAllInterrupts();
|
sl@0
|
555 |
return TRUE;
|
sl@0
|
556 |
}
|
sl@0
|
557 |
return FALSE;
|
sl@0
|
558 |
}
|
sl@0
|
559 |
|
sl@0
|
560 |
|
sl@0
|
561 |
/** Mark the start of an 'interrupt' in the Win32 emulator.
|
sl@0
|
562 |
This must be called in interrupt threads before using any other kernel APIs,
|
sl@0
|
563 |
and should be paired with a call to EndOfInterrupt().
|
sl@0
|
564 |
|
sl@0
|
565 |
@pre Win32 'interrupt' thread context
|
sl@0
|
566 |
*/
|
sl@0
|
567 |
EXPORT_C void StartOfInterrupt()
|
sl@0
|
568 |
{
|
sl@0
|
569 |
__ASSERT_WITH_MESSAGE_DEBUG(!IsScheduledThread(),"Win32 'interrupt' thread context","StartOfInterrupt"); // check that we are a scheduled thread
|
sl@0
|
570 |
Interrupt.Begin();
|
sl@0
|
571 |
}
|
sl@0
|
572 |
|
sl@0
|
573 |
|
sl@0
|
574 |
/** Mark the end of an 'interrupt' in the Win32 emulator.
|
sl@0
|
575 |
This checks to see if we need to reschedule.
|
sl@0
|
576 |
|
sl@0
|
577 |
@pre Win32 'interrupt' thread context
|
sl@0
|
578 |
*/
|
sl@0
|
579 |
EXPORT_C void EndOfInterrupt()
|
sl@0
|
580 |
{
|
sl@0
|
581 |
__ASSERT_WITH_MESSAGE_DEBUG(!IsScheduledThread(),"Win32 'interrupt' thread context","EndOfInterrupt"); // check that we are a scheduled thread
|
sl@0
|
582 |
Interrupt.End();
|
sl@0
|
583 |
}
|
sl@0
|
584 |
|
sl@0
|
585 |
|
sl@0
|
586 |
void Win32Interrupt::Init()
|
sl@0
|
587 |
{
|
sl@0
|
588 |
iQ=CreateSemaphoreA(NULL, 0, KMaxTInt, NULL);
|
sl@0
|
589 |
__NK_ASSERT_ALWAYS(iQ);
|
sl@0
|
590 |
//
|
sl@0
|
591 |
// create the NThread which exists solely to service reschedules for interrupts
|
sl@0
|
592 |
// this makes the End() much simpler as it merely needs to kick this thread
|
sl@0
|
593 |
SNThreadCreateInfo ni;
|
sl@0
|
594 |
memclr(&ni, sizeof(ni));
|
sl@0
|
595 |
ni.iFunction=&Reschedule;
|
sl@0
|
596 |
ni.iTimeslice=-1;
|
sl@0
|
597 |
ni.iPriority=1;
|
sl@0
|
598 |
NKern::ThreadCreate(&iScheduler, ni);
|
sl@0
|
599 |
NKern::Lock();
|
sl@0
|
600 |
TScheduler::YieldTo(&iScheduler);
|
sl@0
|
601 |
Restore(0);
|
sl@0
|
602 |
}
|
sl@0
|
603 |
|
sl@0
|
604 |
TInt Win32Interrupt::Mask()
|
sl@0
|
605 |
{
|
sl@0
|
606 |
if (!iQ)
|
sl@0
|
607 |
return 0; // interrupt scheme not enabled yet
|
sl@0
|
608 |
DWORD id=GetCurrentThreadId();
|
sl@0
|
609 |
if (__e32_atomic_add_ord32(&iLock, 1))
|
sl@0
|
610 |
{
|
sl@0
|
611 |
if (id==iOwner)
|
sl@0
|
612 |
return iLevel++;
|
sl@0
|
613 |
__NK_ASSERT_ALWAYS(WaitForSingleObject(iQ,INFINITE) == WAIT_OBJECT_0);
|
sl@0
|
614 |
iRescheduleOnExit=IsScheduledThread() &&
|
sl@0
|
615 |
(TheScheduler.iRescheduleNeededFlag || TheScheduler.iDfcPendingFlag);
|
sl@0
|
616 |
}
|
sl@0
|
617 |
else
|
sl@0
|
618 |
iRescheduleOnExit=FALSE;
|
sl@0
|
619 |
__NK_ASSERT_ALWAYS(iOwner==0 && iLevel==0);
|
sl@0
|
620 |
iOwner=id;
|
sl@0
|
621 |
iLevel=1;
|
sl@0
|
622 |
return 0;
|
sl@0
|
623 |
}
|
sl@0
|
624 |
|
sl@0
|
625 |
void Win32Interrupt::Restore(TInt aLevel)
|
sl@0
|
626 |
{
|
sl@0
|
627 |
if (!iQ)
|
sl@0
|
628 |
return; // interrupt scheme not enabled yet
|
sl@0
|
629 |
DWORD id=GetCurrentThreadId();
|
sl@0
|
630 |
for (;;)
|
sl@0
|
631 |
{
|
sl@0
|
632 |
__NK_ASSERT_ALWAYS(id == iOwner);
|
sl@0
|
633 |
TInt count = iLevel - aLevel;
|
sl@0
|
634 |
if (count <= 0)
|
sl@0
|
635 |
return; // alredy restored to that level
|
sl@0
|
636 |
TBool reschedule = FALSE;
|
sl@0
|
637 |
iLevel = aLevel; // update this value before releasing the lock
|
sl@0
|
638 |
if (aLevel == 0)
|
sl@0
|
639 |
{
|
sl@0
|
640 |
// we release the lock
|
sl@0
|
641 |
iOwner = 0;
|
sl@0
|
642 |
if (iRescheduleOnExit && TheScheduler.iKernCSLocked == 0)
|
sl@0
|
643 |
reschedule = TRUE; // need to trigger reschedule on full release
|
sl@0
|
644 |
}
|
sl@0
|
645 |
// now release the lock
|
sl@0
|
646 |
if (__e32_atomic_add_ord32(&iLock, TUint32(-count)) == (TUint32)count)
|
sl@0
|
647 |
{ // fully released, check for reschedule
|
sl@0
|
648 |
if (!reschedule)
|
sl@0
|
649 |
return;
|
sl@0
|
650 |
}
|
sl@0
|
651 |
else
|
sl@0
|
652 |
{ // not fully released
|
sl@0
|
653 |
if (aLevel == 0)
|
sl@0
|
654 |
__NK_ASSERT_ALWAYS(ReleaseSemaphore(iQ,1,NULL));
|
sl@0
|
655 |
return;
|
sl@0
|
656 |
}
|
sl@0
|
657 |
// unlocked everything but a reschedule may be required
|
sl@0
|
658 |
TheScheduler.iKernCSLocked = 1;
|
sl@0
|
659 |
TScheduler::Reschedule();
|
sl@0
|
660 |
// return with the kernel unlocked, but interrupts disabled
|
sl@0
|
661 |
// instead of going recursive with a call to EnableAllInterrupts() we iterate
|
sl@0
|
662 |
aLevel=0;
|
sl@0
|
663 |
}
|
sl@0
|
664 |
}
|
sl@0
|
665 |
|
sl@0
|
666 |
void Win32Interrupt::Begin()
|
sl@0
|
667 |
{
|
sl@0
|
668 |
Mask();
|
sl@0
|
669 |
__NK_ASSERT_ALWAYS(iInterrupted==0); // check we haven't done this already
|
sl@0
|
670 |
__NK_ASSERT_ALWAYS(!IsScheduledThread()); // check that we aren't a scheduled thread
|
sl@0
|
671 |
NThread* pC;
|
sl@0
|
672 |
for (;;)
|
sl@0
|
673 |
{
|
sl@0
|
674 |
pC=static_cast<NThread*>(TheScheduler.iCurrentThread);
|
sl@0
|
675 |
DWORD r=SuspendThread(pC->iWinThread);
|
sl@0
|
676 |
if (pC == TheScheduler.iCurrentThread)
|
sl@0
|
677 |
{
|
sl@0
|
678 |
// there was no race while suspending the thread, so we can carry on
|
sl@0
|
679 |
__NK_ASSERT_ALWAYS(r != 0xffffffff);
|
sl@0
|
680 |
break;
|
sl@0
|
681 |
}
|
sl@0
|
682 |
// We suspended the thread while doing a context switch, resume it and try again
|
sl@0
|
683 |
if (r != 0xffffffff)
|
sl@0
|
684 |
__NK_ASSERT_ALWAYS(TInt(ResumeThread(pC->iWinThread)) > 0); // check thread was previously suspended
|
sl@0
|
685 |
}
|
sl@0
|
686 |
#ifdef BTRACE_CPU_USAGE
|
sl@0
|
687 |
BTrace0(BTrace::ECpuUsage,BTrace::EIrqStart);
|
sl@0
|
688 |
#endif
|
sl@0
|
689 |
iInterrupted = pC;
|
sl@0
|
690 |
}
|
sl@0
|
691 |
|
sl@0
|
692 |
void Win32Interrupt::End()
|
sl@0
|
693 |
{
|
sl@0
|
694 |
__NK_ASSERT_ALWAYS(iOwner == GetCurrentThreadId()); // check we are the interrupting thread
|
sl@0
|
695 |
NThread* pC = iInterrupted;
|
sl@0
|
696 |
__NK_ASSERT_ALWAYS(pC==TheScheduler.iCurrentThread);
|
sl@0
|
697 |
iInterrupted = 0;
|
sl@0
|
698 |
if (iLock == 1 && TheScheduler.iKernCSLocked == 0 &&
|
sl@0
|
699 |
(TheScheduler.iRescheduleNeededFlag || TheScheduler.iDfcPendingFlag) &&
|
sl@0
|
700 |
pC->IsSafeToPreempt())
|
sl@0
|
701 |
{
|
sl@0
|
702 |
TheScheduler.iKernCSLocked = 1; // prevent further pre-emption
|
sl@0
|
703 |
if (pC->iWakeup == NThread::EIdle)
|
sl@0
|
704 |
{
|
sl@0
|
705 |
// wake up the NULL thread, it will always reschedule immediately
|
sl@0
|
706 |
pC->WakeUp();
|
sl@0
|
707 |
}
|
sl@0
|
708 |
else
|
sl@0
|
709 |
{
|
sl@0
|
710 |
// pre-empt the current thread and poke the 'scheduler' thread
|
sl@0
|
711 |
__NK_ASSERT_ALWAYS(pC->iWakeup == NThread::ERelease);
|
sl@0
|
712 |
pC->iWakeup = NThread::EResume;
|
sl@0
|
713 |
UpdateThreadCpuTime(*pC, iScheduler);
|
sl@0
|
714 |
RescheduleNeeded();
|
sl@0
|
715 |
NKern::EnableAllInterrupts();
|
sl@0
|
716 |
iScheduler.WakeUp();
|
sl@0
|
717 |
return;
|
sl@0
|
718 |
}
|
sl@0
|
719 |
}
|
sl@0
|
720 |
else
|
sl@0
|
721 |
{
|
sl@0
|
722 |
// no thread reschedle, so emit trace...
|
sl@0
|
723 |
#ifdef BTRACE_CPU_USAGE
|
sl@0
|
724 |
BTrace0(BTrace::ECpuUsage,BTrace::EIrqEnd);
|
sl@0
|
725 |
#endif
|
sl@0
|
726 |
}
|
sl@0
|
727 |
|
sl@0
|
728 |
if (((NThread*)pC)->iInKernel == 0 && // thread is running in user mode
|
sl@0
|
729 |
pC->iUserModeCallbacks != NULL && // and has callbacks queued
|
sl@0
|
730 |
TheScheduler.iKernCSLocked == 0 && // and is not currently processing a diversion
|
sl@0
|
731 |
pC->IsSafeToPreempt()) // and can be safely prempted at this point
|
sl@0
|
732 |
{
|
sl@0
|
733 |
TheScheduler.iKernCSLocked = 1;
|
sl@0
|
734 |
pC->ApplyDiversion();
|
sl@0
|
735 |
}
|
sl@0
|
736 |
NKern::EnableAllInterrupts();
|
sl@0
|
737 |
__NK_ASSERT_ALWAYS(TInt(ResumeThread(pC->iWinThread)) > 0); // check thread was previously suspended
|
sl@0
|
738 |
}
|
sl@0
|
739 |
|
sl@0
|
740 |
void Win32Interrupt::Reschedule(TAny*)
|
sl@0
|
741 |
//
|
sl@0
|
742 |
// The entry-point for the interrupt-rescheduler thread.
|
sl@0
|
743 |
//
|
sl@0
|
744 |
// This spends its whole life going around the TScheduler::Reschedule() loop
|
sl@0
|
745 |
// selecting another thread to run.
|
sl@0
|
746 |
//
|
sl@0
|
747 |
{
|
sl@0
|
748 |
TheScheduler.iKernCSLocked = 1;
|
sl@0
|
749 |
RescheduleNeeded();
|
sl@0
|
750 |
TScheduler::Reschedule();
|
sl@0
|
751 |
FAULT();
|
sl@0
|
752 |
}
|
sl@0
|
753 |
|
sl@0
|
754 |
void Win32Interrupt::ForceReschedule()
|
sl@0
|
755 |
{
|
sl@0
|
756 |
RescheduleNeeded();
|
sl@0
|
757 |
iScheduler.WakeUp();
|
sl@0
|
758 |
}
|
sl@0
|
759 |
|
sl@0
|
760 |
void SchedulerEscape()
|
sl@0
|
761 |
{
|
sl@0
|
762 |
NThread& me=CheckedCurrentThread();
|
sl@0
|
763 |
EnterKernel();
|
sl@0
|
764 |
__NK_ASSERT_ALWAYS(TheScheduler.iKernCSLocked==0); // Can't call Escape() with the Emulator/kernel already locked
|
sl@0
|
765 |
NKern::ThreadEnterCS();
|
sl@0
|
766 |
NKern::Lock();
|
sl@0
|
767 |
me.iNState=NThreadBase::EBlocked;
|
sl@0
|
768 |
TheScheduler.Remove(&me);
|
sl@0
|
769 |
me.iWakeup=NThread::EEscaped;
|
sl@0
|
770 |
SetThreadPriority(me.iWinThread,THREAD_PRIORITY_ABOVE_NORMAL);
|
sl@0
|
771 |
Interrupt.ForceReschedule(); // schedules some other thread so we can carry on outside the scheduler domain
|
sl@0
|
772 |
// this will change the value of iCurrentThread to ensure the 'escaped' invariants are set
|
sl@0
|
773 |
}
|
sl@0
|
774 |
|
sl@0
|
775 |
void ReenterDfc(TAny* aPtr)
|
sl@0
|
776 |
{
|
sl@0
|
777 |
NThread& me = *static_cast<NThread*>(aPtr);
|
sl@0
|
778 |
me.iWakeup = NThread::ERelease;
|
sl@0
|
779 |
me.CheckSuspendThenReady();
|
sl@0
|
780 |
}
|
sl@0
|
781 |
|
sl@0
|
782 |
void SchedulerReenter()
|
sl@0
|
783 |
{
|
sl@0
|
784 |
NThread* me=SchedulerThread();
|
sl@0
|
785 |
__NK_ASSERT_ALWAYS(me);
|
sl@0
|
786 |
__NK_ASSERT_ALWAYS(me->iWakeup == NThread::EEscaped);
|
sl@0
|
787 |
TDfc idfc(&ReenterDfc, me);
|
sl@0
|
788 |
StartOfInterrupt();
|
sl@0
|
789 |
idfc.Add();
|
sl@0
|
790 |
EndOfInterrupt();
|
sl@0
|
791 |
SetThreadPriority(me->iWinThread,THREAD_PRIORITY_NORMAL);
|
sl@0
|
792 |
__NK_ASSERT_ALWAYS(WaitForSingleObject(me->iScheduleLock, INFINITE) == WAIT_OBJECT_0);
|
sl@0
|
793 |
// when released, the kernel is locked and handed over to us
|
sl@0
|
794 |
// need to complete the reschedule protocol in this thread now
|
sl@0
|
795 |
TScheduler::Reschedule();
|
sl@0
|
796 |
NKern::EnableAllInterrupts();
|
sl@0
|
797 |
NKern::ThreadLeaveCS();
|
sl@0
|
798 |
LeaveKernel();
|
sl@0
|
799 |
}
|
sl@0
|
800 |
|
sl@0
|
801 |
|
sl@0
|
802 |
/** Return the current processor context type
|
sl@0
|
803 |
(thread, IDFC, interrupt or escaped thread)
|
sl@0
|
804 |
|
sl@0
|
805 |
@return A value from NKern::TContext enumeration (including EEscaped)
|
sl@0
|
806 |
@pre Any context
|
sl@0
|
807 |
|
sl@0
|
808 |
@see NKern::TContext
|
sl@0
|
809 |
*/
|
sl@0
|
810 |
EXPORT_C TInt NKern::CurrentContext()
|
sl@0
|
811 |
{
|
sl@0
|
812 |
NThread* t = SchedulerThread();
|
sl@0
|
813 |
if (!t)
|
sl@0
|
814 |
return NKern::EInterrupt;
|
sl@0
|
815 |
if (TheScheduler.iInIDFC)
|
sl@0
|
816 |
return NKern::EIDFC;
|
sl@0
|
817 |
if (t->iWakeup == NThread::EEscaped)
|
sl@0
|
818 |
return NKern::EEscaped;
|
sl@0
|
819 |
__NK_ASSERT_ALWAYS(NKern::Crashed() || t == TheScheduler.iCurrentThread);
|
sl@0
|
820 |
return NKern::EThread;
|
sl@0
|
821 |
}
|
sl@0
|
822 |
|
sl@0
|
823 |
//
|
sl@0
|
824 |
// We use SuspendThread and ResumeThread to preempt threads. This can cause
|
sl@0
|
825 |
// deadlock if the thread is using windows synchronisation primitives (eg
|
sl@0
|
826 |
// critical sections). This isn't too much of a problem most of the time,
|
sl@0
|
827 |
// because threads generally use the symbian environment rather than the native
|
sl@0
|
828 |
// windows APIs. However exceptions are an issue - they can happen at any time,
|
sl@0
|
829 |
// and cause execution of native windows code over which we have no control.
|
sl@0
|
830 |
//
|
sl@0
|
831 |
// To work around this we examine the call stack to see if the thread is inside
|
sl@0
|
832 |
// one of the windows exception handling functions. If so, preemption is
|
sl@0
|
833 |
// deferred.
|
sl@0
|
834 |
//
|
sl@0
|
835 |
|
sl@0
|
836 |
#include <winnt.h>
|
sl@0
|
837 |
|
sl@0
|
838 |
const TInt KWin32NonPreemptibleFunctionCount = 2;
|
sl@0
|
839 |
|
sl@0
|
840 |
struct TWin32FunctionInfo
|
sl@0
|
841 |
{
|
sl@0
|
842 |
TUint iStartAddr;
|
sl@0
|
843 |
TUint iLength;
|
sl@0
|
844 |
};
|
sl@0
|
845 |
|
sl@0
|
846 |
static TWin32FunctionInfo Win32NonPreemptibleFunctions[KWin32NonPreemptibleFunctionCount];
|
sl@0
|
847 |
|
sl@0
|
848 |
TWin32FunctionInfo Win32FindExportedFunction(const char* aModuleName, const char* aFunctionName)
|
sl@0
|
849 |
{
|
sl@0
|
850 |
HMODULE library = GetModuleHandleA(aModuleName);
|
sl@0
|
851 |
__NK_ASSERT_ALWAYS(library != NULL);
|
sl@0
|
852 |
|
sl@0
|
853 |
// Find the start address of the function
|
sl@0
|
854 |
TUint start = (TUint)GetProcAddress(library, aFunctionName);
|
sl@0
|
855 |
__NK_ASSERT_ALWAYS(start);
|
sl@0
|
856 |
|
sl@0
|
857 |
// Now have to check all other exports to find the end of the function
|
sl@0
|
858 |
TUint end = 0xffffffff;
|
sl@0
|
859 |
TInt i = 1;
|
sl@0
|
860 |
for (;;)
|
sl@0
|
861 |
{
|
sl@0
|
862 |
TUint addr = (TUint)GetProcAddress(library, MAKEINTRESOURCEA(i));
|
sl@0
|
863 |
if (!addr)
|
sl@0
|
864 |
break;
|
sl@0
|
865 |
if (addr > start && addr < end)
|
sl@0
|
866 |
end = addr;
|
sl@0
|
867 |
++i;
|
sl@0
|
868 |
}
|
sl@0
|
869 |
__NK_ASSERT_ALWAYS(end != 0xffffffff);
|
sl@0
|
870 |
|
sl@0
|
871 |
TWin32FunctionInfo result = { start, end - start };
|
sl@0
|
872 |
return result;
|
sl@0
|
873 |
}
|
sl@0
|
874 |
|
sl@0
|
875 |
void Win32FindNonPreemptibleFunctions()
|
sl@0
|
876 |
{
|
sl@0
|
877 |
Win32NonPreemptibleFunctions[0] = Win32FindExportedFunction("kernel32.dll", "RaiseException");
|
sl@0
|
878 |
Win32NonPreemptibleFunctions[1] = Win32FindExportedFunction("ntdll.dll", "KiUserExceptionDispatcher");
|
sl@0
|
879 |
}
|
sl@0
|
880 |
|
sl@0
|
881 |
TBool Win32IsThreadInNonPreemptibleFunction(HANDLE aWinThread, TLinAddr aStackTop)
|
sl@0
|
882 |
{
|
sl@0
|
883 |
const TInt KMaxSearchDepth = 16; // 12 max observed while handling exceptions
|
sl@0
|
884 |
const TInt KMaxStackSize = 1024 * 1024; // Default reserved stack size on windows
|
sl@0
|
885 |
const TInt KMaxFrameSize = 4096;
|
sl@0
|
886 |
|
sl@0
|
887 |
CONTEXT c;
|
sl@0
|
888 |
c.ContextFlags=CONTEXT_FULL;
|
sl@0
|
889 |
GetThreadContext(aWinThread, &c);
|
sl@0
|
890 |
|
sl@0
|
891 |
TUint eip = c.Eip;
|
sl@0
|
892 |
TUint ebp = c.Ebp;
|
sl@0
|
893 |
TUint lastEbp = c.Esp;
|
sl@0
|
894 |
|
sl@0
|
895 |
// Walk the call stack
|
sl@0
|
896 |
for (TInt i = 0 ; i < KMaxSearchDepth ; ++i)
|
sl@0
|
897 |
{
|
sl@0
|
898 |
for (TInt j = 0 ; j < KWin32NonPreemptibleFunctionCount ; ++j)
|
sl@0
|
899 |
{
|
sl@0
|
900 |
const TWin32FunctionInfo& info = Win32NonPreemptibleFunctions[j];
|
sl@0
|
901 |
if (TUint(eip - info.iStartAddr) < info.iLength)
|
sl@0
|
902 |
{
|
sl@0
|
903 |
__KTRACE_OPT(KSCHED, DEBUGPRINT("Thread is in non-preemptible function %d at frame %d: eip == %08x", j, i, eip));
|
sl@0
|
904 |
return TRUE;
|
sl@0
|
905 |
}
|
sl@0
|
906 |
}
|
sl@0
|
907 |
|
sl@0
|
908 |
// Check frame pointer is valid before dereferencing it
|
sl@0
|
909 |
if (TUint(aStackTop - ebp) > KMaxStackSize || TUint(ebp - lastEbp) > KMaxFrameSize || ebp & 3)
|
sl@0
|
910 |
break;
|
sl@0
|
911 |
|
sl@0
|
912 |
TUint* frame = (TUint*)ebp;
|
sl@0
|
913 |
lastEbp = ebp;
|
sl@0
|
914 |
ebp = frame[0];
|
sl@0
|
915 |
eip = frame[1];
|
sl@0
|
916 |
}
|
sl@0
|
917 |
|
sl@0
|
918 |
return FALSE;
|
sl@0
|
919 |
}
|
sl@0
|
920 |
|
sl@0
|
921 |
TBool NThread::IsSafeToPreempt()
|
sl@0
|
922 |
{
|
sl@0
|
923 |
return !Win32IsThreadInNonPreemptibleFunction(iWinThread, iUserStackBase);
|
sl@0
|
924 |
}
|
sl@0
|
925 |
|
sl@0
|
926 |
void LeaveKernel()
|
sl@0
|
927 |
{
|
sl@0
|
928 |
TInt& k=CheckedCurrentThread().iInKernel;
|
sl@0
|
929 |
__NK_ASSERT_DEBUG(k>0);
|
sl@0
|
930 |
if (k==1) // just about to leave kernel
|
sl@0
|
931 |
{
|
sl@0
|
932 |
NThread& t = CheckedCurrentThread();
|
sl@0
|
933 |
__NK_ASSERT_ALWAYS(t.iCsCount==0);
|
sl@0
|
934 |
__NK_ASSERT_ALWAYS(t.iHeldFastMutex==0);
|
sl@0
|
935 |
__NK_ASSERT_ALWAYS(TheScheduler.iKernCSLocked==0);
|
sl@0
|
936 |
NKern::DisableAllInterrupts();
|
sl@0
|
937 |
t.CallUserModeCallbacks();
|
sl@0
|
938 |
NKern::EnableAllInterrupts();
|
sl@0
|
939 |
}
|
sl@0
|
940 |
--k;
|
sl@0
|
941 |
}
|
sl@0
|
942 |
|