273
|
1 |
// Copyright (c) 1998-2010 Nokia Corporation and/or its subsidiary(-ies).
|
0
|
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".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// e32\nkern\win32\ncthrd.cpp
|
273
|
15 |
//
|
0
|
16 |
//
|
|
17 |
|
|
18 |
// NThreadBase member data
|
|
19 |
#define __INCLUDE_NTHREADBASE_DEFINES__
|
|
20 |
|
|
21 |
#include "nk_priv.h"
|
|
22 |
#include <emulator.h>
|
|
23 |
|
273
|
24 |
#if defined(__CW32__) && defined(__MWERKS__) && (__MWERKS__ < 0x3200)
|
|
25 |
// Early versions didn't support try/except :(
|
|
26 |
#error "This compiler is no longer supported, because it doesn't provide C++ exception generation and handling"
|
|
27 |
#endif
|
|
28 |
|
0
|
29 |
extern "C" void ExcFault(TAny*);
|
|
30 |
|
|
31 |
// initial Win32 thread stack size
|
|
32 |
const TInt KInitialStackSize = 0x1000;
|
|
33 |
|
|
34 |
// maximum size of the parameter block passed to a new thread
|
|
35 |
const TInt KMaxParameterBlock = 512;
|
|
36 |
|
|
37 |
// data passed to new thread to enable hand-off of the parameter block
|
|
38 |
struct SCreateThread
|
|
39 |
{
|
|
40 |
const SNThreadCreateInfo* iInfo;
|
|
41 |
NFastMutex iHandoff;
|
|
42 |
};
|
|
43 |
|
273
|
44 |
|
|
45 |
/** Set some global properties of the emulator
|
|
46 |
Called by the Win32 base port during boot.
|
|
47 |
|
|
48 |
@param aTrace TRUE means trace Win32 thread ID for every thread created
|
|
49 |
@param aSingleCpu TRUE means lock the emulator process to a single CPU
|
|
50 |
|
|
51 |
@internalTechnology
|
|
52 |
*/
|
|
53 |
EXPORT_C void NThread::SetProperties(TBool aTrace, TInt aSingleCpu)
|
|
54 |
{
|
|
55 |
Win32TraceThreadId = aTrace;
|
|
56 |
Win32SingleCpu = aSingleCpu;
|
|
57 |
}
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
void NThread__HandleException(TWin32ExcInfo aExc)
|
|
62 |
//
|
|
63 |
// Final stage NKern exception handler.
|
|
64 |
//
|
|
65 |
// The first stage of exception processing (in ExceptionHandler()) entered the
|
|
66 |
// kernel and locked it, so we have to undo those two operations before returning.
|
|
67 |
// However, if the kernel was already locked when the exception occurred, it is
|
|
68 |
// a fatal condition and the system will be faulted.
|
|
69 |
//
|
|
70 |
// Note that the parameter struct is passed by value, this allows for direct
|
|
71 |
// access to the exception context created on the call stack by NThread::Exception().
|
|
72 |
//
|
|
73 |
{
|
|
74 |
NKern::Unlock();
|
|
75 |
if (TheScheduler.iKernCSLocked)
|
|
76 |
ExcFault(&aExc);
|
|
77 |
|
|
78 |
// Complete the exception data. Note that the call to EnterKernel() in
|
|
79 |
// ExceptionHandler() will have incremented iInKernel after the exception
|
|
80 |
// occurred.
|
|
81 |
NThread* me = static_cast<NThread*>(TheScheduler.iCurrentThread);
|
|
82 |
__NK_ASSERT_DEBUG(me->iInKernel);
|
|
83 |
aExc.iFlags = me->iInKernel == 1 ? 0 : TWin32ExcInfo::EExcInKernel;
|
|
84 |
aExc.iHandler = NULL;
|
|
85 |
|
|
86 |
// run NThread exception handler in 'kernel' mode
|
|
87 |
me->iHandlers->iExceptionHandler(&aExc, me);
|
|
88 |
LeaveKernel();
|
|
89 |
|
|
90 |
// If a 'user' handler is set by the kernel handler, run it
|
|
91 |
if (aExc.iHandler)
|
|
92 |
aExc.iHandler(aExc.iParam[0], aExc.iParam[1]);
|
|
93 |
}
|
|
94 |
|
|
95 |
__NAKED__ void NThread::Exception()
|
|
96 |
//
|
|
97 |
// Trampoline to nKern exception handler
|
|
98 |
// Must preserve all registers in the structure defined by TWin32Exc
|
|
99 |
//
|
|
100 |
// This is an intermediate layer, to which control has been diverted by
|
|
101 |
// NThread::ExceptionHandler(). It constructs a TWin32Exc structure on
|
|
102 |
// the stack and passes it NThread__ExceptionHandler().
|
|
103 |
//
|
|
104 |
// At this point we are no longer in Win32 exception context.
|
|
105 |
//
|
|
106 |
{
|
|
107 |
// this is the TWin32Exc structure
|
|
108 |
__asm push Win32ExcAddress // save return address followed by EBP first to help debugger
|
|
109 |
__asm push ebp
|
|
110 |
__asm mov ebp, esp
|
|
111 |
__asm push cs
|
|
112 |
__asm pushfd
|
|
113 |
__asm push gs
|
|
114 |
__asm push fs
|
|
115 |
__asm push es
|
|
116 |
__asm push ds
|
|
117 |
__asm push ss
|
|
118 |
__asm push edi
|
|
119 |
__asm push esi
|
|
120 |
__asm lea esi, [ebp+8]
|
|
121 |
__asm push esi // original esp
|
|
122 |
__asm push ebx
|
|
123 |
__asm push edx
|
|
124 |
__asm push ecx
|
|
125 |
__asm push eax
|
|
126 |
__asm push Win32ExcDataAddress
|
|
127 |
__asm push Win32ExcCode
|
|
128 |
__asm sub esp, 20 // struct init completed by NThread__HandleException()
|
|
129 |
|
|
130 |
__asm call NThread__HandleException
|
|
131 |
|
|
132 |
__asm add esp, 28
|
|
133 |
__asm pop eax
|
|
134 |
__asm pop ecx
|
|
135 |
__asm pop edx
|
|
136 |
__asm pop ebx
|
|
137 |
__asm pop esi // original ESP - ignore
|
|
138 |
__asm pop esi
|
|
139 |
__asm pop edi
|
|
140 |
__asm pop ebp // original SS - ignore
|
|
141 |
__asm pop ds
|
|
142 |
__asm pop es
|
|
143 |
__asm pop fs
|
|
144 |
__asm pop gs
|
|
145 |
__asm popfd
|
|
146 |
__asm pop ebp // original CS - ignore
|
|
147 |
__asm pop ebp
|
|
148 |
__asm ret
|
|
149 |
}
|
|
150 |
|
|
151 |
// From e32/commmon/win32/seh.cpp
|
|
152 |
extern DWORD CallFinalSEHHandler(EXCEPTION_RECORD* aException, CONTEXT* aContext);
|
|
153 |
|
|
154 |
extern void DivertHook();
|
|
155 |
|
|
156 |
DWORD NThread::ExceptionHandler(EXCEPTION_RECORD* aException, CONTEXT* aContext)
|
|
157 |
//
|
|
158 |
// Win32 exception handler for EPOC threads
|
|
159 |
//
|
|
160 |
// This is the outermost wrapper, called from ExceptionFilter() of via manual
|
|
161 |
// interception of the Win32 exception mechanism if using a really old compiler
|
|
162 |
//
|
|
163 |
{
|
|
164 |
if (aException->ExceptionCode == EXCEPTION_BREAKPOINT)
|
|
165 |
{
|
|
166 |
// Hardcoded breakpoint
|
|
167 |
//
|
|
168 |
// Jump directly to NT's default unhandled exception handler which will
|
|
169 |
// either display a dialog, directly invoke the JIT debugger or do nothing
|
|
170 |
// dependent upon the AeDebug and ErrorMode registry settings.
|
|
171 |
//
|
|
172 |
// Note this handler is always installed on the SEH chain and is always
|
|
173 |
// the last handler on this chain, as it is installed by NT in kernel32.dll
|
|
174 |
// before invoking the Win32 thread function.
|
|
175 |
return CallFinalSEHHandler(aException, aContext);
|
|
176 |
}
|
|
177 |
|
|
178 |
// deal with conflict between preemption and diversion
|
|
179 |
// the diversion will have been applied to the pre-exception context, not
|
|
180 |
// the current context, and thus will get 'lost'. Wake-up of a pre-empted
|
|
181 |
// thread with a diversion will not unlock the kernel, so we need to deal
|
|
182 |
// with the possibility that the kernel may be locked if a diversion exists
|
|
183 |
NThread& me = *static_cast<NThread*>(TheScheduler.iCurrentThread);
|
|
184 |
if (me.iDiverting && me.iDivertFn)
|
|
185 |
{
|
|
186 |
// The thread is being forced to exit - run the diversion outside of Win32 exception handler
|
|
187 |
__NK_ASSERT_ALWAYS(TheScheduler.iKernCSLocked == 1);
|
|
188 |
aContext->Eip = (TUint32)&DivertHook;
|
|
189 |
}
|
|
190 |
else
|
|
191 |
{
|
|
192 |
if (me.iDiverting)
|
|
193 |
{
|
|
194 |
// The thread is being prodded to pick up its callbacks. This will happen when the
|
|
195 |
// exception handler calls LeaveKernel(), so we can remove the diversion
|
|
196 |
__NK_ASSERT_ALWAYS(TheScheduler.iKernCSLocked == 1);
|
|
197 |
if (aException->ExceptionAddress == &DivertHook)
|
|
198 |
aException->ExceptionAddress = me.iDivertReturn;
|
|
199 |
me.iDivertReturn = NULL;
|
|
200 |
me.iDiverting = EFalse;
|
|
201 |
EnterKernel(TRUE);
|
|
202 |
}
|
|
203 |
else
|
|
204 |
{
|
|
205 |
EnterKernel();
|
|
206 |
TheScheduler.iKernCSLocked = 1; // prevent pre-emption
|
|
207 |
}
|
|
208 |
|
|
209 |
// If the kernel was already locked, this will be detected in the next stage handler
|
|
210 |
// (NThread::Exception()), which we arrange to run outside the Win32 exception context
|
|
211 |
Win32ExcAddress = aException->ExceptionAddress;
|
|
212 |
Win32ExcDataAddress = (TAny*)aException->ExceptionInformation[1];
|
|
213 |
Win32ExcCode = aException->ExceptionCode;
|
|
214 |
aContext->Eip = (TUint32)&Exception;
|
|
215 |
}
|
|
216 |
|
|
217 |
return ExceptionContinueExecution;
|
|
218 |
}
|
|
219 |
|
|
220 |
LONG WINAPI NThread::ExceptionFilter(EXCEPTION_POINTERS* aExc)
|
|
221 |
//
|
|
222 |
// Filter wrapper for main Win32 exception handler
|
|
223 |
//
|
|
224 |
{
|
|
225 |
LONG ret = EXCEPTION_CONTINUE_SEARCH;
|
|
226 |
|
|
227 |
switch (ExceptionHandler(aExc->ExceptionRecord, aExc->ContextRecord))
|
|
228 |
{
|
|
229 |
case ExceptionContinueExecution:
|
|
230 |
ret = EXCEPTION_CONTINUE_EXECUTION;
|
|
231 |
break;
|
|
232 |
|
|
233 |
case ExceptionContinueSearch:
|
|
234 |
default:
|
|
235 |
break;
|
|
236 |
}
|
|
237 |
|
|
238 |
return ret;
|
|
239 |
}
|
|
240 |
|
|
241 |
|
|
242 |
DWORD WINAPI NThread::StartThread(LPVOID aParam)
|
|
243 |
//
|
|
244 |
// Win32 thread function for nKern threads.
|
|
245 |
//
|
|
246 |
// The thread first enters this function after the nScheduler has resumed
|
|
247 |
// it, following the context switch induced by the hand-off mutex.
|
|
248 |
//
|
|
249 |
// The parameter block for this thread needs to be copied into its
|
|
250 |
// own context, before releasing the mutex and handing control back to
|
|
251 |
// the creating thread.
|
|
252 |
//
|
|
253 |
{
|
|
254 |
SCreateThread* init = static_cast<SCreateThread*>(aParam);
|
|
255 |
NThread& me = *static_cast<NThread*>(init->iHandoff.iHoldingThread);
|
|
256 |
me.iWinThreadId = GetCurrentThreadId();
|
|
257 |
SchedulerRegister(me);
|
|
258 |
#ifdef BTRACE_FAST_MUTEX
|
|
259 |
BTraceContext4(BTrace::EFastMutex, BTrace::EFastMutexWait, &init->iHandoff);
|
|
260 |
#endif
|
|
261 |
NKern::Unlock();
|
|
262 |
|
|
263 |
// intercept win32 exceptions in a debuggabble way
|
|
264 |
__try
|
|
265 |
{
|
|
266 |
// save the thread entry point and parameter block
|
|
267 |
const SNThreadCreateInfo& info = *init->iInfo;
|
|
268 |
NThreadFunction threadFunction = info.iFunction;
|
|
269 |
TUint8 parameterBlock[KMaxParameterBlock];
|
|
270 |
TAny* parameter = (TAny*)info.iParameterBlock;
|
|
271 |
|
|
272 |
if (info.iParameterBlockSize)
|
|
273 |
{
|
|
274 |
__NK_ASSERT_DEBUG(TUint(info.iParameterBlockSize) <= TUint(KMaxParameterBlock));
|
|
275 |
memcpy(parameterBlock, info.iParameterBlock, info.iParameterBlockSize);
|
|
276 |
parameter = parameterBlock;
|
|
277 |
}
|
|
278 |
|
|
279 |
|
|
280 |
// Calculate stack base
|
|
281 |
me.iUserStackBase = (((TLinAddr)¶meterBlock) + 0xfff) & ~0xfff;
|
|
282 |
|
|
283 |
// some useful diagnostics for debugging
|
|
284 |
if (Win32TraceThreadId)
|
|
285 |
KPrintf("Thread %T created @ 0x%x - Win32 Thread ID 0x%x", init->iHandoff.iHoldingThread, init->iHandoff.iHoldingThread, GetCurrentThreadId());
|
|
286 |
|
|
287 |
#ifdef MONITOR_THREAD_CPU_TIME
|
|
288 |
me.iLastStartTime = 0; // Don't count NThread setup in cpu time
|
|
289 |
#endif
|
|
290 |
|
|
291 |
// start-up complete, release the handoff mutex, which will re-suspend us
|
|
292 |
NKern::FMSignal(&init->iHandoff);
|
|
293 |
|
|
294 |
// thread has been resumed: invoke the thread function
|
|
295 |
threadFunction(parameter);
|
|
296 |
}
|
|
297 |
__except (ExceptionFilter(GetExceptionInformation()))
|
|
298 |
{
|
|
299 |
// Do nothing - filter does all the work and hooks into EPOC
|
|
300 |
// h/w exception mechanism if necessary by thread diversion
|
|
301 |
}
|
|
302 |
|
|
303 |
NKern::Exit();
|
|
304 |
return 0;
|
|
305 |
}
|
|
306 |
|
|
307 |
|
|
308 |
|
0
|
309 |
/**
|
|
310 |
* Set the Win32 thread priority based on the thread type.
|
273
|
311 |
* Interrupt/Event threads must be able to preempt normal
|
|
312 |
* nKern threads, so they get a higher (Win32) priority.
|
0
|
313 |
*/
|
|
314 |
static void SetPriority(HANDLE aThread, TEmulThreadType aType)
|
|
315 |
{
|
|
316 |
TInt p;
|
273
|
317 |
|
0
|
318 |
switch (aType)
|
|
319 |
{
|
|
320 |
default:
|
|
321 |
FAULT();
|
273
|
322 |
|
0
|
323 |
case EThreadEvent:
|
|
324 |
p = THREAD_PRIORITY_ABOVE_NORMAL;
|
|
325 |
break;
|
273
|
326 |
|
0
|
327 |
case EThreadNKern:
|
|
328 |
p = THREAD_PRIORITY_NORMAL;
|
|
329 |
break;
|
|
330 |
}
|
|
331 |
|
273
|
332 |
CheckedSetThreadPriority(aThread, p);
|
0
|
333 |
SetThreadPriorityBoost(aThread, TRUE); // disable priority boost (for NT)
|
|
334 |
}
|
|
335 |
|
|
336 |
/** Create a Win32 thread for use in the emulator.
|
|
337 |
|
|
338 |
@param aType Type of thread (Event or NKern) - determines Win32 priority
|
|
339 |
@param aThreadFunc Entry point of thread
|
|
340 |
@param aPtr Argument passed to entry point
|
|
341 |
@param aRun TRUE if thread should be resumed immediately
|
|
342 |
@return The Win32 handle to the thread, 0 if an error occurred
|
|
343 |
|
|
344 |
@pre Call either in thread context.
|
|
345 |
@pre Do not call from bare Win32 threads.
|
|
346 |
|
|
347 |
@see TEmulThreadType
|
|
348 |
*/
|
|
349 |
EXPORT_C HANDLE CreateWin32Thread(TEmulThreadType aType, LPTHREAD_START_ROUTINE aThreadFunc, LPVOID aPtr, TBool aRun)
|
|
350 |
{
|
|
351 |
__NK_ASSERT_DEBUG(!TheScheduler.iCurrentThread || NKern::CurrentContext() == NKern::EThread);
|
|
352 |
|
|
353 |
__LOCK_HOST;
|
|
354 |
|
|
355 |
DWORD id;
|
|
356 |
HANDLE handle = CreateThread(NULL , KInitialStackSize, aThreadFunc, aPtr, CREATE_SUSPENDED, &id);
|
|
357 |
if (handle)
|
|
358 |
{
|
|
359 |
SetPriority(handle, aType);
|
|
360 |
if (aRun)
|
|
361 |
ResumeThread(handle);
|
|
362 |
}
|
|
363 |
return handle;
|
|
364 |
}
|
|
365 |
|
|
366 |
static HANDLE InitThread()
|
|
367 |
//
|
|
368 |
// Set up the initial thread and return the thread handle
|
|
369 |
//
|
|
370 |
{
|
|
371 |
HANDLE p = GetCurrentProcess();
|
|
372 |
HANDLE me;
|
273
|
373 |
DWORD r = DuplicateHandle(p, GetCurrentThread(), p, &me, 0, FALSE, DUPLICATE_SAME_ACCESS);
|
|
374 |
__NK_ASSERT_ALWAYS(r != 0); // r is zero on error
|
0
|
375 |
SetPriority(me, EThreadNKern);
|
|
376 |
return me;
|
|
377 |
}
|
|
378 |
|
|
379 |
TInt NThread::Create(SNThreadCreateInfo& aInfo, TBool aInitial)
|
|
380 |
{
|
|
381 |
iWinThread = NULL;
|
|
382 |
iWinThreadId = 0;
|
|
383 |
iScheduleLock = NULL;
|
|
384 |
iInKernel = 1;
|
273
|
385 |
iDivertFn = NULL;
|
0
|
386 |
iWakeup = aInitial ? ERelease : EResumeLocked; // mark new threads as created (=> win32 suspend)
|
|
387 |
|
273
|
388 |
TInt r = NThreadBase::Create(aInfo, aInitial);
|
|
389 |
if (r != KErrNone)
|
0
|
390 |
return r;
|
|
391 |
|
|
392 |
// the rest has to be all or nothing, we must complete it
|
|
393 |
iScheduleLock = CreateEventA(NULL, FALSE, FALSE, NULL);
|
|
394 |
if (iScheduleLock == NULL)
|
|
395 |
return Emulator::LastError();
|
|
396 |
|
|
397 |
if (aInitial)
|
|
398 |
{
|
|
399 |
iWinThread = InitThread();
|
|
400 |
FastCounterInit();
|
|
401 |
#ifdef MONITOR_THREAD_CPU_TIME
|
|
402 |
iLastStartTime = NKern::FastCounter();
|
|
403 |
#endif
|
273
|
404 |
iUserStackBase = (((TLinAddr)&r) + 0xfff) & ~0xfff; // base address of stack
|
0
|
405 |
SchedulerInit(*this);
|
|
406 |
return KErrNone;
|
|
407 |
}
|
|
408 |
|
|
409 |
// create the thread proper
|
|
410 |
//
|
|
411 |
SCreateThread start;
|
|
412 |
start.iInfo = &aInfo;
|
|
413 |
iWinThread = CreateWin32Thread(EThreadNKern, &StartThread, &start, FALSE);
|
|
414 |
if (iWinThread == NULL)
|
|
415 |
{
|
|
416 |
r = Emulator::LastError();
|
|
417 |
CloseHandle(iScheduleLock);
|
|
418 |
return r;
|
|
419 |
}
|
|
420 |
|
|
421 |
#ifdef BTRACE_THREAD_IDENTIFICATION
|
273
|
422 |
BTrace4(BTrace::EThreadIdentification, BTrace::ENanoThreadCreate, this);
|
0
|
423 |
#endif
|
|
424 |
// switch to the new thread to hand over the parameter block
|
|
425 |
NKern::Lock();
|
|
426 |
ForceResume(); // mark the thread as ready
|
|
427 |
// give the thread ownership of the handoff mutex
|
|
428 |
start.iHandoff.iHoldingThread = this;
|
|
429 |
iHeldFastMutex = &start.iHandoff;
|
|
430 |
Suspend(1); // will defer as holding a fast mutex (implicit critical section)
|
|
431 |
// do the hand-over
|
|
432 |
start.iHandoff.Wait();
|
|
433 |
start.iHandoff.Signal();
|
|
434 |
NKern::Unlock();
|
|
435 |
|
|
436 |
return KErrNone;
|
|
437 |
}
|
|
438 |
|
|
439 |
|
|
440 |
|
|
441 |
void NThread::Diverted()
|
|
442 |
//
|
273
|
443 |
// This function is called in the context of a thread that is being diverted.
|
|
444 |
// This can be for either of two reasons: if iDivertFn has been set, that
|
|
445 |
// function will be called and is not expected to return i.e. it should force
|
279
|
446 |
// the thread to exit (in this case, the thread may already be in the kernel,
|
|
447 |
// but only at a place where it's safe for it to be killed). Otherwise, the
|
|
448 |
// thread must be in user mode, and it's forced to make a null trip through
|
|
449 |
// the kernel, causing it to run pending user-mode callbacks on the way out.
|
273
|
450 |
//
|
|
451 |
// On entry, the kernel is locked and interrupts enabled
|
0
|
452 |
//
|
|
453 |
{
|
|
454 |
NThread& me = *static_cast<NThread*>(TheScheduler.iCurrentThread);
|
|
455 |
__NK_ASSERT_ALWAYS(TheScheduler.iKernCSLocked == 1);
|
273
|
456 |
__NK_ASSERT_ALWAYS(me.iDiverting);
|
|
457 |
NThread::TDivert divertFn = me.iDivertFn;
|
|
458 |
me.iDivertFn = NULL;
|
0
|
459 |
me.iDivertReturn = NULL;
|
273
|
460 |
me.iDiverting = EFalse;
|
|
461 |
|
|
462 |
EnterKernel(TRUE);
|
|
463 |
|
|
464 |
if (divertFn)
|
|
465 |
divertFn(); // does not return
|
|
466 |
|
0
|
467 |
NKern::Unlock();
|
|
468 |
LeaveKernel();
|
|
469 |
}
|
|
470 |
|
|
471 |
void NThread__Diverted()
|
|
472 |
{
|
|
473 |
NThread::Diverted();
|
|
474 |
}
|
|
475 |
|
|
476 |
__NAKED__ void DivertHook()
|
|
477 |
{
|
|
478 |
// The stack frame is set up like this:
|
|
479 |
//
|
|
480 |
// | return address |
|
|
481 |
// | frame pointer |
|
|
482 |
// | flags |
|
|
483 |
// | saved eax |
|
|
484 |
// | saved ecx |
|
|
485 |
// | saved edx |
|
273
|
486 |
//
|
0
|
487 |
__asm push eax // reserve word for return address
|
|
488 |
__asm push ebp
|
273
|
489 |
__asm mov ebp, esp
|
0
|
490 |
__asm pushfd
|
|
491 |
__asm push eax
|
|
492 |
__asm push ecx
|
|
493 |
__asm push edx
|
|
494 |
__asm mov eax, [TheScheduler.iCurrentThread]
|
|
495 |
__asm mov eax, [eax]NThread.iDivertReturn
|
|
496 |
__asm mov [esp + 20], eax // store return address
|
|
497 |
__asm call NThread__Diverted
|
|
498 |
__asm pop edx
|
|
499 |
__asm pop ecx
|
|
500 |
__asm pop eax
|
|
501 |
__asm popfd
|
|
502 |
__asm pop ebp
|
|
503 |
__asm ret
|
|
504 |
}
|
|
505 |
|
|
506 |
|
|
507 |
void NThread::ApplyDiversion()
|
273
|
508 |
//
|
|
509 |
// Arrange that the thread will be diverted when next it runs.
|
|
510 |
// This can be for either of two reasons: if iDivertFn has been set,
|
|
511 |
// that function will be called and is not expected to return i.e.
|
|
512 |
// it should force the thread to exit. Otherwise, the thread will
|
|
513 |
// make a null trip through the kernel, causing it to run pending
|
|
514 |
// user-mode callbacks on the way out.
|
|
515 |
//
|
|
516 |
// This uses the Win32 CONTEXT functions to change the thread's PC
|
|
517 |
// so that execution restarts at DivertHook ...
|
|
518 |
//
|
0
|
519 |
{
|
|
520 |
// Called with interrupts disabled and kernel locked
|
|
521 |
__NK_ASSERT_ALWAYS(TheScheduler.iKernCSLocked == 1);
|
273
|
522 |
__NK_ASSERT_ALWAYS(iDivertReturn == NULL || iDiverting);
|
|
523 |
|
|
524 |
if (iDiverting)
|
0
|
525 |
return;
|
273
|
526 |
|
0
|
527 |
CONTEXT c;
|
273
|
528 |
c.ContextFlags = CONTEXT_CONTROL;
|
|
529 |
CheckedGetThreadContext(iWinThread, &c);
|
0
|
530 |
iDivertReturn = (TAny*)c.Eip;
|
273
|
531 |
c.Eip = (TUint32)&DivertHook;
|
|
532 |
c.ContextFlags = CONTEXT_CONTROL;
|
|
533 |
CheckedSetThreadContext(iWinThread, &c);
|
|
534 |
iDiverting = ETrue;
|
0
|
535 |
}
|
|
536 |
|
273
|
537 |
void NThread::Divert(TDivert aDivertFn)
|
0
|
538 |
//
|
273
|
539 |
// Arrange that the thread will exit by calling aDivertFn when next
|
|
540 |
// it runs. The diversion function will be called with the kernel
|
|
541 |
// locked and interrupts enabled. It is not expected to return.
|
0
|
542 |
//
|
|
543 |
{
|
273
|
544 |
iDivertFn = aDivertFn;
|
0
|
545 |
if (iWakeup == EResume)
|
|
546 |
iWakeup = EResumeDiverted;
|
|
547 |
else
|
|
548 |
__NK_ASSERT_ALWAYS(iWakeup == ERelease);
|
|
549 |
}
|
|
550 |
|
|
551 |
void NThread::ExitSync()
|
|
552 |
//
|
|
553 |
// Diversion used to terminate 'stillborn' threads.
|
|
554 |
// On entry, kernel is locked, interrupts are enabled and we hold an interlock mutex
|
|
555 |
//
|
|
556 |
{
|
273
|
557 |
NThreadBase& me = *TheScheduler.iCurrentThread;
|
0
|
558 |
me.iHeldFastMutex->Signal(); // release the interlock
|
273
|
559 |
me.iNState = EDead; // mark ourselves as dead which will take thread out of scheduler
|
0
|
560 |
TheScheduler.Remove(&me);
|
|
561 |
RescheduleNeeded();
|
|
562 |
TScheduler::Reschedule(); // this won't return
|
|
563 |
FAULT();
|
|
564 |
}
|
|
565 |
|
|
566 |
void NThread::Stillborn()
|
|
567 |
//
|
|
568 |
// Called if the new thread creation was aborted - so it will not be killed in the usual manner
|
|
569 |
//
|
|
570 |
// This function needs to exit the thread synchronously as on return we will destroy the thread control block
|
273
|
571 |
// Thus we need to use an interlock that ensure that the target thread runs the exit handler before we continue
|
0
|
572 |
//
|
|
573 |
{
|
|
574 |
// check if the Win32 thread was created
|
|
575 |
if (!iWinThread)
|
|
576 |
return;
|
|
577 |
|
|
578 |
NKern::Lock();
|
|
579 |
Divert(&ExitSync);
|
|
580 |
ForceResume();
|
|
581 |
// create and assign mutex to stillborn thread
|
|
582 |
NFastMutex interlock;
|
273
|
583 |
interlock.iHoldingThread = this;
|
|
584 |
iHeldFastMutex = &interlock;
|
0
|
585 |
interlock.Wait(); // interlock on thread exit handler
|
|
586 |
interlock.Signal();
|
|
587 |
NKern::Unlock();
|
|
588 |
}
|
|
589 |
|
|
590 |
void NThread::ExitAsync()
|
|
591 |
//
|
|
592 |
// Diversion used to terminate 'killed' threads.
|
|
593 |
// On entry, kernel is locked and interrupts are enabled
|
|
594 |
//
|
|
595 |
{
|
|
596 |
NThreadBase& me = *TheScheduler.iCurrentThread;
|
273
|
597 |
__NK_ASSERT_ALWAYS(static_cast<NThread&>(me).iInKernel > 0);
|
0
|
598 |
me.iCsCount = 0;
|
|
599 |
me.Exit();
|
|
600 |
}
|
|
601 |
|
|
602 |
inline void NThread::DoForceExit()
|
|
603 |
{
|
|
604 |
__NK_ASSERT_DEBUG(TheScheduler.iKernCSLocked);
|
|
605 |
Divert(&ExitAsync);
|
|
606 |
}
|
|
607 |
|
|
608 |
void NThreadBase::ForceExit()
|
|
609 |
//
|
|
610 |
// Called to force the thread to exit when it resumes
|
|
611 |
//
|
|
612 |
{
|
|
613 |
static_cast<NThread*>(this)->DoForceExit();
|
|
614 |
}
|
|
615 |
|
273
|
616 |
void NThreadBase::OnExit()
|
0
|
617 |
{
|
|
618 |
}
|
|
619 |
|
273
|
620 |
void NThreadBase::OnKill()
|
0
|
621 |
{
|
|
622 |
}
|
|
623 |
|