author | William Roberts <williamr@symbian.org> |
Thu, 29 Jul 2010 23:52:13 +0100 | |
branch | GCC_SURGE |
changeset 237 | 2604c9de91e0 |
parent 201 | 43365a9b78a3 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 2005-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\nkernsmp\nkern.cpp |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
// NThreadBase member data |
|
19 |
#define __INCLUDE_NTHREADBASE_DEFINES__ |
|
20 |
||
21 |
#include "nk_priv.h" |
|
22 |
||
23 |
/****************************************************************************** |
|
24 |
* Fast mutex |
|
25 |
******************************************************************************/ |
|
26 |
||
27 |
/** Acquires the fast mutex. |
|
28 |
||
29 |
This will block until the mutex is available, and causes |
|
30 |
the thread to enter an implicit critical section until the mutex is released. |
|
31 |
||
32 |
Generally threads would use NKern::FMWait() which manipulates the kernel lock |
|
33 |
for you. |
|
34 |
||
35 |
@pre Kernel must be locked, with lock count 1. |
|
36 |
@pre The calling thread holds no fast mutexes. |
|
37 |
||
38 |
@post Kernel is locked, with lock count 1. |
|
39 |
@post The calling thread holds the mutex. |
|
40 |
||
41 |
@see NFastMutex::Signal() |
|
42 |
@see NKern::FMWait() |
|
43 |
*/ |
|
44 |
EXPORT_C void NFastMutex::Wait() |
|
45 |
{ |
|
46 |
NThreadBase* pC = NCurrentThreadL(); |
|
47 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED_ONCE|MASK_NO_FAST_MUTEX,"NFastMutex::Wait"); |
|
48 |
||
49 |
pC->iHeldFastMutex = this; // to handle kill/suspend between here and setting iHeldFastMutex |
|
50 |
DoWaitL(); |
|
51 |
} |
|
52 |
||
53 |
void NFastMutex::DoWaitL() |
|
54 |
{ |
|
55 |
NThreadBase* pC = NCurrentThreadL(); |
|
56 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T FMWait %M",pC,this)); |
|
57 |
TBool waited = FALSE; // set if we actually had to wait |
|
58 |
iMutexLock.LockOnly(); // acquire mutex spin lock |
|
59 |
__e32_atomic_ior_rlx_ptr(&iHoldingThread, 1); // set contention flag to make sure any other thread must acquire the mutex spin lock |
|
60 |
pC->AcqSLock(); |
|
61 |
FOREVER |
|
62 |
{ |
|
63 |
if (pC->iFastMutexDefer == 1) |
|
64 |
--pC->iParent->iFreezeCpu; |
|
65 |
pC->iFastMutexDefer = 0; |
|
66 |
NThreadBase* pH = (NThreadBase*)(TLinAddr(iHoldingThread) &~ 1); |
|
67 |
if (!pH) |
|
68 |
{ |
|
69 |
// mutex is free |
|
70 |
TInt wp = iWaitQ.HighestPriority(); // -1 if no other thread wants the mutex |
|
71 |
||
72 |
// don't grab mutex if we have been suspended/killed/migrated by the previous holding thread |
|
73 |
if (!pC->iSuspended && pC->iCsFunction!=NThreadBase::ECSDivertPending && (!pC->iParent->iCpuChange || pC->iParent->iFreezeCpu)) |
|
74 |
{ |
|
75 |
TInt p = pC->iPriority; |
|
76 |
if (p>wp || (p==wp && waited)) |
|
77 |
{ |
|
78 |
// if we are highest priority waiting thread or equal and we have waited then grab the mutex |
|
79 |
// don't just grab it if we are equal priority and someone else was already waiting |
|
80 |
// set contention flag if other threads waiting or if current thread has a round robin outstanding |
|
81 |
pC->iMutexPri = (TUint8)(wp>=0 ? wp : 0); // pC's actual priority doesn't change since p>=wp |
|
82 |
iHoldingThread = (wp>=0 || TUint32(pC->iTime)==0x80000000u) ? (NThreadBase*)(TLinAddr(pC)|1) : pC; |
|
83 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T got mutex %M CF=%d WP=%d",TLinAddr(iHoldingThread)&~1,this,TLinAddr(iHoldingThread)&1,wp)); |
|
84 |
pC->RelSLock(); |
|
85 |
iMutexLock.UnlockOnly(); |
|
86 |
#ifdef BTRACE_FAST_MUTEX |
|
87 |
BTraceContext4(BTrace::EFastMutex, BTrace::EFastMutexWait, this); |
|
88 |
#endif |
|
89 |
return; |
|
90 |
} |
|
91 |
} |
|
92 |
} |
|
93 |
pC->iFastMutexDefer = 2; // signal to scheduler to allow ctxsw without incrementing iParent->iFreezeCpu |
|
94 |
if (!pC->iSuspended && pC->iCsFunction!=NThreadBase::ECSDivertPending && (!pC->iParent->iCpuChange || pC->iParent->iFreezeCpu)) |
|
95 |
{ |
|
96 |
// this forces priority changes to wait for the mutex lock |
|
97 |
pC->iLinkedObjType = NThreadBase::EWaitFastMutex; |
|
98 |
pC->iLinkedObj = this; |
|
99 |
pC->iWaitState.SetUpWait(NThreadBase::EWaitFastMutex, NThreadWaitState::EWtStObstructed, this); |
|
100 |
pC->iWaitLink.iPriority = pC->iPriority; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
101 |
if (waited) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
102 |
iWaitQ.AddHead(&pC->iWaitLink); // we were next at this priority |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
103 |
else |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
104 |
iWaitQ.Add(&pC->iWaitLink); |
0 | 105 |
pC->RelSLock(); |
106 |
if (pH) |
|
107 |
pH->SetMutexPriority(this); |
|
108 |
do_pause: |
|
109 |
iMutexLock.UnlockOnly(); |
|
110 |
RescheduleNeeded(); |
|
111 |
#ifdef BTRACE_FAST_MUTEX |
|
112 |
BTraceContext4(BTrace::EFastMutex, BTrace::EFastMutexBlock, this); |
|
113 |
#endif |
|
114 |
NKern::PreemptionPoint(); // we block here until the mutex is released and we are 'nominated' for it or we are suspended/killed |
|
115 |
iMutexLock.LockOnly(); |
|
116 |
pC->AcqSLock(); |
|
117 |
if (pC->iPauseCount || pC->iSuspended || pC->iCsFunction==NThreadBase::ECSDivertPending || (pC->iParent->iCpuChange && !pC->iParent->iFreezeCpu)) |
|
118 |
{ |
|
119 |
pC->RelSLock(); |
|
120 |
goto do_pause; // let pause/suspend/kill take effect |
|
121 |
} |
|
122 |
// if thread was suspended it will have been removed from the wait queue |
|
123 |
if (!pC->iLinkedObj) |
|
124 |
goto thread_suspended; |
|
125 |
iWaitQ.Remove(&pC->iWaitLink); // take ourselves off the wait/contend queue while we try to grab the mutex |
|
126 |
pC->iWaitLink.iNext = 0; |
|
127 |
pC->iLinkedObj = 0; |
|
128 |
pC->iLinkedObjType = NThreadBase::EWaitNone; |
|
129 |
waited = TRUE; |
|
130 |
// if we are suspended or killed, we loop round again and do the 'else' clause next time |
|
131 |
} |
|
132 |
else |
|
133 |
{ |
|
134 |
pC->RelSLock(); |
|
135 |
if (pC->iSuspended || pC->iCsFunction==NThreadBase::ECSDivertPending) |
|
136 |
{ |
|
137 |
// wake up next thread to take this one's place |
|
138 |
if (!pH && !iWaitQ.IsEmpty()) |
|
139 |
{ |
|
140 |
NThreadBase* pT = _LOFF(iWaitQ.First(), NThreadBase, iWaitLink); |
|
141 |
pT->AcqSLock(); |
|
142 |
// if thread is still blocked on this fast mutex, release it but leave it on the wait queue |
|
143 |
// NOTE: it can't be suspended |
|
144 |
pT->iWaitState.UnBlockT(NThreadBase::EWaitFastMutex, this, KErrNone); |
|
145 |
pT->RelSLock(); |
|
146 |
} |
|
147 |
} |
|
148 |
iMutexLock.UnlockOnly(); |
|
149 |
NKern::PreemptionPoint(); // thread suspends/dies/migrates here |
|
150 |
iMutexLock.LockOnly(); |
|
151 |
pC->AcqSLock(); |
|
152 |
thread_suspended: |
|
153 |
waited = FALSE; |
|
154 |
// set contention flag to make sure any other thread must acquire the mutex spin lock |
|
155 |
// need to do it again since mutex may have been released while thread was suspended |
|
156 |
__e32_atomic_ior_rlx_ptr(&iHoldingThread, 1); |
|
157 |
} |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
||
162 |
#ifndef __FAST_MUTEX_MACHINE_CODED__ |
|
163 |
/** Releases a previously acquired fast mutex. |
|
164 |
||
165 |
Generally, threads would use NKern::FMSignal() which manipulates the kernel lock |
|
166 |
for you. |
|
167 |
||
168 |
@pre The calling thread holds the mutex. |
|
169 |
@pre Kernel must be locked. |
|
170 |
||
171 |
@post Kernel is locked. |
|
172 |
||
173 |
@see NFastMutex::Wait() |
|
174 |
@see NKern::FMSignal() |
|
175 |
*/ |
|
176 |
EXPORT_C void NFastMutex::Signal() |
|
177 |
{ |
|
178 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED,"NFastMutex::Signal"); |
|
179 |
#ifdef BTRACE_FAST_MUTEX |
|
180 |
BTraceContext4(BTrace::EFastMutex, BTrace::EFastMutexSignal, this); |
|
181 |
#endif |
|
182 |
NThreadBase* pC = NCurrentThreadL(); |
|
183 |
((volatile TUint32&)pC->iHeldFastMutex) |= 1; // flag to indicate about to release mutex |
|
184 |
||
185 |
if (__e32_atomic_cas_rel_ptr(&iHoldingThread, &pC, 0)) |
|
186 |
{ |
|
187 |
// tricky if suspend/kill here |
|
188 |
// suspend/kill should check flag set above and aMutex->iHoldingThread |
|
189 |
// if bit 0 of iHeldFastMutex set and iHoldingThread==pC then set iHeldFastMutex=0 and proceed |
|
190 |
||
191 |
// no-one else was waiting for the mutex - simple |
|
192 |
pC->iHeldFastMutex = 0; |
|
193 |
return; |
|
194 |
} |
|
195 |
||
196 |
// there was contention so do it the hard way |
|
197 |
DoSignalL(); |
|
198 |
} |
|
199 |
#endif |
|
200 |
||
201 |
void NFastMutex::DoSignalL() |
|
202 |
{ |
|
203 |
NThreadBase* pC = NCurrentThreadL(); |
|
204 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T FMSignal %M",pC,this)); |
|
205 |
__ASSERT_WITH_MESSAGE_DEBUG(HeldByCurrentThread(),"The calling thread holds the mutex","NFastMutex::Signal"); |
|
206 |
||
207 |
iMutexLock.LockOnly(); |
|
208 |
if (!iWaitQ.IsEmpty()) |
|
209 |
{ |
|
210 |
NThreadBase* pT = _LOFF(iWaitQ.First(), NThreadBase, iWaitLink); |
|
211 |
pT->AcqSLock(); |
|
212 |
||
213 |
// if thread is still blocked on this fast mutex, release it but leave it on the wait queue |
|
214 |
// NOTE: it can't be suspended |
|
215 |
pT->iWaitState.UnBlockT(NThreadBase::EWaitFastMutex, this, KErrNone); |
|
216 |
pT->RelSLock(); |
|
217 |
iHoldingThread = (NThreadBase*)1; // mark mutex as released but contended |
|
218 |
} |
|
219 |
else |
|
220 |
iHoldingThread = 0; // mark mutex as released and uncontended |
|
221 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("SiHT=%d",iHoldingThread)); |
|
222 |
pC->AcqSLock(); |
|
223 |
pC->iHeldFastMutex = 0; |
|
224 |
iMutexLock.UnlockOnly(); |
|
225 |
pC->iMutexPri = 0; |
|
226 |
if (pC->iPriority != pC->iBasePri) |
|
227 |
{ |
|
228 |
// lose any inherited priority |
|
229 |
pC->LoseInheritedPriorityT(); |
|
230 |
} |
|
231 |
if (TUint32(pC->iTime)==0x80000000u) |
|
232 |
{ |
|
233 |
pC->iTime = 0; |
|
234 |
RescheduleNeeded(); // handle deferred timeslicing |
|
235 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("DTS %T",pC)); |
|
236 |
} |
|
237 |
if (pC->iFastMutexDefer) |
|
238 |
{ |
|
239 |
pC->iFastMutexDefer = 0; |
|
240 |
--pC->iParent->iFreezeCpu; |
|
241 |
} |
|
242 |
if (pC->iParent->iCpuChange && !pC->iParent->iFreezeCpu) |
|
243 |
RescheduleNeeded(); // need to migrate to another CPU |
|
244 |
if (!pC->iCsCount && pC->iCsFunction) |
|
245 |
pC->DoCsFunctionT(); |
|
246 |
pC->RelSLock(); |
|
247 |
} |
|
248 |
||
249 |
||
250 |
/** Checks if the current thread holds this fast mutex |
|
251 |
||
252 |
@return TRUE if the current thread holds this fast mutex |
|
253 |
@return FALSE if not |
|
254 |
@pre Call in thread context. |
|
255 |
*/ |
|
256 |
EXPORT_C TBool NFastMutex::HeldByCurrentThread() |
|
257 |
{ |
|
258 |
return (TLinAddr(iHoldingThread)&~1) == (TLinAddr)NKern::CurrentThread(); |
|
259 |
} |
|
260 |
||
261 |
||
262 |
/** Returns the fast mutex held by the calling thread, if any. |
|
263 |
||
264 |
@return If the calling thread currently holds a fast mutex, this function |
|
265 |
returns a pointer to it; otherwise it returns NULL. |
|
266 |
@pre Call in thread context. |
|
267 |
*/ |
|
268 |
EXPORT_C NFastMutex* NKern::HeldFastMutex() |
|
269 |
{ |
|
270 |
NThreadBase* t = NKern::CurrentThread(); |
|
271 |
NFastMutex* m = (NFastMutex*)(TLinAddr(t->iHeldFastMutex)&~3); |
|
272 |
return (m && m->HeldByCurrentThread()) ? m : 0; |
|
273 |
} |
|
274 |
||
275 |
||
276 |
#ifndef __FAST_MUTEX_MACHINE_CODED__ |
|
277 |
/** Acquires a fast mutex. |
|
278 |
||
279 |
This will block until the mutex is available, and causes |
|
280 |
the thread to enter an implicit critical section until the mutex is released. |
|
281 |
||
282 |
@param aMutex The fast mutex to acquire. |
|
283 |
||
284 |
@post The calling thread holds the mutex. |
|
285 |
||
286 |
@see NFastMutex::Wait() |
|
287 |
@see NKern::FMSignal() |
|
288 |
||
289 |
@pre No fast mutex can be held. |
|
290 |
@pre Call in a thread context. |
|
291 |
@pre Kernel must be unlocked |
|
292 |
@pre interrupts enabled |
|
293 |
||
294 |
*/ |
|
295 |
EXPORT_C void NKern::FMWait(NFastMutex* aMutex) |
|
296 |
{ |
|
297 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NFMW %M", aMutex)); |
|
298 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"NKern::FMWait"); |
|
299 |
NThreadBase* pC = NKern::CurrentThread(); |
|
300 |
||
301 |
// If the reschedule IPI from an external suspend or kill occurs after this |
|
302 |
// point the initiating CPU must observe the write to iHeldFastMutex before |
|
303 |
// the cas operation. |
|
304 |
pC->iHeldFastMutex = aMutex; // kill/suspend after this point should set mutex contention flag |
|
305 |
NThreadBase* expect = 0; |
|
306 |
if (__e32_atomic_cas_acq_ptr(&aMutex->iHoldingThread, &expect, pC)) |
|
307 |
{ |
|
308 |
// mutex was free and we have just claimed it - simple |
|
309 |
#ifdef BTRACE_FAST_MUTEX |
|
310 |
BTraceContext4(BTrace::EFastMutex, BTrace::EFastMutexWait, aMutex); |
|
311 |
#endif |
|
312 |
return; |
|
313 |
} |
|
314 |
||
315 |
// care required if suspend/kill here |
|
316 |
||
317 |
// there is contention so do it the hard way |
|
318 |
NKern::Lock(); |
|
319 |
aMutex->DoWaitL(); |
|
320 |
NKern::Unlock(); |
|
321 |
} |
|
322 |
||
323 |
||
324 |
/** Releases a previously acquired fast mutex. |
|
325 |
||
326 |
@param aMutex The fast mutex to release. |
|
327 |
||
328 |
@pre The calling thread holds the mutex. |
|
329 |
||
330 |
@see NFastMutex::Signal() |
|
331 |
@see NKern::FMWait() |
|
332 |
*/ |
|
333 |
EXPORT_C void NKern::FMSignal(NFastMutex* aMutex) |
|
334 |
{ |
|
335 |
NThreadBase* pC = NKern::CurrentThread(); |
|
336 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NFMS %M", aMutex)); |
|
337 |
#ifdef BTRACE_FAST_MUTEX |
|
338 |
BTraceContext4(BTrace::EFastMutex, BTrace::EFastMutexSignal, aMutex); |
|
339 |
#endif |
|
340 |
((volatile TUint32&)pC->iHeldFastMutex) |= 1; // flag to indicate about to release mutex |
|
341 |
||
342 |
if (__e32_atomic_cas_rel_ptr(&aMutex->iHoldingThread, &pC, 0)) |
|
343 |
{ |
|
344 |
// no-one else was waiting for the mutex and we have just released it |
|
345 |
||
346 |
// tricky if suspend/kill here |
|
347 |
// suspend/kill should check flag set above and aMutex->iHoldingThread |
|
348 |
// if bit 0 of iHeldFastMutex set and iHoldingThread==pC then set iHeldFastMutex=0 and proceed |
|
349 |
||
350 |
// If the reschedule IPI from an external suspend or kill occurs after this |
|
351 |
// point the initiating CPU must observe the write to iHeldFastMutex after |
|
352 |
// the cas operation. |
|
353 |
pC->iHeldFastMutex = 0; |
|
354 |
return; |
|
355 |
} |
|
356 |
||
357 |
// there was contention so do it the hard way |
|
358 |
NKern::Lock(); |
|
359 |
aMutex->DoSignalL(); |
|
360 |
NKern::Unlock(); |
|
361 |
} |
|
362 |
||
363 |
/** Acquires the System Lock. |
|
364 |
||
365 |
This will block until the mutex is available, and causes |
|
366 |
the thread to enter an implicit critical section until the mutex is released. |
|
367 |
||
368 |
@post System lock is held. |
|
369 |
||
370 |
@see NKern::UnlockSystem() |
|
371 |
@see NKern::FMWait() |
|
372 |
||
373 |
@pre No fast mutex can be held. |
|
374 |
@pre Call in a thread context. |
|
375 |
@pre Kernel must be unlocked |
|
376 |
@pre interrupts enabled |
|
377 |
||
378 |
*/ |
|
379 |
EXPORT_C void NKern::LockSystem() |
|
380 |
{ |
|
381 |
NKern::FMWait(&TheScheduler.iLock); |
|
382 |
} |
|
383 |
||
384 |
||
385 |
/** Releases the System Lock. |
|
386 |
||
387 |
@pre System lock must be held. |
|
388 |
||
389 |
@see NKern::LockSystem() |
|
390 |
@see NKern::FMSignal() |
|
391 |
*/ |
|
392 |
EXPORT_C void NKern::UnlockSystem() |
|
393 |
{ |
|
394 |
NKern::FMSignal(&TheScheduler.iLock); |
|
395 |
} |
|
396 |
||
397 |
||
398 |
/** Temporarily releases a fast mutex if there is contention. |
|
399 |
||
400 |
If there is another thread attempting to acquire the mutex, the calling |
|
401 |
thread releases the mutex and then acquires it again. |
|
402 |
||
403 |
This is more efficient than the equivalent code: |
|
404 |
||
405 |
@code |
|
406 |
NKern::FMSignal(); |
|
407 |
NKern::FMWait(); |
|
408 |
@endcode |
|
409 |
||
410 |
@return TRUE if the mutex was relinquished, FALSE if not. |
|
411 |
||
412 |
@pre The mutex must be held. |
|
413 |
||
414 |
@post The mutex is held. |
|
415 |
*/ |
|
416 |
EXPORT_C TBool NKern::FMFlash(NFastMutex* aM) |
|
417 |
{ |
|
418 |
NThreadBase* pC = NKern::CurrentThread(); |
|
419 |
__ASSERT_WITH_MESSAGE_DEBUG(aM->HeldByCurrentThread(),"The calling thread holds the mutex","NKern::FMFlash"); |
|
420 |
TBool w = (pC->iMutexPri >= pC->iBasePri); // a thread of greater or equal priority is waiting |
|
421 |
if (w) |
|
422 |
{ |
|
423 |
NKern::Lock(); |
|
424 |
aM->Signal(); |
|
425 |
NKern::PreemptionPoint(); |
|
426 |
aM->Wait(); |
|
427 |
NKern::Unlock(); |
|
428 |
} |
|
429 |
#ifdef BTRACE_FAST_MUTEX |
|
430 |
else |
|
431 |
{ |
|
432 |
BTraceContext4(BTrace::EFastMutex, BTrace::EFastMutexFlash, aM); |
|
433 |
} |
|
434 |
#endif |
|
435 |
return w; |
|
436 |
} |
|
437 |
||
438 |
||
439 |
/** Temporarily releases the System Lock if there is contention. |
|
440 |
||
441 |
If there |
|
442 |
is another thread attempting to acquire the System lock, the calling |
|
443 |
thread releases the mutex and then acquires it again. |
|
444 |
||
445 |
This is more efficient than the equivalent code: |
|
446 |
||
447 |
@code |
|
448 |
NKern::UnlockSystem(); |
|
449 |
NKern::LockSystem(); |
|
450 |
@endcode |
|
451 |
||
452 |
Note that this can only allow higher priority threads to use the System |
|
453 |
lock as lower priority cannot cause contention on a fast mutex. |
|
454 |
||
455 |
@return TRUE if the system lock was relinquished, FALSE if not. |
|
456 |
||
457 |
@pre System lock must be held. |
|
458 |
||
459 |
@post System lock is held. |
|
460 |
||
461 |
@see NKern::LockSystem() |
|
462 |
@see NKern::UnlockSystem() |
|
463 |
*/ |
|
464 |
EXPORT_C TBool NKern::FlashSystem() |
|
465 |
{ |
|
466 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED,"NKern::FlashSystem"); |
|
467 |
return NKern::FMFlash(&TheScheduler.iLock); |
|
468 |
} |
|
469 |
#endif |
|
470 |
||
471 |
/****************************************************************************** |
|
472 |
* Fast semaphore |
|
473 |
******************************************************************************/ |
|
474 |
||
475 |
/** Sets the owner of a fast semaphore. |
|
476 |
||
477 |
@param aThread The thread to own this semaphore. If aThread==0, then the |
|
478 |
owner is set to the current thread. |
|
479 |
||
480 |
@pre Kernel must be locked. |
|
481 |
@pre If changing ownership form one thread to another, the there must be no |
|
482 |
pending signals or waits. |
|
483 |
@pre Call either in a thread or an IDFC context. |
|
484 |
||
485 |
@post Kernel is locked. |
|
486 |
*/ |
|
487 |
EXPORT_C void NFastSemaphore::SetOwner(NThreadBase* aThread) |
|
488 |
{ |
|
489 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED|MASK_NOT_ISR,"NFastSemaphore::SetOwner"); |
|
490 |
if (!aThread) |
|
491 |
aThread = NCurrentThreadL(); |
|
492 |
if (iOwningThread && iOwningThread!=aThread) |
|
493 |
{ |
|
494 |
__NK_ASSERT_ALWAYS(!iCount); // Can't change owner if iCount!=0 |
|
495 |
} |
|
496 |
iOwningThread = aThread; |
|
497 |
} |
|
498 |
||
499 |
||
500 |
#ifndef __FAST_SEM_MACHINE_CODED__ |
|
501 |
/** Waits on a fast semaphore. |
|
502 |
||
503 |
Decrements the signal count for the semaphore and |
|
504 |
removes the calling thread from the ready-list if the semaphore becomes |
|
505 |
unsignalled. Only the thread that owns a fast semaphore can wait on it. |
|
506 |
||
507 |
Note that this function does not block, it merely updates the NThread state, |
|
508 |
rescheduling will only occur when the kernel is unlocked. Generally threads |
|
509 |
would use NKern::FSWait() which manipulates the kernel lock for you. |
|
510 |
||
511 |
@pre The calling thread must own the semaphore. |
|
512 |
@pre No fast mutex can be held. |
|
513 |
@pre Kernel must be locked. |
|
514 |
||
515 |
@post Kernel is locked. |
|
516 |
||
517 |
@see NFastSemaphore::Signal() |
|
518 |
@see NKern::FSWait() |
|
519 |
@see NKern::Unlock() |
|
520 |
*/ |
|
521 |
EXPORT_C void NFastSemaphore::Wait() |
|
522 |
{ |
|
523 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED|MASK_NO_FAST_MUTEX,"NFastSemaphore::Wait"); |
|
524 |
NThreadBase* pC = NCurrentThreadL(); |
|
525 |
__ASSERT_WITH_MESSAGE_ALWAYS(pC==iOwningThread,"The calling thread must own the semaphore","NFastSemaphore::Wait"); |
|
526 |
pC->iWaitState.SetUpWait(NThreadBase::EWaitFastSemaphore, 0, this); |
|
527 |
if (Dec(pC)) // full barrier |
|
528 |
pC->iWaitState.CancelWait(); // don't have to wait |
|
529 |
else |
|
530 |
RescheduleNeeded(); // have to wait |
|
531 |
} |
|
532 |
||
533 |
||
534 |
/** Signals a fast semaphore. |
|
535 |
||
536 |
Increments the signal count of a fast semaphore by |
|
537 |
one and releases any waiting thread if the semphore becomes signalled. |
|
538 |
||
539 |
Note that a reschedule will not occur before this function returns, this will |
|
540 |
only take place when the kernel is unlocked. Generally threads |
|
541 |
would use NKern::FSSignal() which manipulates the kernel lock for you. |
|
542 |
||
543 |
@pre Kernel must be locked. |
|
544 |
@pre Call either in a thread or an IDFC context. |
|
545 |
||
546 |
@post Kernel is locked. |
|
547 |
||
548 |
@see NFastSemaphore::Wait() |
|
549 |
@see NKern::FSSignal() |
|
550 |
@see NKern::Unlock() |
|
551 |
*/ |
|
552 |
EXPORT_C void NFastSemaphore::Signal() |
|
553 |
{ |
|
554 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED|MASK_NOT_ISR,"NFastSemaphore::Signal"); |
|
555 |
NThreadBase* t = Inc(1); // release semantics |
|
556 |
if (t) |
|
557 |
{ |
|
558 |
t->AcqSLock(); |
|
559 |
t->iWaitState.UnBlockT(NThreadBase::EWaitFastSemaphore, this, KErrNone); |
|
560 |
t->RelSLock(); |
|
561 |
} |
|
562 |
} |
|
563 |
||
564 |
||
565 |
/** Signals a fast semaphore multiple times. |
|
566 |
||
567 |
@pre Kernel must be locked. |
|
568 |
@pre Call either in a thread or an IDFC context. |
|
569 |
||
570 |
@post Kernel is locked. |
|
571 |
||
572 |
@internalComponent |
|
573 |
*/ |
|
574 |
EXPORT_C void NFastSemaphore::SignalN(TInt aCount) |
|
575 |
{ |
|
576 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED|MASK_NOT_ISR,"NFastSemaphore::SignalN"); |
|
577 |
__NK_ASSERT_DEBUG(aCount>=0); |
|
578 |
if (aCount > 0) |
|
579 |
{ |
|
580 |
NThreadBase* t = Inc(aCount); |
|
581 |
if (t) |
|
582 |
{ |
|
583 |
t->AcqSLock(); |
|
584 |
t->iWaitState.UnBlockT(NThreadBase::EWaitFastSemaphore, this, KErrNone); |
|
585 |
t->RelSLock(); |
|
586 |
} |
|
587 |
} |
|
588 |
} |
|
589 |
||
590 |
||
591 |
/** Cancels a wait on a fast semaphore. |
|
592 |
||
593 |
@pre Kernel must be locked. |
|
594 |
@pre Call either in a thread or an IDFC context. |
|
595 |
||
596 |
@post Kernel is locked. |
|
597 |
||
598 |
@internalComponent |
|
599 |
*/ |
|
600 |
void NFastSemaphore::WaitCancel() |
|
601 |
{ |
|
602 |
Inc(1); |
|
603 |
} |
|
604 |
||
605 |
||
606 |
/** Waits for a signal on the current thread's I/O semaphore. |
|
607 |
||
608 |
@pre No fast mutex can be held. |
|
609 |
@pre Call in a thread context. |
|
610 |
@pre Kernel must be unlocked |
|
611 |
@pre interrupts enabled |
|
612 |
*/ |
|
613 |
EXPORT_C void NKern::WaitForAnyRequest() |
|
614 |
{ |
|
615 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"NKern::WaitForAnyRequest"); |
|
616 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("WfAR")); |
|
617 |
NThreadBase* t = NKern::LockC(); |
|
618 |
NFastSemaphore* s = &t->iRequestSemaphore; |
|
619 |
t->iWaitState.SetUpWait(NThreadBase::EWaitFastSemaphore, 0, s); |
|
620 |
if (s->Dec(t)) // fully ordered semantics |
|
621 |
t->iWaitState.CancelWait(); // don't have to wait |
|
622 |
else |
|
623 |
RescheduleNeeded(); // have to wait |
|
624 |
NKern::Unlock(); |
|
625 |
} |
|
626 |
#endif |
|
627 |
||
628 |
||
629 |
/** Resets a fast semaphore. |
|
630 |
||
631 |
@pre Kernel must be locked. |
|
632 |
@pre Call either in a thread or an IDFC context. |
|
633 |
||
634 |
@post Kernel is locked. |
|
635 |
||
636 |
@internalComponent |
|
637 |
*/ |
|
638 |
EXPORT_C void NFastSemaphore::Reset() |
|
639 |
{ |
|
640 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED|MASK_NOT_ISR,"NFastSemaphore::Reset"); |
|
641 |
NThreadBase* t = DoReset(); |
|
642 |
if (t) |
|
643 |
{ |
|
644 |
t->AcqSLock(); |
|
645 |
t->iWaitState.UnBlockT(NThreadBase::EWaitFastSemaphore, this, KErrNone); |
|
646 |
t->RelSLock(); |
|
647 |
} |
|
648 |
} |
|
649 |
||
650 |
||
651 |
/** Sets the owner of a fast semaphore. |
|
652 |
||
653 |
@param aSem The semaphore to change ownership off. |
|
654 |
@param aThread The thread to own this semaphore. If aThread==0, then the |
|
655 |
owner is set to the current thread. |
|
656 |
||
657 |
@pre If changing ownership form one thread to another, the there must be no |
|
658 |
pending signals or waits. |
|
659 |
*/ |
|
660 |
EXPORT_C void NKern::FSSetOwner(NFastSemaphore* aSem,NThreadBase* aThread) |
|
661 |
{ |
|
662 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NKern::FSSetOwner %m %T",aSem,aThread)); |
|
663 |
NKern::Lock(); |
|
664 |
aSem->SetOwner(aThread); |
|
665 |
NKern::Unlock(); |
|
666 |
} |
|
667 |
||
668 |
#ifndef __FAST_SEM_MACHINE_CODED__ |
|
669 |
/** Waits on a fast semaphore. |
|
670 |
||
671 |
Decrements the signal count for the semaphore |
|
672 |
and waits for a signal if the semaphore becomes unsignalled. Only the |
|
673 |
thread that owns a fast semaphore can wait on it. |
|
674 |
||
675 |
@param aSem The semaphore to wait on. |
|
676 |
||
677 |
@pre The calling thread must own the semaphore. |
|
678 |
@pre No fast mutex can be held. |
|
679 |
||
680 |
@see NFastSemaphore::Wait() |
|
681 |
*/ |
|
682 |
EXPORT_C void NKern::FSWait(NFastSemaphore* aSem) |
|
683 |
{ |
|
684 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NFSW %m",aSem)); |
|
685 |
NKern::Lock(); |
|
686 |
aSem->Wait(); |
|
687 |
NKern::Unlock(); |
|
688 |
} |
|
689 |
||
690 |
||
691 |
/** Signals a fast semaphore. |
|
692 |
||
693 |
Increments the signal count of a fast semaphore |
|
694 |
by one and releases any waiting thread if the semaphore becomes signalled. |
|
695 |
||
696 |
@param aSem The semaphore to signal. |
|
697 |
||
698 |
@see NKern::FSWait() |
|
699 |
||
700 |
@pre Interrupts must be enabled. |
|
701 |
@pre Do not call from an ISR |
|
702 |
*/ |
|
703 |
EXPORT_C void NKern::FSSignal(NFastSemaphore* aSem) |
|
704 |
{ |
|
705 |
CHECK_PRECONDITIONS(MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR,"NKern::FSSignal(NFastSemaphore*)"); |
|
706 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NFSS %m",aSem)); |
|
707 |
NKern::Lock(); |
|
708 |
aSem->Signal(); |
|
709 |
NKern::Unlock(); |
|
710 |
} |
|
711 |
||
712 |
||
713 |
/** Signals a fast semaphore multiple times. |
|
714 |
||
715 |
Increments the signal count of a |
|
716 |
fast semaphore by aCount and releases any waiting thread if the semphore |
|
717 |
becomes signalled. |
|
718 |
||
719 |
@param aSem The semaphore to signal. |
|
720 |
@param aCount The number of times to signal the semaphore. |
|
721 |
||
722 |
@see NKern::FSWait() |
|
723 |
||
724 |
@pre Interrupts must be enabled. |
|
725 |
@pre Do not call from an ISR |
|
726 |
*/ |
|
727 |
EXPORT_C void NKern::FSSignalN(NFastSemaphore* aSem, TInt aCount) |
|
728 |
{ |
|
729 |
CHECK_PRECONDITIONS(MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR,"NKern::FSSignalN(NFastSemaphore*, TInt)"); |
|
730 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NFSSN %m %d",aSem,aCount)); |
|
731 |
__NK_ASSERT_DEBUG(aCount>=0); |
|
732 |
if (aCount == 0) |
|
733 |
return; |
|
734 |
NKern::Lock(); |
|
735 |
aSem->SignalN(aCount); |
|
736 |
NKern::Unlock(); |
|
737 |
} |
|
738 |
||
739 |
||
740 |
/** Signals the request semaphore of a nanothread. |
|
741 |
||
742 |
This function is intended to be used by the EPOC layer and personality |
|
743 |
layers. Device drivers should use Kern::RequestComplete instead. |
|
744 |
||
745 |
@param aThread Nanothread to signal. Must be non NULL. |
|
746 |
||
747 |
@see Kern::RequestComplete() |
|
748 |
||
749 |
@pre Interrupts must be enabled. |
|
750 |
@pre Do not call from an ISR |
|
751 |
*/ |
|
752 |
EXPORT_C void NKern::ThreadRequestSignal(NThread* aThread) |
|
753 |
{ |
|
754 |
NKern::FSSignal(&aThread->iRequestSemaphore); |
|
755 |
} |
|
756 |
||
757 |
||
758 |
/** Signals the request semaphore of a nanothread several times. |
|
759 |
||
760 |
This function is intended to be used by the EPOC layer and personality |
|
761 |
layers. Device drivers should use Kern::RequestComplete instead. |
|
762 |
||
763 |
@param aThread Nanothread to signal. If NULL, the current thread is signaled. |
|
764 |
@param aCount Number of times the request semaphore must be signaled. |
|
765 |
||
766 |
@pre aCount >= 0 |
|
767 |
||
768 |
@see Kern::RequestComplete() |
|
769 |
*/ |
|
770 |
EXPORT_C void NKern::ThreadRequestSignal(NThread* aThread, TInt aCount) |
|
771 |
{ |
|
772 |
__ASSERT_WITH_MESSAGE_DEBUG(aCount >= 0,"aCount >= 0","NKern::ThreadRequestSignal"); |
|
773 |
if (!aThread) |
|
774 |
aThread = (NThread*)NKern::CurrentThread(); |
|
775 |
NKern::FSSignalN(&aThread->iRequestSemaphore, aCount); |
|
776 |
} |
|
777 |
#endif |
|
778 |
||
779 |
||
780 |
||
781 |
/** Atomically signals a fast semaphore and releases a fast mutex. |
|
782 |
||
783 |
Rescheduling only occurs after both synchronisation operations are complete. |
|
784 |
||
785 |
@param aSem The semaphore to signal. |
|
786 |
@param aMutex The mutex to release. If NULL, the System Lock is released |
|
787 |
||
788 |
@pre The calling thread must hold the mutex. |
|
789 |
||
790 |
@see NKern::FMSignal() |
|
791 |
*/ |
|
792 |
EXPORT_C void NKern::FSSignal(NFastSemaphore* aSem, NFastMutex* aMutex) |
|
793 |
{ |
|
794 |
if (!aMutex) |
|
795 |
aMutex=&TheScheduler.iLock; |
|
796 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NFSS %m +FM %M",aSem,aMutex)); |
|
797 |
NKern::Lock(); |
|
798 |
aSem->Signal(); |
|
799 |
aMutex->Signal(); |
|
800 |
NKern::Unlock(); |
|
801 |
} |
|
802 |
||
803 |
||
804 |
/** Atomically signals a fast semaphore multiple times and releases a fast mutex. |
|
805 |
||
806 |
Rescheduling only occurs after both synchronisation operations are complete. |
|
807 |
||
808 |
@param aSem The semaphore to signal. |
|
809 |
@param aCount The number of times to signal the semaphore. |
|
810 |
@param aMutex The mutex to release. If NULL, the System Lock is released. |
|
811 |
||
812 |
@pre The calling thread must hold the mutex. |
|
813 |
||
814 |
@see NKern::FMSignal() |
|
815 |
*/ |
|
816 |
EXPORT_C void NKern::FSSignalN(NFastSemaphore* aSem, TInt aCount, NFastMutex* aMutex) |
|
817 |
{ |
|
818 |
if (!aMutex) |
|
819 |
aMutex=&TheScheduler.iLock; |
|
820 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NFSSN %m %d + FM %M",aSem,aCount,aMutex)); |
|
821 |
NKern::Lock(); |
|
822 |
aSem->SignalN(aCount); |
|
823 |
aMutex->Signal(); |
|
824 |
NKern::Unlock(); |
|
825 |
} |
|
826 |
||
827 |
||
828 |
/****************************************************************************** |
|
829 |
* Thread |
|
830 |
******************************************************************************/ |
|
831 |
||
832 |
void NThreadBase::DoCsFunctionT() |
|
833 |
{ |
|
834 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T nDoCsFuncT %d",this,iCsFunction)); |
|
835 |
TInt f=iCsFunction; |
|
836 |
if (f==0) |
|
837 |
return; |
|
838 |
if (f>0) |
|
839 |
{ |
|
840 |
// suspend this thread f times |
|
841 |
iCsFunction = 0; |
|
842 |
iSuspendCount += f; |
|
843 |
iSuspended = 1; |
|
844 |
RescheduleNeeded(); |
|
845 |
return; |
|
846 |
} |
|
847 |
if (f==ECSExitPending || f==ECSDivertPending) |
|
848 |
{ |
|
849 |
// We need to exit now |
|
850 |
RelSLock(); |
|
851 |
Exit(); // this won't return |
|
852 |
} |
|
853 |
// UnknownState(ELeaveCS,f); // call into RTOS personality |
|
854 |
__NK_ASSERT_ALWAYS(0); |
|
855 |
} |
|
856 |
||
857 |
TBool NThreadBase::DoSuspendOrKillT(TInt aCount, TSubScheduler* aS) |
|
858 |
{ |
|
859 |
TBool result = TRUE; |
|
860 |
if (aCount>=0) |
|
861 |
{ |
|
862 |
if (iSuspended) |
|
863 |
result = FALSE; |
|
864 |
iSuspendCount+=aCount; |
|
865 |
iSuspended = 1; |
|
866 |
if (!iCurrent) |
|
867 |
{ |
|
868 |
if (aS) |
|
869 |
UnReadyT(); |
|
870 |
else if (iReady) |
|
871 |
{ |
|
872 |
NThreadGroup* g = (NThreadGroup*)iParent; |
|
873 |
g->iNThreadList.Remove(this); |
|
874 |
} |
|
875 |
} |
|
876 |
if (this == NCurrentThreadL()) |
|
877 |
RescheduleNeeded(); |
|
878 |
if (aS) |
|
879 |
aS->iReadyListLock.UnlockOnly(); |
|
880 |
} |
|
881 |
else |
|
882 |
{ |
|
883 |
iCsFunction = ECSDivertPending; |
|
884 |
iSuspendCount = 0; |
|
885 |
iSuspended = 0; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
886 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
887 |
// If thread is killed before first resumption, set iACount=1 |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
888 |
__e32_atomic_tau_ord8(&iACount, 1, 0, 1); |
0 | 889 |
if (aS) |
890 |
aS->iReadyListLock.UnlockOnly(); |
|
891 |
DoReleaseT(KErrDied,0); |
|
892 |
if (!iReady && !iPauseCount) |
|
893 |
ReadyT(0); |
|
894 |
} |
|
895 |
return result; |
|
896 |
} |
|
897 |
||
898 |
// If aCount>=0 suspend the thread aCount times |
|
899 |
// If aCount<0 kill the thread |
|
900 |
TBool NThreadBase::SuspendOrKill(TInt aCount) |
|
901 |
{ |
|
902 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T nSuspendOrKill %d", this, aCount)); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
903 |
if (aCount==0 || i_NThread_Initial) |
0 | 904 |
return FALSE; |
905 |
TBool result = FALSE; |
|
906 |
TBool concurrent = FALSE; |
|
907 |
TSubScheduler* ss = 0; |
|
908 |
AcqSLock(); |
|
909 |
NFastMutex* wfm = 0; |
|
910 |
if (iLinkedObj && iLinkedObjType==EWaitFastMutex) |
|
911 |
wfm = (NFastMutex*)iLinkedObj; |
|
912 |
if (iCsFunction<0) |
|
913 |
goto done2; // if already exiting ignore suspend or kill |
|
914 |
if (wfm) |
|
915 |
{ |
|
916 |
// if thread is waiting on a fast mutex, need to acquire mutex lock |
|
917 |
++iPauseCount; |
|
918 |
RelSLock(); |
|
919 |
wfm->iMutexLock.LockOnly(); |
|
920 |
AcqSLock(); |
|
921 |
UnPauseT(); |
|
922 |
} |
|
923 |
if (iReady && iParent->iReady) |
|
924 |
{ |
|
925 |
ss = TheSubSchedulers + (iParent->iReady & EReadyCpuMask); |
|
926 |
ss->iReadyListLock.LockOnly(); |
|
927 |
} |
|
928 |
concurrent = (iCurrent && this!=NCurrentThreadL()); |
|
929 |
if (iWaitState.ThreadIsDead()) // already dead so suspension/kill is a no-op |
|
930 |
goto done; |
|
931 |
if (concurrent) |
|
932 |
{ |
|
933 |
// thread is actually running on another CPU |
|
934 |
// interrupt that CPU and wait for it to enter interrupt mode |
|
935 |
// this allows a snapshot of the thread state to be observed |
|
936 |
// in this state, the thread cannot enter or leave a critical section |
|
937 |
send_resched_ipi_and_wait(iLastCpu); |
|
938 |
} |
|
939 |
if (iCsCount) |
|
940 |
{ |
|
941 |
suspend_or_kill_in_cs: |
|
942 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("n Suspend %T (CSF %d) %d",this,iCsFunction,aCount)); |
|
943 |
if (aCount>0) // -ve means thread is about to exit |
|
944 |
iCsFunction+=aCount; // so thread will suspend itself when it leaves the critical section |
|
945 |
else |
|
946 |
iCsFunction = ECSExitPending; |
|
947 |
goto done; |
|
948 |
} |
|
949 |
// iCsCount==0 and it can't become nonzero until we release the thread spin lock |
|
950 |
// (since threads may not set iCsCount to a nonzero value with the kernel lock held) |
|
951 |
// Make sure the thread isn't actually about to exit by itself |
|
952 |
if (iCsFunction<0) |
|
953 |
goto done; // if already exiting ignore suspend or kill |
|
954 |
if (wfm) |
|
955 |
{ |
|
956 |
wfm->iWaitQ.Remove(&iWaitLink); // take thread off the wait/contend queue |
|
957 |
iWaitLink.iNext = 0; |
|
958 |
iLinkedObj = 0; |
|
959 |
iLinkedObjType = EWaitNone; |
|
960 |
result = DoSuspendOrKillT(aCount, ss); |
|
961 |
if (aCount>0) |
|
962 |
DoReleaseT(KErrGeneral, 0); // thread isn't blocked any more, just suspended |
|
963 |
RelSLock(); |
|
964 |
||
965 |
// May need to adjust holding thread's inherited priority. |
|
966 |
// May need to wake up next thread to take this one's place. |
|
967 |
NThreadBase* pH = (NThreadBase*)(TLinAddr(wfm->iHoldingThread) &~ 1); |
|
968 |
if (pH) |
|
969 |
pH->SetMutexPriority(wfm); |
|
970 |
else if (!pH && !wfm->iWaitQ.IsEmpty()) |
|
971 |
{ |
|
972 |
NThreadBase* pT = _LOFF(wfm->iWaitQ.First(), NThreadBase, iWaitLink); |
|
973 |
pT->AcqSLock(); |
|
974 |
pT->iWaitState.UnBlockT(NThreadBase::EWaitFastMutex, wfm, KErrNone); |
|
975 |
pT->RelSLock(); |
|
976 |
} |
|
977 |
wfm->iMutexLock.UnlockOnly(); |
|
978 |
return result; |
|
979 |
} |
|
980 |
if (CheckFastMutexDefer()) |
|
981 |
goto suspend_or_kill_in_cs; |
|
982 |
||
983 |
// thread not in critical section, so suspend it |
|
984 |
result = DoSuspendOrKillT(aCount, ss); |
|
985 |
goto done2; |
|
986 |
||
987 |
done: |
|
988 |
if (wfm) |
|
989 |
wfm->iMutexLock.UnlockOnly(); |
|
990 |
if (ss) |
|
991 |
ss->iReadyListLock.UnlockOnly(); |
|
992 |
done2: |
|
993 |
RelSLock(); |
|
994 |
||
995 |
return result; |
|
996 |
} |
|
997 |
||
998 |
||
999 |
/** Suspends a nanothread the specified number of times. |
|
1000 |
||
1001 |
For use by RTOS personality layers. |
|
1002 |
Do not use this function directly on a Symbian OS thread. |
|
1003 |
Since the kernel is locked on entry, any reschedule will be deferred until |
|
1004 |
it is unlocked. |
|
1005 |
The suspension will be deferred if the target thread is currently in a |
|
1006 |
critical section; in this case the suspension will take effect when it exits |
|
1007 |
the critical section. |
|
1008 |
The thread's unknown state handler will be invoked with function ESuspend and |
|
1009 |
parameter aCount if the current NState is not recognised and it is not in a |
|
1010 |
critical section. |
|
1011 |
||
1012 |
@param aCount = the number of times to suspend. |
|
1013 |
@return TRUE, if the suspension has taken immediate effect; |
|
1014 |
FALSE, if the thread is in a critical section or is already suspended. |
|
1015 |
||
1016 |
@pre Kernel must be locked. |
|
1017 |
@pre Call in a thread context. |
|
1018 |
||
1019 |
@post Kernel is locked. |
|
1020 |
*/ |
|
1021 |
EXPORT_C TBool NThreadBase::Suspend(TInt aCount) |
|
1022 |
{ |
|
1023 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED|MASK_NOT_ISR|MASK_NOT_IDFC,"NThreadBase::Suspend"); |
|
1024 |
__NK_ASSERT_ALWAYS(aCount>=0); |
|
1025 |
||
1026 |
// If thread is executing a critical section, we must defer the suspend |
|
1027 |
||
1028 |
return SuspendOrKill(aCount); |
|
1029 |
} |
|
1030 |
||
1031 |
||
1032 |
TBool NThreadBase::Resume(TBool aForce) |
|
1033 |
{ |
|
1034 |
TBool result = FALSE; |
|
1035 |
AcqSLock(); |
|
1036 |
if (iWaitState.ThreadIsDead() || iCsFunction<0) // already dead or dying so resume is a no-op |
|
1037 |
goto done; |
|
1038 |
||
1039 |
if (iCsFunction>0) |
|
1040 |
{ |
|
1041 |
if (aForce) |
|
1042 |
iCsFunction = 0; |
|
1043 |
else |
|
1044 |
--iCsFunction; |
|
1045 |
} |
|
1046 |
else if (iSuspendCount) |
|
1047 |
{ |
|
1048 |
if (aForce) |
|
1049 |
iSuspendCount = 0; |
|
1050 |
else |
|
1051 |
--iSuspendCount; |
|
1052 |
if (!iSuspendCount) |
|
1053 |
{ |
|
1054 |
result = TRUE; |
|
1055 |
iSuspended = 0; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1056 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1057 |
// On first resumption set iACount=1 |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1058 |
// From then on the thread must be killed before being deleted |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1059 |
__e32_atomic_tau_ord8(&iACount, 1, 0, 1); |
0 | 1060 |
if (!iPauseCount && !iReady && !iWaitState.iWtC.iWtStFlags) |
1061 |
ReadyT(0); |
|
1062 |
} |
|
1063 |
} |
|
1064 |
||
1065 |
done: |
|
1066 |
RelSLock(); |
|
1067 |
return result; |
|
1068 |
} |
|
1069 |
||
1070 |
/** Resumes a nanothread, cancelling one suspension. |
|
1071 |
||
1072 |
For use by RTOS personality layers. |
|
1073 |
Do not use this function directly on a Symbian OS thread. |
|
1074 |
Since the kernel is locked on entry, any reschedule will be deferred until |
|
1075 |
it is unlocked. |
|
1076 |
If the target thread is currently in a critical section this will simply |
|
1077 |
cancel one deferred suspension. |
|
1078 |
The thread's unknown state handler will be invoked with function EResume if |
|
1079 |
the current NState is not recognised and it is not in a critical section. |
|
1080 |
||
1081 |
@return TRUE, if the resumption has taken immediate effect; |
|
1082 |
FALSE, if the thread is in a critical section or is still suspended. |
|
1083 |
||
1084 |
@pre Kernel must be locked. |
|
1085 |
@pre Call either in a thread or an IDFC context. |
|
1086 |
||
1087 |
@post Kernel must be locked. |
|
1088 |
*/ |
|
1089 |
EXPORT_C TBool NThreadBase::Resume() |
|
1090 |
{ |
|
1091 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED|MASK_NOT_ISR|MASK_NOT_IDFC,"NThreadBase::Resume"); |
|
1092 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T nRsm",this)); |
|
1093 |
||
1094 |
return Resume(FALSE); |
|
1095 |
} |
|
1096 |
||
1097 |
||
1098 |
/** Resumes a nanothread, cancelling all outstanding suspensions. |
|
1099 |
||
1100 |
For use by RTOS personality layers. |
|
1101 |
Do not use this function directly on a Symbian OS thread. |
|
1102 |
Since the kernel is locked on entry, any reschedule will be deferred until |
|
1103 |
it is unlocked. |
|
1104 |
If the target thread is currently in a critical section this will simply |
|
1105 |
cancel all deferred suspensions. |
|
1106 |
The thread's unknown state handler will be invoked with function EForceResume |
|
1107 |
if the current NState is not recognised and it is not in a critical section. |
|
1108 |
||
1109 |
@return TRUE, if the resumption has taken immediate effect; |
|
1110 |
FALSE, if the thread is in a critical section. |
|
1111 |
||
1112 |
@pre Kernel must be locked. |
|
1113 |
@pre Call either in a thread or an IDFC context. |
|
1114 |
||
1115 |
@post Kernel is locked. |
|
1116 |
*/ |
|
1117 |
EXPORT_C TBool NThreadBase::ForceResume() |
|
1118 |
{ |
|
1119 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED|MASK_NOT_ISR,"NThreadBase::ForceResume"); |
|
1120 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T nFRsm",this)); |
|
1121 |
||
1122 |
return Resume(TRUE); |
|
1123 |
} |
|
1124 |
||
1125 |
||
1126 |
void NThreadBase::DoReleaseT(TInt aReturnCode, TUint aMode) |
|
1127 |
{ |
|
1128 |
TAny* wobj = 0; |
|
1129 |
TUint32 b = iWaitState.ReleaseT(wobj, aReturnCode); // cancels timer if necessary |
|
1130 |
||
1131 |
// if wait pending or no wait, done |
|
1132 |
// if wait in effect and nothing else stopping it, make thread ready |
|
1133 |
// cancel any outstanding wait on fast semaphore if abnormal release |
|
1134 |
// FIXME: Potential problems with abnormal release of generic wait objects |
|
1135 |
if (aReturnCode<0 && ((b>>8)&0xff)==NThreadBase::EWaitFastSemaphore && wobj) |
|
1136 |
((NFastSemaphore*)wobj)->WaitCancel(); |
|
1137 |
||
1138 |
if ((b & NThreadWaitState::EWtStWaitActive) && !iPauseCount && !iSuspended) |
|
1139 |
ReadyT(aMode); |
|
1140 |
} |
|
1141 |
||
1142 |
/** Releases a waiting nanokernel thread. |
|
1143 |
||
1144 |
For use by RTOS personality layers. |
|
1145 |
Do not use this function directly on a Symbian OS thread. |
|
1146 |
This function should make the thread ready (provided it is not explicitly |
|
1147 |
suspended) and cancel any wait timeout. It should also remove it from any |
|
1148 |
wait queues. |
|
1149 |
If aReturnCode is nonnegative it indicates normal completion of the wait. |
|
1150 |
If aReturnCode is negative it indicates early/abnormal completion of the |
|
1151 |
wait and so any wait object should be reverted as if the wait had never |
|
1152 |
occurred (eg semaphore count should be incremented as this thread has not |
|
1153 |
actually acquired the semaphore). |
|
1154 |
The thread's unknown state handler will be invoked with function ERelease |
|
1155 |
and parameter aReturnCode if the current NState is not recognised. |
|
1156 |
||
1157 |
@param aReturnCode The reason code for release. |
|
1158 |
||
1159 |
@pre Kernel must be locked. |
|
1160 |
@pre Call either in a thread or an IDFC context. |
|
1161 |
||
1162 |
@post Kernel is locked. |
|
1163 |
*/ |
|
1164 |
EXPORT_C void NThreadBase::Release(TInt aReturnCode, TUint aMode) |
|
1165 |
{ |
|
1166 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED|MASK_NOT_ISR,"NThreadBase::Release"); |
|
1167 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T nRel %d",this,aReturnCode)); |
|
1168 |
AcqSLock(); |
|
1169 |
DoReleaseT(aReturnCode, aMode); |
|
1170 |
RelSLock(); |
|
1171 |
} |
|
1172 |
||
1173 |
||
1174 |
/** Signals a nanokernel thread's request semaphore. |
|
1175 |
||
1176 |
This can also be used on Symbian OS threads. |
|
1177 |
||
1178 |
@pre Kernel must be locked. |
|
1179 |
@pre Call either in a thread or an IDFC context. |
|
1180 |
||
1181 |
@post Kernel is locked. |
|
1182 |
*/ |
|
1183 |
EXPORT_C void NThreadBase::RequestSignal() |
|
1184 |
{ |
|
1185 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED|MASK_NOT_ISR,"NThreadBase::RequestSignal"); |
|
1186 |
iRequestSemaphore.Signal(); |
|
1187 |
} |
|
1188 |
||
1189 |
||
1190 |
void exit_sync_fn(TAny* aDfc) |
|
1191 |
{ |
|
1192 |
((TDfc*)aDfc)->Enque(); |
|
1193 |
} |
|
1194 |
||
1195 |
void NThreadBase::Exit() |
|
1196 |
{ |
|
1197 |
// The current thread is exiting |
|
1198 |
// Enter with kernel locked, don't return |
|
1199 |
__NK_ASSERT_DEBUG(this==NCurrentThreadL()); |
|
1200 |
||
1201 |
OnExit(); |
|
1202 |
||
1203 |
TInt threadCS = iCsCount; |
|
1204 |
TInt kernCS = SubScheduler().iKernLockCount; |
|
1205 |
iCsCount = 1; |
|
1206 |
AcqSLock(); |
|
1207 |
iCsFunction = ECSExitInProgress; |
|
1208 |
NFastMutex* m = NKern::HeldFastMutex(); |
|
1209 |
iHeldFastMutex = 0; |
|
1210 |
RelSLock(); |
|
1211 |
NKern::Unlock(); |
|
1212 |
__KTRACE_OPT(KSCHED,DEBUGPRINT("Exit %T %u",this,NTickCount())); |
|
1213 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T nExit, CSC %d HeldFM %M KernCS %d",this,threadCS,iHeldFastMutex,kernCS)); |
|
1214 |
if (kernCS!=1) |
|
1215 |
FAULT(); |
|
1216 |
if (m) |
|
1217 |
FAULT(); |
|
1218 |
if (threadCS) |
|
1219 |
FAULT(); |
|
1220 |
TDfc* pD = NULL; |
|
1221 |
NThreadExitHandler xh = iHandlers->iExitHandler; |
|
1222 |
if (xh) |
|
1223 |
pD = (*xh)((NThread*)this); // call exit handler |
|
1224 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1225 |
// if CPU freeze still active, remove it |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1226 |
NKern::EndFreezeCpu(0); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1227 |
|
0 | 1228 |
// detach any tied events |
1229 |
DetachTiedEvents(); |
|
1230 |
||
1231 |
NKern::LeaveGroup(); // detach from group if exit handler didn't do it |
|
1232 |
||
1233 |
NKern::Lock(); |
|
1234 |
#ifdef BTRACE_THREAD_IDENTIFICATION |
|
1235 |
BTrace4(BTrace::EThreadIdentification,BTrace::ENanoThreadDestroy,this); |
|
1236 |
#endif |
|
1237 |
__NK_ASSERT_ALWAYS(iCsFunction == ECSExitInProgress); |
|
1238 |
iWaitState.SetDead(pD); // doesn't return |
|
1239 |
FAULT(); |
|
1240 |
} |
|
1241 |
||
1242 |
/** Kills a nanokernel thread. |
|
1243 |
||
1244 |
For use by RTOS personality layers. |
|
1245 |
Do not use this function directly on a Symbian OS thread. |
|
1246 |
||
1247 |
When acting on the calling thread, causes the calling thread to exit. |
|
1248 |
||
1249 |
When acting on another thread, causes that thread to exit unless it is |
|
1250 |
currently in a critical section. In this case the thread is marked as |
|
1251 |
"exit pending" and will exit as soon as it leaves the critical section. |
|
1252 |
||
1253 |
In either case the exiting thread first invokes its exit handler (if it |
|
1254 |
exists). The handler runs with preemption enabled and with the thread in a |
|
1255 |
critical section so that it may not be suspended or killed again. The |
|
1256 |
handler may return a pointer to a TDfc, which will be enqueued just before |
|
1257 |
the thread finally terminates (after the kernel has been relocked). This DFC |
|
1258 |
will therefore execute once the NThread has been safely removed from the |
|
1259 |
scheduler and is intended to be used to cleanup the NThread object and any |
|
1260 |
associated personality layer resources. |
|
1261 |
||
1262 |
@pre Kernel must be locked. |
|
1263 |
@pre Call in a thread context. |
|
1264 |
@pre If acting on calling thread, calling thread must not be in a |
|
1265 |
critical section; if it is the kernel will fault. Also, the kernel |
|
1266 |
must be locked exactly once (iKernCSLocked = 1). |
|
1267 |
||
1268 |
@post Kernel is locked, if not acting on calling thread. |
|
1269 |
@post Does not return if it acts on the calling thread. |
|
1270 |
*/ |
|
1271 |
EXPORT_C void NThreadBase::Kill() |
|
1272 |
{ |
|
1273 |
// Kill a thread |
|
1274 |
// Enter with kernel locked |
|
1275 |
// Exit with kernel locked if not current thread, otherwise does not return |
|
1276 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED_ONCE|MASK_NOT_IDFC|MASK_NOT_ISR,"NThreadBase::Kill"); |
|
1277 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T nKill",this)); |
|
1278 |
OnKill(); // platform-specific hook |
|
1279 |
NThreadBase* pC = NCurrentThreadL(); |
|
1280 |
if (this==pC) |
|
1281 |
{ |
|
1282 |
if (iCsFunction==ECSExitInProgress) |
|
1283 |
FAULT(); |
|
1284 |
Exit(); // this will not return |
|
1285 |
} |
|
1286 |
SuspendOrKill(-1); |
|
1287 |
} |
|
1288 |
||
1289 |
||
1290 |
/** Change the CPU affinity of a thread |
|
1291 |
||
1292 |
@pre Kernel must be locked. |
|
1293 |
@pre Call in a thread context. |
|
1294 |
||
1295 |
@param The number of the CPU to which this thread should be locked, or |
|
1296 |
KCpuAny if it should be able to run on any CPU. |
|
1297 |
@return The previous affinity mask. |
|
1298 |
*/ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1299 |
TUint32 NSchedulable::SetCpuAffinityT(TUint32 aAffinity) |
0 | 1300 |
{ |
1301 |
// check aAffinity is valid |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1302 |
NThreadBase* t = 0; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1303 |
NThreadGroup* g = 0; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1304 |
NSchedulable* p = iParent; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1305 |
if (!p) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1306 |
g = (NThreadGroup*)this, p=g; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1307 |
else |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1308 |
t = (NThreadBase*)this; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1309 |
if (iParent && iParent!=this) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1310 |
g = (NThreadGroup*)iParent; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1311 |
TUint32 old_aff = p->iCpuAffinity; |
0 | 1312 |
TBool make_ready = FALSE; |
1313 |
TSubScheduler* ss0 = &SubScheduler(); |
|
1314 |
TSubScheduler* ss = 0; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1315 |
#ifdef KNKERN |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1316 |
if (iParent) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1317 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1318 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T nSetCpu %08x->%08x, F:%d R:%02x PR:%02x",this,iParent->iCpuAffinity,aAffinity,iParent->iFreezeCpu,iReady,iParent->iReady)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1319 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1320 |
else |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1321 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1322 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%G nSetCpu %08x->%08x, F:%d R:%02x",this,iCpuAffinity,aAffinity,iFreezeCpu,iReady)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1323 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1324 |
#endif |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1325 |
if (t && t->i_NThread_Initial) |
0 | 1326 |
goto done; // can't change affinity of initial thread |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1327 |
if (aAffinity == NTHREADBASE_CPU_AFFINITY_MASK) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1328 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1329 |
p->iTransientCpu = 0; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1330 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1331 |
else if ( (aAffinity & (KCpuAffinityPref|NTHREADBASE_CPU_AFFINITY_MASK)) == KCpuAffinityPref) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1332 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1333 |
p->iTransientCpu = 0; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1334 |
p->iPreferredCpu = TUint8((aAffinity & (EReadyCpuMask|EReadyCpuSticky)) | EReadyOffset); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1335 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1336 |
else if ( (aAffinity & (KCpuAffinityTransient|KCpuAffinityPref|NTHREADBASE_CPU_AFFINITY_MASK)) == KCpuAffinityTransient) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1337 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1338 |
p->iTransientCpu = TUint8(aAffinity & EReadyCpuMask) | EReadyOffset; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1339 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1340 |
else |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1341 |
p->iCpuAffinity = NSchedulable::PreprocessCpuAffinity(aAffinity); // set new affinity, might not take effect yet |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1342 |
if (!p->iReady) |
0 | 1343 |
goto done; // thread/group not currently on a ready list so can just change affinity |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1344 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1345 |
// Check if the thread needs to migrate or can stay where it is |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1346 |
if (!p->ShouldMigrate(p->iReady & EReadyCpuMask)) |
0 | 1347 |
goto done; // don't need to move thread, so just change affinity |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1348 |
ss = TheSubSchedulers + (p->iReady & EReadyCpuMask); |
0 | 1349 |
ss->iReadyListLock.LockOnly(); |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1350 |
if (p->iCurrent) |
0 | 1351 |
{ |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1352 |
p->iCpuChange = TRUE; // mark CPU change pending |
0 | 1353 |
if (ss == ss0) |
1354 |
RescheduleNeeded(); |
|
1355 |
else |
|
1356 |
// kick other CPU now so migration happens before acquisition of fast mutex |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1357 |
send_resched_ipi_and_wait(p->iReady & EReadyCpuMask); |
0 | 1358 |
} |
1359 |
else |
|
1360 |
{ |
|
1361 |
// Note: Need to know here if any thread in group would return TRUE from CheckFastMutexDefer() |
|
1362 |
// This is handled by the scheduler - when a thread belonging to a group is context switched |
|
1363 |
// out while holding a fast mutex its iFastMutexDefer is set to 1 and the group's iFreezeCpu |
|
1364 |
// is incremented. |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1365 |
if (p->iFreezeCpu || (iParent==this && t->CheckFastMutexDefer())) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1366 |
p->iCpuChange = TRUE; // CPU frozen or fast mutex held so just mark deferred CPU migration |
0 | 1367 |
else |
1368 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1369 |
ss->SSRemoveEntry(p); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1370 |
p->iReady = 0; |
0 | 1371 |
make_ready = TRUE; |
1372 |
} |
|
1373 |
} |
|
1374 |
ss->iReadyListLock.UnlockOnly(); |
|
1375 |
if (make_ready) |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1376 |
p->ReadyT(0); |
0 | 1377 |
done: |
1378 |
return old_aff; |
|
1379 |
} |
|
1380 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1381 |
/** Force the current thread onto a particular CPU |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1382 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1383 |
@pre Kernel must not be locked. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1384 |
@pre Call in a thread context. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1385 |
@pre Current thread must not be in a group |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1386 |
@pre Current thread must not hold a fast mutex |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1387 |
@pre Current thread must have an active CPU freeze |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1388 |
@pre Current thread must not be an initial thread |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1389 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1390 |
@param The number of the CPU to which this thread should be moved |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1391 |
*/ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1392 |
void NKern::JumpTo(TInt aCpu) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1393 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1394 |
// check aAffinity is valid |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1395 |
NThreadBase* t = NKern::CurrentThread(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1396 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T NJumpTo %d", t, aCpu)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1397 |
if (NKern::HeldFastMutex()) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1398 |
__crash(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1399 |
t->LAcqSLock(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1400 |
if (t->iParent!=t) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1401 |
__crash(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1402 |
if (!t->iFreezeCpu) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1403 |
__crash(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1404 |
if (t->i_NThread_Initial) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1405 |
__crash(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1406 |
if (TUint(aCpu) >= (TUint)NKern::NumberOfCpus()) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1407 |
__crash(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1408 |
TUint8 fc = (TUint8)(aCpu | NSchedulable::EReadyOffset); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1409 |
if (t->iCurrent != fc) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1410 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1411 |
t->iForcedCpu = fc; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1412 |
t->iCpuChange = TRUE; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1413 |
RescheduleNeeded(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1414 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1415 |
t->RelSLockU(); // reschedules and jumps to new CPU |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1416 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1417 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1418 |
TBool NSchedulable::ShouldMigrate(TInt aCpu) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1419 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1420 |
// Check if the thread's current CPU is compatible with the new affinity |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1421 |
TUint32 active = TheScheduler.iThreadAcceptCpus; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1422 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1423 |
// If it can't stay where it is, migrate |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1424 |
if (!CheckCpuAgainstAffinity(aCpu, iCpuAffinity, active)) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1425 |
return TRUE; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1426 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1427 |
TInt cpu = iTransientCpu ? iTransientCpu : iPreferredCpu; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1428 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1429 |
// No preferred or transient CPU, so can stay where it is |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1430 |
if (!cpu) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1431 |
return FALSE; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1432 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1433 |
// If thread isn't on preferred CPU but could be, migrate |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1434 |
cpu &= EReadyCpuMask; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1435 |
if (cpu!=aCpu && CheckCpuAgainstAffinity(cpu, iCpuAffinity, active)) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1436 |
return TRUE; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1437 |
return FALSE; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1438 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1439 |
|
0 | 1440 |
|
1441 |
/****************************************************************************** |
|
1442 |
* Thread wait state |
|
1443 |
******************************************************************************/ |
|
1444 |
#ifndef __NTHREAD_WAITSTATE_MACHINE_CODED__ |
|
1445 |
void NThreadWaitState::SetUpWait(TUint aType, TUint aFlags, TAny* aWaitObj) |
|
1446 |
{ |
|
1447 |
SetUpWait(aType, aFlags, aWaitObj, 0); |
|
1448 |
} |
|
1449 |
||
1450 |
void NThreadWaitState::SetUpWait(TUint aType, TUint aFlags, TAny* aWaitObj, TUint32 aTimeout) |
|
1451 |
{ |
|
1452 |
aFlags &= EWtStObstructed; |
|
1453 |
aFlags |= EWtStWaitPending; |
|
1454 |
aType &= 0xff; |
|
1455 |
TUint64 ws64 = (TUint32)aWaitObj; |
|
1456 |
ws64 <<= 32; |
|
1457 |
ws64 |= ((aType<<8)|aFlags); |
|
1458 |
TUint64 oldws64 = __e32_atomic_swp_rlx64(&iWtSt64, ws64); |
|
1459 |
if (I64LOW(oldws64)!=0) |
|
1460 |
__crash(); // ??we were already waiting for something else?? |
|
1461 |
iTimer.iTriggerTime = aTimeout; |
|
1462 |
} |
|
1463 |
||
1464 |
void NThreadWaitState::CancelWait() |
|
1465 |
{ |
|
1466 |
TUint64 oldws64 = __e32_atomic_swp_rlx64(&iWtSt64, 0); |
|
1467 |
if (oldws64 & (EWtStDead|EWtStWaitActive)) |
|
1468 |
__crash(); |
|
1469 |
} |
|
1470 |
||
1471 |
TInt NThreadWaitState::DoWait() |
|
1472 |
{ |
|
1473 |
TUint64 oldws64 = iWtSt64; |
|
1474 |
TUint64 ws64; |
|
1475 |
TUint32 timeout = iTimer.iTriggerTime; |
|
1476 |
TUint32 set = timeout ? (EWtStWaitActive|EWtStTimeout) : EWtStWaitActive; |
|
1477 |
do { |
|
1478 |
TUint32 ws32 = I64LOW(oldws64); |
|
1479 |
if (ws32 & EWtStDead) |
|
1480 |
return KErrDied; |
|
1481 |
if (!(ws32 & EWtStWaitPending)) |
|
1482 |
return KErrGeneral; |
|
1483 |
ws64 = oldws64; |
|
1484 |
ws64 &= ~TUint64(EWtStWaitPending); |
|
1485 |
ws64 |= TUint64(set); |
|
1486 |
} while(!__e32_atomic_cas_rlx64(&iWtSt64, &oldws64, ws64)); |
|
1487 |
if (timeout) |
|
1488 |
{ |
|
1489 |
if (iTimer.OneShot(timeout, TRUE)!=KErrNone) |
|
1490 |
__crash(); |
|
1491 |
++iTimer.iNTimerSpare1; |
|
1492 |
} |
|
1493 |
return TUint32(oldws64)>>8; |
|
1494 |
} |
|
1495 |
||
1496 |
TInt NThreadWaitState::UnBlockT(TUint aType, TAny* aWaitObj, TInt aReturnValue) |
|
1497 |
{ |
|
1498 |
TUint64 exp = TUint32(aWaitObj); |
|
1499 |
exp <<= 32; |
|
1500 |
exp |= (aType<<8); |
|
1501 |
TUint64 oldws64 = iWtSt64; |
|
1502 |
TUint64 ws64; |
|
1503 |
do { |
|
1504 |
if ((oldws64 ^ exp) < TUint64(EWtStDead)) |
|
1505 |
ws64 = TUint64(TUint32(aReturnValue))<<32; |
|
1506 |
else |
|
1507 |
ws64 = oldws64; |
|
1508 |
} while(!__e32_atomic_cas_rel64(&iWtSt64, &oldws64, ws64)); |
|
1509 |
if ((oldws64 ^ exp) >= TUint64(EWtStDead)) |
|
1510 |
return KErrGeneral; // not unblocked - no matching wait |
|
1511 |
if (oldws64 & EWtStTimeout) |
|
1512 |
CancelTimerT(); |
|
1513 |
if (oldws64 & EWtStWaitActive) |
|
1514 |
{ |
|
1515 |
NThreadBase* t = Thread(); |
|
1516 |
if (!t->iPauseCount && !t->iSuspended) |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1517 |
t->ReadyT(oldws64 & EWtStObstructed); |
0 | 1518 |
} |
1519 |
return KErrNone; |
|
1520 |
} |
|
1521 |
||
1522 |
TUint32 NThreadWaitState::ReleaseT(TAny*& aWaitObj, TInt aReturnValue) |
|
1523 |
{ |
|
1524 |
TUint64 leave = EWtStDead; |
|
1525 |
TUint64 set = TUint64(TUint32(aReturnValue))<<32; |
|
1526 |
TUint64 ws64 = __e32_atomic_axo_ord64(&iWtSt64, leave, set); |
|
1527 |
aWaitObj = (TAny*)I64HIGH(ws64); |
|
1528 |
TUint32 ws32 = I64LOW(ws64); |
|
1529 |
if (ws32 & EWtStTimeout) |
|
1530 |
CancelTimerT(); |
|
1531 |
return ws32; |
|
1532 |
} |
|
1533 |
#endif |
|
1534 |
||
1535 |
void NThreadWaitState::SetDead(TDfc* aKillDfc) |
|
1536 |
{ |
|
1537 |
TDfc syncDfc(&exit_sync_fn, aKillDfc, TheTimerQ.iDfc.iDfcQ, 0); |
|
1538 |
NThreadBase* t = Thread(); |
|
1539 |
t->AcqSLock(); |
|
1540 |
iWtC.iWtStFlags = NThreadWaitState::EWtStDead; |
|
1541 |
iWtC.iWtObjType = NThreadBase::EWaitNone; |
|
1542 |
CancelTimerT(); |
|
1543 |
if (aKillDfc && iTimer.iNTimerSpare1) |
|
1544 |
{ |
|
1545 |
// There is an outstanding timer expiry handler still running |
|
1546 |
// so we must synchronise with DfcThread1. |
|
1547 |
// Add a priority 0 DFC to DfcThread1 so this thread's exit DFC can |
|
1548 |
// only run after the timer expiry handler has completed. |
|
1549 |
aKillDfc = &syncDfc; |
|
1550 |
} |
|
1551 |
iWtC.iKillDfc = aKillDfc; |
|
1552 |
RescheduleNeeded(); |
|
1553 |
t->RelSLock(); |
|
1554 |
NKern::Unlock(); // this won't return |
|
1555 |
} |
|
1556 |
||
1557 |
void NThreadWaitState::CancelTimerT() |
|
1558 |
{ |
|
1559 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T nCancelTimerT ",Thread())); |
|
1560 |
if (iTimer.Cancel()) |
|
1561 |
--iTimer.iNTimerSpare1; |
|
1562 |
else |
|
1563 |
{ |
|
1564 |
// Potential race condition - timer must have completed but expiry |
|
1565 |
// handler has not yet run. Signal to the handler that it should do |
|
1566 |
// nothing by flipping the bottom bit of iTimer.iPtr |
|
1567 |
// This condition cannot possibly recur until the expiry handler has |
|
1568 |
// run since all expiry handlers run in DfcThread1. |
|
1569 |
volatile TLinAddr& x = *(volatile TLinAddr*)&iTimer.iPtr; |
|
1570 |
x ^= 1; |
|
1571 |
} |
|
1572 |
} |
|
1573 |
||
1574 |
// Timeout handler, called in DfcThread1 |
|
1575 |
// NOTE: aPtr is sampled with the timer queue locked, so if Cancel() on the timer fails |
|
1576 |
// and iTimer.iPtr is then changed, aPtr here will differ from iTimer.iPtr. |
|
1577 |
// This fact is used here to detect expiry of cancelled timers. |
|
1578 |
void NThreadWaitState::TimerExpired(TAny* aPtr) |
|
1579 |
{ |
|
1580 |
TLinAddr cookie = (TLinAddr)aPtr; |
|
1581 |
NThreadWaitState* pW = (NThreadWaitState*)(cookie &~ 3); |
|
1582 |
NThread* pT = (NThread*)pW->Thread(); |
|
1583 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T nTmExp",pT)); |
|
1584 |
NThreadTimeoutHandler th = pT->iHandlers->iTimeoutHandler; |
|
1585 |
pT->LAcqSLock(); |
|
1586 |
TUint flags = pW->iWtSt32[0]; |
|
1587 |
if (!(flags & EWtStWaitActive) || ((flags>>8)&0xff)!=NThreadBase::EWaitBlocked) |
|
1588 |
th = 0; |
|
1589 |
if (th) |
|
1590 |
{ |
|
1591 |
// Use higher level timeout handler |
|
1592 |
pT->RelSLockU(); |
|
1593 |
(*th)(pT, NThreadBase::ETimeoutPreamble); |
|
1594 |
TInt param = NThreadBase::ETimeoutPostamble; |
|
1595 |
pT->LAcqSLock(); |
|
1596 |
TLinAddr current_cookie = *(volatile TLinAddr*)&pW->iTimer.iPtr; |
|
1597 |
if ((cookie ^ current_cookie) & 1) |
|
1598 |
{ |
|
1599 |
// The timer was cancelled just after expiring but before this function |
|
1600 |
// managed to acquire the thread spin lock, so it's spurious |
|
1601 |
param = NThreadBase::ETimeoutSpurious; |
|
1602 |
} |
|
1603 |
pT->RelSLockU(); |
|
1604 |
(*th)(pT, param); |
|
1605 |
pT->LAcqSLock(); |
|
1606 |
--pW->iTimer.iNTimerSpare1; // note timer has expired |
|
1607 |
pT->RelSLockU(); |
|
1608 |
return; |
|
1609 |
} |
|
1610 |
TLinAddr current_cookie = *(volatile TLinAddr*)&pW->iTimer.iPtr; |
|
1611 |
if ((cookie ^ current_cookie) & 1) |
|
1612 |
// The timer was cancelled just after expiring but before this function |
|
1613 |
// managed to acquire the thread spin lock, so just return without doing anything. |
|
1614 |
goto done; |
|
1615 |
pT->DoReleaseT(KErrTimedOut,0); |
|
1616 |
done: |
|
1617 |
pT->RelSLockU(); |
|
1618 |
} |
|
1619 |
||
1620 |
||
1621 |
||
1622 |
/****************************************************************************** |
|
1623 |
* NKern:: static functions |
|
1624 |
******************************************************************************/ |
|
1625 |
||
1626 |
/** Suspends the execution of a thread. |
|
1627 |
||
1628 |
This function is intended to be used by the EPOC layer and personality layers. |
|
1629 |
Do not use this function directly on a Symbian OS thread - use Kern::ThreadSuspend(). |
|
1630 |
||
1631 |
If the thread is in a critical section or holds a fast mutex, the suspension will |
|
1632 |
be deferred until the thread leaves the critical section or signals the fast mutex. |
|
1633 |
Otherwise the thread will be suspended with immediate effect. If the thread it's |
|
1634 |
running, the execution of the thread will be suspended and a reschedule will occur. |
|
1635 |
||
1636 |
@param aThread Thread to be suspended. |
|
1637 |
@param aCount Number of times to suspend this thread. |
|
1638 |
||
1639 |
@return TRUE, if the thread had changed the state from non-suspended to suspended; |
|
1640 |
FALSE, otherwise. |
|
1641 |
||
1642 |
@see Kern::ThreadSuspend() |
|
1643 |
*/ |
|
1644 |
EXPORT_C TBool NKern::ThreadSuspend(NThread* aThread, TInt aCount) |
|
1645 |
{ |
|
1646 |
NKern::Lock(); |
|
1647 |
TBool r=aThread->Suspend(aCount); |
|
1648 |
NKern::Unlock(); |
|
1649 |
return r; |
|
1650 |
} |
|
1651 |
||
1652 |
||
1653 |
/** Resumes the execution of a thread. |
|
1654 |
||
1655 |
This function is intended to be used by the EPOC layer and personality layers. |
|
1656 |
Do not use this function directly on a Symbian OS thread - use Kern::ThreadResume(). |
|
1657 |
||
1658 |
This function resumes the thread once. If the thread was suspended more than once |
|
1659 |
the thread will remain suspended. |
|
1660 |
If the thread is in a critical section, this function will decrease the number of |
|
1661 |
deferred suspensions. |
|
1662 |
||
1663 |
@param aThread Thread to be resumed. |
|
1664 |
||
1665 |
@return TRUE, if the thread had changed the state from suspended to non-suspended; |
|
1666 |
FALSE, otherwise. |
|
1667 |
||
1668 |
@see Kern::ThreadResume() |
|
1669 |
*/ |
|
1670 |
EXPORT_C TBool NKern::ThreadResume(NThread* aThread) |
|
1671 |
{ |
|
1672 |
NKern::Lock(); |
|
1673 |
TBool r=aThread->Resume(); |
|
1674 |
NKern::Unlock(); |
|
1675 |
return r; |
|
1676 |
} |
|
1677 |
||
1678 |
||
1679 |
/** Resumes the execution of a thread and signals a mutex. |
|
1680 |
||
1681 |
This function is intended to be used by the EPOC layer and personality layers. |
|
1682 |
Do not use this function directly on a Symbian OS thread - use Kern::ThreadResume(). |
|
1683 |
||
1684 |
This function resumes the thread once. If the thread was suspended more than once |
|
1685 |
the thread will remain suspended. |
|
1686 |
If the thread is in a critical section, this function will decrease the number of |
|
1687 |
deferred suspensions. |
|
1688 |
||
1689 |
@param aThread Thread to be resumed. |
|
1690 |
@param aMutex Mutex to be signalled. If NULL, the scheduler's mutex will be signalled. |
|
1691 |
||
1692 |
@return TRUE, if the thread had changed the state from suspended to non-suspended; |
|
1693 |
FALSE, otherwise. |
|
1694 |
||
1695 |
@see Kern::ThreadResume() |
|
1696 |
*/ |
|
1697 |
EXPORT_C TBool NKern::ThreadResume(NThread* aThread, NFastMutex* aMutex) |
|
1698 |
{ |
|
1699 |
if (!aMutex) |
|
1700 |
aMutex=&TheScheduler.iLock; |
|
1701 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T NRsm + FM %M",aThread,aMutex)); |
|
1702 |
NKern::Lock(); |
|
1703 |
TBool r=aThread->Resume(); |
|
1704 |
aMutex->Signal(); |
|
1705 |
NKern::Unlock(); |
|
1706 |
return r; |
|
1707 |
} |
|
1708 |
||
1709 |
||
1710 |
/** Forces the execution of a thread to be resumed. |
|
1711 |
||
1712 |
This function is intended to be used by the EPOC layer and personality layers. |
|
1713 |
Do not use this function directly on a Symbian OS thread - use Kern::ThreadResume(). |
|
1714 |
||
1715 |
This function cancels all suspensions on a thread. |
|
1716 |
||
1717 |
@param aThread Thread to be resumed. |
|
1718 |
||
1719 |
@return TRUE, if the thread had changed the state from suspended to non-suspended; |
|
1720 |
FALSE, otherwise. |
|
1721 |
||
1722 |
@see Kern::ThreadResume() |
|
1723 |
*/ |
|
1724 |
EXPORT_C TBool NKern::ThreadForceResume(NThread* aThread) |
|
1725 |
{ |
|
1726 |
NKern::Lock(); |
|
1727 |
TBool r=aThread->ForceResume(); |
|
1728 |
NKern::Unlock(); |
|
1729 |
return r; |
|
1730 |
} |
|
1731 |
||
1732 |
||
1733 |
/** Forces the execution of a thread to be resumed and signals a mutex. |
|
1734 |
||
1735 |
This function is intended to be used by the EPOC layer and personality layers. |
|
1736 |
Do not use this function directly on a Symbian OS thread - use Kern::ThreadResume(). |
|
1737 |
||
1738 |
This function cancels all suspensions on a thread. |
|
1739 |
||
1740 |
@param aThread Thread to be resumed. |
|
1741 |
@param aMutex Mutex to be signalled. If NULL, the scheduler's mutex will be signalled. |
|
1742 |
||
1743 |
@return TRUE, if the thread had changed the state from suspended to non-suspended; |
|
1744 |
FALSE, otherwise. |
|
1745 |
||
1746 |
@see Kern::ThreadResume() |
|
1747 |
*/ |
|
1748 |
EXPORT_C TBool NKern::ThreadForceResume(NThread* aThread, NFastMutex* aMutex) |
|
1749 |
{ |
|
1750 |
if (!aMutex) |
|
1751 |
aMutex=&TheScheduler.iLock; |
|
1752 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T NFRsm + FM %M",aThread,aMutex)); |
|
1753 |
NKern::Lock(); |
|
1754 |
TBool r=aThread->ForceResume(); |
|
1755 |
aMutex->Signal(); |
|
1756 |
NKern::Unlock(); |
|
1757 |
return r; |
|
1758 |
} |
|
1759 |
||
1760 |
||
1761 |
/** Awakens a nanothread. |
|
1762 |
||
1763 |
This function is used to implement synchronisation primitives in the EPOC |
|
1764 |
kernel (e.g. DMutex and DSemaphore) and in personality layers. It is not |
|
1765 |
intended to be used directly by device drivers. |
|
1766 |
||
1767 |
If the nanothread is waiting on a fast semaphore, waiting for a DFC, or is |
|
1768 |
blocked in a call to NKern::Block, it is awakened and put back on the ready |
|
1769 |
list. Otherwise, the thread state is unchanged. In particular, nothing |
|
1770 |
happens if the nanothread has been explicitly suspended. |
|
1771 |
||
1772 |
@param aThread Thread to release. |
|
1773 |
@param aReturnValue Value returned by NKern::Block if the thread was blocked. |
|
1774 |
||
1775 |
@see NKern::Block() |
|
1776 |
||
1777 |
@pre Interrupts must be enabled. |
|
1778 |
@pre Do not call from an ISR |
|
1779 |
*/ |
|
1780 |
EXPORT_C void NKern::ThreadRelease(NThread* aThread, TInt aReturnValue) |
|
1781 |
{ |
|
1782 |
CHECK_PRECONDITIONS(MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR,"NKern::ThreadRelease(NThread*, TInt)"); |
|
1783 |
NKern::Lock(); |
|
1784 |
aThread->Release(aReturnValue,0); |
|
1785 |
NKern::Unlock(); |
|
1786 |
} |
|
1787 |
||
1788 |
||
1789 |
/** Atomically awakens a nanothread and signals a fast mutex. |
|
1790 |
||
1791 |
This function is used to implement synchronisation primitives in the EPOC |
|
1792 |
kernel (e.g. DMutex and DSemaphore) and in personality layers. It is not |
|
1793 |
intended to be used directly by device drivers. |
|
1794 |
||
1795 |
@param aThread Thread to release. |
|
1796 |
@param aReturnValue Value returned by NKern::Block if the thread was blocked. |
|
1797 |
@param aMutex Fast mutex to signal. If NULL, the system lock is signalled. |
|
1798 |
||
1799 |
@see NKern::ThreadRelease(NThread*, TInt) |
|
1800 |
@see NKern::Block() |
|
1801 |
||
1802 |
@pre Call in a thread context. |
|
1803 |
@pre Interrupts must be enabled. |
|
1804 |
@pre Kernel must be unlocked. |
|
1805 |
@pre Specified mutex must be held |
|
1806 |
*/ |
|
1807 |
EXPORT_C void NKern::ThreadRelease(NThread* aThread, TInt aReturnValue, NFastMutex* aMutex) |
|
1808 |
{ |
|
1809 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC,"NKern::ThreadRelease(NThread*,TInt,NFastMutex*)"); |
|
1810 |
if (!aMutex) |
|
1811 |
aMutex=&TheScheduler.iLock; |
|
1812 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T NRel ret %d + FM %M",aThread,aReturnValue,aMutex)); |
|
1813 |
NKern::Lock(); |
|
1814 |
aThread->Release(aReturnValue,0); |
|
1815 |
aMutex->Signal(); |
|
1816 |
NKern::Unlock(); |
|
1817 |
} |
|
1818 |
||
1819 |
||
1820 |
/** Changes the priority of a thread. |
|
1821 |
||
1822 |
This function is intended to be used by the EPOC layer and personality layers. |
|
1823 |
Do not use this function directly on a Symbian OS thread - use Kern::ThreadSetPriority(). |
|
1824 |
||
1825 |
@param aThread Thread to receive the new priority. |
|
1826 |
@param aPriority New priority for aThread. |
|
1827 |
||
1828 |
@see Kern::SetThreadPriority() |
|
1829 |
*/ |
|
1830 |
EXPORT_C void NKern::ThreadSetPriority(NThread* aThread, TInt aPriority) |
|
1831 |
{ |
|
1832 |
NKern::Lock(); |
|
1833 |
aThread->SetPriority(aPriority); |
|
1834 |
NKern::Unlock(); |
|
1835 |
} |
|
1836 |
||
1837 |
||
1838 |
/** Changes the priority of a thread and signals a mutex. |
|
1839 |
||
1840 |
This function is intended to be used by the EPOC layer and personality layers. |
|
1841 |
Do not use this function directly on a Symbian OS thread - use Kern::ThreadSetPriority(). |
|
1842 |
||
1843 |
@param aThread Thread to receive the new priority. |
|
1844 |
@param aPriority New priority for aThread. |
|
1845 |
@param aMutex Mutex to be signalled. If NULL, the scheduler's mutex will be signalled. |
|
1846 |
||
1847 |
@see Kern::SetThreadPriority() |
|
1848 |
*/ |
|
1849 |
EXPORT_C void NKern::ThreadSetPriority(NThread* aThread, TInt aPriority, NFastMutex* aMutex) |
|
1850 |
{ |
|
1851 |
if (!aMutex) |
|
1852 |
aMutex=&TheScheduler.iLock; |
|
1853 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T NSPri->%d + FM %M",aThread,aPriority,aMutex)); |
|
1854 |
NKern::Lock(); |
|
1855 |
aThread->SetPriority(aPriority); |
|
1856 |
aMutex->Signal(); |
|
1857 |
NKern::Unlock(); |
|
1858 |
} |
|
1859 |
||
1860 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1861 |
/** Changes the nominal priority of a thread. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1862 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1863 |
This function is intended to be used by the EPOC layer and personality layers. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1864 |
Do not use this function directly on a Symbian OS thread - use Kern::ThreadSetPriority(). |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1865 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1866 |
@param aThread Thread to receive the new priority. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1867 |
@param aPriority New inherited priority for aThread. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1868 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1869 |
@see Kern::SetThreadPriority() |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1870 |
*/ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1871 |
void NKern::ThreadSetNominalPriority(NThread* aThread, TInt aPriority) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1872 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1873 |
NKern::Lock(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1874 |
aThread->SetNominalPriority(aPriority); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1875 |
NKern::Unlock(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1876 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1877 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1878 |
|
0 | 1879 |
/** Atomically signals the request semaphore of a nanothread and a fast mutex. |
1880 |
||
1881 |
This function is intended to be used by the EPOC layer and personality |
|
1882 |
layers. Device drivers should use Kern::RequestComplete instead. |
|
1883 |
||
1884 |
@param aThread Nanothread to signal. Must be non NULL. |
|
1885 |
@param aMutex Fast mutex to signal. If NULL, the system lock is signaled. |
|
1886 |
||
1887 |
@see Kern::RequestComplete() |
|
1888 |
||
1889 |
@pre Call in a thread context. |
|
1890 |
@pre Interrupts must be enabled. |
|
1891 |
@pre Kernel must be unlocked. |
|
1892 |
@pre Specified mutex must be held |
|
1893 |
*/ |
|
1894 |
EXPORT_C void NKern::ThreadRequestSignal(NThread* aThread, NFastMutex* aMutex) |
|
1895 |
{ |
|
1896 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC,"NKern::ThreadRequestSignal(NThread*,NFastMutex*)"); |
|
1897 |
if (!aMutex) |
|
1898 |
aMutex = &TheScheduler.iLock; |
|
1899 |
NKern::Lock(); |
|
1900 |
aThread->iRequestSemaphore.Signal(); |
|
1901 |
aMutex->Signal(); |
|
1902 |
NKern::Unlock(); |
|
1903 |
} |
|
1904 |
||
1905 |
||
1906 |
/** Kills a nanothread. |
|
1907 |
||
1908 |
This function is intended to be used by the EPOC layer and personality layers. |
|
1909 |
Do not use this function directly on a Symbian OS thread - use Kern::ThreadKill(). |
|
1910 |
||
1911 |
This function does not return if the current thread is killed. |
|
1912 |
This function is asynchronous (i.e. the thread to kill may still be alive when the call returns). |
|
1913 |
||
1914 |
@param aThread Thread to kill. Must be non NULL. |
|
1915 |
||
1916 |
@pre If acting on calling thread, calling thread must not be in a |
|
1917 |
critical section |
|
1918 |
@pre Thread must not already be exiting. |
|
1919 |
||
1920 |
@see Kern::ThreadKill() |
|
1921 |
*/ |
|
1922 |
EXPORT_C void NKern::ThreadKill(NThread* aThread) |
|
1923 |
{ |
|
1924 |
NKern::Lock(); |
|
1925 |
aThread->Kill(); |
|
1926 |
NKern::Unlock(); |
|
1927 |
} |
|
1928 |
||
1929 |
||
1930 |
/** Atomically kills a nanothread and signals a fast mutex. |
|
1931 |
||
1932 |
This function is intended to be used by the EPOC layer and personality layers. |
|
1933 |
Do not use this function directly on a Symbian OS thread - use Kern::ThreadKill(). |
|
1934 |
||
1935 |
@param aThread Thread to kill. Must be non NULL. |
|
1936 |
@param aMutex Fast mutex to signal. If NULL, the system lock is signalled. |
|
1937 |
||
1938 |
@pre If acting on calling thread, calling thread must not be in a |
|
1939 |
critical section |
|
1940 |
@pre Thread must not already be exiting. |
|
1941 |
||
1942 |
@see NKern::ThreadKill(NThread*) |
|
1943 |
*/ |
|
1944 |
EXPORT_C void NKern::ThreadKill(NThread* aThread, NFastMutex* aMutex) |
|
1945 |
{ |
|
1946 |
if (!aMutex) |
|
1947 |
aMutex = &TheScheduler.iLock; |
|
1948 |
NThreadBase* pC = NKern::LockC(); |
|
1949 |
if (aThread==pC) |
|
1950 |
{ |
|
1951 |
__NK_ASSERT_DEBUG(pC->iCsCount==0); // Make sure thread isn't in critical section |
|
1952 |
__NK_ASSERT_ALWAYS(aMutex->HeldByCurrentThread()); |
|
1953 |
pC->AcqSLock(); |
|
1954 |
aThread->iCsFunction = NThreadBase::ECSExitPending; |
|
1955 |
pC->RelSLock(); |
|
1956 |
aMutex->iHoldingThread = (NThreadBase*)(TLinAddr(aThread) | 1); |
|
1957 |
aMutex->Signal(); // this will make us exit |
|
1958 |
FAULT(); // should never get here |
|
1959 |
} |
|
1960 |
else |
|
1961 |
{ |
|
1962 |
aThread->Kill(); |
|
1963 |
aMutex->Signal(); |
|
1964 |
} |
|
1965 |
NKern::Unlock(); |
|
1966 |
} |
|
1967 |
||
1968 |
||
1969 |
/** Enters thread critical section. |
|
1970 |
||
1971 |
This function can safely be used in device drivers. |
|
1972 |
||
1973 |
The current thread will enter its critical section. While in critical section |
|
1974 |
the thread cannot be suspended or killed. Any suspension or kill will be deferred |
|
1975 |
until the thread leaves the critical section. |
|
1976 |
Some API explicitly require threads to be in critical section before calling that |
|
1977 |
API. |
|
1978 |
Only User threads need to call this function as the concept of thread critical |
|
1979 |
section applies to User threads only. |
|
1980 |
||
1981 |
@pre Call in a thread context. |
|
1982 |
@pre Kernel must be unlocked. |
|
1983 |
*/ |
|
1984 |
EXPORT_C void NKern::ThreadEnterCS() |
|
1985 |
{ |
|
1986 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_NOT_ISR|MASK_NOT_IDFC,"NKern::ThreadEnterCS"); |
|
1987 |
NThreadBase* pC = NKern::CurrentThread(); |
|
1988 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T NEntCS",pC)); |
|
1989 |
__NK_ASSERT_DEBUG(pC->iCsCount>=0); |
|
1990 |
++pC->iCsCount; |
|
1991 |
} |
|
1992 |
||
1993 |
NThread* NKern::_ThreadEnterCS() |
|
1994 |
{ |
|
1995 |
NThreadBase* pC = NKern::CurrentThread(); |
|
1996 |
__NK_ASSERT_DEBUG(pC->iCsCount>=0); |
|
1997 |
++pC->iCsCount; |
|
1998 |
return (NThread*)pC; |
|
1999 |
} |
|
2000 |
||
2001 |
||
2002 |
/** Leaves thread critical section. |
|
2003 |
||
2004 |
This function can safely be used in device drivers. |
|
2005 |
||
2006 |
The current thread will leave its critical section. If the thread was suspended/killed |
|
2007 |
while in critical section, the thread will be suspended/killed after leaving the |
|
2008 |
critical section by calling this function. |
|
2009 |
Only User threads need to call this function as the concept of thread critical |
|
2010 |
section applies to User threads only. |
|
2011 |
||
2012 |
@pre Call in a thread context. |
|
2013 |
@pre Kernel must be unlocked. |
|
2014 |
*/ |
|
2015 |
EXPORT_C void NKern::ThreadLeaveCS() |
|
2016 |
{ |
|
2017 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_NOT_ISR|MASK_NOT_IDFC,"NKern::ThreadLeaveCS"); |
|
2018 |
NThreadBase* pC = NKern::LockC(); |
|
2019 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T NLvCS",pC)); |
|
2020 |
pC->AcqSLock(); |
|
2021 |
__NK_ASSERT_DEBUG(pC->iCsCount>0); |
|
2022 |
if (--pC->iCsCount==0 && pC->iCsFunction!=0) |
|
2023 |
{ |
|
2024 |
NFastMutex* m = HeldFastMutex(); |
|
2025 |
if (m) |
|
2026 |
m->iHoldingThread = (NThreadBase*)(TLinAddr(pC) | 1); |
|
2027 |
else |
|
2028 |
pC->DoCsFunctionT(); |
|
2029 |
} |
|
2030 |
pC->RelSLock(); |
|
2031 |
NKern::Unlock(); |
|
2032 |
} |
|
2033 |
||
2034 |
void NKern::_ThreadLeaveCS() |
|
2035 |
{ |
|
2036 |
NThreadBase* pC = NKern::LockC(); |
|
2037 |
pC->AcqSLock(); |
|
2038 |
__NK_ASSERT_DEBUG(pC->iCsCount>0); |
|
2039 |
if (--pC->iCsCount==0 && pC->iCsFunction!=0) |
|
2040 |
{ |
|
2041 |
NFastMutex* m = HeldFastMutex(); |
|
2042 |
if (m) |
|
2043 |
m->iHoldingThread = (NThreadBase*)(TLinAddr(pC) | 1); |
|
2044 |
else |
|
2045 |
pC->DoCsFunctionT(); |
|
2046 |
} |
|
2047 |
pC->RelSLock(); |
|
2048 |
NKern::Unlock(); |
|
2049 |
} |
|
2050 |
||
2051 |
/** Freeze the CPU of the current thread |
|
2052 |
||
2053 |
After this the current thread will not migrate to another processor |
|
2054 |
||
2055 |
@return A cookie to be passed to NKern::EndFreezeCpu() to allow nesting |
|
2056 |
*/ |
|
2057 |
EXPORT_C TInt NKern::FreezeCpu() |
|
2058 |
{ |
|
2059 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_NOT_ISR|MASK_NOT_IDFC,"NKern::FreezeCpu"); |
|
2060 |
NKern::Lock(); |
|
2061 |
TSubScheduler& ss = SubScheduler(); |
|
2062 |
NThreadBase* pC = ss.iCurrentThread; |
|
2063 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T NFrzCpu",pC)); |
|
2064 |
if (pC->iFreezeCpu) |
|
2065 |
{ |
|
2066 |
NKern::Unlock(); |
|
2067 |
return 1; |
|
2068 |
} |
|
2069 |
pC->iFreezeCpu = 1; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2070 |
__e32_atomic_add_rlx32(&ss.iDeferShutdown, 1); |
0 | 2071 |
if (pC->iParent != pC) |
2072 |
{ |
|
2073 |
pC->AcqSLock(); |
|
2074 |
++pC->iParent->iFreezeCpu; |
|
2075 |
pC->RelSLock(); |
|
2076 |
} |
|
2077 |
NKern::Unlock(); |
|
2078 |
return 0; |
|
2079 |
} |
|
2080 |
||
2081 |
||
2082 |
/** Unfreeze the current thread's CPU |
|
2083 |
||
2084 |
After this the current thread will again be eligible to migrate to another processor |
|
2085 |
||
2086 |
@param aCookie the value returned by NKern::FreezeCpu() |
|
2087 |
*/ |
|
2088 |
EXPORT_C void NKern::EndFreezeCpu(TInt aCookie) |
|
2089 |
{ |
|
2090 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_NOT_ISR|MASK_NOT_IDFC,"NKern::EndFreezeCpu"); |
|
2091 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("%T NEndFrz %d",NKern::CurrentThread(),aCookie)); |
|
2092 |
if (aCookie) |
|
2093 |
return; |
|
2094 |
NKern::Lock(); |
|
2095 |
TSubScheduler& ss = SubScheduler(); |
|
2096 |
NThreadBase* pC = ss.iCurrentThread; |
|
2097 |
if (pC->iFreezeCpu) |
|
2098 |
{ |
|
2099 |
pC->iFreezeCpu = 0; |
|
2100 |
mb(); |
|
2101 |
if (pC->iParent != pC) |
|
2102 |
{ |
|
2103 |
pC->AcqSLock(); |
|
2104 |
if (!--pC->iParent->iFreezeCpu && pC->iParent->iCpuChange) |
|
2105 |
RescheduleNeeded(); |
|
2106 |
pC->RelSLock(); |
|
2107 |
} |
|
2108 |
else if (pC->iCpuChange) // deferred CPU change? |
|
2109 |
RescheduleNeeded(); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2110 |
__e32_atomic_add_rlx32(&ss.iDeferShutdown, TUint32(-1)); |
0 | 2111 |
} |
2112 |
NKern::Unlock(); |
|
2113 |
} |
|
2114 |
||
2115 |
||
2116 |
/** Change the CPU affinity of a thread |
|
2117 |
||
2118 |
@pre Call in a thread context. |
|
2119 |
||
2120 |
@param The new CPU affinity mask |
|
2121 |
@return The old affinity mask |
|
2122 |
*/ |
|
2123 |
EXPORT_C TUint32 NKern::ThreadSetCpuAffinity(NThread* aThread, TUint32 aAffinity) |
|
2124 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2125 |
aThread->LAcqSLock(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2126 |
TUint32 r = aThread->SetCpuAffinityT(aAffinity); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2127 |
aThread->RelSLockU(); |
0 | 2128 |
return r; |
2129 |
} |
|
2130 |
||
2131 |
||
2132 |
/** Modify a thread's timeslice |
|
2133 |
||
2134 |
@pre Call in a thread context. |
|
2135 |
||
2136 |
@param aTimeslice The new timeslice value |
|
2137 |
*/ |
|
2138 |
EXPORT_C void NKern::ThreadSetTimeslice(NThread* aThread, TInt aTimeslice) |
|
2139 |
{ |
|
2140 |
NKern::Lock(); |
|
2141 |
aThread->AcqSLock(); |
|
2142 |
if (aThread->iTimeslice == aThread->iTime || aTimeslice<0) |
|
2143 |
aThread->iTime = aTimeslice; |
|
2144 |
aThread->iTimeslice = aTimeslice; |
|
2145 |
aThread->RelSLock(); |
|
2146 |
NKern::Unlock(); |
|
2147 |
} |
|
2148 |
||
2149 |
||
2150 |
/** Blocks current nanothread. |
|
2151 |
||
2152 |
This function is used to implement synchronisation primitives in the EPOC |
|
2153 |
layer and in personality layers. It is not intended to be used directly by |
|
2154 |
device drivers. |
|
2155 |
||
2156 |
@param aTimeout If greater than 0, the nanothread will be blocked for at most |
|
2157 |
aTimeout microseconds. |
|
2158 |
@param aMode Bitmask whose possible values are documented in TBlockMode. |
|
2159 |
@param aMutex Fast mutex to operate on. If NULL, the system lock is used. |
|
2160 |
||
2161 |
@see NKern::ThreadRelease() |
|
2162 |
@see TBlockMode |
|
2163 |
||
2164 |
@pre Call in a thread context. |
|
2165 |
@pre Interrupts must be enabled. |
|
2166 |
@pre Kernel must be unlocked. |
|
2167 |
@pre Specified mutex must be held |
|
2168 |
*/ |
|
2169 |
EXPORT_C TInt NKern::Block(TUint32 aTimeout, TUint aMode, NFastMutex* aMutex) |
|
2170 |
{ |
|
2171 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC,"NKern::Block(TUint32,TUint,NFastMutex*)"); |
|
2172 |
if (!aMutex) |
|
2173 |
aMutex = &TheScheduler.iLock; |
|
2174 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NKern::Block time %d mode %d FM %M",aTimeout,aMode,aMutex)); |
|
2175 |
if (aMode & EEnterCS) |
|
2176 |
NKern::_ThreadEnterCS(); // NOTE: MUST DO THIS BEFORE CALLING NKern::Lock() |
|
2177 |
NThreadBase* pC = NKern::LockC(); |
|
2178 |
TUint flags = (aMode & NKern::EObstruct) ? NThreadWaitState::EWtStObstructed : 0; |
|
2179 |
pC->iWaitState.SetUpWait(NThreadBase::EWaitBlocked, flags, 0, aTimeout); |
|
2180 |
if (aMode & ERelease) |
|
2181 |
aMutex->Signal(); |
|
2182 |
RescheduleNeeded(); |
|
2183 |
NKern::Unlock(); // thread blocks here |
|
2184 |
TInt r = pC->iWaitState.iWtC.iRetVal; // sample here since it will be overwritten if we block on the fast mutex |
|
2185 |
if (aMode & EClaim) |
|
2186 |
FMWait(aMutex); |
|
2187 |
return r; |
|
2188 |
} |
|
2189 |
||
2190 |
||
2191 |
/** |
|
2192 |
@pre Call in a thread context. |
|
2193 |
@pre Interrupts must be enabled. |
|
2194 |
@pre Kernel must be unlocked. |
|
2195 |
@pre No fast mutex can be held |
|
2196 |
*/ |
|
2197 |
/** @see NKern::Block(TUint32, TUint, NFastMutex*) */ |
|
2198 |
EXPORT_C TInt NKern::Block(TUint32 aTimeout, TUint aMode) |
|
2199 |
{ |
|
2200 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"NKern::Block(TUint32,TUint)"); |
|
2201 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NKern::Block time %d mode %d",aTimeout,aMode)); |
|
2202 |
if (aMode & EEnterCS) |
|
2203 |
NKern::_ThreadEnterCS(); // NOTE: MUST DO THIS BEFORE CALLING NKern::Lock() |
|
2204 |
NThreadBase* pC = NKern::LockC(); |
|
2205 |
TUint flags = (aMode & NKern::EObstruct) ? NThreadWaitState::EWtStObstructed : 0; |
|
2206 |
pC->iWaitState.SetUpWait(NThreadBase::EWaitBlocked, flags, 0, aTimeout); |
|
2207 |
RescheduleNeeded(); |
|
2208 |
NKern::Unlock(); // thread blocks here |
|
2209 |
return pC->iWaitState.iWtC.iRetVal; |
|
2210 |
} |
|
2211 |
||
2212 |
||
2213 |
||
2214 |
||
2215 |
/** |
|
2216 |
Places the current nanothread into a wait state on an externally |
|
2217 |
defined wait object. |
|
2218 |
||
2219 |
For use by RTOS personality layers. |
|
2220 |
Do not use this function directly on a Symbian OS thread. |
|
2221 |
||
2222 |
Since the kernel is locked on entry, any reschedule will be deferred until |
|
2223 |
it is unlocked. The thread should be added to any necessary wait queue after |
|
2224 |
a call to this function, since this function removes it from the ready list. |
|
2225 |
The thread's wait timer is started if aTimeout is nonzero. |
|
2226 |
The thread's NState and wait object are updated. |
|
2227 |
||
2228 |
Call NThreadBase::Release() when the wait condition is resolved. |
|
2229 |
||
2230 |
@param aTimeout The maximum time for which the thread should block, in nanokernel timer ticks. |
|
2231 |
A zero value means wait forever. |
|
2232 |
If the thread is still blocked when the timeout expires, |
|
2233 |
then the timeout state handler will be called. |
|
2234 |
@param aState The nanokernel thread state (N-State) value to be set. |
|
2235 |
This state corresponds to the externally defined wait object. |
|
2236 |
This value will be written into the member NThreadBase::iNState. |
|
2237 |
@param aWaitObj A pointer to an externally defined wait object. |
|
2238 |
This value will be written into the member NThreadBase::iWaitObj. |
|
2239 |
||
2240 |
@pre Kernel must be locked. |
|
2241 |
@pre Call in a thread context. |
|
2242 |
||
2243 |
@post Kernel is locked. |
|
2244 |
||
2245 |
@see NThreadBase::Release() |
|
2246 |
*/ |
|
2247 |
EXPORT_C void NKern::NanoBlock(TUint32 aTimeout, TUint aState, TAny* aWaitObj) |
|
2248 |
{ |
|
2249 |
CHECK_PRECONDITIONS(MASK_KERNEL_LOCKED|MASK_NOT_ISR|MASK_NOT_IDFC,"NKern::NanoBlock"); |
|
2250 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NanoBlock time %d state %d obj %08x", aTimeout, aState, aWaitObj)); |
|
2251 |
NThreadBase* pC = NCurrentThreadL(); |
|
2252 |
pC->iWaitState.SetUpWait(aState, aState>>8, aWaitObj, aTimeout); |
|
2253 |
RescheduleNeeded(); |
|
2254 |
} |
|
2255 |
||
2256 |
||
2257 |
||
2258 |
||
2259 |
EXPORT_C void NKern::Sleep(TUint32 aTime) |
|
2260 |
/** |
|
2261 |
Puts the current nanothread to sleep for the specified duration. |
|
2262 |
||
2263 |
It can be called from Symbian OS threads. |
|
2264 |
||
2265 |
@param aTime sleep time in nanokernel timer ticks. |
|
2266 |
||
2267 |
@pre No fast mutex can be held. |
|
2268 |
@pre Kernel must be unlocked. |
|
2269 |
@pre Call in a thread context. |
|
2270 |
@pre Interrupts must be enabled. |
|
2271 |
*/ |
|
2272 |
{ |
|
2273 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"NKern::Sleep"); |
|
2274 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NSlp %d",aTime)); |
|
2275 |
NThreadBase* pC = NKern::LockC(); |
|
2276 |
pC->iWaitState.SetUpWait(NThreadBase::EWaitSleep, 0, 0, aTime); |
|
2277 |
RescheduleNeeded(); |
|
2278 |
NKern::Unlock(); |
|
2279 |
} |
|
2280 |
||
2281 |
||
2282 |
/** Terminates the current nanothread. |
|
2283 |
||
2284 |
Calls to this function never return. |
|
2285 |
||
2286 |
For use by RTOS personality layers. |
|
2287 |
Do not use this function directly on a Symbian OS thread. |
|
2288 |
||
2289 |
@pre Call in a thread context. |
|
2290 |
@pre Interrupts must be enabled. |
|
2291 |
@pre Kernel must be unlocked. |
|
2292 |
*/ |
|
2293 |
EXPORT_C void NKern::Exit() |
|
2294 |
{ |
|
2295 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC,"NKern::Exit"); |
|
2296 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NExit")); |
|
2297 |
NKern::LockC()->Exit(); // this won't return |
|
2298 |
FAULT(); |
|
2299 |
} |
|
2300 |
||
2301 |
||
2302 |
/** Terminates the current nanothread at the next possible point. |
|
2303 |
||
2304 |
If the calling thread is not currently in a critical section and does not |
|
2305 |
currently hold a fast mutex, it exits immediately and this function does |
|
2306 |
not return. On the other hand if the thread is in a critical section or |
|
2307 |
holds a fast mutex the thread continues executing but it will exit as soon |
|
2308 |
as it leaves the critical section and/or releases the fast mutex. |
|
2309 |
||
2310 |
@pre Call in a thread context. |
|
2311 |
@pre Interrupts must be enabled. |
|
2312 |
@pre Kernel must be unlocked. |
|
2313 |
*/ |
|
2314 |
EXPORT_C void NKern::DeferredExit() |
|
2315 |
{ |
|
2316 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC,"NKern::DeferredExit"); |
|
2317 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NDefExit")); |
|
2318 |
NFastMutex* m = HeldFastMutex(); |
|
2319 |
NThreadBase* pC = NKern::LockC(); |
|
2320 |
if (!m && !pC->iCsCount) |
|
2321 |
pC->Exit(); // this won't return |
|
2322 |
pC->AcqSLock(); |
|
2323 |
if (pC->iCsFunction >= 0) // don't touch it if we are already exiting |
|
2324 |
pC->iCsFunction = NThreadBase::ECSExitPending; |
|
2325 |
pC->RelSLock(); |
|
2326 |
if (m && !pC->iCsCount) |
|
2327 |
m->iHoldingThread = (NThreadBase*)(TLinAddr(pC) | 1); |
|
2328 |
NKern::Unlock(); |
|
2329 |
} |
|
2330 |
||
2331 |
||
2332 |
/** Prematurely terminates the current thread's timeslice |
|
2333 |
||
2334 |
@pre Kernel must be unlocked. |
|
2335 |
@pre Call in a thread context. |
|
2336 |
||
2337 |
@post Kernel is unlocked. |
|
2338 |
*/ |
|
2339 |
EXPORT_C void NKern::YieldTimeslice() |
|
2340 |
{ |
|
2341 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_NOT_ISR|MASK_NOT_IDFC,"NKern::YieldTimeslice"); |
|
2342 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NKern::YieldTimeslice")); |
|
2343 |
NThreadBase* t = NKern::LockC(); |
|
2344 |
t->iTime = 0; |
|
2345 |
mb(); |
|
2346 |
if (t->iNext!=t || t->iParent->iNext!=t->iParent) |
|
2347 |
RescheduleNeeded(); |
|
2348 |
NKern::Unlock(); |
|
2349 |
} |
|
2350 |
||
2351 |
||
2352 |
/** Returns the number of CPUs available to Symbian OS |
|
2353 |
||
2354 |
@return the number of CPUs |
|
2355 |
||
2356 |
@pre Call in any context. |
|
2357 |
*/ |
|
2358 |
EXPORT_C TInt NKern::NumberOfCpus() |
|
2359 |
{ |
|
2360 |
return TheScheduler.iNumCpus; |
|
2361 |
} |
|
2362 |
||
2363 |
||
2364 |
/** Rotates the specified CPU ready list for threads at the specified priority. |
|
2365 |
||
2366 |
For use by RTOS personality layers to allow external control of round-robin |
|
2367 |
scheduling. Not intended for direct use by device drivers. |
|
2368 |
||
2369 |
@param aPriority = priority at which threads should be rotated. |
|
2370 |
-1 means use calling thread's priority. |
|
2371 |
@param aCpu CPU to act on |
|
2372 |
||
2373 |
@pre Kernel must be unlocked. |
|
2374 |
@pre Call in a thread context. |
|
2375 |
||
2376 |
@post Kernel is unlocked. |
|
2377 |
*/ |
|
2378 |
||
2379 |
EXPORT_C void NKern::RotateReadyList(TInt aPriority, TInt aCpu) |
|
2380 |
{ |
|
2381 |
// CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_NOT_ISR|MASK_NOT_IDFC,"NKern::RotateReadyList"); |
|
2382 |
// __KTRACE_OPT(KNKERN,DEBUGPRINT("NKern::RotateReadyList %d",aPriority)); |
|
2383 |
// if (aPriority<0 || aPriority>=KNumPriorities) |
|
2384 |
// aPriority=NKern::CurrentThread()->iPriority; |
|
2385 |
// NKern::Lock(); |
|
2386 |
// TheScheduler.RotateReadyList(aPriority); |
|
2387 |
// NKern::Unlock(); |
|
2388 |
} |
|
2389 |
||
2390 |
||
2391 |
/** Rotates the ready list for threads at the specified priority. |
|
2392 |
||
2393 |
For use by RTOS personality layers to allow external control of round-robin |
|
2394 |
scheduling. Not intended for direct use by device drivers. |
|
2395 |
||
2396 |
@param aPriority = priority at which threads should be rotated. |
|
2397 |
-1 means use calling thread's priority. |
|
2398 |
||
2399 |
@pre Kernel must be unlocked. |
|
2400 |
@pre Call in a thread context. |
|
2401 |
||
2402 |
@post Kernel is unlocked. |
|
2403 |
*/ |
|
2404 |
EXPORT_C void NKern::RotateReadyList(TInt aPriority) |
|
2405 |
{ |
|
2406 |
RotateReadyList(aPriority, -1); |
|
2407 |
} |
|
2408 |
||
2409 |
||
2410 |
/** Returns a pointer to the thread group to which the current thread belongs, |
|
2411 |
if any. Returns NULL if current thread is a standalone thread. |
|
2412 |
||
2413 |
@pre Call in a thread context. |
|
2414 |
*/ |
|
2415 |
EXPORT_C NThreadGroup* NKern::CurrentGroup() |
|
2416 |
{ |
|
2417 |
NThreadBase* pC = NKern::CurrentThread(); |
|
2418 |
return (pC->iParent == pC) ? (NThreadGroup*)0 : (NThreadGroup*)pC->iParent; |
|
2419 |
} |
|
2420 |
||
2421 |
||
2422 |
/** Detaches the current thread from the group to which it currently belongs, |
|
2423 |
if any. Returns a pointer to the group (NULL if none). |
|
2424 |
||
2425 |
@pre Call in a thread context. |
|
2426 |
@pre Interrupts enabled |
|
2427 |
@pre Kernel unlocked |
|
2428 |
*/ |
|
2429 |
EXPORT_C NThreadGroup* NKern::LeaveGroup() |
|
2430 |
{ |
|
2431 |
CHECK_PRECONDITIONS(MASK_NOT_ISR|MASK_NOT_IDFC|MASK_INTERRUPTS_ENABLED|MASK_KERNEL_UNLOCKED, "NKern::LeaveGroup"); |
|
2432 |
NKern::Lock(); |
|
2433 |
TSubScheduler& ss = SubScheduler(); |
|
2434 |
NThreadBase* pC = ss.iCurrentThread; |
|
2435 |
pC->iNewParent = 0; // cancel any pending Join |
|
2436 |
NThreadGroup* g = (pC->iParent == pC) ? (NThreadGroup*)0 : (NThreadGroup*)pC->iParent; |
|
2437 |
TBool make_group_ready = FALSE; |
|
2438 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NLeaveGroup %T (%G)",pC,g)); |
|
2439 |
if (g) |
|
2440 |
{ |
|
2441 |
while (!pC->TiedEventLeaveInterlock()) |
|
2442 |
{ |
|
2443 |
TInt irq = NKern::DisableAllInterrupts(); |
|
2444 |
ss.QueueDfcs(); |
|
2445 |
NKern::RestoreInterrupts(irq); |
|
2446 |
} |
|
2447 |
pC->AcqSLock(); |
|
2448 |
ss.iReadyListLock.LockOnly(); |
|
2449 |
pC->UnReadyT(); |
|
2450 |
pC->iParent = pC; |
|
2451 |
g->iCurrent = 0; // since current thread is no longer in g |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2452 |
TUint64 now = NKern::Timestamp(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2453 |
g->iLastRunTime.i64 = now; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2454 |
g->iTotalCpuTime.i64 += (now - g->iLastStartTime.i64); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2455 |
if (--g->iActiveState == 0) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2456 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2457 |
// group no longer active |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2458 |
g->iTotalActiveTime.i64 += (now - g->iLastActivationTime.i64); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2459 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2460 |
ss.SSAddEntryHead(pC); |
0 | 2461 |
pC->iReady = TUint8(ss.iCpuNum | NSchedulable::EReadyOffset); |
2462 |
pC->iCpuAffinity = g->iCpuAffinity; // keep same CPU affinity |
|
2463 |
// if we're frozen, the group's freeze count was incremented |
|
2464 |
if (pC->iFreezeCpu) |
|
2465 |
--g->iFreezeCpu; |
|
2466 |
// if we've been marked as deferring, the group's freeze count was incremented |
|
2467 |
if (pC->iFastMutexDefer == 1) |
|
2468 |
{ |
|
2469 |
--g->iFreezeCpu; |
|
2470 |
pC->iFastMutexDefer = 0; |
|
2471 |
} |
|
2472 |
// if the group was waiting to change cpu then this thread needs to change still |
|
2473 |
if (g->iCpuChange) |
|
2474 |
{ |
|
2475 |
pC->iCpuChange = g->iCpuChange; |
|
2476 |
RescheduleNeeded(); |
|
2477 |
if (!g->iFreezeCpu) |
|
2478 |
{ |
|
2479 |
// we were the last thread in the group stopping it from moving |
|
2480 |
// but there may be no other threads left after UnReadyT'ing this one |
|
2481 |
g->iCpuChange = FALSE; |
|
2482 |
if (g->iReady) |
|
2483 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2484 |
ss.SSRemoveEntry(g); |
0 | 2485 |
g->iReady = 0; |
2486 |
make_group_ready = TRUE; |
|
2487 |
} |
|
2488 |
} |
|
2489 |
} |
|
2490 |
ss.iReadyListLock.UnlockOnly(); |
|
2491 |
--g->iThreadCount; |
|
2492 |
if (make_group_ready) |
|
2493 |
g->ReadyT(0); |
|
2494 |
g->RelSLock(); // since pC is no longer attached to g |
|
2495 |
pC->RelSLock(); |
|
2496 |
} |
|
2497 |
NKern::Unlock(); |
|
2498 |
return g; |
|
2499 |
} |
|
2500 |
||
2501 |
||
2502 |
/** Adds the current thread to the specified group. |
|
2503 |
||
2504 |
@param aGroup = pointer to group to join |
|
2505 |
||
2506 |
@pre Call in a thread context, not in one of the idle threads. |
|
2507 |
@pre Interrupts enabled |
|
2508 |
@pre Kernel unlocked |
|
2509 |
@pre Thread does not hold a fast mutex |
|
2510 |
@pre Thread does not have a freeze on CPU migration |
|
2511 |
@pre Current thread is not already in a group |
|
2512 |
*/ |
|
2513 |
EXPORT_C void NKern::JoinGroup(NThreadGroup* aGroup) |
|
2514 |
{ |
|
2515 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD, "NKern::JoinGroup"); |
|
2516 |
NKern::Lock(); |
|
2517 |
TSubScheduler& ss = SubScheduler(); |
|
2518 |
NThreadBase* pC = ss.iCurrentThread; |
|
2519 |
__ASSERT_WITH_MESSAGE_DEBUG(pC->iParent==pC, "Thread not already in a group", "NKern::JoinGroup"); |
|
2520 |
__ASSERT_WITH_MESSAGE_DEBUG(!pC->iFreezeCpu, "No interdiction on CPU migration", "NKern::JoinGroup"); |
|
2521 |
__ASSERT_WITH_MESSAGE_DEBUG(!pC->i_NThread_Initial, "Not idle thread", "NKern::JoinGroup"); |
|
2522 |
__NK_ASSERT_ALWAYS(pC->iParent==pC && !pC->iFreezeCpu); |
|
2523 |
__KTRACE_OPT(KNKERN,DEBUGPRINT("NJoinGroup %T->%G",pC,aGroup)); |
|
2524 |
pC->AcqSLock(); |
|
2525 |
aGroup->AcqSLock(); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2526 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2527 |
// Check if current CPU is compatible with group's affinity |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2528 |
TBool migrate = !CheckCpuAgainstAffinity(ss.iCpuNum, aGroup->iCpuAffinity); |
0 | 2529 |
if (!aGroup->iReady || aGroup->iReady==pC->iReady) |
2530 |
{ |
|
2531 |
// group not ready or ready on this CPU |
|
2532 |
if (!migrate) |
|
2533 |
{ |
|
2534 |
ss.iReadyListLock.LockOnly(); |
|
2535 |
pC->UnReadyT(); |
|
2536 |
pC->iParent = aGroup; |
|
2537 |
aGroup->iNThreadList.AddHead(pC); |
|
2538 |
if (!aGroup->iReady) |
|
2539 |
{ |
|
2540 |
aGroup->iPriority = pC->iPriority; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2541 |
ss.SSAddEntryHead(aGroup); |
0 | 2542 |
aGroup->iReady = TUint8(ss.iCpuNum | NSchedulable::EReadyOffset); |
2543 |
} |
|
2544 |
else if (pC->iPriority > aGroup->iPriority) |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2545 |
ss.SSChgEntryP(aGroup, pC->iPriority); |
0 | 2546 |
pC->iReady = NSchedulable::EReadyGroup; |
2547 |
aGroup->iCurrent = aGroup->iReady; |
|
2548 |
ss.iReadyListLock.UnlockOnly(); |
|
2549 |
++aGroup->iThreadCount; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2550 |
TUint64 now = NKern::Timestamp(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2551 |
aGroup->iLastStartTime.i64 = now; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2552 |
if (++aGroup->iActiveState == 1) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2553 |
aGroup->iLastActivationTime.i64 = now; |
0 | 2554 |
goto done; |
2555 |
} |
|
2556 |
} |
|
2557 |
// this thread needs to migrate to another CPU |
|
2558 |
pC->iNewParent = aGroup; |
|
2559 |
RescheduleNeeded(); |
|
2560 |
||
2561 |
// the following reschedule definitely joins the group even if the |
|
2562 |
// thread's CPU affinity is incompatible with that of the group |
|
2563 |
// (the thread's CPU affinity is subsequently determined by that of |
|
2564 |
// the group) |
|
2565 |
||
2566 |
done: |
|
2567 |
if (pC->iParent != aGroup) |
|
2568 |
aGroup->RelSLock(); |
|
2569 |
pC->RelSLock(); |
|
2570 |
while (!pC->TiedEventJoinInterlock()) |
|
2571 |
{ |
|
2572 |
TInt irq = NKern::DisableAllInterrupts(); |
|
2573 |
ss.QueueDfcs(); |
|
2574 |
NKern::RestoreInterrupts(irq); |
|
2575 |
} |
|
2576 |
NKern::Unlock(); |
|
2577 |
} |
|
2578 |
||
2579 |
||
2580 |
/****************************************************************************** |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2581 |
* Iterable Doubly Linked List |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2582 |
******************************************************************************/ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2583 |
TInt SIterDQIterator::Step(SIterDQLink*& aObj, TInt aMaxSteps) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2584 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2585 |
if (aMaxSteps <= 0) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2586 |
aMaxSteps = KMaxCpus + 3; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2587 |
SIterDQLink* p = Next(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2588 |
SIterDQLink* q = p; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2589 |
__NK_ASSERT_DEBUG(p!=0); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2590 |
for(; p->IsIterator() && --aMaxSteps>0; p=p->Next()) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2591 |
{} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2592 |
if (p->IsObject()) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2593 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2594 |
// found object |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2595 |
Deque(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2596 |
InsertAfter(p); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2597 |
aObj = p; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2598 |
return KErrNone; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2599 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2600 |
if (p->IsAnchor()) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2601 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2602 |
// reached end of list |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2603 |
if (p != q) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2604 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2605 |
Deque(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2606 |
InsertBefore(p); // put at the end |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2607 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2608 |
aObj = 0; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2609 |
return KErrEof; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2610 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2611 |
// Maximum allowed number of other iterators skipped |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2612 |
Deque(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2613 |
InsertAfter(p); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2614 |
aObj = 0; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2615 |
return KErrGeneral; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2616 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2617 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2618 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2619 |
/****************************************************************************** |
0 | 2620 |
* Priority Lists |
2621 |
******************************************************************************/ |
|
2622 |
||
2623 |
#ifndef __PRI_LIST_MACHINE_CODED__ |
|
2624 |
/** Returns the priority of the highest priority item present on a priority list. |
|
2625 |
||
2626 |
@return The highest priority present or -1 if the list is empty. |
|
2627 |
*/ |
|
2628 |
EXPORT_C TInt TPriListBase::HighestPriority() |
|
2629 |
{ |
|
2630 |
return __e32_find_ms1_64(iPresent64); |
|
2631 |
} |
|
2632 |
||
2633 |
||
2634 |
/** Finds the highest priority item present on a priority list. |
|
2635 |
||
2636 |
If multiple items at the same priority are present, return the first to be |
|
2637 |
added in chronological order. |
|
2638 |
||
2639 |
@return A pointer to the item or NULL if the list is empty. |
|
2640 |
*/ |
|
2641 |
EXPORT_C TPriListLink* TPriListBase::First() |
|
2642 |
{ |
|
2643 |
TInt p = HighestPriority(); |
|
2644 |
return p >=0 ? static_cast<TPriListLink*>(iQueue[p]) : NULL; |
|
2645 |
} |
|
2646 |
||
2647 |
||
2648 |
/** Adds an item to a priority list at the tail of the queue for its priority. |
|
2649 |
||
2650 |
@param aLink A pointer to the item - must not be NULL. |
|
2651 |
*/ |
|
2652 |
EXPORT_C void TPriListBase::Add(TPriListLink* aLink) |
|
2653 |
{ |
|
2654 |
TInt p = aLink->iPriority; |
|
2655 |
SDblQueLink* head = iQueue[p]; |
|
2656 |
if (head) |
|
2657 |
{ |
|
2658 |
// already some at this priority |
|
2659 |
aLink->InsertBefore(head); |
|
2660 |
} |
|
2661 |
else |
|
2662 |
{ |
|
2663 |
// 'create' new list |
|
2664 |
iQueue[p] = aLink; |
|
2665 |
aLink->iNext = aLink->iPrev = aLink; |
|
2666 |
iPresent[p>>5] |= 1u << (p & 0x1f); |
|
2667 |
} |
|
2668 |
} |
|
2669 |
||
2670 |
||
2671 |
/** Removes an item from a priority list. |
|
2672 |
||
2673 |
@param aLink A pointer to the item - must not be NULL. |
|
2674 |
*/ |
|
2675 |
EXPORT_C void TPriListBase::Remove(TPriListLink* aLink) |
|
2676 |
{ |
|
2677 |
if (!aLink->Alone()) |
|
2678 |
{ |
|
2679 |
// not the last on this list |
|
2680 |
TInt p = aLink->iPriority; |
|
2681 |
if (iQueue[p] == aLink) |
|
2682 |
iQueue[p] = aLink->iNext; |
|
2683 |
aLink->Deque(); |
|
2684 |
} |
|
2685 |
else |
|
2686 |
{ |
|
2687 |
TInt p = aLink->iPriority; |
|
2688 |
iQueue[p] = 0; |
|
2689 |
iPresent[p>>5] &= ~(1u << (p & 0x1f)); |
|
2690 |
KILL_LINK(aLink); |
|
2691 |
} |
|
2692 |
} |
|
2693 |
||
2694 |
||
2695 |
/** Changes the priority of an item on a priority list. |
|
2696 |
||
2697 |
@param aLink A pointer to the item to act on - must not be NULL. |
|
2698 |
@param aNewPriority A new priority for the item. |
|
2699 |
*/ |
|
2700 |
EXPORT_C void TPriListBase::ChangePriority(TPriListLink* aLink, TInt aNewPriority) |
|
2701 |
{ |
|
2702 |
if (aLink->iPriority!=aNewPriority) |
|
2703 |
{ |
|
2704 |
Remove(aLink); |
|
2705 |
aLink->iPriority=TUint8(aNewPriority); |
|
2706 |
Add(aLink); |
|
2707 |
} |
|
2708 |
} |
|
2709 |
#endif |
|
2710 |
||
2711 |
/** Adds an item to a priority list at the head of the queue for its priority. |
|
2712 |
||
2713 |
@param aLink A pointer to the item - must not be NULL. |
|
2714 |
*/ |
|
2715 |
EXPORT_C void TPriListBase::AddHead(TPriListLink* aLink) |
|
2716 |
{ |
|
2717 |
TInt p = aLink->iPriority; |
|
2718 |
SDblQueLink* head = iQueue[p]; |
|
2719 |
iQueue[p] = aLink; |
|
2720 |
if (head) |
|
2721 |
{ |
|
2722 |
// already some at this priority |
|
2723 |
aLink->InsertBefore(head); |
|
2724 |
} |
|
2725 |
else |
|
2726 |
{ |
|
2727 |
// 'create' new list |
|
2728 |
aLink->iNext = aLink->iPrev = aLink; |
|
2729 |
iPresent[p>>5] |= 1u << (p & 0x1f); |
|
2730 |
} |
|
2731 |
} |
|
2732 |
||
2733 |
||
2734 |
/****************************************************************************** |
|
2735 |
* Generic IPIs |
|
2736 |
******************************************************************************/ |
|
2737 |
||
2738 |
extern "C" { |
|
2739 |
extern void send_generic_ipis(TUint32); |
|
2740 |
||
2741 |
void generic_ipi_isr(TSubScheduler* aS) |
|
2742 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2743 |
TScheduler& s = TheScheduler; |
0 | 2744 |
TGenericIPI* ipi = aS->iNextIPI; |
2745 |
if (!ipi) |
|
2746 |
return; |
|
2747 |
TUint32 m = aS->iCpuMask; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2748 |
SDblQueLink* anchor = &s.iGenIPIList.iA; |
0 | 2749 |
while (ipi != anchor) |
2750 |
{ |
|
2751 |
__e32_atomic_and_acq32(&ipi->iCpusIn, ~m); |
|
2752 |
(*ipi->iFunc)(ipi); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2753 |
TInt irq = s.iGenIPILock.LockIrqSave(); |
0 | 2754 |
TGenericIPI* n = (TGenericIPI*)ipi->iNext; |
2755 |
ipi->iCpusOut &= ~m; |
|
2756 |
if (ipi->iCpusOut == 0) |
|
2757 |
{ |
|
2758 |
ipi->Deque(); |
|
2759 |
mb(); |
|
2760 |
ipi->iNext = 0; |
|
2761 |
} |
|
2762 |
ipi = n; |
|
2763 |
while (ipi!=anchor && !(ipi->iCpusIn & m)) |
|
2764 |
ipi = (TGenericIPI*)ipi->iNext; |
|
2765 |
if (ipi == anchor) |
|
2766 |
aS->iNextIPI = 0; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2767 |
s.iGenIPILock.UnlockIrqRestore(irq); |
0 | 2768 |
} |
2769 |
} |
|
2770 |
} |
|
2771 |
||
2772 |
void TGenericIPI::Queue(TGenericIPIFn aFunc, TUint32 aCpuMask) |
|
2773 |
{ |
|
2774 |
__KTRACE_OPT(KSCHED2,DEBUGPRINT("GenIPI F=%08x M=%08x", aFunc, aCpuMask)); |
|
2775 |
iFunc = aFunc; |
|
2776 |
TScheduler& s = TheScheduler; |
|
2777 |
TInt i; |
|
2778 |
TUint32 ipis = 0; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2779 |
TInt irq = s.iGenIPILock.LockIrqSave(); |
0 | 2780 |
if (aCpuMask & 0x80000000u) |
2781 |
{ |
|
2782 |
if (aCpuMask==0xffffffffu) |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2783 |
aCpuMask = s.iIpiAcceptCpus; |
0 | 2784 |
else if (aCpuMask==0xfffffffeu) |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2785 |
aCpuMask = s.iIpiAcceptCpus &~ SubScheduler().iCpuMask; |
0 | 2786 |
else |
2787 |
aCpuMask = 0; |
|
2788 |
} |
|
2789 |
iCpusIn = aCpuMask; |
|
2790 |
iCpusOut = aCpuMask; |
|
2791 |
if (!aCpuMask) |
|
2792 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2793 |
s.iGenIPILock.UnlockIrqRestore(irq); |
0 | 2794 |
iNext = 0; |
2795 |
return; |
|
2796 |
} |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2797 |
s.iGenIPIList.Add(this); |
0 | 2798 |
for (i=0; i<s.iNumCpus; ++i) |
2799 |
{ |
|
2800 |
if (!(aCpuMask & (1<<i))) |
|
2801 |
continue; |
|
2802 |
TSubScheduler& ss = *s.iSub[i]; |
|
2803 |
if (!ss.iNextIPI) |
|
2804 |
{ |
|
2805 |
ss.iNextIPI = this; |
|
2806 |
ipis |= (1<<i); |
|
2807 |
} |
|
2808 |
} |
|
2809 |
send_generic_ipis(ipis); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2810 |
s.iGenIPILock.UnlockIrqRestore(irq); |
0 | 2811 |
__KTRACE_OPT(KSCHED2,DEBUGPRINT("GenIPI ipis=%08x", ipis)); |
2812 |
} |
|
2813 |
||
2814 |
void TGenericIPI::QueueAll(TGenericIPIFn aFunc) |
|
2815 |
{ |
|
2816 |
Queue(aFunc, 0xffffffffu); |
|
2817 |
} |
|
2818 |
||
2819 |
void TGenericIPI::QueueAllOther(TGenericIPIFn aFunc) |
|
2820 |
{ |
|
2821 |
Queue(aFunc, 0xfffffffeu); |
|
2822 |
} |
|
2823 |
||
2824 |
// Call from thread or IDFC with interrupts enabled |
|
2825 |
void TGenericIPI::WaitEntry() |
|
2826 |
{ |
|
2827 |
CHECK_PRECONDITIONS(MASK_NOT_ISR|MASK_INTERRUPTS_ENABLED,"TGenericIPI::WaitEntry"); |
|
2828 |
while (iCpusIn) |
|
2829 |
{ |
|
2830 |
__chill(); |
|
2831 |
} |
|
2832 |
mb(); |
|
2833 |
} |
|
2834 |
||
2835 |
// Call from thread or IDFC with interrupts enabled |
|
2836 |
void TGenericIPI::WaitCompletion() |
|
2837 |
{ |
|
2838 |
CHECK_PRECONDITIONS(MASK_NOT_ISR|MASK_INTERRUPTS_ENABLED,"TGenericIPI::WaitCompletion"); |
|
2839 |
volatile TInt* p = (volatile TInt*)&iNext; |
|
2840 |
while (*p) |
|
2841 |
{ |
|
2842 |
__chill(); |
|
2843 |
} |
|
2844 |
mb(); |
|
2845 |
} |
|
2846 |
||
2847 |
/** Stop all other CPUs |
|
2848 |
||
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2849 |
Call with kernel unlocked, returns with kernel locked. |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2850 |
Returns mask of CPUs halted plus current CPU. |
0 | 2851 |
*/ |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2852 |
TUint32 TStopIPI::StopCPUs() |
0 | 2853 |
{ |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2854 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"TStopIPI::StopCPUs()"); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2855 |
TScheduler& s = TheScheduler; |
0 | 2856 |
iFlag = 0; |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2857 |
NKern::ThreadEnterCS(); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2858 |
|
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2859 |
// Stop any cores powering up or down for now |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2860 |
// A core already on the way down will stop just before the transition to SHUTDOWN_FINAL |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2861 |
// A core already on the way up will carry on powering up |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2862 |
TInt irq = s.iGenIPILock.LockIrqSave(); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2863 |
++s.iCCDeferCount; // stops bits in iIpiAcceptCpus being cleared, but doesn't stop them being set |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2864 |
// but iIpiAcceptCpus | s.iCpusComingUp is constant |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2865 |
TUint32 act2 = s.iIpiAcceptCpus; // CPUs still accepting IPIs |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2866 |
TUint32 cu = s.iCpusComingUp; // CPUs powering up |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2867 |
s.iGenIPILock.UnlockIrqRestore(irq); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2868 |
TUint32 cores = act2 | cu; |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2869 |
if (cu) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2870 |
{ |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2871 |
// wait for CPUs coming up to start accepting IPIs |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2872 |
while (cores & ~s.iIpiAcceptCpus) |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2873 |
{ |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2874 |
__snooze(); // snooze until cores have come up |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2875 |
} |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2876 |
} |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2877 |
NKern::Lock(); |
0 | 2878 |
QueueAllOther(&Isr); // send IPIs to all other CPUs |
2879 |
WaitEntry(); // wait for other CPUs to reach the ISR |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2880 |
return cores; |
0 | 2881 |
} |
2882 |
||
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2883 |
|
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2884 |
/** Release the stopped CPUs |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2885 |
|
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2886 |
Call with kernel locked, returns with kernel unlocked. |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2887 |
*/ |
0 | 2888 |
void TStopIPI::ReleaseCPUs() |
2889 |
{ |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2890 |
__e32_atomic_store_rel32(&iFlag, 1); // allow other CPUs to proceed |
0 | 2891 |
WaitCompletion(); // wait for them to finish with this IPI |
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2892 |
NKern::Unlock(); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2893 |
TheScheduler.CCUnDefer(); |
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2894 |
NKern::ThreadLeaveCS(); |
0 | 2895 |
} |
2896 |
||
2897 |
void TStopIPI::Isr(TGenericIPI* a) |
|
2898 |
{ |
|
2899 |
TStopIPI* s = (TStopIPI*)a; |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2900 |
while (!__e32_atomic_load_acq32(&s->iFlag)) |
0 | 2901 |
{ |
2902 |
__chill(); |
|
2903 |
} |
|
201
43365a9b78a3
Revision: 201027
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
2904 |
__e32_io_completion_barrier(); |
0 | 2905 |
} |
2906 |
||
2907 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2908 |
/****************************************************************************** |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2909 |
* TCoreCycler - general method to execute something on all active cores |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2910 |
******************************************************************************/ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2911 |
TCoreCycler::TCoreCycler() |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2912 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2913 |
iCores = 0; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2914 |
iG = 0; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2915 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2916 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2917 |
void TCoreCycler::Init() |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2918 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2919 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"TCoreCycler::Init()"); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2920 |
TScheduler& s = TheScheduler; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2921 |
NKern::ThreadEnterCS(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2922 |
iG = NKern::LeaveGroup(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2923 |
NThread* t = NKern::CurrentThread(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2924 |
if (t->iCoreCycling) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2925 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2926 |
__crash(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2927 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2928 |
t->iCoreCycling = TRUE; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2929 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2930 |
// Stop any cores powering up or down for now |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2931 |
// A core already on the way down will stop just before the transition to SHUTDOWN_FINAL |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2932 |
// A core already on the way up will carry on powering up |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2933 |
TInt irq = s.iGenIPILock.LockIrqSave(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2934 |
++s.iCCDeferCount; // stops bits in iIpiAcceptCpus being cleared, but doesn't stop them being set |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2935 |
// but iIpiAcceptCpus | s.iCpusComingUp is constant |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2936 |
TUint32 act2 = s.iIpiAcceptCpus; // CPUs still accepting IPIs |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2937 |
TUint32 cu = s.iCpusComingUp; // CPUs powering up |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2938 |
TUint32 gd = s.iCpusGoingDown; // CPUs no longer accepting IPIs on the way down |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2939 |
s.iGenIPILock.UnlockIrqRestore(irq); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2940 |
if (gd) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2941 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2942 |
// wait for CPUs going down to reach INACTIVE state |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2943 |
TUint32 remain = gd; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2944 |
FOREVER |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2945 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2946 |
TInt i; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2947 |
for (i=0; i<KMaxCpus; ++i) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2948 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2949 |
if (remain & (1u<<i)) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2950 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2951 |
// platform specific function returns TRUE when core has detached from SMP cluster |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2952 |
if (s.iSub[i]->Detached()) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2953 |
remain &= ~(1u<<i); // core is now down |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2954 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2955 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2956 |
if (!remain) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2957 |
break; // all done |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2958 |
else |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2959 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2960 |
__snooze(); // snooze until cores have gone down |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2961 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2962 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2963 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2964 |
iCores = act2 | cu; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2965 |
if (cu) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2966 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2967 |
// wait for CPUs coming up to start accepting IPIs |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2968 |
while (iCores & ~s.iIpiAcceptCpus) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2969 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2970 |
__snooze(); // snooze until cores have come up |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2971 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2972 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2973 |
iFrz = NKern::FreezeCpu(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2974 |
if (iFrz) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2975 |
__crash(); // already frozen so won't be able to migrate :-( |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2976 |
iInitialCpu = NKern::CurrentCpu(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2977 |
iCurrentCpu = iInitialCpu; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2978 |
iRemain = iCores; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2979 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2980 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2981 |
TInt TCoreCycler::Next() |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2982 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2983 |
NThread* t = NKern::CurrentThread(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2984 |
if (iCores == 0) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2985 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2986 |
Init(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2987 |
return KErrNone; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2988 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2989 |
if (NKern::CurrentCpu() != iCurrentCpu) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2990 |
__crash(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2991 |
iRemain &= ~(1u<<iCurrentCpu); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2992 |
TInt nextCpu = iRemain ? __e32_find_ms1_32(iRemain) : iInitialCpu; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2993 |
if (nextCpu != iCurrentCpu) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2994 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2995 |
NKern::JumpTo(nextCpu); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2996 |
iCurrentCpu = nextCpu; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2997 |
if (NKern::CurrentCpu() != iCurrentCpu) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2998 |
__crash(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2999 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3000 |
if (iRemain) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3001 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3002 |
return KErrNone; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3003 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3004 |
NKern::EndFreezeCpu(iFrz); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3005 |
iCores = 0; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3006 |
TScheduler& s = TheScheduler; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3007 |
s.CCUnDefer(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3008 |
t->iCoreCycling = FALSE; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3009 |
if (iG) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3010 |
NKern::JoinGroup(iG); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3011 |
NKern::ThreadLeaveCS(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3012 |
return KErrEof; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3013 |
} |