author | William Roberts <williamr@symbian.org> |
Mon, 19 Jul 2010 15:53:40 +0100 | |
changeset 213 | cd1a471fd308 |
parent 201 | 43365a9b78a3 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 1994-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". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// e32\kernel\sthread.cpp |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
#include <kernel/kern_priv.h> |
|
19 |
#include "execs.h" |
|
20 |
#include <kernel/emi.h> |
|
21 |
||
22 |
#define iMState iWaitLink.iSpare1 // Allow a sensible name to be used for iMState |
|
23 |
#define iExiting iWaitLink.iSpare2 // Allow a sensible name to be used for iExiting |
|
24 |
#define iWaitListReserved iWaitLink.iSpare3 // Allow a sensible name to be used for iWaitListReserved |
|
25 |
#define __CHECK_PRIORITIES(t) __ASSERT_DEBUG((t)->iWaitLink.iPriority==(t)->iNThread.i_NThread_BasePri,Kern::Fault("WaitLinkPriT",__LINE__)) |
|
26 |
||
27 |
_LIT(KLitKill,"Kill"); |
|
28 |
_LIT(KLitTerminate,"Terminate"); |
|
29 |
||
30 |
extern const SNThreadHandlers EpocThreadHandlers = |
|
31 |
{ |
|
32 |
&DThread::EpocThreadExitHandler, |
|
33 |
NTHREAD_DEFAULT_STATE_HANDLER, |
|
34 |
&Exc::Dispatch, |
|
35 |
&DThread::EpocThreadTimeoutHandler |
|
36 |
}; |
|
37 |
||
38 |
/******************************************** |
|
39 |
* Thread cleanup entries |
|
40 |
********************************************/ |
|
41 |
EXPORT_C TThreadCleanup::TThreadCleanup() |
|
42 |
: iThread(NULL) |
|
43 |
{ |
|
44 |
} |
|
45 |
||
46 |
// Enter and return with system locked. |
|
47 |
void TThreadCleanup::ChangePriority(TInt aNewPriority) |
|
48 |
{ |
|
49 |
__KTRACE_OPT(KTHREAD,Kern::Printf("TThreadCleanup::ChangePriority %d to %d, thread %O nest %d",iPriority,aNewPriority,iThread,K::PINestLevel)); |
|
50 |
if (aNewPriority != iPriority) |
|
51 |
{ |
|
52 |
if (iThread) |
|
53 |
{ |
|
54 |
iThread->iCleanupQ.ChangePriority(this,aNewPriority); |
|
55 |
if (++K::PINestLevel<KMaxPriorityInheritanceNesting) |
|
56 |
iThread->SetRequiredPriority(); |
|
57 |
} |
|
58 |
else |
|
59 |
iPriority=TUint8(aNewPriority); |
|
60 |
} |
|
61 |
} |
|
62 |
||
63 |
// Enter and return with system locked. |
|
64 |
/** |
|
65 |
@pre Interrupts must be enabled. |
|
66 |
@pre Kernel must be unlocked. |
|
67 |
@pre System must be locked |
|
68 |
@pre Call in a thread context. |
|
69 |
*/ |
|
70 |
EXPORT_C void TThreadCleanup::Remove() |
|
71 |
{ |
|
72 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED|MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
73 |
"TThreadCleanup::Remove"); |
|
74 |
// __KTRACE_OPT(KTHREAD,Kern::Printf("TThreadCleanup::Remove priority %d, thread %O",iPriority,iThread)); |
|
75 |
iThread->iCleanupQ.Remove(this); |
|
76 |
if (iPriority>=iThread->iNThread.i_NThread_BasePri) // can't affect priority if lower than current |
|
77 |
iThread->SetRequiredPriority(); |
|
78 |
} |
|
79 |
||
80 |
/******************************************** |
|
81 |
* Thread |
|
82 |
********************************************/ |
|
83 |
||
84 |
#ifdef KTHREAD |
|
85 |
void DThread::DefaultUnknownStateHandler(DThread* aThread, TInt anOperation, TInt aParameter) |
|
86 |
#else |
|
87 |
void DThread::DefaultUnknownStateHandler(DThread* aThread, TInt, TInt) |
|
88 |
#endif |
|
89 |
{ |
|
90 |
__KTRACE_OPT(KTHREAD,Kern::Printf("UNKNOWN THREAD MSTATE!! thread %O Mstate=%d, operation=%d, parameter=%d", |
|
91 |
aThread,aThread->iMState,anOperation,aParameter)); |
|
92 |
Kern::Fault("THREAD STATE",aThread->iMState); |
|
93 |
} |
|
94 |
||
95 |
DThread::DThread() |
|
96 |
: iTls(KTlsArrayGranularity,_FOFF(STls,iHandle)), |
|
97 |
iDebugMask(0xffffffff), iKillDfc(NULL,this,K::SvMsgQ,2), |
|
98 |
iUnknownStateHandler(DefaultUnknownStateHandler), |
|
99 |
iExitType((TUint8)EExitPending) |
|
100 |
#ifdef __SMP__ |
|
101 |
, iSMPSafeCallback(SMPSafeCallback) |
|
102 |
#endif |
|
103 |
{ |
|
104 |
iKernMsg.iSem.iOwningThread=&iNThread; |
|
105 |
iMState=ECreated; |
|
106 |
iExiting=EFalse; |
|
107 |
} |
|
108 |
||
109 |
// Enter and return with system unlocked. |
|
110 |
void DThread::Destruct() |
|
111 |
{ |
|
112 |
__KTRACE_OPT(KTHREAD,Kern::Printf("DThread::Destruct %O",this)); |
|
113 |
__NK_ASSERT_DEBUG(((TUint)iNThread.iUserModeCallbacks & ~3) == NULL); |
|
114 |
iTls.Close(); |
|
115 |
if (iSyncMsgPtr) |
|
116 |
{ |
|
102
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
117 |
// The sync message might still be outstanding; in particular it may be |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
118 |
// on the kernel server's list of messages to be cleaned up on behalf of |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
119 |
// dead clients. So we only release it here if it's free; otherwise, we |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
120 |
// mutate the type, so that cleanup will return it to the Global pool. |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
121 |
NKern::LockSystem(); |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
122 |
if (iSyncMsgPtr->IsFree()) |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
123 |
{ |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
124 |
NKern::UnlockSystem(); |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
125 |
__KTRACE_OPT(KSERVER,Kern::Printf("DThread::Destruct(%08X) releasing sync message at %08X", this, iSyncMsgPtr)); |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
126 |
iSyncMsgPtr->ReleaseMessagePool(RMessageK::ESync, 1); |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
127 |
} |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
128 |
else |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
129 |
{ |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
130 |
__KTRACE_OPT(KSERVER,Kern::Printf("DThread::Destruct(%08X) mutating sync message at %08X", this, iSyncMsgPtr)); |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
131 |
iSyncMsgPtr->iMsgType = RMessageK::EGlobal; |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
132 |
NKern::UnlockSystem(); |
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
133 |
} |
0 | 134 |
iSyncMsgPtr = NULL; |
135 |
} |
|
136 |
FreeSupervisorStack(); |
|
137 |
iHandles.Close(iOwningProcess); |
|
138 |
Kern::SafeClose(iExtTempObj,NULL); |
|
139 |
if (iNThread.iExtraContextSize > 0) |
|
140 |
Kern::Free(iNThread.iExtraContext); |
|
141 |
} |
|
142 |
||
143 |
// Enter and return with system unlocked. |
|
144 |
void DThread::Release() |
|
145 |
{ |
|
146 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Thread %O Release()",this)); |
|
147 |
if (iWaitListReserved) |
|
148 |
{ |
|
149 |
iWaitListReserved = 0; |
|
150 |
TThreadWaitList::ThreadDestroyed(); |
|
151 |
} |
|
152 |
iTls.Close(); |
|
153 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Close temporary object %O",iTempObj)); |
|
154 |
Kern::SafeClose(iTempObj,NULL); |
|
155 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Decrement temporary message %08x",iTempMsg)); |
|
156 |
if (iTempMsg) |
|
157 |
{ |
|
158 |
NKern::LockSystem(); |
|
159 |
iTempMsg->CloseRef(); |
|
160 |
NKern::UnlockSystem(); |
|
161 |
} |
|
162 |
iTempMsg=NULL; |
|
163 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Free temporary allocation %08x",iTempAlloc)); |
|
164 |
Kern::Free(iTempAlloc); |
|
165 |
iTempAlloc=NULL; |
|
166 |
||
167 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Removing closing libraries")); |
|
168 |
if (!iClosingLibs.IsEmpty()) |
|
169 |
RemoveClosingLibs(); |
|
170 |
||
171 |
__KTRACE_OPT(KTHREAD, Kern::Printf("Unmapping code segs delayed by User::Leave() (if any)")); |
|
172 |
if (iLeaveDepth) |
|
173 |
CleanupLeave(iLeaveDepth); |
|
174 |
||
175 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Deleting handles...")); |
|
176 |
iHandles.Close(iOwningProcess); |
|
177 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Deleted handles")); |
|
178 |
||
179 |
// deallocate the user stack if any |
|
180 |
if (iThreadType==EThreadUser) |
|
181 |
FreeUserStack(); |
|
182 |
||
183 |
// cancel the timer |
|
184 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Cancelling timer")); |
|
185 |
iTimer.Cancel(NULL); |
|
186 |
||
187 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Freeing supervisor-mode stack")); |
|
188 |
FreeSupervisorStack(); |
|
189 |
#ifdef BTRACE_THREAD_IDENTIFICATION |
|
190 |
BTrace12(BTrace::EThreadIdentification,BTrace::EThreadDestroy,&iNThread,iOwningProcess,iId); |
|
191 |
#endif |
|
192 |
__DEBUG_EVENT(EEventRemoveThread, this); |
|
193 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Closing thread")); |
|
194 |
Close(iOwningProcess); // close the thread |
|
195 |
} |
|
196 |
||
197 |
// Enter and return with system locked. |
|
198 |
/** |
|
199 |
@pre Interrupts must be enabled. |
|
200 |
@pre Kernel must be unlocked. |
|
201 |
@pre System must be locked |
|
202 |
@pre Call in a thread context. |
|
203 |
*/ |
|
204 |
EXPORT_C void DThread::AddCleanup(TThreadCleanup* aCleanup) |
|
205 |
{ |
|
206 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED|MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
207 |
"DThread::AddCleanup"); |
|
208 |
aCleanup->iThread=this; |
|
209 |
iCleanupQ.Add(aCleanup); |
|
210 |
if (aCleanup->iPriority>iNThread.i_NThread_BasePri) // can't affect priority unless it's higher than current |
|
211 |
SetActualPriority(aCleanup->iPriority); // this cleanup item must be the highest priority |
|
212 |
} |
|
213 |
||
214 |
// Enter and return with system locked. |
|
215 |
void DThread::Suspend(TInt aCount) |
|
216 |
{ |
|
217 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Suspend thread %O, count %d",this,aCount)); |
|
218 |
||
219 |
TBool r=NKern::ThreadSuspend(&iNThread,aCount); |
|
220 |
if (!r) |
|
221 |
return; // suspend was deferred (always so if self-suspend) |
|
222 |
||
223 |
// suspend has taken effect - fix up wait queues |
|
224 |
K::PINestLevel=0; |
|
225 |
switch (iMState) |
|
226 |
{ |
|
227 |
case ECreated: |
|
228 |
case EDead: |
|
229 |
case EReady: |
|
230 |
case EWaitSemaphoreSuspended: |
|
231 |
case EWaitMutexSuspended: |
|
232 |
case EWaitCondVarSuspended: |
|
233 |
break; |
|
234 |
case EWaitSemaphore: |
|
235 |
((DSemaphore*)iWaitObj)->SuspendWaitingThread(this); |
|
236 |
break; |
|
237 |
case EWaitMutex: |
|
238 |
((DMutex*)iWaitObj)->SuspendWaitingThread(this); |
|
239 |
break; |
|
240 |
case EHoldMutexPending: |
|
241 |
((DMutex*)iWaitObj)->SuspendPendingThread(this); |
|
242 |
break; |
|
243 |
case EWaitCondVar: |
|
244 |
((DCondVar*)iWaitObj)->SuspendWaitingThread(this); |
|
245 |
break; |
|
246 |
default: |
|
247 |
UnknownState(ESuspend,0); |
|
248 |
break; |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
// Enter and return with system locked. |
|
253 |
void DThread::Resume() |
|
254 |
{ |
|
255 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Resume thread %O",this)); |
|
256 |
||
257 |
TBool r=NKern::ThreadResume(&iNThread); |
|
258 |
if (!r) |
|
259 |
return; // just cancelled deferred suspends |
|
260 |
||
261 |
// resume has taken effect - fix up wait queues |
|
262 |
K::PINestLevel=0; |
|
263 |
switch (iMState) |
|
264 |
{ |
|
265 |
case ECreated: |
|
266 |
case EDead: |
|
267 |
case EReady: |
|
268 |
case EWaitSemaphore: |
|
269 |
case EWaitMutex: |
|
270 |
case EHoldMutexPending: |
|
271 |
case EWaitCondVar: |
|
272 |
break; |
|
273 |
case EWaitSemaphoreSuspended: |
|
274 |
((DSemaphore*)iWaitObj)->ResumeWaitingThread(this); |
|
275 |
break; |
|
276 |
case EWaitMutexSuspended: |
|
277 |
((DMutex*)iWaitObj)->ResumeWaitingThread(this); |
|
278 |
break; |
|
279 |
case EWaitCondVarSuspended: |
|
280 |
((DCondVar*)iWaitObj)->ResumeWaitingThread(this); |
|
281 |
break; |
|
282 |
default: |
|
283 |
UnknownState(EResume,0); |
|
284 |
break; |
|
285 |
} |
|
286 |
} |
|
287 |
||
288 |
void DThread::ForceResume() |
|
289 |
// |
|
290 |
// Resume the thread regardless of nested suspensions |
|
291 |
// Enter and return with system locked. |
|
292 |
// |
|
293 |
{ |
|
294 |
__KTRACE_OPT(KTHREAD,Kern::Printf("ForceResume thread %O",this)); |
|
295 |
||
296 |
TBool r=NKern::ThreadForceResume(&iNThread); |
|
297 |
if (!r) |
|
298 |
return; // just cancelled deferred suspends |
|
299 |
||
300 |
// resume has taken effect - fix up wait queues |
|
301 |
K::PINestLevel=0; |
|
302 |
switch (iMState) |
|
303 |
{ |
|
304 |
case ECreated: |
|
305 |
case EDead: |
|
306 |
case EReady: |
|
307 |
case EWaitSemaphore: |
|
308 |
case EWaitMutex: |
|
309 |
case EHoldMutexPending: |
|
310 |
case EWaitCondVar: |
|
311 |
break; |
|
312 |
case EWaitSemaphoreSuspended: |
|
313 |
((DSemaphore*)iWaitObj)->ResumeWaitingThread(this); |
|
314 |
break; |
|
315 |
case EWaitMutexSuspended: |
|
316 |
((DMutex*)iWaitObj)->ResumeWaitingThread(this); |
|
317 |
break; |
|
318 |
case EWaitCondVarSuspended: |
|
319 |
((DCondVar*)iWaitObj)->ResumeWaitingThread(this); |
|
320 |
break; |
|
321 |
default: |
|
322 |
UnknownState(EResume,0); |
|
323 |
break; |
|
324 |
} |
|
325 |
} |
|
326 |
||
327 |
// Cancel the thread's timer |
|
328 |
// Enter and return with system locked. |
|
329 |
EXPORT_C void DThread::CancelTimer() |
|
330 |
{ |
|
331 |
iTimer.Cancel(NULL); |
|
332 |
iTimer.iState = (TUint8)TTimer::EIdle; |
|
333 |
} |
|
334 |
||
335 |
// Release the thread's wait on any sync object (not explicit suspend) |
|
336 |
// Enter and return with system locked. |
|
337 |
/** |
|
338 |
@pre Interrupts must be enabled. |
|
339 |
@pre Kernel must be unlocked. |
|
340 |
@pre System must be locked |
|
341 |
@pre Call in a thread context. |
|
342 |
*/ |
|
343 |
EXPORT_C TInt DThread::ReleaseWait(TInt aReturnCode) |
|
344 |
{ |
|
345 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED|MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
346 |
"DThread::ReleaseWait"); |
|
347 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Thread %O ReleaseWait() retcode=%d",this,aReturnCode)); |
|
348 |
switch(iMState) |
|
349 |
{ |
|
350 |
case ECreated: |
|
351 |
case EDead: |
|
352 |
case EReady: |
|
353 |
return KErrGeneral; |
|
354 |
case EWaitSemaphore: |
|
355 |
((DSemaphore*)iWaitObj)->WaitCancel(this); |
|
356 |
break; |
|
357 |
case EWaitSemaphoreSuspended: |
|
358 |
((DSemaphore*)iWaitObj)->WaitCancelSuspended(this); |
|
359 |
break; |
|
360 |
case EWaitMutex: |
|
361 |
((DMutex*)iWaitObj)->WaitCancel(this); |
|
362 |
break; |
|
363 |
case EWaitMutexSuspended: |
|
364 |
((DMutex*)iWaitObj)->WaitCancelSuspended(this); |
|
365 |
break; |
|
366 |
case EHoldMutexPending: |
|
367 |
((DMutex*)iWaitObj)->RemovePendingThread(this); |
|
368 |
break; |
|
369 |
case EWaitCondVar: |
|
370 |
((DCondVar*)iWaitObj)->WaitCancel(this); |
|
371 |
break; |
|
372 |
case EWaitCondVarSuspended: |
|
373 |
((DCondVar*)iWaitObj)->WaitCancelSuspended(this); |
|
374 |
break; |
|
375 |
default: |
|
376 |
UnknownState(EReleaseWait,aReturnCode); |
|
377 |
break; |
|
378 |
} |
|
379 |
iWaitObj=NULL; |
|
380 |
iMState=EReady; |
|
381 |
NKern::ThreadRelease(&iNThread,aReturnCode); |
|
382 |
return KErrNone; |
|
383 |
} |
|
384 |
||
385 |
void DThread::EpocThreadTimeoutHandler(NThread* aThread, TInt aOp) |
|
386 |
{ |
|
387 |
if (aOp == NThreadBase::ETimeoutPreamble) |
|
388 |
{ |
|
389 |
NKern::LockSystem(); |
|
390 |
return; |
|
391 |
} |
|
392 |
if (aOp == NThreadBase::ETimeoutPostamble) |
|
393 |
{ |
|
394 |
DThread* pT = _LOFF(aThread, DThread, iNThread); |
|
395 |
pT->ReleaseWait(KErrTimedOut); |
|
396 |
} |
|
397 |
NKern::UnlockSystem(); |
|
398 |
} |
|
399 |
||
400 |
#ifdef SYMBIAN_CURB_SYSTEMSERVER_PRIORITIES |
|
401 |
const TUint8 KPrioritySystemServerMore = 23; |
|
402 |
#else |
|
403 |
const TUint8 KPrioritySystemServerMore = 24; |
|
404 |
#endif // SYMBIAN_CURB_SYSTEMSERVER_PRIORITIES |
|
405 |
const TInt KMaxPriorityWithoutProtServ = KPrioritySystemServerMore; |
|
406 |
||
407 |
// Mapping table for thread+process priority to thread absolute priority |
|
408 |
LOCAL_D const TUint8 ThreadPriorityTable[64] = |
|
409 |
{ |
|
410 |
// Idle MuchLess Less Normal More MuchMore RealTime |
|
411 |
/*Low*/ 1, 1, 2, 3, 4, 5, 22, 0, |
|
412 |
/*Background*/ 3, 5, 6, 7, 8, 9, 22, 0, |
|
413 |
/*Foreground*/ 3, 10, 11, 12, 13, 14, 22, 0, |
|
414 |
/*High*/ 3, 17, 18, 19, 20, 22, 23, 0, |
|
415 |
/*SystemServer1*/ 9, 15, 16, 21, KPrioritySystemServerMore, 25, 28, 0, |
|
416 |
/*SystemServer2*/ 9, 15, 16, 21, KPrioritySystemServerMore, 25, 28, 0, |
|
417 |
/*SystemServer3*/ 9, 15, 16, 21, KPrioritySystemServerMore, 25, 28, 0, |
|
418 |
/*RealTimeServer*/ 18, 26, 27, 28, 29, 30, 31, 0 |
|
419 |
}; |
|
420 |
||
421 |
TInt DThread::CalcDefaultThreadPriority() |
|
422 |
{ |
|
423 |
TInt r; |
|
424 |
TInt tp=iThreadPriority; |
|
425 |
if (tp>=0) |
|
426 |
r=(tp<KNumPriorities)?tp:KNumPriorities-1; // absolute thread priorities |
|
427 |
else |
|
428 |
{ |
|
429 |
tp+=8; |
|
430 |
if (tp<0) |
|
431 |
tp=0; |
|
432 |
TInt pp=iOwningProcess->iPriority; // process priority in range 0 to 7 |
|
433 |
TInt i=(pp<<3)+tp; |
|
434 |
r=ThreadPriorityTable[i]; // map thread+process priority to actual priority |
|
435 |
if ((r > KMaxPriorityWithoutProtServ) && |
|
436 |
!(this->HasCapability(ECapabilityProtServ,__PLATSEC_DIAGNOSTIC_STRING("Warning: thread priority capped to below realtime range")))) |
|
437 |
r = KMaxPriorityWithoutProtServ; |
|
438 |
} |
|
439 |
__KTRACE_OPT(KTHREAD,Kern::Printf("CalcDefaultThreadPriority tp %d pp %d abs %d",iThreadPriority,iOwningProcess->iPriority,r)); |
|
440 |
return r; |
|
441 |
} |
|
442 |
||
443 |
// Enter and return with system locked. |
|
444 |
/** |
|
445 |
@pre Interrupts must be enabled. |
|
446 |
@pre Kernel must be unlocked. |
|
447 |
@pre System must be locked |
|
448 |
@pre Call in a thread context. |
|
449 |
*/ |
|
450 |
EXPORT_C void DThread::SetThreadPriority(TInt aThreadPriority) |
|
451 |
{ |
|
452 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED|MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
453 |
"DThread::SetThreadPriority"); |
|
454 |
__KTRACE_OPT(KTHREAD,Kern::Printf("DThread %O SetThreadPriority %d",this,aThreadPriority)); |
|
455 |
iThreadPriority=aThreadPriority; |
|
456 |
TInt def=CalcDefaultThreadPriority(); |
|
457 |
SetDefaultPriority(def); |
|
458 |
} |
|
459 |
||
460 |
// Enter and return with system locked. |
|
461 |
void DThread::SetDefaultPriority(TInt aDefaultPriority) |
|
462 |
{ |
|
463 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Thread %O SetDefaultPriority %d",this,aDefaultPriority)); |
|
464 |
#ifdef BTRACE_THREAD_PRIORITY |
|
465 |
BTrace12(BTrace::EThreadPriority,BTrace::EDThreadPriority,&this->iNThread,iThreadPriority,aDefaultPriority); |
|
466 |
#endif |
|
467 |
#if defined(_DEBUG) && !defined(__SMP__) |
|
468 |
// Cap priorities at 1 if we're doing crazy scheduling |
|
469 |
if (TheSuperPage().KernelConfigFlags() & EKernelConfigCrazyScheduling && aDefaultPriority > 1) |
|
470 |
aDefaultPriority = 1; |
|
471 |
#endif |
|
472 |
iDefaultPriority=aDefaultPriority; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
473 |
NKern::ThreadSetNominalPriority(&iNThread, aDefaultPriority); |
0 | 474 |
K::PINestLevel=0; |
475 |
SetRequiredPriority(); |
|
476 |
} |
|
477 |
||
478 |
// Enter and return with system locked. |
|
479 |
void DThread::SetRequiredPriority() |
|
480 |
{ |
|
481 |
TInt p=iDefaultPriority; |
|
482 |
TInt c=iCleanupQ.HighestPriority(); |
|
483 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Thread %O SetRequiredPriority def %d cleanup %d nest %d", |
|
484 |
this,p,c,K::PINestLevel)); |
|
485 |
if (c>p) |
|
486 |
p=c; |
|
487 |
if (p!=iNThread.i_NThread_BasePri) |
|
488 |
SetActualPriority(p); |
|
489 |
} |
|
490 |
||
491 |
// Enter and return with system locked. |
|
492 |
void DThread::SetActualPriority(TInt anActualPriority) |
|
493 |
{ |
|
494 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Thread %O MState %d SetActualPriority %d",this,iMState,anActualPriority)); |
|
495 |
TInt newp=anActualPriority; |
|
496 |
__ASSERT_DEBUG(newp>=0 && newp<KNumPriorities, K::Fault(K::EBadThreadPriority)); |
|
497 |
NKern::ThreadSetPriority(&iNThread,newp); |
|
498 |
switch(iMState) |
|
499 |
{ |
|
500 |
case ECreated: |
|
501 |
case EWaitSemaphoreSuspended: |
|
502 |
case EWaitMutexSuspended: |
|
503 |
case EWaitCondVarSuspended: |
|
504 |
case EReady: |
|
505 |
case EDead: |
|
506 |
iWaitLink.iPriority=TUint8(newp); |
|
507 |
break; |
|
508 |
case EWaitSemaphore: |
|
509 |
((DSemaphore*)iWaitObj)->ChangeWaitingThreadPriority(this,newp); |
|
510 |
break; |
|
511 |
case EWaitMutex: |
|
512 |
((DMutex*)iWaitObj)->ChangeWaitingThreadPriority(this,newp); |
|
513 |
break; |
|
514 |
case EHoldMutexPending: |
|
515 |
((DMutex*)iWaitObj)->ChangePendingThreadPriority(this,newp); |
|
516 |
break; |
|
517 |
case EWaitCondVar: |
|
518 |
((DCondVar*)iWaitObj)->ChangeWaitingThreadPriority(this,newp); |
|
519 |
break; |
|
520 |
default: |
|
521 |
UnknownState(EChangePriority,newp); |
|
522 |
break; |
|
523 |
} |
|
524 |
__CHECK_PRIORITIES(this); |
|
525 |
} |
|
526 |
||
527 |
// Enter with system locked, return with system unlocked. |
|
528 |
// Assumes thread MState is READY. |
|
529 |
void DThread::RevertPriority() |
|
530 |
{ |
|
531 |
TInt p=iDefaultPriority; |
|
532 |
TInt c=iCleanupQ.HighestPriority(); |
|
533 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Thread %O RevertPriority def %d cleanup %d", |
|
534 |
this,iDefaultPriority,c)); |
|
535 |
if (c>p) |
|
536 |
p=c; |
|
537 |
if (p!=iNThread.i_NThread_BasePri) |
|
538 |
{ |
|
539 |
iWaitLink.iPriority=TUint8(p); |
|
540 |
NKern::ThreadSetPriority(&iNThread,p,SYSTEM_LOCK); // anti-thrash |
|
541 |
} |
|
542 |
else |
|
543 |
NKern::UnlockSystem(); |
|
544 |
} |
|
545 |
||
546 |
// Enter and return with system unlocked. |
|
547 |
TInt DThread::DoCreate(SThreadCreateInfo& aInfo) |
|
548 |
{ |
|
549 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Thread %O DoCreate: Function %08x Ptr %08x",this,aInfo.iFunction,aInfo.iPtr)); |
|
550 |
__KTRACE_OPT(KTHREAD,Kern::Printf("type %d, sup stack=%08x, sup stack size=%x",aInfo.iType,aInfo.iSupervisorStack,aInfo.iSupervisorStackSize)); |
|
551 |
__KTRACE_OPT(KTHREAD,Kern::Printf("user stack=%08x size %x, init priority=%d",aInfo.iUserStack,aInfo.iUserStackSize,aInfo.iInitialThreadPriority)); |
|
552 |
iSupervisorStackSize=aInfo.iSupervisorStackSize; |
|
553 |
if (iSupervisorStackSize==0) |
|
554 |
iSupervisorStackSize=K::SupervisorThreadStackSize; |
|
555 |
if (iThreadType==EThreadInitial || iThreadType==EThreadAPInitial || iThreadType==EThreadMinimalSupervisor) |
|
556 |
iSupervisorStack=aInfo.iSupervisorStack; |
|
557 |
TInt r=KErrNone; |
|
558 |
if (!iSupervisorStack) |
|
559 |
r=AllocateSupervisorStack(); |
|
560 |
if (r!=KErrNone) |
|
561 |
return r; |
|
562 |
iSyncMsgPtr = RMessageK::ClaimMessagePool(RMessageK::ESync, 1, NULL); |
|
563 |
__KTRACE_OPT(KSERVER,Kern::Printf("DThread::DoCreate(%08X) claimed sync message at %08X", this, iSyncMsgPtr)); |
|
564 |
if (K::MsgInfo.iChunk && !iSyncMsgPtr) |
|
565 |
return KErrNoMemory; |
|
566 |
SNThreadCreateInfo ni; |
|
567 |
ni.iFunction=EpocThreadFunction; |
|
568 |
ni.iPriority=1; // Overridden by nkern for initial thread(s) |
|
569 |
#if defined(_DEBUG) && !defined(__SMP__) |
|
570 |
// When doing crazy scheduling, all threads get just 1-tick timeslices |
|
571 |
if (TheSuperPage().KernelConfigFlags() & EKernelConfigCrazyScheduling) |
|
572 |
ni.iTimeslice = (iThreadType==EThreadMinimalSupervisor || iThreadType==EThreadAPInitial || iThreadType==EThreadInitial) ? -1 : 1; |
|
573 |
else |
|
574 |
#endif |
|
575 |
{ |
|
576 |
ni.iTimeslice = ( iThreadType==EThreadMinimalSupervisor |
|
577 |
|| iThreadType==EThreadAPInitial |
|
578 |
|| iThreadType==EThreadInitial |
|
579 |
) ? -1 |
|
580 |
: NKern::TimesliceTicks(EDefaultUserTimeSliceMs*1000); |
|
581 |
} |
|
582 |
ni.iAttributes=0; // overridden if necessary by memory model |
|
583 |
ni.iHandlers = &EpocThreadHandlers; |
|
584 |
ni.iFastExecTable=(const SFastExecTable*)EpocFastExecTable; |
|
585 |
ni.iSlowExecTable=(const SSlowExecTable*)EpocSlowExecTable; |
|
586 |
ni.iParameterBlock=(const TUint32*)&aInfo; |
|
587 |
ni.iParameterBlockSize=aInfo.iTotalSize; |
|
588 |
ni.iStackBase=iSupervisorStack; |
|
589 |
ni.iStackSize=iSupervisorStackSize; |
|
590 |
#ifdef __SMP__ |
|
591 |
TUint32 config = TheSuperPage().KernelConfigFlags(); |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
592 |
ni.iGroup = 0; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
593 |
ni.iCpuAffinity = KCpuAffinityAny; |
0 | 594 |
if (iThreadType==EThreadUser) |
595 |
{ |
|
596 |
// user thread |
|
597 |
if ((config & EKernelConfigSMPUnsafeCPU0) && iOwningProcess->iSMPUnsafeCount) |
|
598 |
{ |
|
599 |
ni.iCpuAffinity = 0; // compatibility mode |
|
600 |
} |
|
601 |
else |
|
602 |
{ |
|
603 |
if ((config & EKernelConfigSMPUnsafeCompat) && iOwningProcess->iSMPUnsafeCount) |
|
604 |
ni.iGroup = iOwningProcess->iSMPUnsafeGroup; |
|
605 |
} |
|
606 |
} |
|
607 |
else |
|
608 |
{ |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
609 |
if (config & EKernelConfigSMPLockKernelThreadsCore0) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
610 |
{ |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
611 |
// kernel thread |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
612 |
ni.iCpuAffinity = 0; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
613 |
} |
0 | 614 |
} |
615 |
#endif |
|
616 |
if (iThreadType!=EThreadInitial) |
|
617 |
{ |
|
618 |
if (iSupervisorStack) |
|
619 |
memset(iSupervisorStack,0xee,iSupervisorStackSize); |
|
620 |
if (iThreadType != EThreadAPInitial) |
|
621 |
{ |
|
622 |
TAny* ec = iNThread.iExtraContext; |
|
623 |
TInt ecs = iNThread.iExtraContextSize; |
|
624 |
r=NKern::ThreadCreate(&iNThread,ni); |
|
625 |
if (r!=KErrNone) |
|
626 |
return r; |
|
627 |
iNThread.SetExtraContext(ec, ecs); |
|
628 |
NKern::LockSystem(); |
|
629 |
SetThreadPriority(aInfo.iInitialThreadPriority); |
|
630 |
NKern::UnlockSystem(); |
|
631 |
} |
|
632 |
} |
|
633 |
else |
|
634 |
iMState=EReady; |
|
635 |
r=SetupContext(aInfo); |
|
636 |
if (r==KErrNone) |
|
637 |
r=iTimer.Create(); |
|
638 |
return r; |
|
639 |
} |
|
640 |
||
641 |
void svThreadKill(TAny* aPtr) |
|
642 |
{ |
|
643 |
((DThread*)aPtr)->SvKill(); |
|
644 |
} |
|
645 |
||
646 |
// Enter and return with system unlocked. |
|
647 |
TDfc* DThread::EpocThreadExitHandler(NThread* aThread) |
|
648 |
{ |
|
649 |
DThread* pT=_LOFF(aThread,DThread,iNThread); |
|
650 |
pT->Exit(); |
|
651 |
pT->iKillDfc.SetFunction(svThreadKill); |
|
652 |
return &pT->iKillDfc; // NKERN will queue this before terminating this thread |
|
653 |
} |
|
654 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
655 |
#if defined(__SMP__) && defined(KTIMING) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
656 |
TUint64 tix2us(TUint64 aTicks, TUint32 aFreq) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
657 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
658 |
TUint64 e6(1000000); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
659 |
aTicks *= e6; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
660 |
aTicks += TUint64(aFreq>>1); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
661 |
aTicks /= TUint64(aFreq); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
662 |
TUint64 us = aTicks % e6; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
663 |
TUint64 sec = aTicks / e6; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
664 |
return (sec<<32)|us; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
665 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
666 |
#endif |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
667 |
|
0 | 668 |
void DThread::Exit() |
669 |
// |
|
670 |
// This function runs in the context of the exiting thread |
|
671 |
// Enter and leave with system unlocked |
|
672 |
// |
|
673 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
674 |
#if defined(__SMP__) && defined(KTIMING) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
675 |
if (KDebugNum(KTIMING)) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
676 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
677 |
TUint64 rc = iNThread.iRunCount.i64; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
678 |
NSchedulable::SCpuStats stats; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
679 |
NKern::Lock(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
680 |
iNThread.GetCpuStats(NSchedulable::E_RunTime|NSchedulable::E_ActiveTime, stats); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
681 |
NKern::Unlock(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
682 |
TUint64 cputime = stats.iRunTime; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
683 |
TUint64 acttime = stats.iActiveTime; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
684 |
TUint32 f = NKern::CpuTimeMeasFreq(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
685 |
TUint64 avgcpu = rc ? cputime / rc : 0; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
686 |
TUint64 ratio = (acttime*100)/cputime; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
687 |
TUint64 cpud = tix2us(cputime, f); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
688 |
TUint64 actd = tix2us(acttime, f); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
689 |
TUint64 avgd = tix2us(avgcpu, f); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
690 |
Kern::Printf("Thread %O RC=%u CPU=%u.%06us ACT=%u.%06us AVG=%u.%06us RATIO=%d%%", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
691 |
this, TUint32(rc), |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
692 |
I64HIGH(cpud), I64LOW(cpud), |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
693 |
I64HIGH(actd), I64LOW(actd), |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
694 |
I64HIGH(avgd), I64LOW(avgd), |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
695 |
TUint32(ratio)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
696 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
697 |
#endif |
0 | 698 |
#ifdef KPANIC |
699 |
if (iExitType==EExitPanic) |
|
700 |
{ |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
701 |
__KTRACE_OPT2(KPANIC,KSCHED,Kern::Printf("Thread %O Panic %S %d",this,&iExitCategory,iExitReason)); |
0 | 702 |
} |
703 |
else if (iExitType==EExitTerminate) |
|
704 |
{ |
|
705 |
__KTRACE_OPT2(KPANIC,KSCHED,Kern::Printf("Thread %O Terminated %d",this,iExitReason)); |
|
706 |
} |
|
707 |
else if (iExitType==EExitKill && iExitReason!=KErrNone) |
|
708 |
{ |
|
709 |
__KTRACE_OPT2(KPANIC,KSCHED,Kern::Printf("Thread %O Killed %d",this,iExitReason)); |
|
710 |
} |
|
711 |
#endif |
|
712 |
if (iExitType!=EExitKill && (iFlags & (KThreadFlagSystemPermanent|KThreadFlagSystemCritical))) |
|
713 |
K::Fault(K::ESystemThreadPanic); |
|
714 |
if (iFlags & KThreadFlagSystemPermanent) |
|
715 |
K::Fault(K::EPermanentThreadExit); |
|
716 |
if (this==K::EventThread) |
|
717 |
K::Fault(K::EThrdEventHookDied); |
|
718 |
||
719 |
// call crash debugger after application panic if KALLTHREADSSYSTEM bit is set |
|
720 |
if (iExitType==EExitPanic && KDebugNum(KALLTHREADSSYSTEM)) |
|
721 |
K::Fault(K::ESystemThreadPanic); |
|
722 |
||
723 |
#if defined(_DEBUG) && !defined(__SMP__) |
|
724 |
// Delay cleanup if we're using the delayed scheduler |
|
725 |
if (KDebugNum(KCRAZYSCHEDDELAY)) |
|
726 |
NKern::Sleep(1); |
|
727 |
#endif |
|
728 |
||
729 |
// Clear any paging exc trap as if exiting this thread causes any page faults |
|
730 |
// the thread must not resume execution from the trap handler. |
|
731 |
iPagingExcTrap = 0; |
|
732 |
||
733 |
NKern::LockSystem(); |
|
734 |
ReleaseWait(KErrDied); |
|
735 |
iExiting=ETrue; // used to make logons complete early |
|
736 |
NKern::UnlockSystem(); |
|
737 |
||
738 |
// regularize the thread state before main exit processing |
|
739 |
DoExit1(); |
|
740 |
||
741 |
#ifdef __EMI_SUPPORT__ |
|
742 |
EMI::CallExitHandler(this); |
|
743 |
#endif |
|
744 |
||
745 |
// Hook into debugger if any. Not conditioned by __DEBUGGER_SUPPORT__ so |
|
746 |
// minimal post-mortem debugging is possible even without full debugger support. |
|
747 |
DKernelEventHandler::Dispatch(EEventKillThread, this, NULL); |
|
748 |
||
749 |
TBool kill_process = (iFlags&KThreadFlagProcessPermanent) || ((iFlags&KThreadFlagProcessCritical) && iExitType!=EExitKill); |
|
750 |
DProcess* pP=iOwningProcess; |
|
751 |
if (kill_process) |
|
752 |
pP->Die((TExitType)iExitType,iExitReason,iExitCategory); |
|
753 |
||
754 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Cancelling kernel message")); |
|
755 |
iKernMsg.Cancel(); |
|
756 |
||
757 |
NKern::LockSystem(); |
|
758 |
||
759 |
if (iIpcCount) |
|
760 |
{ |
|
761 |
iIpcCount |= 0x80000000u; |
|
762 |
Open(); |
|
763 |
} |
|
764 |
||
765 |
// clean up - release held mutexes etc. |
|
766 |
while(iCleanupQ.NonEmpty()) |
|
767 |
{ |
|
768 |
K::PINestLevel=0; |
|
769 |
TThreadCleanup* pCln=iCleanupQ.First(); |
|
770 |
pCln->Cleanup(); // this also removes the cleanup entry |
|
771 |
NKern::FlashSystem(); |
|
772 |
} |
|
773 |
NKern::UnlockSystem(); |
|
774 |
||
775 |
// complete target logons |
|
776 |
TLogon::CompleteAll(iTargetLogons, TLogon::ETarget, iExitReason); |
|
777 |
||
778 |
// remove any outstanding owned logons |
|
779 |
TLogon::CompleteAll(iOwnedLogons, TLogon::EOwned, KErrNone); |
|
780 |
||
781 |
// remove any outstanding miscellaneous notifiers |
|
782 |
KillMiscNotifiers(); |
|
783 |
||
784 |
// close the heap originally used by this thread |
|
785 |
CloseCreatedHeap(); |
|
786 |
||
787 |
// release any CPU resources (eg coprocessors) |
|
788 |
DoExit2(); |
|
789 |
||
790 |
//#ifdef KPANIC |
|
791 |
#if 0 |
|
792 |
extern void DumpMemory(const char* aTitle, TLinAddr aStart, TLinAddr aSize); |
|
793 |
||
794 |
if (KDebugNum(KPANIC) && (iExitType!=EExitKill || iExitReason!=KErrNone)) |
|
795 |
{ |
|
796 |
Kern::Printf("Thread %O iSavedSP=%08x", this, iNThread.iSavedSP); |
|
797 |
DumpMemory("Supervisor Stack", TLinAddr(iSupervisorStack), iSupervisorStackSize); |
|
798 |
if (iUserStackRunAddress) |
|
799 |
{ |
|
800 |
DumpMemory("User Stack", iUserStackRunAddress, iUserStackSize); |
|
801 |
} |
|
802 |
} |
|
803 |
#endif |
|
804 |
||
805 |
// stop the M-state machine |
|
806 |
NKern::LockSystem(); |
|
807 |
iMState=EDead; |
|
808 |
NKern::UnlockSystem(); |
|
809 |
} |
|
810 |
||
811 |
void DThread::DoExit1() |
|
812 |
{ |
|
813 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Thread %O DoExit1",this)); |
|
814 |
} |
|
815 |
||
816 |
/** Terminates the execution of a thread |
|
817 |
||
818 |
It terminates the specified thread and it sets its exit info. |
|
819 |
This is an asynchronous operation. |
|
820 |
||
821 |
This method can only be used by a thread to kill itself, or to kill |
|
822 |
a user thread (iThreadType==EThreadUser). An attempt to kill a non-user |
|
823 |
thread which is not the currently running thread will cause the system to fault |
|
824 |
with KERN 94 (ENonUserThreadKilled). |
|
825 |
||
826 |
@pre System Locked |
|
827 |
@post System Un-locked |
|
828 |
*/ |
|
829 |
void DThread::Die(TExitType aType, TInt aReason, const TDesC& aCategory) |
|
830 |
{ |
|
831 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED,"DThread::Die"); |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
832 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Thread %O Die: %d,%d,%S",this,aType,aReason,&aCategory)); |
0 | 833 |
SetExitInfo(aType,aReason,aCategory); |
834 |
||
835 |
// If necessary, decrement count of running user threads in this process. We get here if the |
|
836 |
// thread exited without calling User::Exit (eg panic, termination). |
|
837 |
if (iUserThreadState == EUserThreadCreated || iUserThreadState == EUserThreadRunning) |
|
838 |
{ |
|
839 |
__e32_atomic_tas_ord32(&iOwningProcess->iUserThreadsRunning, 1, -1, 0); |
|
840 |
iUserThreadState = EUserThreadExiting; |
|
841 |
} |
|
842 |
||
843 |
if (this==TheCurrentThread) |
|
844 |
{ |
|
845 |
SetDefaultPriority(KDefaultExitPriority); |
|
846 |
NKern::ThreadKill(&iNThread,SYSTEM_LOCK); // this will not return |
|
847 |
K::Fault(K::EDeadThreadRunning); |
|
848 |
} |
|
849 |
if(iThreadType!=EThreadUser) |
|
850 |
K::Fault(K::ENonUserThreadKilled); |
|
851 |
NKern::ThreadKill(&iNThread); |
|
852 |
SetDefaultPriority(KDefaultExitPriority); |
|
853 |
NKern::UnlockSystem(); |
|
854 |
} |
|
855 |
||
856 |
void DThread::SvKill() |
|
857 |
{ |
|
858 |
// |
|
859 |
// This function runs in the kernel server context |
|
860 |
// Enter and leave with system unlocked |
|
861 |
// |
|
862 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Thread %O SvKill()",this)); |
|
863 |
||
864 |
// move any queued user-mode callbacks to this thread and cancel them |
|
865 |
NKern::MoveUserModeCallbacks(NCurrentThread(), &iNThread); |
|
866 |
NKern::CancelUserModeCallbacks(); |
|
867 |
||
868 |
DProcess* pP=iOwningProcess; |
|
869 |
||
870 |
// take this thread off the process thread list |
|
871 |
Kern::MutexWait(*pP->iProcessLock); |
|
872 |
if (iProcessLink.iNext) |
|
873 |
{ |
|
874 |
iProcessLink.Deque(); |
|
875 |
iProcessLink.iNext=NULL; |
|
876 |
} |
|
877 |
TBool all_threads_gone=pP->iThreadQ.IsEmpty(); |
|
878 |
Kern::MutexSignal(*pP->iProcessLock); |
|
879 |
||
880 |
// if all threads have exited, complete process logons |
|
881 |
if (all_threads_gone) |
|
882 |
{ |
|
883 |
NKern::LockSystem(); |
|
884 |
if (pP->iExitType == EExitPending) |
|
885 |
{ |
|
886 |
// process has exited by last thread exiting, take its exit reason |
|
887 |
pP->iExitType = (TUint8)iExitType; |
|
888 |
pP->iExitReason = iExitReason; |
|
889 |
pP->iExitCategory = iExitCategory; |
|
890 |
} |
|
891 |
NKern::UnlockSystem(); |
|
892 |
__KTRACE_OPT(KPROC,Kern::Printf("Completing process logons for %O",pP)); |
|
893 |
TLogon::CompleteAll(pP->iTargetLogons, TLogon::ETarget, pP->iExitReason); |
|
894 |
} |
|
895 |
||
896 |
// notify undertakers + change notifiers |
|
897 |
Kern::NotifyChanges(EChangesThreadDeath); |
|
898 |
Kern::NotifyThreadDeath(this); |
|
899 |
||
900 |
// close handles, free stacks, close thread |
|
901 |
Release(); |
|
902 |
||
903 |
// if all threads in process now dead, clean up process |
|
904 |
if (all_threads_gone) |
|
905 |
pP->Release(); |
|
906 |
} |
|
907 |
||
908 |
void DThread::AbortTimer(TBool aAbortAbsolute) |
|
909 |
{ |
|
910 |
TInt typeMask=aAbortAbsolute?(TTimer::ELocked|TTimer::EAbsolute):(TTimer::ELocked); |
|
911 |
iTimer.Abort(this,typeMask); |
|
912 |
} |
|
913 |
||
914 |
||
915 |
void DThread::SetPaging(TUint& aCreateFlags) |
|
916 |
{// Default implementation that doesn't allow threads to be paged. |
|
917 |
// It is virtual so can be overridden on memory models that allow data paging. |
|
918 |
__ASSERT_COMPILE(EThreadCreateFlagPagingUnspec == 0); |
|
919 |
aCreateFlags &= ~EThreadCreateFlagPagingMask; |
|
920 |
} |
|
921 |
||
922 |
||
923 |
TInt DThread::Create(SThreadCreateInfo& aInfo) |
|
924 |
{ |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
925 |
__KTRACE_OPT(KTHREAD,Kern::Printf("DThread::Create %S owner %O size %03x", &aInfo.iName, |
0 | 926 |
iOwningProcess, aInfo.iTotalSize)); |
927 |
||
928 |
if (aInfo.iTotalSize < (TInt)sizeof(SThreadCreateInfo)) |
|
929 |
return KErrArgument; |
|
930 |
if (aInfo.iTotalSize > KMaxThreadCreateInfo) |
|
931 |
return KErrArgument; |
|
932 |
SetOwner(iOwningProcess); |
|
933 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Owner set")); |
|
934 |
TInt r=KErrNone; |
|
935 |
if (aInfo.iName.Length()!=0) |
|
936 |
{ |
|
937 |
SetProtection(DObject::EGlobal); |
|
938 |
r=SetName(&aInfo.iName); |
|
939 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Name set, %d",r)); |
|
940 |
if (r!=KErrNone) |
|
941 |
return r; |
|
942 |
} |
|
943 |
else |
|
944 |
SetProtection(DObject::EProtected); |
|
945 |
||
946 |
iId = K::NewId(); |
|
947 |
iThreadPriority=aInfo.iInitialThreadPriority; |
|
948 |
iThreadType=(TUint8)aInfo.iType; |
|
949 |
||
950 |
// Determine the data paging properties for the thread and create user stack. |
|
951 |
if (iThreadType == EThreadUser) |
|
952 |
{ |
|
953 |
// This is a user side thread so aInfo will be a SStdEpocThreadCreateInfo. |
|
954 |
__NK_ASSERT_DEBUG(aInfo.iTotalSize == sizeof(SStdEpocThreadCreateInfo)); |
|
955 |
SStdEpocThreadCreateInfo& info = (SStdEpocThreadCreateInfo&)aInfo; |
|
956 |
SetPaging(info.iFlags); // Will update info.iFlags to have paged or unpaged set. |
|
957 |
r = AllocateUserStack(info.iUserStackSize, info.iFlags & EThreadCreateFlagPaged); |
|
958 |
if (r != KErrNone) |
|
959 |
return r; |
|
960 |
} |
|
961 |
||
962 |
// inherit system critical + process critical from process (implements AllThreasdCritical) |
|
963 |
if (iThreadType == EThreadInitial || iThreadType == EThreadAPInitial) |
|
964 |
iFlags |= KThreadFlagSystemPermanent; // initial thread can't exit for any reason |
|
965 |
else if (iThreadType == EThreadUser) |
|
966 |
iFlags |= (iOwningProcess->iFlags & KThreadFlagProcessCritical); |
|
967 |
else |
|
968 |
iFlags |= KThreadFlagSystemCritical; // kernel threads can't panic |
|
969 |
||
970 |
// create platform-dependent stuff |
|
971 |
r=DoCreate(aInfo); |
|
972 |
if (r!=KErrNone) |
|
973 |
return r; |
|
974 |
r = TThreadWaitList::ThreadCreated(); |
|
975 |
if (r!=KErrNone) |
|
976 |
return r; |
|
977 |
iWaitListReserved = 1; |
|
978 |
if (iThreadType!=EThreadInitial && iThreadType!=EThreadMinimalSupervisor) |
|
979 |
r=K::Containers[EThread]->Add(this); |
|
980 |
return r; |
|
981 |
} |
|
982 |
||
983 |
TInt DThread::SetPriority(TThreadPriority aPriority) |
|
984 |
{ |
|
985 |
TInt tp=0; |
|
986 |
switch(aPriority) |
|
987 |
{ |
|
988 |
case EPriorityMuchLess: tp=EThrdPriorityMuchLess; break; |
|
989 |
case EPriorityLess: tp=EThrdPriorityLess; break; |
|
990 |
case EPriorityNormal: tp=EThrdPriorityNormal; break; |
|
991 |
case EPriorityMore: tp=EThrdPriorityMore; break; |
|
992 |
case EPriorityMuchMore: tp=EThrdPriorityMuchMore; break; |
|
993 |
case EPriorityRealTime: tp=EThrdPriorityRealTime; break; |
|
994 |
case EPriorityAbsoluteVeryLow: tp=EThrdPriorityAbsoluteVeryLow; break; |
|
995 |
case EPriorityAbsoluteLowNormal: tp=EThrdPriorityAbsoluteLowNormal; break; |
|
996 |
case EPriorityAbsoluteLow: tp=EThrdPriorityAbsoluteLow; break; |
|
997 |
case EPriorityAbsoluteBackgroundNormal: tp=EThrdPriorityAbsoluteBackgroundNormal; break; |
|
998 |
case EPriorityAbsoluteBackground: tp=EThrdPriorityAbsoluteBackground; break; |
|
999 |
case EPriorityAbsoluteForegroundNormal: tp=EThrdPriorityAbsoluteForegroundNormal; break; |
|
1000 |
case EPriorityAbsoluteForeground: tp=EThrdPriorityAbsoluteForeground; break; |
|
1001 |
case EPriorityAbsoluteHighNormal: tp=EThrdPriorityAbsoluteHighNormal; break; |
|
1002 |
case EPriorityAbsoluteHigh: tp=EThrdPriorityAbsoluteHigh; break; |
|
1003 |
case EPriorityAbsoluteRealTime1: tp=EThrdPriorityAbsoluteRealTime1; break; |
|
1004 |
case EPriorityAbsoluteRealTime2: tp=EThrdPriorityAbsoluteRealTime2; break; |
|
1005 |
case EPriorityAbsoluteRealTime3: tp=EThrdPriorityAbsoluteRealTime3; break; |
|
1006 |
case EPriorityAbsoluteRealTime4: tp=EThrdPriorityAbsoluteRealTime4; break; |
|
1007 |
case EPriorityAbsoluteRealTime5: tp=EThrdPriorityAbsoluteRealTime5; break; |
|
1008 |
case EPriorityAbsoluteRealTime6: tp=EThrdPriorityAbsoluteRealTime6; break; |
|
1009 |
case EPriorityAbsoluteRealTime7: tp=EThrdPriorityAbsoluteRealTime7; break; |
|
1010 |
case EPriorityAbsoluteRealTime8: tp=EThrdPriorityAbsoluteRealTime8; break; |
|
1011 |
default: |
|
1012 |
K::PanicCurrentThread(EBadPriority); |
|
1013 |
} |
|
1014 |
SetThreadPriority(tp); |
|
1015 |
return KErrNone; |
|
1016 |
} |
|
1017 |
||
1018 |
#ifndef __REQUEST_COMPLETE_MACHINE_CODED__ |
|
1019 |
||
1020 |
void DThread::RequestComplete(TRequestStatus*& aStatus, TInt aReason) |
|
1021 |
// |
|
1022 |
// Signal this threads request semaphore. |
|
1023 |
// Enter with system locked, return with system unlocked. |
|
1024 |
// |
|
1025 |
{ |
|
1026 |
||
1027 |
__KTRACE_OPT(KDATAPAGEWARN,Kern::Printf("Data paging: Use of deprecated DThread::RequestComplete API by %O", TheCurrentThread)); |
|
1028 |
TRequestStatus *theStatus=aStatus; |
|
1029 |
aStatus=NULL; // to indicate that this request has been completed |
|
1030 |
__KTRACE_OPT2(KTHREAD,KSEMAPHORE,Kern::Printf("DThread::RequestComplete %O %d->%08x",this,aReason,theStatus)); |
|
1031 |
||
1032 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
1033 |
||
1034 |
TInt r = KErrDied; |
|
1035 |
if (iMState!=EDead) |
|
1036 |
{ |
|
1037 |
TIpcExcTrap xt; |
|
1038 |
xt.iLocalBase=0; |
|
1039 |
xt.iRemoteBase=(TLinAddr)theStatus; |
|
1040 |
xt.iSize=sizeof(TInt); |
|
1041 |
xt.iDir=1; |
|
1042 |
r=xt.Trap(this); |
|
1043 |
if (r==KErrNone) |
|
1044 |
{ |
|
1045 |
// On some memory models(such as moving), RawRead may update the content of xt. It |
|
1046 |
// happens if home address is accessed (instead of the provided run address) or if it |
|
1047 |
// reads/writes in chunks. |
|
1048 |
r=RawWrite(theStatus,&aReason,sizeof(TInt),0,this,&xt); |
|
1049 |
xt.UnTrap(); |
|
1050 |
} |
|
1051 |
} |
|
1052 |
||
1053 |
if (r == KErrNone) |
|
1054 |
{ |
|
1055 |
#ifdef BTRACE_REQUESTS |
|
1056 |
BTraceContext12(BTrace::ERequests,BTrace::ERequestComplete,&this->iNThread,theStatus,aReason); |
|
1057 |
#endif |
|
1058 |
NKern::ThreadRequestSignal(&iNThread, SYSTEM_LOCK); |
|
1059 |
} |
|
1060 |
else |
|
1061 |
NKern::UnlockSystem(); |
|
1062 |
||
1063 |
#else |
|
1064 |
||
1065 |
NKern::UnlockSystem(); |
|
1066 |
if (iMState!=EDead && Kern::ThreadRawWrite(this,theStatus,&aReason,sizeof(TInt))==KErrNone) |
|
1067 |
{ |
|
1068 |
#ifdef BTRACE_REQUESTS |
|
1069 |
BTraceContext12(BTrace::ERequests,BTrace::ERequestComplete,&this->iNThread,theStatus,aReason); |
|
1070 |
#endif |
|
1071 |
NKern::ThreadRequestSignal(&iNThread); |
|
1072 |
} |
|
1073 |
||
1074 |
#endif |
|
1075 |
} |
|
1076 |
||
1077 |
#endif |
|
1078 |
||
1079 |
#if !defined(__REQUEST_COMPLETE_MACHINE_CODED__) || defined(__MEMMODEL_FLEXIBLE__) |
|
1080 |
||
1081 |
/** Write back a completion code and signal a thread's request semaphore, indicating that an asynchronous request has completed. |
|
1082 |
||
1083 |
@param aThread Thread to be signaled |
|
1084 |
@param aStatus A TRequestStatus instance that will receive the request status code. |
|
1085 |
It must reside in user side address space. |
|
1086 |
@param aReason Request status code. KErrCancel indicates the request has been canceled. |
|
1087 |
||
1088 |
@pre No fast mutex can be held. |
|
1089 |
@pre Call in a thread context. |
|
1090 |
@pre Kernel must be unlocked |
|
1091 |
@pre interrupts enabled |
|
1092 |
||
1093 |
@deprecated |
|
1094 |
*/ |
|
1095 |
EXPORT_C void Kern::RequestComplete(DThread* aThread, TRequestStatus*& aStatus, TInt aReason) |
|
1096 |
{ |
|
1097 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::RequestComplete"); |
|
1098 |
__KTRACE_OPT(KDATAPAGEWARN,Kern::Printf("Data paging: Use of deprecated Kern::RequestComplete API by %O", TheCurrentThread)); |
|
1099 |
NKern::LockSystem(); |
|
1100 |
if (aStatus) |
|
1101 |
aThread->RequestComplete(aStatus,aReason); |
|
1102 |
else |
|
1103 |
NKern::UnlockSystem(); |
|
1104 |
} |
|
1105 |
||
1106 |
#endif |
|
1107 |
||
1108 |
#ifndef __REQUEST_COMPLETE_MACHINE_CODED__ |
|
1109 |
||
1110 |
/** Complete a request made by the current thread. |
|
1111 |
||
1112 |
This writes the completion code and signals the current thread's request semaphore, indicating |
|
1113 |
that an asynchronous request has completed. |
|
1114 |
||
1115 |
Note that this must only be called in the context of the thread that made the request. |
|
1116 |
||
1117 |
@param aStatus A TRequestStatus instance that will receive the request status code. |
|
1118 |
It must reside in the user side address space of the current thread. |
|
1119 |
@param aReason Request status code. KErrCancel indicates the request has been canceled. |
|
1120 |
||
1121 |
@pre Call in a thread context. |
|
1122 |
@pre Kernel must be unlocked |
|
1123 |
@pre interrupts enabled |
|
1124 |
||
1125 |
@publishedPartner |
|
1126 |
@released |
|
1127 |
*/ |
|
1128 |
EXPORT_C void Kern::RequestComplete(TRequestStatus*& aStatus, TInt aReason) |
|
1129 |
{ |
|
1130 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED | MASK_INTERRUPTS_ENABLED | MASK_NOT_ISR | MASK_NOT_IDFC,"Kern::RequestComplete"); |
|
1131 |
TRequestStatus* status = (TRequestStatus*)__e32_atomic_swp_rel_ptr(&aStatus, 0); |
|
1132 |
if (status && KUSafeWrite(status, &aReason, sizeof(aReason)) == NULL) |
|
1133 |
NKern::ThreadRequestSignal(NKern::CurrentThread()); |
|
1134 |
} |
|
1135 |
||
1136 |
#endif |
|
1137 |
||
1138 |
/******************************************** |
|
1139 |
* User heap |
|
1140 |
********************************************/ |
|
1141 |
||
1142 |
class RUserAllocator : public RAllocator |
|
1143 |
{ |
|
1144 |
public: |
|
1145 |
TBool Close(); |
|
1146 |
TInt* GetHandleList(TInt& aCount); |
|
1147 |
}; |
|
1148 |
||
1149 |
// Decrement the object's reference count and return true if it reached zero |
|
1150 |
TBool RUserAllocator::Close() |
|
1151 |
{ |
|
1152 |
return Kern::KUSafeDec(iAccessCount) == 1; |
|
1153 |
} |
|
1154 |
||
1155 |
TInt* RUserAllocator::GetHandleList(TInt& aCount) |
|
1156 |
{ |
|
1157 |
__KTRACE_OPT(KTHREAD,Kern::Printf("RUserAllocator::GetHandleList() %08x", this)); |
|
1158 |
TInt* h[2] = {NULL, NULL}; |
|
1159 |
if (Close()) |
|
1160 |
kumemget32(h, &iHandleCount, sizeof(h)); |
|
1161 |
aCount = (TInt)h[0]; |
|
1162 |
return h[1]; |
|
1163 |
} |
|
1164 |
||
1165 |
#ifdef __USERSIDE_THREAD_DATA__ |
|
1166 |
void TLocalThreadData::Close() |
|
1167 |
{ |
|
1168 |
RUserAllocator* tlsHeap; |
|
1169 |
kumemget32(&tlsHeap, &iTlsHeap, sizeof(tlsHeap)); |
|
1170 |
if (tlsHeap) |
|
1171 |
tlsHeap->Close(); |
|
1172 |
} |
|
1173 |
#endif |
|
1174 |
||
1175 |
void DThread::CloseCreatedHeap() |
|
1176 |
{ |
|
1177 |
__KTRACE_OPT(KTHREAD,Kern::Printf("DThread::CloseCreatedHeap() %O",this)); |
|
1178 |
TInt r; |
|
1179 |
#ifdef __USERSIDE_THREAD_DATA__ |
|
1180 |
if (iFlags & KThreadFlagLocalThreadDataValid) |
|
1181 |
{ |
|
1182 |
TLocalThreadData* threadData = (TLocalThreadData*)(iUserStackRunAddress + iUserStackSize - KLocalThreadDataSize); |
|
1183 |
XTRAP(r, XT_DEFAULT, threadData->Close()); |
|
1184 |
__KTRACE_OPT(KTHREAD, Kern::Printf("TLocalThreadData::Close() r=%d", r)); |
|
1185 |
if (r != KErrNone) |
|
1186 |
return; |
|
1187 |
} |
|
1188 |
#endif |
|
1189 |
RUserAllocator* pA = (RUserAllocator*)__e32_atomic_swp_ord_ptr(&iCreatedAllocator, 0); |
|
1190 |
if (!pA) |
|
1191 |
return; |
|
1192 |
TInt c = 0; |
|
1193 |
TInt* h = NULL; |
|
1194 |
XTRAP(r, XT_DEFAULT, h = pA->GetHandleList(c)); |
|
1195 |
__KTRACE_OPT(KTHREAD,Kern::Printf("RUserAllocator::SvClose() r=%d c=%d",r,c)); |
|
1196 |
if (r!=KErrNone || (TUint)c>(TUint)RAllocator::EMaxHandles) |
|
1197 |
return; |
|
1198 |
||
1199 |
TInt handle[RAllocator::EMaxHandles]; |
|
1200 |
XTRAP(r, XT_DEFAULT, kumemget32(handle, h, c*sizeof(TInt))); |
|
1201 |
__KTRACE_OPT(KTHREAD,Kern::Printf("RUserAllocator::SvClose() r=%d h=%08x",r,h)); |
|
1202 |
if (r!=KErrNone) |
|
1203 |
return; |
|
1204 |
h = handle; |
|
1205 |
TInt* e = h + c; |
|
1206 |
for (; h<e; ++h) |
|
1207 |
{ |
|
1208 |
if (*h) |
|
1209 |
HandleClose(*h); |
|
1210 |
} |
|
1211 |
} |
|
1212 |
||
1213 |
TInt DThread::SetTls(TInt aHandle, TInt aDllUid, TAny* aPtr) |
|
1214 |
// |
|
1215 |
// Set the Thread Local Storage variable for a DLL. |
|
1216 |
// |
|
1217 |
{ |
|
1218 |
STls tls; |
|
1219 |
tls.iHandle = aHandle; |
|
1220 |
tls.iDllUid = aDllUid; |
|
1221 |
tls.iPtr = aPtr; |
|
1222 |
TInt i; |
|
1223 |
TInt r=iTls.FindInSignedKeyOrder(tls,i); |
|
1224 |
if (r==KErrNone) |
|
1225 |
{ |
|
1226 |
iTls[i] = tls; |
|
1227 |
return KErrNone; |
|
1228 |
} |
|
1229 |
return iTls.Insert(tls,i); |
|
1230 |
} |
|
1231 |
||
1232 |
TAny* DThread::Tls(TInt aHandle, TInt aDllUid) |
|
1233 |
// |
|
1234 |
// Retrieve the Thread Local Storage variable for a DLL. |
|
1235 |
// |
|
1236 |
{ |
|
1237 |
STls tls; |
|
1238 |
tls.iHandle=aHandle; |
|
1239 |
TInt r=iTls.FindInSignedKeyOrder(tls); |
|
1240 |
if (r>=0 && iTls[r].iDllUid==aDllUid) |
|
1241 |
return iTls[r].iPtr; |
|
1242 |
return NULL; |
|
1243 |
} |
|
1244 |
||
1245 |
void DThread::FreeTls(TInt aHandle) |
|
1246 |
// |
|
1247 |
// Remove the Thread Local Storage variable for a DLL. |
|
1248 |
// |
|
1249 |
{ |
|
1250 |
STls tls; |
|
1251 |
tls.iHandle=aHandle; |
|
1252 |
TInt r=iTls.FindInSignedKeyOrder(tls); |
|
1253 |
if (r>=0) |
|
1254 |
{ |
|
1255 |
iTls.Remove(r); |
|
1256 |
iTls.Compress(); |
|
1257 |
} |
|
1258 |
} |
|
1259 |
||
1260 |
TInt DThread::Rename(const TDesC& aName) |
|
1261 |
{ |
|
1262 |
TKName n; |
|
1263 |
Name(n); // get current name |
|
1264 |
if (n.MatchF(aName)==0) |
|
1265 |
return KErrNone; // new name is the same so nothing to do |
|
1266 |
K::Containers[EThread]->Wait(); |
|
1267 |
TInt r=K::Containers[EThread]->CheckUniqueFullName(this,aName); |
|
1268 |
if (r==KErrNone) |
|
1269 |
{ |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
102
diff
changeset
|
1270 |
__KTRACE_OPT(KTHREAD,Kern::Printf("DThread::Rename %O to %S",this,&aName)); |
0 | 1271 |
r=SetName(&aName); |
1272 |
#ifdef BTRACE_THREAD_IDENTIFICATION |
|
1273 |
Name(n); |
|
1274 |
BTraceN(BTrace::EThreadIdentification,BTrace::EThreadName,&iNThread,iOwningProcess,n.Ptr(),n.Size()); |
|
1275 |
#endif |
|
1276 |
} |
|
1277 |
K::Containers[EThread]->Signal(); |
|
1278 |
__COND_DEBUG_EVENT(r==KErrNone, EEventUpdateThread, this); |
|
1279 |
return(r); |
|
1280 |
} |
|
1281 |
||
1282 |
||
1283 |
/** |
|
1284 |
@pre Interrupts must be enabled. |
|
1285 |
@pre Kernel must be unlocked. |
|
1286 |
@pre System must be locked |
|
1287 |
@pre Call in a thread context. |
|
1288 |
*/ |
|
1289 |
TInt DThread::GetDesInfo(const TAny* aDes, TInt& aLength, TInt& aMaxLength, TUint8*& aPtr, TBool aWriteable) |
|
1290 |
// |
|
1291 |
// Get remote descriptor info. |
|
1292 |
// Enter and leave with system locked |
|
1293 |
// |
|
1294 |
{ |
|
1295 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
1296 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED|MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
1297 |
"DThread::GetDesInfo"); |
|
1298 |
#else |
|
1299 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
1300 |
"DThread::GetDesInfo"); |
|
1301 |
#endif |
|
1302 |
TDesHeader d; |
|
1303 |
TInt r=ReadAndParseDesHeader(aDes,d); |
|
1304 |
if (r!=KErrNone) |
|
1305 |
return r; |
|
1306 |
aLength = d.Length(); |
|
1307 |
aPtr = (TUint8*)d.DataPtr(); |
|
1308 |
if (d.IsWriteable()) |
|
1309 |
aMaxLength = d.MaxLength(); |
|
1310 |
else |
|
1311 |
{ |
|
1312 |
if (aWriteable) |
|
1313 |
return KErrBadDescriptor; |
|
1314 |
aMaxLength = 0; |
|
1315 |
} |
|
1316 |
return KErrNone; |
|
1317 |
} |
|
1318 |
||
1319 |
/** |
|
1320 |
@pre Interrupts must be enabled. |
|
1321 |
@pre Kernel must be unlocked. |
|
1322 |
@pre System must be locked |
|
1323 |
@pre Call in a thread context. |
|
1324 |
*/ |
|
1325 |
TInt DThread::GetDesLength(const TAny* aPtr) |
|
1326 |
// |
|
1327 |
// Get the length of a descriptor. |
|
1328 |
// Enter and leave with system locked |
|
1329 |
// |
|
1330 |
{ |
|
1331 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
1332 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED|MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
1333 |
"DThread::GetDesLength"); |
|
1334 |
#else |
|
1335 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
1336 |
"DThread::GetDesLength"); |
|
1337 |
#endif |
|
1338 |
TDesHeader d; |
|
1339 |
TInt r=ReadAndParseDesHeader(aPtr,d); |
|
1340 |
if (r<0) |
|
1341 |
return r; |
|
1342 |
return d.Length(); |
|
1343 |
} |
|
1344 |
||
1345 |
/** |
|
1346 |
@pre Interrupts must be enabled. |
|
1347 |
@pre Kernel must be unlocked. |
|
1348 |
@pre System must be locked |
|
1349 |
@pre Call in a thread context. |
|
1350 |
*/ |
|
1351 |
TInt DThread::GetDesMaxLength(const TAny* aPtr) |
|
1352 |
// |
|
1353 |
// Get the maximum length of a descriptor. |
|
1354 |
// Enter and leave with system locked |
|
1355 |
// |
|
1356 |
{ |
|
1357 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
1358 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED|MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
1359 |
"DThread::GetDesMaxLength"); |
|
1360 |
#else |
|
1361 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
1362 |
"DThread::GetDesMaxLength"); |
|
1363 |
#endif |
|
1364 |
TDesHeader d; |
|
1365 |
TInt r=ReadAndParseDesHeader(aPtr,d); |
|
1366 |
if (r!=KErrNone) |
|
1367 |
return r; |
|
1368 |
return d.MaxLength(); |
|
1369 |
} |
|
1370 |
||
1371 |
/** |
|
1372 |
Reads from this thread's address space. |
|
1373 |
Enter and leave with system locked |
|
1374 |
||
1375 |
@param aPtr Points to the source descriptor. It must reside in this thread's address space. |
|
1376 |
@param aDest Points to the destination buffer, which is in the current process's address space. |
|
1377 |
@param aMax Specifies maximum number of characters to read. |
|
1378 |
@param anOffset The offset in the source descriptor to copy from. |
|
1379 |
@param aMode if orred by KCheckLocalAddress, aDest memory in the current process will be accessed with user attributes. |
|
1380 |
@return nonnegative value indicates the number of characters transferred. |
|
1381 |
KErrDied if this thread has died. |
|
1382 |
KErrBadDescriptor if the attempt to read aPtr descriptor caused the exception. |
|
1383 |
||
1384 |
@pre Interrupts must be enabled. |
|
1385 |
@pre Kernel must be unlocked. |
|
1386 |
@pre System must be locked |
|
1387 |
@pre Call in a thread context. |
|
1388 |
*/ |
|
1389 |
TInt DThread::DesRead(const TAny* aPtr, TUint8* aDest, TInt aMax, TInt anOffset, TInt aMode) |
|
1390 |
{ |
|
1391 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
1392 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED|MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
1393 |
"DThread::DesRead"); |
|
1394 |
#else |
|
1395 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
1396 |
"DThread::DesRead"); |
|
1397 |
#endif |
|
1398 |
TDesHeader d; |
|
1399 |
TInt r=ReadAndParseDesHeader(aPtr,d); |
|
1400 |
if (r!=KErrNone) |
|
1401 |
return r; |
|
1402 |
return DoDesRead(d, aDest, aMax, anOffset, aMode); |
|
1403 |
} |
|
1404 |
||
1405 |
// Read descriptor from thread's address space, given the descriptor header |
|
1406 |
TInt DThread::DoDesRead(const TDesHeader& aDesInfo, TUint8* aDest, TInt aMax, TInt anOffset, TInt aMode) |
|
1407 |
{ |
|
1408 |
if (anOffset<0 || aMax<0) |
|
1409 |
return KErrArgument; |
|
1410 |
TInt len=aDesInfo.Length(); |
|
1411 |
len-=anOffset; |
|
1412 |
if (len<0) |
|
1413 |
len=0; |
|
1414 |
if (len>aMax) |
|
1415 |
len=aMax; |
|
1416 |
TInt l=len; |
|
1417 |
if (aMode & KChunkShiftBy1) |
|
1418 |
{ |
|
1419 |
l<<=1; |
|
1420 |
anOffset<<=1; |
|
1421 |
} |
|
1422 |
const TUint8* pS = (TUint8*)aDesInfo.DataPtr() + anOffset; |
|
1423 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
1424 |
NKern::FlashSystem(); |
|
1425 |
#endif |
|
1426 |
TIpcExcTrap xt; |
|
1427 |
xt.iLocalBase=(TLinAddr)aDest; |
|
1428 |
xt.iRemoteBase=(TLinAddr)pS; |
|
1429 |
xt.iSize=l; |
|
1430 |
xt.iDir=0; |
|
1431 |
TInt r=xt.Trap(this); |
|
1432 |
if (r==0) |
|
1433 |
{ |
|
1434 |
//On some memory models(such as moving), RawRead may update the content of xt. It happens if home address |
|
1435 |
// is accessed (instead of the provided run address) or if it reads in chunks. |
|
1436 |
r=RawRead(pS,aDest,l,aMode&KCheckLocalAddress, &xt); |
|
1437 |
xt.UnTrap(); |
|
1438 |
} |
|
1439 |
return (r<0)?r:len; |
|
1440 |
} |
|
1441 |
||
1442 |
/** |
|
1443 |
Write to the thread's address space. |
|
1444 |
Enter and leave with system locked |
|
1445 |
||
1446 |
@param aPtr points to descriptor to write to. It is in remote (this thread's) address space. |
|
1447 |
@param aSrc points to source buffer, which is in the current process's address space |
|
1448 |
@param aLength is number of characters to copy |
|
1449 |
@param anOffset The offset in aPtr descriptor where to start writing. |
|
1450 |
@param aOrigThread The thread on behalf of which this operation is performed (eg client of device driver). If NULL, current thread is assumed. |
|
1451 |
@return KErrDied if this thread has died. |
|
1452 |
KErrBadDescriptor if the attempt to read aPtr descriptor caused the exception. |
|
1453 |
KErrNone otherwise |
|
1454 |
||
1455 |
@pre Interrupts must be enabled. |
|
1456 |
@pre Kernel must be unlocked. |
|
1457 |
@pre System must be locked |
|
1458 |
@pre Call in a thread context. |
|
1459 |
*/ |
|
1460 |
TInt DThread::DesWrite(const TAny* aPtr, const TUint8* aSrc, TInt aLength, TInt anOffset, TInt aMode, DThread* anOriginatingThread) |
|
1461 |
{ |
|
1462 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
1463 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED|MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
1464 |
"DThread::DesWrite"); |
|
1465 |
#else |
|
1466 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC, |
|
1467 |
"DThread::DesWrite"); |
|
1468 |
#endif |
|
1469 |
TDesHeader d; |
|
1470 |
TInt r=ReadAndParseDesHeader(aPtr, d); |
|
1471 |
if (r!=KErrNone) |
|
1472 |
return r; |
|
1473 |
r = DoDesWrite(aPtr, d, aSrc, aLength, anOffset, aMode, anOriginatingThread); |
|
1474 |
return r < KErrNone ? r : KErrNone; |
|
1475 |
} |
|
1476 |
||
1477 |
||
1478 |
// Write descriptor to thread's address space, given the descriptor header |
|
1479 |
// Return the new flags + length word, or one of the system-wide error codes |
|
1480 |
TInt DThread::DoDesWrite(const TAny* aPtr, TDesHeader& aDesInfo, const TUint8* aSrc, TInt aLength, TInt anOffset, TInt aMode, DThread* anOriginatingThread) |
|
1481 |
{ |
|
1482 |
if (anOffset<0 || aLength<0) |
|
1483 |
return KErrArgument; |
|
1484 |
if (!aDesInfo.IsWriteable()) |
|
1485 |
return KErrBadDescriptor; |
|
1486 |
TUint maxLen=aDesInfo.MaxLength(); |
|
1487 |
TUint8* pT=(TUint8*)aDesInfo.DataPtr(); // remote descriptor Ptr() |
|
1488 |
TUint finalLen=aLength+anOffset; |
|
1489 |
if (finalLen>maxLen) |
|
1490 |
{ |
|
1491 |
if (aMode & KTruncateToMaxLength) |
|
1492 |
{ |
|
1493 |
aLength=maxLen-anOffset; |
|
1494 |
finalLen=maxLen; |
|
1495 |
if (aLength<0) |
|
1496 |
return KErrOverflow; |
|
1497 |
} |
|
1498 |
else |
|
1499 |
return KErrOverflow; |
|
1500 |
} |
|
1501 |
TInt type=aDesInfo.Type(); |
|
1502 |
TUint typelen=TUint(type)<<KShiftDesType | TUint(finalLen); |
|
1503 |
TInt shift=(aMode&KChunkShiftBy1)?1:0; |
|
1504 |
anOffset<<=shift; |
|
1505 |
aLength<<=shift; |
|
1506 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
1507 |
NKern::FlashSystem(); |
|
1508 |
#endif |
|
1509 |
TIpcExcTrap xt; |
|
1510 |
xt.iDir=1; |
|
1511 |
TInt r=xt.Trap(this); |
|
1512 |
if (r==0) |
|
1513 |
{ |
|
1514 |
xt.iLocalBase=(TLinAddr)aSrc; |
|
1515 |
xt.iRemoteBase=(TLinAddr)(pT+anOffset); |
|
1516 |
xt.iSize=aLength; |
|
1517 |
if (aLength) |
|
1518 |
{ |
|
1519 |
//On some memory models(such as moving), RawRead may update the content of xt. It happens if home address |
|
1520 |
// is accessed (instead of the provided run address) or if it reads in chunks. |
|
1521 |
r=RawWrite(pT+anOffset,aSrc,aLength,aMode&KCheckLocalAddress,anOriginatingThread, &xt); |
|
1522 |
} |
|
1523 |
if ((aMode & KDoNotUpdateDesLength) == 0) |
|
1524 |
{ |
|
1525 |
if (r == KErrNone && type == EBufCPtr) |
|
1526 |
{ |
|
1527 |
TUint8* pL = pT - sizeof(TDesC); |
|
1528 |
xt.iLocalBase=0; |
|
1529 |
xt.iRemoteBase=(TLinAddr)pL; |
|
1530 |
xt.iSize=sizeof(finalLen); |
|
1531 |
r=RawWrite(pL,&finalLen,sizeof(finalLen),0,anOriginatingThread, &xt); |
|
1532 |
} |
|
1533 |
xt.iLocalBase=0; |
|
1534 |
xt.iRemoteBase=(TLinAddr)aPtr; |
|
1535 |
xt.iSize=sizeof(finalLen); |
|
1536 |
if (r==KErrNone) |
|
1537 |
r=RawWrite(aPtr,&typelen,sizeof(typelen),0,anOriginatingThread, &xt); |
|
1538 |
} |
|
1539 |
xt.UnTrap(); |
|
1540 |
} |
|
1541 |
return r == KErrNone ? typelen : r; |
|
1542 |
} |
|
1543 |
||
1544 |
TBool DThread::IsExceptionHandled(TExcType aType) |
|
1545 |
{ |
|
1546 |
if (iExceptionHandler==NULL) |
|
1547 |
return EFalse; |
|
1548 |
if (iFlags&KThreadFlagLastChance) |
|
1549 |
return EFalse; |
|
1550 |
switch (aType) |
|
1551 |
{ |
|
1552 |
case EExcGeneral: |
|
1553 |
case EExcUserInterrupt: |
|
1554 |
return iExceptionMask & KExceptionUserInterrupt; |
|
1555 |
case EExcIntegerDivideByZero: |
|
1556 |
case EExcIntegerOverflow: |
|
1557 |
return iExceptionMask & KExceptionInteger; |
|
1558 |
case EExcSingleStep: |
|
1559 |
case EExcBreakPoint: |
|
1560 |
return iExceptionMask & KExceptionDebug; |
|
1561 |
case EExcBoundsCheck: |
|
1562 |
case EExcInvalidOpCode: |
|
1563 |
case EExcDoubleFault: |
|
1564 |
case EExcStackFault: |
|
1565 |
case EExcAccessViolation: |
|
1566 |
case EExcPrivInstruction: |
|
1567 |
case EExcAlignment: |
|
1568 |
case EExcPageFault: |
|
1569 |
return iExceptionMask & KExceptionFault; |
|
1570 |
case EExcFloatDenormal: |
|
1571 |
case EExcFloatDivideByZero: |
|
1572 |
case EExcFloatInexactResult: |
|
1573 |
case EExcFloatInvalidOperation: |
|
1574 |
case EExcFloatOverflow: |
|
1575 |
case EExcFloatStackCheck: |
|
1576 |
case EExcFloatUnderflow: |
|
1577 |
return iExceptionMask & KExceptionFpe; |
|
1578 |
case EExcAbort: |
|
1579 |
return iExceptionMask & KExceptionAbort; |
|
1580 |
case EExcKill: |
|
1581 |
return iExceptionMask & KExceptionKill; |
|
1582 |
default: |
|
1583 |
return EFalse; |
|
1584 |
} |
|
1585 |
} |
|
1586 |
||
1587 |
/** |
|
1588 |
Terminates the current thread. |
|
1589 |
||
1590 |
@param aReason Reason to be set in the current thread's exit information. |
|
1591 |
||
1592 |
@pre No fast mutex can be held. |
|
1593 |
@pre Call in a thread context. |
|
1594 |
@pre Interrupts must be enabled. |
|
1595 |
@pre Kernel must be unlocked. |
|
1596 |
@pre Calling thread must not be in a critical section |
|
1597 |
@pre Can be used in a device driver. |
|
1598 |
||
1599 |
@post It doesn't return. |
|
1600 |
*/ |
|
1601 |
EXPORT_C void Kern::Exit(TInt aReason) |
|
1602 |
{ |
|
1603 |
// CHECK_PRECONDITIONS(MASK_THREAD_STANDARD|MASK_NO_CRITICAL,"Kern::Exit"); |
|
1604 |
#ifdef _DEBUG |
|
1605 |
#if (defined (__KERNEL_APIS_CONTEXT_CHECKS_WARNING__)||defined (__KERNEL_APIS_CONTEXT_CHECKS_FAULT__)) |
|
1606 |
if (TheCurrentThread->iThreadType==EThreadUser) |
|
1607 |
{ |
|
1608 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD|MASK_NO_CRITICAL,"Kern::Exit"); |
|
1609 |
} |
|
1610 |
else |
|
1611 |
{ |
|
1612 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::Exit"); |
|
1613 |
} |
|
1614 |
#endif |
|
1615 |
#endif |
|
1616 |
||
1617 |
NKern::LockSystem(); |
|
1618 |
TheCurrentThread->Die(EExitKill,aReason,KNullDesC); |
|
1619 |
} |
|
1620 |
||
1621 |
#ifdef KTHREAD |
|
1622 |
TInt DThread::RaiseException(TExcType aType) |
|
1623 |
#else |
|
1624 |
TInt DThread::RaiseException(TExcType) |
|
1625 |
#endif |
|
1626 |
{ |
|
1627 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Thread %O RaiseException(%d)",this,aType)); |
|
1628 |
if (this==TheCurrentThread) |
|
1629 |
return KErrGeneral; |
|
1630 |
else if (iThreadType!=EThreadUser) |
|
1631 |
return KErrAccessDenied; |
|
1632 |
return KErrNotSupported; |
|
1633 |
} |
|
1634 |
||
1635 |
void DThread::SetExitInfo(TExitType aType, TInt aReason, const TDesC& aCategory) |
|
1636 |
// |
|
1637 |
// Set a thread's exit info. |
|
1638 |
// Called with system locked. |
|
1639 |
// |
|
1640 |
{ |
|
1641 |
if (iExitType==EExitPending) |
|
1642 |
{ |
|
1643 |
if (iOwningProcess->iExitType!=EExitPending) |
|
1644 |
{ |
|
1645 |
// process has already exited, so take its exit info. |
|
1646 |
iExitType=iOwningProcess->iExitType; |
|
1647 |
iExitReason=iOwningProcess->iExitReason; |
|
1648 |
iExitCategory=iOwningProcess->iExitCategory; |
|
1649 |
return; |
|
1650 |
} |
|
1651 |
iExitType=(TUint8)aType; |
|
1652 |
iExitReason=aReason; |
|
1653 |
if (iExitType==EExitKill) |
|
1654 |
iExitCategory=KLitKill; |
|
1655 |
else if (iExitType==EExitTerminate) |
|
1656 |
iExitCategory=KLitTerminate; |
|
1657 |
else if (aType==EExitPanic) |
|
1658 |
iExitCategory=aCategory; |
|
1659 |
} |
|
1660 |
} |
|
1661 |
||
1662 |
void DThread::CleanupLeave(TInt aDepth) |
|
1663 |
// |
|
1664 |
// Enter and return with system unlocked and calling thread in critical section. |
|
1665 |
// |
|
1666 |
{ |
|
1667 |
__ASSERT_CRITICAL; |
|
1668 |
__NK_ASSERT_DEBUG(iLeaveDepth); |
|
1669 |
||
1670 |
DProcess* pP = iOwningProcess; |
|
1671 |
||
1672 |
iLeaveDepth -= aDepth; |
|
1673 |
||
1674 |
if ((!iLeaveDepth) && (__e32_atomic_add_ord32(&pP->iThreadsLeaving, TUint32(-1)) == 1) && (!pP->iGarbageList.IsEmpty())) |
|
1675 |
{ |
|
1676 |
DCodeSeg::Wait(); |
|
1677 |
||
1678 |
// Avoid race condition where code segs are put onto garbage list by another thread leaving, |
|
1679 |
// between the decrement of iThreadsLeaving above and aquiring the code seg mutex |
|
1680 |
if (!pP->iThreadsLeaving) |
|
1681 |
{ |
|
1682 |
while (!pP->iGarbageList.IsEmpty()) |
|
1683 |
{ |
|
1684 |
DLibrary* pL = _LOFF(pP->iGarbageList.First()->Deque(), DLibrary, iGbgLink); |
|
1685 |
__NK_ASSERT_DEBUG(pL->iAccessCount); |
|
1686 |
pL->iGbgLink.iNext = NULL; |
|
1687 |
||
1688 |
pL->ReallyRemoveFromProcess(); |
|
1689 |
pL->DObject::Close(NULL); |
|
1690 |
} |
|
1691 |
} |
|
1692 |
||
1693 |
DCodeSeg::Signal(); |
|
1694 |
} |
|
1695 |
} |
|
1696 |
||
1697 |
/** Function only temporarily supported to aid migration to process emulation... |
|
1698 |
*/ |
|
1699 |
TInt ExecHandler::ThreadAsProcess(DThread* aThread, TInt aLibrary) |
|
1700 |
{ |
|
1701 |
__KTRACE_OPT(KEXEC,Kern::Printf("ExecHandler::ThreadAsProcess %O",aThread)); |
|
1702 |
if (aThread->iOwningProcess->iSecurityZone!=TheCurrentThread->iOwningProcess->iSecurityZone) |
|
1703 |
K::ProcessIsolationFailure(__PLATSEC_DIAGNOSTIC_STRING("Use of RThread::Create(const TDesC& aName,TThreadFunction aFunction,TInt aStackSize,TAny* aPtr,RLibrary* aLibrary,RHeap* aHeap, TInt aHeapMinSize,TInt aHeapMaxSize,TOwnerType aType)")); |
|
1704 |
DObject* pL = K::ObjectFromHandle(aLibrary,ELibrary); |
|
1705 |
pL->CheckedOpen(); |
|
1706 |
TInt r = aThread->Open(); |
|
1707 |
NKern::ThreadEnterCS(); |
|
1708 |
NKern::UnlockSystem(); |
|
1709 |
if(r==KErrNone) |
|
1710 |
{ |
|
1711 |
TInt h; |
|
1712 |
r = aThread->MakeHandleAndOpen(EOwnerThread,pL,h); |
|
1713 |
aThread->Close(NULL); |
|
1714 |
} |
|
1715 |
pL->Close(NULL); |
|
1716 |
NKern::ThreadLeaveCS(); |
|
1717 |
return r; |
|
1718 |
} |
|
1719 |
||
1720 |
/** |
|
1721 |
Sets the realtime state for the current thread. |
|
1722 |
||
1723 |
@param aNewState The new realtime state for the thread. |
|
1724 |
||
1725 |
@pre No fast mutex can be held. |
|
1726 |
@pre Call in a thread context. |
|
1727 |
@pre Kernel must be unlocked |
|
1728 |
@pre Interrupts enabled |
|
1729 |
@pre Can be used in a device driver. |
|
1730 |
*/ |
|
1731 |
EXPORT_C void Kern::SetRealtimeState(TThreadRealtimeState aNewState) |
|
1732 |
{ |
|
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1733 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::SetRealtimeState"); |
0 | 1734 |
TheCurrentThread->SetRealtimeState(aNewState); |
1735 |
} |
|
1736 |
||
1737 |
void DThread::SetRealtimeState(TThreadRealtimeState aState) |
|
1738 |
{ |
|
1739 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Thread %O SetRealtimeState %d", this, aState)); |
|
1740 |
const TUint32 clear = KThreadFlagRealtime | KThreadFlagRealtimeTest; |
|
1741 |
TUint32 set = 0; |
|
1742 |
switch (aState) |
|
1743 |
{ |
|
1744 |
case ERealtimeStateOff: set = 0; break; |
|
1745 |
case ERealtimeStateOn: set = KThreadFlagRealtime; break; |
|
1746 |
case ERealtimeStateWarn: set = KThreadFlagRealtime|KThreadFlagRealtimeTest; break; |
|
1747 |
||
1748 |
default: |
|
1749 |
K::PanicCurrentThread(EInvalidRealtimeState); |
|
1750 |
} |
|
1751 |
NKern::LockSystem(); |
|
1752 |
iFlags=(iFlags & ~clear) | set; |
|
1753 |
NKern::UnlockSystem(); |
|
1754 |
} |
|
1755 |
||
1756 |
TBool DThread::IsRealtime() |
|
1757 |
{ |
|
1758 |
if(iFlags&KThreadFlagRealtime) |
|
1759 |
return ETrue; |
|
1760 |
return EFalse; |
|
1761 |
} |
|
1762 |
||
1763 |
void K::CheckThreadNotRealtime(const char* aTraceMessage) |
|
1764 |
{ |
|
1765 |
if(TheCurrentThread->IsRealtime()) |
|
1766 |
IllegalFunctionForRealtimeThread(NULL,aTraceMessage); |
|
1767 |
} |
|
1768 |
||
1769 |
/** |
|
1770 |
Panic the specified thread with EIllegalFunctionForRealtimeThread. |
|
1771 |
If the thread is the current one, and it is in a critical section, then the panic |
|
1772 |
will be deferred until the critical section has been left. |
|
1773 |
@return True if thread was/will be panicked. False if function should be made 'legal' again. |
|
1774 |
@pre No fast mutexes held |
|
1775 |
*/ |
|
1776 |
TBool K::IllegalFunctionForRealtimeThread(DThread* aThread,const char* aTraceMessage) |
|
1777 |
{ |
|
1778 |
DThread* pC=TheCurrentThread; |
|
1779 |
if(!aThread) |
|
1780 |
aThread = pC; |
|
1781 |
||
1782 |
// If current thread has RealtimeTest just emit a warning trace... |
|
1783 |
if(pC->iFlags&KThreadFlagRealtimeTest) |
|
1784 |
{ |
|
1785 |
__KTRACE_OPT(KREALTIME,Kern::Printf("*Realtime* WARNING - Illegal function for Thread %O - %s",aThread,aTraceMessage)); |
|
1786 |
#ifndef _DEBUG |
|
1787 |
(void)aTraceMessage; // stop unreferenced formal parameter warning |
|
1788 |
#endif |
|
1789 |
return EFalse; |
|
1790 |
} |
|
1791 |
||
1792 |
// Kill the thread... |
|
1793 |
__KTRACE_OPT2(KREALTIME,KPANIC,Kern::Printf("*Realtime* ERROR - Illegal function for Thread %O - %s",aThread,aTraceMessage)); |
|
1794 |
NKern::LockSystem(); |
|
1795 |
if(aThread!=pC || aThread->iNThread.iCsCount==0) |
|
1796 |
{ |
|
1797 |
aThread->Die(EExitPanic,EIllegalFunctionForRealtimeThread,KLitKernExec()); |
|
1798 |
} |
|
1799 |
else |
|
1800 |
{ |
|
1801 |
// Want to kill current thread but it is in a critical section, so poke the thread |
|
1802 |
// with state so it panics when it leaves the critical section |
|
1803 |
aThread->SetExitInfo(EExitPanic,EIllegalFunctionForRealtimeThread,KLitKernExec()); |
|
1804 |
NKern::DeferredExit(); |
|
1805 |
NKern::UnlockSystem(); |
|
1806 |
} |
|
1807 |
return ETrue; |
|
1808 |
} |
|
1809 |
||
1810 |
void DThread::BTracePrime(TInt aCategory) |
|
1811 |
{ |
|
1812 |
#ifdef BTRACE_THREAD_IDENTIFICATION |
|
1813 |
if(aCategory==BTrace::EThreadIdentification || aCategory==-1) |
|
1814 |
{ |
|
1815 |
DProcess* process = iOwningProcess; |
|
1816 |
TKName nameBuf; |
|
1817 |
process->Name(nameBuf); |
|
1818 |
BTraceN(BTrace::EThreadIdentification,BTrace::EProcessName,&iNThread,process,nameBuf.Ptr(),nameBuf.Size()); |
|
1819 |
Name(nameBuf); |
|
1820 |
BTraceN(BTrace::EThreadIdentification,BTrace::EThreadName,&iNThread,process,nameBuf.Ptr(),nameBuf.Size()); |
|
1821 |
BTrace12(BTrace::EThreadIdentification,BTrace::EThreadId,&iNThread,process,iId); |
|
1822 |
} |
|
1823 |
#endif |
|
1824 |
} |
|
1825 |
||
1826 |
/****************************************************************************** |
|
1827 |
* Miscellaneous notifications |
|
1828 |
******************************************************************************/ |
|
1829 |
TMiscNotifierMgr::TMiscNotifierMgr() |
|
1830 |
: iCompletionDfc(&CompletionDfcFn, this, 1), |
|
1831 |
iIdleNotifierQ(0), |
|
1832 |
iIdleDfc(&IdleDfcFn, this, 1) |
|
1833 |
{ |
|
1834 |
} |
|
1835 |
||
1836 |
void TMiscNotifierMgr::Init2() |
|
1837 |
{ |
|
1838 |
iCompletionDfc.SetDfcQ(K::SvMsgQ); |
|
1839 |
iIdleDfc.SetDfcQ(K::SvMsgQ); |
|
1840 |
iIdleNotifierQ = new SMiscNotifierQ; |
|
1841 |
__NK_ASSERT_ALWAYS(iIdleNotifierQ); |
|
1842 |
} |
|
1843 |
||
1844 |
// runs in supervisor thread when completions are required |
|
1845 |
void TMiscNotifierMgr::CompletionDfcFn(TAny* aMgr) |
|
1846 |
{ |
|
1847 |
__KTRACE_OPT(KOBJECT,Kern::Printf("TMiscNotifierMgr::CompletionDfcFn")); |
|
1848 |
TMiscNotifierMgr& m = *(TMiscNotifierMgr*)aMgr; |
|
1849 |
m.Lock(); |
|
1850 |
while (!m.iCompleted.IsEmpty()) |
|
1851 |
{ |
|
1852 |
TMiscNotifier* n = _LOFF(m.iCompleted.First()->Deque(), TMiscNotifier, iObjLink); |
|
1853 |
n->iThreadLink.Deque(); |
|
1854 |
m.Unlock(); |
|
1855 |
__KTRACE_OPT(KOBJECT,Kern::Printf("Completing %08x thread %O 0->%08x", n, n->iThread, n->iStatus)); |
|
1856 |
DThread* t = n->iThread; |
|
1857 |
Kern::QueueRequestComplete(t, n, KErrNone); |
|
1858 |
t->Close(NULL); |
|
1859 |
n->Close(); |
|
1860 |
m.Lock(); |
|
1861 |
} |
|
1862 |
m.Unlock(); |
|
1863 |
} |
|
1864 |
||
1865 |
// runs in supervisor thread when idle notification completions are required |
|
1866 |
void TMiscNotifierMgr::IdleDfcFn(TAny* aMgr) |
|
1867 |
{ |
|
1868 |
TMiscNotifierMgr& m = *(TMiscNotifierMgr*)aMgr; |
|
1869 |
m.CompleteNotifications(*m.iIdleNotifierQ); |
|
1870 |
} |
|
1871 |
||
1872 |
void TMiscNotifierMgr::CompleteNotifications(SDblQue& aQ) |
|
1873 |
{ |
|
1874 |
__KTRACE_OPT(KOBJECT,Kern::Printf("TMiscNotifierMgr::CompleteNotifications(%08x)",&aQ)); |
|
1875 |
Lock(); |
|
1876 |
TBool queue_dfc = EFalse; |
|
1877 |
if (!aQ.IsEmpty()) |
|
1878 |
{ |
|
1879 |
// remember if completed list was originally empty |
|
1880 |
queue_dfc = iCompleted.IsEmpty(); |
|
1881 |
||
1882 |
// move items on provided list onto completed list |
|
1883 |
iCompleted.MoveFrom(&aQ); |
|
1884 |
} |
|
1885 |
Unlock(); |
|
1886 |
if (queue_dfc) |
|
1887 |
iCompletionDfc.Enque(); |
|
1888 |
} |
|
1889 |
||
1890 |
||
1891 |
// Create and install a miscellanous notifier |
|
1892 |
// Return 1 if the list was originally empty |
|
1893 |
// Return 0 if success but list not originally empty |
|
1894 |
// Return <0 on error |
|
1895 |
// If aN is non-null on entry and an TMiscNotifier is required, *aN is used instead |
|
1896 |
// of allocating memory. aN is then set to NULL. Similarly for aQ. |
|
1897 |
// If aObj is TRUE, aPtr points to a DObject to which the notifier should be attached |
|
1898 |
// If aObj is FALSE, aPtr points to a SMiscNotifierQ pointer |
|
1899 |
TInt TMiscNotifierMgr::NewMiscNotifier(TRequestStatus* aStatus, TBool aObj, TAny* aPtr, TMiscNotifier*& aN, SMiscNotifierQ*& aQ) |
|
1900 |
{ |
|
1901 |
TInt result = 0; |
|
1902 |
DThread& t = *TheCurrentThread; |
|
1903 |
DObject* pO = aObj ? (DObject*)aPtr : 0; |
|
1904 |
SMiscNotifierQ** pQ = aObj ? 0 : (SMiscNotifierQ**)aPtr; |
|
1905 |
TMiscNotifier* n = aN; |
|
1906 |
if (!n) |
|
1907 |
n = new TMiscNotifier; |
|
1908 |
if (!n) |
|
1909 |
return KErrNoMemory; |
|
1910 |
__KTRACE_OPT(KEXEC,Kern::Printf("NewMiscNotifier at %08x, status=%08x, pO=%O, pQ=%08x",n,aStatus,pO,pQ)); |
|
1911 |
TInt r = n->SetStatus(aStatus); |
|
1912 |
__NK_ASSERT_DEBUG(r == KErrNone); // can't fail |
|
1913 |
(void)r; |
|
1914 |
n->iThread = &t; |
|
1915 |
SMiscNotifierQ* newq = 0; |
|
1916 |
SMiscNotifierQ* q = 0; |
|
1917 |
TBool newq_reqd = pO ? (!pO->HasNotifierQ()) : (!*pQ); |
|
1918 |
FOREVER |
|
1919 |
{ |
|
1920 |
if (!newq && newq_reqd) |
|
1921 |
{ |
|
1922 |
newq = aQ; |
|
1923 |
if (!newq) |
|
1924 |
newq = new SMiscNotifierQ; |
|
1925 |
if (!newq) |
|
1926 |
{ |
|
1927 |
if (n != aN) |
|
1928 |
n->Close(); |
|
1929 |
return KErrNoMemory; |
|
1930 |
} |
|
1931 |
} |
|
1932 |
Lock(); |
|
1933 |
q = pO ? (pO->NotifierQ()) : (*pQ); |
|
1934 |
if (!q) |
|
1935 |
{ |
|
1936 |
if (!newq) |
|
1937 |
{ |
|
1938 |
Unlock(); |
|
1939 |
newq_reqd = ETrue; |
|
1940 |
continue; |
|
1941 |
} |
|
1942 |
q = newq; |
|
1943 |
if (q == aQ) |
|
1944 |
aQ = 0; |
|
1945 |
newq = 0; |
|
1946 |
if (pO) |
|
1947 |
pO->SetNotifierQ(q); |
|
1948 |
else |
|
1949 |
*pQ = q; |
|
1950 |
} |
|
1951 |
if (q->IsEmpty()) |
|
1952 |
result = 1; |
|
1953 |
q->Add(&n->iObjLink); |
|
1954 |
t.iMiscNotifiers.Add(&n->iThreadLink); |
|
1955 |
Unlock(); |
|
1956 |
break; |
|
1957 |
} |
|
1958 |
if (newq && newq!=aQ) |
|
1959 |
Kern::Free(newq); |
|
1960 |
if (n == aN) |
|
1961 |
aN = 0; |
|
1962 |
t.Open(); |
|
1963 |
return result; |
|
1964 |
} |
|
1965 |
||
1966 |
||
1967 |
void ExecHandler::NotifyOnIdle(TRequestStatus* aStatus) |
|
1968 |
{ |
|
1969 |
__KTRACE_OPT(KEXEC,Kern::Printf("ExecHandler::NotifyOnIdle %08x",aStatus)); |
|
1970 |
TMiscNotifierMgr& m = K::TheMiscNotifierMgr; |
|
1971 |
NKern::ThreadEnterCS(); |
|
1972 |
// FIXME: TIME ORDERING PROBLEM IF IDLE DFC IS CURRENTLY PENDING |
|
1973 |
TMiscNotifier* n = 0; |
|
1974 |
SMiscNotifierQ* q = 0; |
|
1975 |
TInt r = m.NewMiscNotifier(aStatus, FALSE, &K::TheMiscNotifierMgr.iIdleNotifierQ, n, q); |
|
1976 |
if (r<0) |
|
1977 |
Kern::RequestComplete(aStatus, r); |
|
1978 |
else if (r>0) |
|
1979 |
m.iIdleDfc.QueueOnIdle(); |
|
1980 |
NKern::ThreadLeaveCS(); |
|
1981 |
} |
|
1982 |
||
1983 |
||
1984 |
// Find all this thread's miscellaneous notifiers with request status equal to aStatus |
|
1985 |
// Dequeue them from both the thread and the object they monitor and place them in |
|
1986 |
// an output doubly linked list. |
|
1987 |
// If aStatus==NULL, extract all this thread's miscellaneous notifiers |
|
1988 |
void DThread::ExtractMiscNotifiers(SDblQue& aQ, TRequestStatus* aStatus) |
|
1989 |
{ |
|
1990 |
__KTRACE_OPT(KTHREAD,Kern::Printf("%T ExtractMiscNotifiers(%08x)", this, aStatus)); |
|
1991 |
TMiscNotifierMgr& m = K::TheMiscNotifierMgr; |
|
1992 |
SDblQueLink* anchor = &iMiscNotifiers.iA; |
|
1993 |
m.Lock(); |
|
1994 |
SDblQueLink* p = anchor->iNext; |
|
1995 |
for (; p!=anchor; p=p->iNext) |
|
1996 |
{ |
|
1997 |
TMiscNotifier* n = _LOFF(p, TMiscNotifier, iThreadLink); |
|
1998 |
if (aStatus && n->StatusPtr() != aStatus) |
|
1999 |
continue; |
|
2000 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Found %08x", n)); |
|
2001 |
p = p->iNext; |
|
2002 |
n->iThreadLink.Deque(); |
|
2003 |
n->iObjLink.Deque(); |
|
2004 |
||
2005 |
// no-one else can see this notifier now |
|
2006 |
aQ.Add(&n->iThreadLink); |
|
2007 |
} |
|
2008 |
m.Unlock(); |
|
2009 |
} |
|
2010 |
||
2011 |
||
2012 |
||
2013 |
void ExecHandler::CancelMiscNotifier(TRequestStatus* aStatus) |
|
2014 |
{ |
|
2015 |
__KTRACE_OPT(KEXEC,Kern::Printf("ExecHandler::CancelMiscNotifier %08x",aStatus)); |
|
2016 |
DThread& t = *TheCurrentThread; |
|
2017 |
SDblQue garbage; |
|
2018 |
NKern::ThreadEnterCS(); |
|
2019 |
t.ExtractMiscNotifiers(garbage, aStatus); |
|
2020 |
while (!garbage.IsEmpty()) |
|
2021 |
{ |
|
2022 |
TMiscNotifier* n = _LOFF(garbage.First()->Deque(), TMiscNotifier, iThreadLink); |
|
2023 |
__KTRACE_OPT(KOBJECT,Kern::Printf("Completing %08x thread %O -3->%08x", n, n->iThread, n->iStatus)); |
|
2024 |
TRequestStatus* s = n->StatusPtr(); |
|
2025 |
n->Close(); // delete before complete in case someone's testing for kernel heap leaks |
|
2026 |
Kern::RequestComplete(s, KErrCancel); |
|
2027 |
t.Close(NULL); |
|
2028 |
} |
|
2029 |
NKern::ThreadLeaveCS(); |
|
2030 |
} |
|
2031 |
||
2032 |
||
2033 |
void ExecHandler::NotifyObjectDestruction(TInt aHandle, TRequestStatus* aStatus) |
|
2034 |
{ |
|
2035 |
TMiscNotifierMgr& m = K::TheMiscNotifierMgr; |
|
2036 |
DObject* pO=NULL; |
|
2037 |
TInt r=K::OpenObjectFromHandle(aHandle,pO); |
|
2038 |
if (r!=KErrNone) |
|
2039 |
K::PanicKernExec(EBadHandle); |
|
2040 |
__KTRACE_OPT(KEXEC,Kern::Printf("ExecHandler::NotifyObjectDestruction %O %08x",pO,aStatus)); |
|
2041 |
TMiscNotifier* n = 0; |
|
2042 |
SMiscNotifierQ* q = 0; |
|
2043 |
r = m.NewMiscNotifier(aStatus, TRUE, pO, n, q); |
|
2044 |
pO->Close(NULL); |
|
2045 |
if (r<0) |
|
2046 |
Kern::RequestComplete(aStatus, r); |
|
2047 |
NKern::ThreadLeaveCS(); |
|
2048 |
} |
|
2049 |
||
2050 |
void DThread::KillMiscNotifiers() |
|
2051 |
{ |
|
2052 |
__KTRACE_OPT(KTHREAD,Kern::Printf("%T KillMiscNotifiers", this)); |
|
2053 |
SDblQue garbage; |
|
2054 |
ExtractMiscNotifiers(garbage, 0); |
|
2055 |
while (!garbage.IsEmpty()) |
|
2056 |
{ |
|
2057 |
TMiscNotifier* n = _LOFF(garbage.First()->Deque(), TMiscNotifier, iThreadLink); |
|
2058 |
// don't bother completing since thread has terminated anyway |
|
2059 |
__KTRACE_OPT(KOBJECT,Kern::Printf("Killing %08x", n)); |
|
2060 |
DThread* t = n->iThread; |
|
2061 |
n->Close(); |
|
2062 |
t->Close(NULL); |
|
2063 |
} |
|
2064 |
} |
|
2065 |
||
2066 |
void DThread::Rendezvous(TInt aReason) |
|
2067 |
// |
|
2068 |
// Enter and return with system unlocked and calling thread in critical section. |
|
2069 |
// |
|
2070 |
{ |
|
2071 |
TLogon::CompleteAll(iTargetLogons, TLogon::ETargetRendezvous, aReason); |
|
2072 |
} |
|
2073 |
||
2074 |
TInt DThread::Logon(TRequestStatus* aStatus, TBool aRendezvous) |
|
2075 |
{ |
|
2076 |
TInt r = KErrNoMemory; |
|
2077 |
DThread* pC = TheCurrentThread; |
|
2078 |
__KTRACE_OPT(KTHREAD, Kern::Printf("Thread %O Logon to thread %O, status at %08x rdv=%x", |
|
2079 |
pC, this, aStatus, aRendezvous)); |
|
2080 |
||
2081 |
TLogon* pL = new TLogon; |
|
2082 |
if (pL) |
|
2083 |
{ |
|
2084 |
TUint32 type = TLogon::ETargetThread; |
|
2085 |
if (aRendezvous) |
|
2086 |
type |= TLogon::ERendezvous; |
|
2087 |
r = pL->Attach(iTargetLogons, pC, this, aStatus, type); |
|
2088 |
if (r != KErrNone) |
|
2089 |
pL->Close(); |
|
2090 |
} |
|
2091 |
||
2092 |
__KTRACE_OPT(KTHREAD, Kern::Printf("DThread::Logon ret %d", r)); |
|
2093 |
return r; |
|
2094 |
} |
|
2095 |
||
2096 |
#ifdef __SMP__ |
|
2097 |
void DThread::SMPSafeCallback(TAny* aThisPtr, TUserModeCallbackReason aReasonCode) |
|
2098 |
{ |
|
2099 |
if (aReasonCode == EUserModeCallbackRun) |
|
2100 |
{ |
|
2101 |
DThread* t = _LOFF(aThisPtr,DThread,iSMPSafeCallback); |
|
2102 |
TUint32 config = TheSuperPage().KernelConfigFlags(); |
|
2103 |
if (config & EKernelConfigSMPUnsafeCPU0) |
|
2104 |
{ |
|
2105 |
if (t->iOwningProcess->iSMPUnsafeCount) |
|
2106 |
NKern::ThreadSetCpuAffinity(&t->iNThread, 0); |
|
2107 |
else |
|
2108 |
NKern::ThreadSetCpuAffinity(&t->iNThread, KCpuAffinityAny); |
|
2109 |
} |
|
2110 |
else if (config & EKernelConfigSMPUnsafeCompat) |
|
2111 |
{ |
|
2112 |
if (t->iOwningProcess->iSMPUnsafeCount) |
|
2113 |
NKern::JoinGroup(t->iOwningProcess->iSMPUnsafeGroup); |
|
2114 |
else |
|
2115 |
NKern::LeaveGroup(); |
|
2116 |
} |
|
2117 |
} |
|
2118 |
} |
|
2119 |
#endif |
|
2120 |
||
2121 |
/******************************************** |
|
2122 |
* TLogon |
|
2123 |
********************************************/ |
|
2124 |
||
2125 |
// |
|
2126 |
// Pseudo-constructor for TLogon class. Fills in all the private members, and |
|
2127 |
// attaches the TLogon object to both the owner's and target's linked lists. |
|
2128 |
// Enter and return with no fast mutexes held and current thread in CS |
|
2129 |
// |
|
2130 |
TInt TLogon::Attach(SDblQue& aList, DThread* aOwner, DObject* aTarget, |
|
2131 |
TRequestStatus* aStatus, TUint32 aType) |
|
2132 |
{ |
|
2133 |
TInt r = KErrNone; |
|
2134 |
Lock(); |
|
2135 |
if (aType & ETargetProcess) |
|
2136 |
{ |
|
2137 |
DProcess* pP = (DProcess*)aTarget; |
|
2138 |
if (pP->iAttributes & DProcess::EBeingLoaded) |
|
2139 |
r = KErrAccessDenied; |
|
2140 |
else if (pP->iExitType != EExitPending) |
|
2141 |
r = KErrDied; |
|
2142 |
} |
|
2143 |
else |
|
2144 |
{ |
|
2145 |
DThread* pT = (DThread*)aTarget; |
|
2146 |
if (pT->iMState == DThread::ECreated) |
|
2147 |
r = KErrAccessDenied; |
|
2148 |
else if (pT->iExiting) |
|
2149 |
r = KErrDied; |
|
2150 |
} |
|
2151 |
if (r == KErrNone) |
|
2152 |
{ |
|
2153 |
iType = aType; |
|
2154 |
r = SetStatus(aStatus); |
|
2155 |
__NK_ASSERT_DEBUG(r == KErrNone); // can't fail |
|
2156 |
iOwningThread = aOwner; |
|
2157 |
iTarget = aTarget; |
|
2158 |
aOwner->Open(); |
|
2159 |
aTarget->Open(); |
|
2160 |
||
2161 |
// Rendezvous entries are always kept ahead of non-rendezvous ones |
|
2162 |
if (aType & ERendezvous) |
|
2163 |
aList.AddHead(&iTargetLink); |
|
2164 |
else |
|
2165 |
aList.Add(&iTargetLink); |
|
2166 |
aOwner->iOwnedLogons.Add(&iOwningThreadLink); |
|
2167 |
} |
|
2168 |
Unlock(); |
|
2169 |
return r; |
|
2170 |
} |
|
2171 |
||
2172 |
// |
|
2173 |
// Cancel a specific logon on the owning thread list aList |
|
2174 |
// The logon to be cancelled is identified by aTarget, aStatus, and aType |
|
2175 |
// Enter and return with no fast mutexes held and current thread in CS |
|
2176 |
// |
|
2177 |
TInt TLogon::Cancel(SDblQue& aList, DObject* aTarget, TRequestStatus* aStatus, TUint32 aType) |
|
2178 |
{ |
|
2179 |
Lock(); |
|
2180 |
SDblQueLink* anchor = &aList.iA; |
|
2181 |
SDblQueLink* pLink = aList.First(); |
|
2182 |
||
2183 |
for ( ; pLink != anchor; pLink = pLink->iNext) |
|
2184 |
{ |
|
2185 |
TLogon* pL = _LOFF(pLink, TLogon, iOwningThreadLink); |
|
2186 |
if (pL->iType == aType && pL->iTarget == aTarget && pL->StatusPtr() == aStatus) |
|
2187 |
{ |
|
2188 |
// found it |
|
2189 |
pL->iOwningThreadLink.Deque(); |
|
2190 |
pL->iTargetLink.Deque(); |
|
2191 |
Unlock(); |
|
2192 |
||
2193 |
// To avoid a race condition after dropping the lock, we must complete this |
|
2194 |
// request BEFORE closing the referenced target object and owning thread ... |
|
2195 |
Kern::QueueRequestComplete(pL->iOwningThread, pL, KErrNone); |
|
2196 |
pL->iTarget->Close(NULL); |
|
2197 |
pL->iOwningThread->Close(NULL); |
|
2198 |
pL->Close(); |
|
2199 |
return KErrNone; |
|
2200 |
} |
|
2201 |
} |
|
2202 |
||
2203 |
// not found |
|
2204 |
Unlock(); |
|
2205 |
return KErrGeneral; |
|
2206 |
} |
|
2207 |
||
2208 |
// |
|
2209 |
// Complete either all logons or only the rendezvous entries on the list aList with reason aReason |
|
2210 |
// Enter and return with no fast mutexes held and current thread in CS |
|
2211 |
// |
|
2212 |
void TLogon::CompleteAll(SDblQue& aList, TComplete aAction, TInt aReason) |
|
2213 |
{ |
|
2214 |
TInt offset = (aAction == EOwned) ? _FOFF(TLogon, iOwningThreadLink) : _FOFF(TLogon, iTargetLink); |
|
2215 |
||
2216 |
FOREVER |
|
2217 |
{ |
|
2218 |
Lock(); |
|
2219 |
if (aList.IsEmpty()) |
|
2220 |
break; |
|
2221 |
||
2222 |
SDblQueLink* pLink = aList.First(); |
|
2223 |
TLogon* pL = (TLogon*)((TUint8*)pLink-offset); |
|
2224 |
||
2225 |
// Rendezvous entries are always ahead of non-rendezvous ones, so stop |
|
2226 |
// if we're only interested in rendezvous and we find a non-rendezvous |
|
2227 |
if (aAction == ETargetRendezvous && !pL->IsRendezvous()) |
|
2228 |
break; |
|
2229 |
||
2230 |
pL->iOwningThreadLink.Deque(); |
|
2231 |
pL->iTargetLink.Deque(); |
|
2232 |
Unlock(); |
|
2233 |
__KTRACE_OPT(KTHREAD, Kern::Printf("Complete Logon to %O type %d", |
|
2234 |
pL->iOwningThread, pL->iType)); |
|
2235 |
||
2236 |
// To avoid a race condition after dropping the lock, we must complete this |
|
2237 |
// request BEFORE closing the referenced target object and owning thread ... |
|
2238 |
Kern::QueueRequestComplete(pL->iOwningThread, pL, aReason); |
|
2239 |
pL->iTarget->Close(NULL); |
|
2240 |
pL->iOwningThread->Close(NULL); |
|
2241 |
pL->Close(); |
|
2242 |
} |
|
2243 |
||
2244 |
Unlock(); |
|
2245 |
} |