author | Mark Wilcox <markw@symbian.org> |
Tue, 25 May 2010 13:20:47 +0100 | |
changeset 116 | 57eea1054f46 |
parent 90 | 947f0dc9f7a8 |
child 257 | 3e88ff8f41d5 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). |
2 |
// All rights reserved. |
|
3 |
// This component and the accompanying materials are made available |
|
4 |
// under the terms of the License "Eclipse Public License v1.0" |
|
5 |
// which accompanies this distribution, and is available |
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// e32\nkern\nk_timer.cpp |
|
15 |
// Fast Millisecond Timer Implementation |
|
16 |
// This file is just a template - you'd be mad not to machine code this |
|
17 |
// |
|
18 |
// |
|
19 |
||
20 |
#include "nk_priv.h" |
|
21 |
||
22 |
#define i_NTimer_iState i8888.iHState1 |
|
23 |
#define i_NTimer_iCompleteInDfc i8888.iHState2 |
|
24 |
||
25 |
const TInt KTimerQDfcPriority=6; |
|
26 |
||
27 |
GLDEF_D NTimerQ TheTimerQ; |
|
28 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
29 |
extern "C" void send_irq_ipi(TSubScheduler*, TInt); |
0 | 30 |
|
31 |
#ifndef __MSTIM_MACHINE_CODED__ |
|
32 |
#ifdef _DEBUG |
|
33 |
#define __DEBUG_CALLBACK(n) {if (iDebugFn) (*iDebugFn)(iDebugPtr,n);} |
|
34 |
#else |
|
35 |
#define __DEBUG_CALLBACK(n) |
|
36 |
#endif |
|
37 |
||
38 |
/** Construct a nanokernel timer tied to a specified thread or group |
|
39 |
||
40 |
||
41 |
@param aTied Pointer to the thread/group to which the timer should be tied |
|
42 |
@param aFunction Pointer to the function to call on timer expiry |
|
43 |
@param aPtr Parameter to pass to the expiry handler |
|
44 |
||
45 |
@pre Any context |
|
46 |
||
47 |
@publishedPartner |
|
48 |
@prototype |
|
49 |
*/ |
|
50 |
EXPORT_C NTimer::NTimer(NSchedulable* aTied, NTimerFn aFunction, TAny* aPtr) |
|
51 |
{ |
|
52 |
iPtr = aPtr; |
|
53 |
iFn = aFunction; |
|
54 |
iHType = EEventHandlerNTimer; |
|
55 |
// i8888.iHState1 = EIdle; done by NEventHandler constructor |
|
56 |
if (aTied) |
|
57 |
{ |
|
58 |
SetTied(aTied); |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
||
63 |
/** Construct a nanokernel timer which mutates into and runs as a DFC on expiry |
|
64 |
The DFC queue is not specified at object construction time, but must be set |
|
65 |
using NTimer::SetDfcQ() before the timer is used. |
|
66 |
||
67 |
@param aFunction Pointer to the function to call on timer expiry |
|
68 |
@param aPtr Parameter to pass to the expiry handler |
|
69 |
@param aPriority Priority of DFC within the queue (0 to 7, where 7 is highest) |
|
70 |
||
71 |
@pre Any context |
|
72 |
||
73 |
@publishedPartner |
|
74 |
@prototype |
|
75 |
*/ |
|
76 |
EXPORT_C NTimer::NTimer(TDfcFn aFunction, TAny* aPtr, TInt aPriority) |
|
77 |
{ |
|
78 |
iPtr = aPtr; |
|
79 |
iFn = aFunction; |
|
80 |
iTied = 0; |
|
81 |
iHType = (TUint8)aPriority; |
|
82 |
// i8888.iHState0 = 0; done by NEventHandler constructor |
|
83 |
// i8888.iHState1 = EIdle; done by NEventHandler constructor |
|
84 |
// i8888.iHState2 = 0; done by NEventHandler constructor |
|
85 |
} |
|
86 |
||
87 |
||
88 |
/** Construct a nanokernel timer which mutates into and runs as a DFC on expiry |
|
89 |
||
90 |
@param aFunction Pointer to the function to call on timer expiry |
|
91 |
@param aPtr Parameter to pass to the expiry handler |
|
92 |
@param aDfcQ Pointer to DFC queue which this timer should use |
|
93 |
@param aPriority Priority of DFC within the queue (0 to 7, where 7 is highest) |
|
94 |
||
95 |
@pre Any context |
|
96 |
||
97 |
@publishedPartner |
|
98 |
@prototype |
|
99 |
*/ |
|
100 |
EXPORT_C NTimer::NTimer(TDfcFn aFunction, TAny* aPtr, TDfcQue* aDfcQ, TInt aPriority) |
|
101 |
{ |
|
102 |
iPtr = aPtr; |
|
103 |
iFn = aFunction; |
|
104 |
iDfcQ = aDfcQ; |
|
105 |
iHType = (TUint8)aPriority; |
|
106 |
// i8888.iHState0 = 0; done by NEventHandler constructor |
|
107 |
// i8888.iHState1 = EIdle; done by NEventHandler constructor |
|
108 |
// i8888.iHState2 = 0; done by NEventHandler constructor |
|
109 |
} |
|
110 |
||
111 |
||
112 |
/** Set the DFC queue to be used by an NTimer constructed using a TDfcFn |
|
113 |
||
114 |
@param aDfcQ Pointer to DFC queue which this timer should use |
|
115 |
||
116 |
@pre Timer cannot be in use |
|
117 |
@pre Any context |
|
118 |
||
119 |
@publishedPartner |
|
120 |
@prototype |
|
121 |
*/ |
|
122 |
EXPORT_C void NTimer::SetDfcQ(TDfcQue* aDfcQ) |
|
123 |
{ |
|
124 |
__NK_ASSERT_ALWAYS(aDfcQ!=0); |
|
125 |
__NK_ASSERT_ALWAYS(iHType < KNumDfcPriorities); |
|
126 |
__NK_ASSERT_ALWAYS(i8816.iHState16==EIdle); |
|
127 |
iDfcQ = aDfcQ; |
|
128 |
} |
|
129 |
||
130 |
||
131 |
/** Tie a nanokernel timer to a thread or group |
|
132 |
||
133 |
@param aTied = pointer to thread or group to which IDFC should be tied |
|
134 |
@return KErrNone if successful |
|
135 |
@return KErrDied if thread has exited or group has been destroyed. |
|
136 |
||
137 |
@pre Call in thread context, interrupts enabled |
|
138 |
@pre Timer must not be queued or running |
|
139 |
@pre Timer must not already be tied |
|
140 |
@pre Must not be a mutating timer (constructed with TDfcFn) |
|
141 |
||
142 |
@publishedPartner |
|
143 |
@prototype |
|
144 |
*/ |
|
145 |
EXPORT_C TInt NTimer::SetTied(NSchedulable* aTied) |
|
146 |
{ |
|
147 |
__NK_ASSERT_ALWAYS(!IsMutating()); |
|
148 |
__NK_ASSERT_ALWAYS(i8888.iHState1 == EIdle); |
|
149 |
__NK_ASSERT_ALWAYS(aTied && !iTied); |
|
150 |
NKern::Lock(); |
|
151 |
TInt r = aTied->AddTiedEvent(this); |
|
152 |
__NK_ASSERT_ALWAYS(r==KErrNone || r==KErrDied); |
|
153 |
NKern::Unlock(); |
|
154 |
return r; |
|
155 |
} |
|
156 |
||
157 |
||
158 |
/** Destroy a nanokernel timer |
|
159 |
||
160 |
@pre Call in thread context, interrupts enabled, preemption enabled |
|
161 |
@pre Calling thread in critical section |
|
162 |
@pre No fast mutex held |
|
163 |
||
164 |
@publishedPartner |
|
165 |
@prototype |
|
166 |
*/ |
|
167 |
EXPORT_C NTimer::~NTimer() |
|
168 |
{ |
|
169 |
if (!IsMutating() && iTied) |
|
170 |
{ |
|
171 |
NKern::Lock(); |
|
172 |
// remove from tied thread/group |
|
173 |
NEventHandler::TiedLock.LockOnly(); |
|
174 |
NSchedulable* tied = iTied; |
|
175 |
DoCancel(ECancelDestroy); |
|
176 |
if (tied) // might have been dequeued by thread/group termination |
|
177 |
{ |
|
178 |
tied->AcqSLock(); |
|
179 |
if (iTiedLink.iNext) |
|
180 |
{ |
|
181 |
iTiedLink.Deque(); |
|
182 |
iTiedLink.iNext = 0; |
|
183 |
} |
|
184 |
iTied = 0; |
|
185 |
tied->RelSLock(); |
|
186 |
} |
|
187 |
NEventHandler::TiedLock.UnlockOnly(); |
|
188 |
NKern::Unlock(); |
|
189 |
} |
|
190 |
else if (IsMutating() && iDfcQ) |
|
191 |
DoCancelMutating(ECancelDestroy); |
|
192 |
else |
|
193 |
DoCancel(ECancelDestroy); |
|
194 |
} |
|
195 |
||
196 |
||
197 |
/** Starts a nanokernel timer in one-shot mode with ISR callback. |
|
198 |
||
199 |
Queues the timer to expire in the specified number of nanokernel ticks. The |
|
200 |
actual wait time will be at least that much and may be up to one tick more. |
|
201 |
The expiry handler will be called in ISR context. |
|
202 |
||
203 |
Note that NKern::TimerTicks() can be used to convert milliseconds to ticks. |
|
204 |
||
205 |
@param aTime Timeout in nanokernel ticks |
|
206 |
||
207 |
@return KErrNone if no error; KErrInUse if timer is already active. |
|
208 |
||
209 |
@pre Any context |
|
210 |
||
211 |
@see NKern::TimerTicks() |
|
212 |
*/ |
|
213 |
EXPORT_C TInt NTimer::OneShot(TInt aTime) |
|
214 |
{ |
|
215 |
return OneShot(aTime,FALSE); |
|
216 |
} |
|
217 |
||
218 |
||
219 |
/** Starts a nanokernel timer in one-shot mode with ISR or DFC callback. |
|
220 |
||
221 |
Queues the timer to expire in the specified number of nanokernel ticks. The |
|
222 |
actual wait time will be at least that much and may be up to one tick more. |
|
223 |
For normal timers (constructed with NTimerFn) the expiry handler will be |
|
224 |
called in either ISR context or in the context of the nanokernel timer |
|
225 |
thread (DfcThread1). For mutating timers (constructed with TDfcFn) the |
|
226 |
expiry handler is called in the context of the thread running the relevant |
|
227 |
TDfcQue. |
|
228 |
||
229 |
Note that NKern::TimerTicks() can be used to convert milliseconds to ticks. |
|
230 |
||
231 |
@param aTime Timeout in nanokernel ticks |
|
232 |
@param aDfc TRUE if DFC callback required, FALSE if ISR callback required. |
|
233 |
Note that this parameter is ignored for mutating timers. |
|
234 |
||
235 |
@return KErrNone if no error |
|
236 |
@return KErrInUse if timer is already active. |
|
237 |
@return KErrDied if tied thread/group has exited |
|
238 |
||
239 |
@pre Any context |
|
240 |
||
241 |
@see NKern::TimerTicks() |
|
242 |
*/ |
|
243 |
EXPORT_C TInt NTimer::OneShot(TInt aTime, TBool aDfc) |
|
244 |
{ |
|
245 |
__NK_ASSERT_DEBUG(aTime>=0); |
|
246 |
/** iFn could be set to NULL after NTimer::OneShot(TInt, TDfc&) call. |
|
247 |
Call-back mechanism cannot be changed in the life time of a timer. */ |
|
248 |
__NK_ASSERT_DEBUG(iFn!=NULL); |
|
249 |
||
250 |
TInt irq = TheTimerQ.iTimerSpinLock.LockIrqSave(); |
|
251 |
if (!IsValid()) |
|
252 |
{ |
|
253 |
TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq); |
|
254 |
return KErrDied; |
|
255 |
} |
|
256 |
TUint16 state = i8816.iHState16; |
|
257 |
if (IsNormal()) |
|
258 |
state &= 0xFF; |
|
259 |
else |
|
260 |
aDfc = FALSE; // mutating timers start as ISR completion |
|
261 |
if (state!=EIdle) |
|
262 |
{ |
|
263 |
TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq); |
|
264 |
return KErrInUse; |
|
265 |
} |
|
266 |
mb(); // ensure that if we observe an idle state all accesses to the NTimer have also been observed |
|
267 |
i_NTimer_iCompleteInDfc=TUint8(aDfc?1:0); |
|
268 |
iTriggerTime=TheTimerQ.iMsCount+(TUint32)aTime; |
|
269 |
TheTimerQ.Add(this); |
|
270 |
TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq); |
|
271 |
return KErrNone; |
|
272 |
} |
|
273 |
||
274 |
||
275 |
/** Starts a nanokernel timer in one-shot mode with callback in dfc thread that provided DFC belongs to. |
|
276 |
||
277 |
Queues the timer to expire in the specified number of nanokernel ticks. The |
|
278 |
actual wait time will be at least that much and may be up to one tick more. |
|
279 |
On expiry aDfc will be queued in ISR context. |
|
280 |
||
281 |
Note that NKern::TimerTicks() can be used to convert milliseconds to ticks. |
|
282 |
||
283 |
@param aTime Timeout in nanokernel ticks |
|
284 |
@param aDfc - Dfc to be queued when the timer expires. |
|
285 |
||
286 |
@return KErrNone if no error |
|
287 |
@return KErrInUse if timer is already active. |
|
288 |
@return KErrDied if tied thread/group has exited |
|
289 |
||
290 |
@pre Any context |
|
291 |
@pre Must not be a mutating timer (constructed with TDfcFn) |
|
292 |
||
293 |
@see NKern::TimerTicks() |
|
294 |
*/ |
|
295 |
EXPORT_C TInt NTimer::OneShot(TInt aTime, TDfc& aDfc) |
|
296 |
{ |
|
297 |
__NK_ASSERT_DEBUG(!IsMutating()); |
|
298 |
__NK_ASSERT_DEBUG(aTime>=0); |
|
299 |
TInt irq = TheTimerQ.iTimerSpinLock.LockIrqSave(); |
|
300 |
if (iHType != EEventHandlerNTimer) |
|
301 |
{ |
|
302 |
TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq); |
|
303 |
return KErrDied; |
|
304 |
} |
|
305 |
if (i_NTimer_iState!=EIdle) |
|
306 |
{ |
|
307 |
TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq); |
|
308 |
return KErrInUse; |
|
309 |
} |
|
310 |
mb(); // ensure that if we observe an idle state all accesses to the NTimer have also been observed |
|
311 |
i_NTimer_iCompleteInDfc = 0; |
|
312 |
iFn = NULL; |
|
313 |
iPtr = (TAny*) &aDfc; |
|
314 |
iTriggerTime=TheTimerQ.iMsCount+(TUint32)aTime; |
|
315 |
TheTimerQ.Add(this); |
|
316 |
TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq); |
|
317 |
return KErrNone; |
|
318 |
} |
|
319 |
||
320 |
||
321 |
/** Starts a nanokernel timer in zero-drift periodic mode with ISR or DFC callback. |
|
322 |
||
323 |
Queues the timer to expire in the specified number of nanokernel ticks, |
|
324 |
measured from the time at which it last expired. This allows exact periodic |
|
325 |
timers to be implemented with no drift caused by delays in requeueing the |
|
326 |
timer. |
|
327 |
||
328 |
The expiry handler will be called in the same context as the previous timer |
|
329 |
expiry. Generally the way this is used is that NTimer::OneShot() is used to start |
|
330 |
the first time interval and this specifies whether the callback is in ISR context |
|
331 |
or in the context of the nanokernel timer thread (DfcThread1) or other Dfc thread. |
|
332 |
The expiry handler then uses NTimer::Again() to requeue the timer. |
|
333 |
||
334 |
@param aTime Timeout in nanokernel ticks |
|
335 |
||
336 |
@return KErrNone if no error |
|
337 |
@return KErrInUse if timer is already active; |
|
338 |
@return KErrArgument if the requested expiry time is in the past. |
|
339 |
@return KErrDied if tied thread/group has exited |
|
340 |
||
341 |
@pre Any context |
|
342 |
*/ |
|
343 |
EXPORT_C TInt NTimer::Again(TInt aTime) |
|
344 |
// |
|
345 |
// Wait aTime from last trigger time - used for periodic timers |
|
346 |
// |
|
347 |
{ |
|
348 |
TInt irq = TheTimerQ.iTimerSpinLock.LockIrqSave(); |
|
349 |
if (!IsValid()) |
|
350 |
{ |
|
351 |
TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq); |
|
352 |
return KErrDied; |
|
353 |
} |
|
354 |
TUint16 state = i8816.iHState16; |
|
355 |
if (IsNormal()) |
|
356 |
state &= 0xFF; |
|
357 |
if (state!=EIdle) |
|
358 |
{ |
|
359 |
TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq); |
|
360 |
return KErrInUse; |
|
361 |
} |
|
362 |
mb(); // ensure that if we observe an idle state all accesses to the NTimer have also been observed |
|
363 |
TUint32 nextTick=TheTimerQ.iMsCount; |
|
364 |
TUint32 trigger=iTriggerTime+(TUint32)aTime; |
|
365 |
TUint32 d=trigger-nextTick; |
|
366 |
if (d>=0x80000000) |
|
367 |
{ |
|
368 |
TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq); |
|
369 |
return KErrArgument; // requested time is in the past |
|
370 |
} |
|
371 |
iTriggerTime=trigger; |
|
372 |
TheTimerQ.Add(this); |
|
373 |
TheTimerQ.iTimerSpinLock.UnlockIrqRestore(irq); |
|
374 |
return KErrNone; |
|
375 |
} |
|
376 |
||
377 |
||
378 |
/** Cancels a nanokernel timer. |
|
379 |
||
380 |
Removes this timer from the nanokernel timer queue. Does nothing if the |
|
381 |
timer is inactive or has already expired. |
|
382 |
Note that if the timer was queued and DFC callback requested it is possible |
|
383 |
for the expiry handler to run even after Cancel() has been called. This will |
|
384 |
occur in the case where DfcThread1 is preempted just before calling the |
|
385 |
expiry handler for this timer and the preempting thread/ISR/IDFC calls |
|
386 |
Cancel() on the timer. |
|
387 |
||
388 |
@pre Any context for a non-mutating NTimer (constructed with NTimerFn) |
|
389 |
@pre For mutating NTimer (constructed with TDfcFn), IDFC or thread context only. |
|
390 |
@return TRUE if timer was actually cancelled |
|
391 |
@return FALSE if timer was not cancelled - this could be because it was not |
|
392 |
active or because its expiry handler was already running on |
|
393 |
another CPU or in the timer DFC. |
|
394 |
*/ |
|
395 |
EXPORT_C TBool NTimer::Cancel() |
|
396 |
{ |
|
397 |
if (IsMutating() && iDfcQ) |
|
398 |
return DoCancelMutating(0); |
|
399 |
return DoCancel(0)!=EIdle; |
|
400 |
} |
|
401 |
||
402 |
void NTimer::DoCancel0(TUint aState) |
|
403 |
{ |
|
404 |
if (aState>ETransferring && aState<=EFinal) // idle or transferring timers are not on a queue |
|
405 |
Deque(); |
|
406 |
switch (aState) |
|
407 |
{ |
|
408 |
case ETransferring: // signal DFC to abort this iteration |
|
409 |
TheTimerQ.iTransferringCancelled=TRUE; |
|
410 |
break; |
|
411 |
case ECritical: // signal DFC to abort this iteration |
|
412 |
TheTimerQ.iCriticalCancelled=TRUE; |
|
413 |
break; |
|
414 |
case EFinal: |
|
415 |
{ |
|
416 |
// Need to clear bit in iPresent if both final queues now empty |
|
417 |
// NOTE: Timer might actually be on the completed queue rather than the final queue |
|
418 |
// but the check is harmless in any case. |
|
419 |
TInt i=iTriggerTime & NTimerQ::ETimerQMask; |
|
420 |
NTimerQ::STimerQ& q=TheTimerQ.iTickQ[i]; |
|
421 |
if (q.iIntQ.IsEmpty() && q.iDfcQ.IsEmpty()) |
|
422 |
TheTimerQ.iPresent &= ~(1<<i); |
|
423 |
break; |
|
424 |
} |
|
425 |
case EIdle: // nothing to do |
|
426 |
case EHolding: // just deque |
|
427 |
case EOrdered: // just deque |
|
428 |
break; |
|
429 |
default: |
|
430 |
__NK_ASSERT_ALWAYS(0); |
|
431 |
} |
|
432 |
} |
|
433 |
||
434 |
TUint NTimer::DoCancel(TUint aFlags) |
|
435 |
{ |
|
436 |
NSchedulable* tied = 0; |
|
437 |
TInt irq = NKern::DisableAllInterrupts(); |
|
438 |
TheTimerQ.iTimerSpinLock.LockOnly(); |
|
439 |
TUint state = i_NTimer_iState; |
|
440 |
mb(); |
|
441 |
if (IsNormal() && state>=EEventQ) |
|
442 |
{ |
|
443 |
// It's on a CPU's event handler queue |
|
444 |
TInt cpu = state - EEventQ; |
|
445 |
if (cpu < TheScheduler.iNumCpus) |
|
446 |
{ |
|
447 |
TSubScheduler* ss = TheSubSchedulers + cpu; |
|
448 |
ss->iEventHandlerLock.LockOnly(); |
|
449 |
state = i_NTimer_iState; |
|
450 |
if (state != EIdle) |
|
451 |
{ |
|
452 |
Deque(); // we got to it first |
|
453 |
tied = iTied; |
|
454 |
i_NTimer_iState = EIdle; |
|
455 |
} |
|
456 |
ss->iEventHandlerLock.UnlockOnly(); |
|
457 |
goto end; |
|
458 |
} |
|
459 |
} |
|
460 |
DoCancel0(state); |
|
461 |
if (IsMutating()) |
|
462 |
i8816.iHState16 = 0; |
|
463 |
else |
|
464 |
i_NTimer_iState=EIdle; |
|
465 |
end: |
|
466 |
if (aFlags & ECancelDestroy) |
|
467 |
iHType = EEventHandlerDummy; |
|
468 |
TheTimerQ.iTimerSpinLock.UnlockOnly(); |
|
469 |
if (tied) |
|
470 |
tied->EndTiedEvent(); // FIXME - Could be called in thread context |
|
471 |
NKern::RestoreInterrupts(irq); |
|
472 |
return state; |
|
473 |
} |
|
474 |
||
475 |
TBool NTimer::DoCancelMutating(TUint aFlags) |
|
476 |
{ |
|
477 |
CHECK_PRECONDITIONS(MASK_NOT_ISR,"NTimer::Cancel (mutating NTimer)"); |
|
478 |
TSubScheduler& ss0 = SubScheduler(); |
|
479 |
TBool wait = FALSE; |
|
480 |
TInt cpu = -1; |
|
481 |
TBool result = TRUE; |
|
482 |
TDfc* d = (TDfc*)this; |
|
483 |
NKern::Lock(); |
|
484 |
TDfcQue* q = iDfcQ; |
|
485 |
NThreadBase* t = q->iThread; |
|
486 |
t->AcqSLock(); |
|
487 |
TheTimerQ.iTimerSpinLock.LockIrq(); |
|
488 |
||
489 |
// 0000->0000, XX00->ZZ00, xxYY->zzYY |
|
490 |
TUint state = d->CancelInitialStateChange(); |
|
491 |
if (state & 0xFF00) |
|
492 |
{ |
|
493 |
// someone else cancelling at the same time - just wait for them to finish |
|
494 |
// they can only be waiting for the cancel IPI |
|
495 |
result = FALSE; |
|
496 |
wait = TRUE; |
|
497 |
goto end; |
|
498 |
} |
|
499 |
if (state == 0) // timer was not active |
|
500 |
{ |
|
501 |
result = FALSE; |
|
502 |
goto end; |
|
503 |
} |
|
504 |
if (state>=ETransferring && state<=EFinal) |
|
505 |
{ |
|
506 |
DoCancel0(state); |
|
507 |
// cancel is complete |
|
508 |
goto reset; |
|
509 |
} |
|
510 |
if (state==1) |
|
511 |
{ |
|
512 |
// on DFC final queue |
|
513 |
q->Remove((TPriListLink*)this); |
|
514 |
goto reset; |
|
515 |
} |
|
516 |
// must be on IDFC queue - need to send cancel IPI |
|
517 |
__NK_ASSERT_ALWAYS((state>>5)==4); |
|
518 |
cpu = state & 0x1f; |
|
519 |
if (TUint(cpu) == ss0.iCpuNum) |
|
520 |
{ |
|
521 |
// it's on this CPU's IDFC queue so just dequeue it and finish |
|
522 |
Deque(); |
|
523 |
cpu = -1; |
|
524 |
reset: |
|
525 |
d->ResetState(); // release semantics |
|
526 |
} |
|
527 |
end: |
|
528 |
if (aFlags & ECancelDestroy) |
|
529 |
iHType = EEventHandlerDummy; |
|
530 |
TheTimerQ.iTimerSpinLock.UnlockIrq(); |
|
531 |
t->RelSLock(); |
|
532 |
if (cpu>=0) |
|
533 |
{ |
|
534 |
TCancelIPI ipi; |
|
535 |
ipi.Send(d, cpu); |
|
536 |
ipi.WaitCompletion(); |
|
537 |
wait = TRUE; |
|
538 |
} |
|
539 |
if (wait) |
|
540 |
{ |
|
541 |
TUint n = 0x01000000; |
|
542 |
while ((i8816.iHState16>>8) & ss0.iCpuMask) |
|
543 |
{ |
|
544 |
__chill(); |
|
545 |
if (!--n) |
|
546 |
__crash(); |
|
547 |
} |
|
548 |
} |
|
549 |
NKern::Unlock(); |
|
550 |
return result; |
|
551 |
} |
|
552 |
#endif |
|
553 |
||
554 |
||
555 |
/** Obtains the address of the nanokernel timer queue object. |
|
556 |
||
557 |
Not intended for general use. Intended only for base ports in order to get |
|
558 |
the address used to call NTimerQ::Tick() with. |
|
559 |
||
560 |
@return The address of the nanokernel timer queue object |
|
561 |
@pre Any context |
|
562 |
*/ |
|
563 |
EXPORT_C TAny* NTimerQ::TimerAddress() |
|
564 |
{ |
|
565 |
return &TheTimerQ; |
|
566 |
} |
|
567 |
||
568 |
NTimerQ::NTimerQ() |
|
569 |
: iDfc(NTimerQ::DfcFn,this,NULL,KTimerQDfcPriority), |
|
570 |
iDfcCompleteCount(1), |
|
571 |
iTimerSpinLock(TSpinLock::EOrderNTimerQ) |
|
572 |
{ |
|
573 |
// NOTE: All other members are initialised to zero since the single instance |
|
574 |
// of NTimerQ resides in .bss |
|
575 |
} |
|
576 |
||
577 |
void NTimerQ::Init1(TInt aTickPeriod) |
|
578 |
{ |
|
579 |
TheTimerQ.iTickPeriod=aTickPeriod; |
|
580 |
__KTRACE_OPT(KBOOT,DEBUGPRINT("NTimerQ::Init1 - period %d us",aTickPeriod)); |
|
581 |
__KTRACE_OPT(KMEMTRACE, DEBUGPRINT("MT:P %d",aTickPeriod)); |
|
582 |
} |
|
583 |
||
584 |
void NTimerQ::Init3(TDfcQue* aDfcQ) |
|
585 |
{ |
|
586 |
__KTRACE_OPT(KBOOT,DEBUGPRINT("NTimerQ::Init3 DFCQ at %08x",aDfcQ)); |
|
587 |
TheTimerQ.iDfc.SetDfcQ(aDfcQ); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
588 |
NThreadBase* t = aDfcQ->iThread; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
589 |
t->iRebalanceAttr = 1; |
0 | 590 |
} |
591 |
||
592 |
#ifndef __MSTIM_MACHINE_CODED__ |
|
593 |
void NTimerQ::Add(NTimer* aTimer) |
|
594 |
// |
|
595 |
// Internal function to add a timer to the queue. |
|
596 |
// Enter and return with timer queue spin lock held. |
|
597 |
// |
|
598 |
{ |
|
599 |
TInt t=TInt(aTimer->iTriggerTime-iMsCount); |
|
600 |
if (t<ENumTimerQueues) |
|
601 |
AddFinal(aTimer); |
|
602 |
else |
|
603 |
{ |
|
604 |
// >=32ms to expiry, so put on holding queue |
|
605 |
aTimer->i_NTimer_iState=NTimer::EHolding; |
|
606 |
iHoldingQ.Add(aTimer); |
|
607 |
} |
|
608 |
} |
|
609 |
||
610 |
void NTimerQ::AddFinal(NTimer* aTimer) |
|
611 |
// |
|
612 |
// Internal function to add a timer to the corresponding final queue. |
|
613 |
// Enter and return with timer queue spin lock held. |
|
614 |
// |
|
615 |
{ |
|
616 |
TInt i=aTimer->iTriggerTime & ETimerQMask; |
|
617 |
SDblQue* pQ; |
|
618 |
if (aTimer->i_NTimer_iCompleteInDfc) |
|
619 |
pQ=&iTickQ[i].iDfcQ; |
|
620 |
else |
|
621 |
pQ=&iTickQ[i].iIntQ; |
|
622 |
iPresent |= (1<<i); |
|
623 |
aTimer->i_NTimer_iState=NTimer::EFinal; |
|
624 |
pQ->Add(aTimer); |
|
625 |
} |
|
626 |
||
627 |
void NTimerQ::DfcFn(TAny* aPtr) |
|
628 |
{ |
|
629 |
((NTimerQ*)aPtr)->Dfc(); |
|
630 |
} |
|
631 |
||
632 |
void NTimerQ::Dfc() |
|
633 |
// |
|
634 |
// Do deferred timer queue processing and/or DFC completions |
|
635 |
// |
|
636 |
{ |
|
637 |
// First transfer entries on the Ordered queue to the Final queues |
|
638 |
FOREVER |
|
639 |
{ |
|
640 |
iTimerSpinLock.LockIrq(); |
|
641 |
if (iOrderedQ.IsEmpty()) |
|
642 |
break; |
|
643 |
NTimer* pC=(NTimer*)iOrderedQ.First(); |
|
644 |
TInt remain=pC->iTriggerTime-iMsCount; |
|
645 |
if (remain>=ENumTimerQueues) |
|
646 |
break; |
|
647 |
||
648 |
// If remaining time <32 ticks, add it to final queue; |
|
649 |
// also if remain < 0 we've 'missed it' so add to final queue. |
|
650 |
pC->Deque(); |
|
651 |
AddFinal(pC); |
|
652 |
iTimerSpinLock.UnlockIrq(); |
|
653 |
__DEBUG_CALLBACK(0); |
|
654 |
} |
|
655 |
iTimerSpinLock.UnlockIrq(); |
|
656 |
__DEBUG_CALLBACK(1); |
|
657 |
||
658 |
// Next transfer entries on the Holding queue to the Ordered queue or final queue |
|
659 |
FOREVER |
|
660 |
{ |
|
661 |
iTimerSpinLock.LockIrq(); |
|
662 |
if (iHoldingQ.IsEmpty()) |
|
663 |
break; |
|
664 |
NTimer* pC=(NTimer*)iHoldingQ.First(); |
|
665 |
pC->Deque(); |
|
666 |
pC->i_NTimer_iState=NTimer::ETransferring; |
|
667 |
iTransferringCancelled=FALSE; |
|
668 |
TUint32 trigger=pC->iTriggerTime; |
|
669 |
if (TInt(trigger-iMsCount)<ENumTimerQueues) |
|
670 |
{ |
|
671 |
// <32ms remaining so put it on final queue |
|
672 |
AddFinal(pC); |
|
673 |
} |
|
674 |
else |
|
675 |
{ |
|
676 |
FOREVER |
|
677 |
{ |
|
678 |
iTimerSpinLock.UnlockIrq(); |
|
679 |
__DEBUG_CALLBACK(2); |
|
680 |
||
681 |
// we now need to walk ordered queue to find correct position for pC |
|
682 |
SDblQueLink* anchor=&iOrderedQ.iA; |
|
683 |
iCriticalCancelled=FALSE; |
|
684 |
iTimerSpinLock.LockIrq(); |
|
685 |
NTimer* pN=(NTimer*)iOrderedQ.First(); |
|
686 |
while (pN!=anchor && !iTransferringCancelled) |
|
687 |
{ |
|
688 |
if ((pN->iTriggerTime-trigger)<0x80000000u) |
|
689 |
break; // insert before pN |
|
690 |
pN->i_NTimer_iState=NTimer::ECritical; |
|
691 |
iTimerSpinLock.UnlockIrq(); |
|
692 |
__DEBUG_CALLBACK(3); |
|
693 |
iTimerSpinLock.LockIrq(); |
|
694 |
if (iCriticalCancelled) |
|
695 |
break; |
|
696 |
pN->i_NTimer_iState=NTimer::EOrdered; |
|
697 |
pN=(NTimer*)pN->iNext; |
|
698 |
} |
|
699 |
||
700 |
if (iTransferringCancelled) |
|
701 |
break; // this one has been cancelled, go on to next one |
|
702 |
if (!iCriticalCancelled) |
|
703 |
{ |
|
704 |
pC->InsertBefore(pN); |
|
705 |
pC->i_NTimer_iState=NTimer::EOrdered; |
|
706 |
break; // done this one |
|
707 |
} |
|
708 |
} |
|
709 |
} |
|
710 |
iTimerSpinLock.UnlockIrq(); |
|
711 |
__DEBUG_CALLBACK(4); |
|
712 |
} |
|
713 |
iTimerSpinLock.UnlockIrq(); |
|
714 |
__DEBUG_CALLBACK(5); |
|
715 |
||
716 |
// Finally do call backs for timers which requested DFC callback |
|
717 |
FOREVER |
|
718 |
{ |
|
719 |
iTimerSpinLock.LockIrq(); |
|
720 |
if (iCompletedQ.IsEmpty()) |
|
721 |
break; |
|
722 |
NTimer* pC=(NTimer*)iCompletedQ.First(); |
|
723 |
pC->Deque(); |
|
724 |
pC->i_NTimer_iState=NTimer::EIdle; |
|
725 |
TAny* p=pC->iPtr; |
|
726 |
NTimerFn f=pC->iFn; |
|
727 |
iTimerSpinLock.UnlockIrq(); |
|
728 |
__DEBUG_CALLBACK(7); |
|
729 |
(*f)(p); |
|
730 |
} |
|
731 |
iTimerSpinLock.UnlockIrq(); |
|
732 |
__e32_atomic_add_rel32(&iDfcCompleteCount, 2); |
|
733 |
} |
|
734 |
||
735 |
||
736 |
/** Tick over the nanokernel timer queue. |
|
737 |
This function should be called by the base port in the system tick timer ISR. |
|
738 |
It should not be called at any other time. |
|
739 |
The value of 'this' to pass is the value returned by NTimerQ::TimerAddress(). |
|
740 |
||
741 |
@see NTimerQ::TimerAddress() |
|
742 |
*/ |
|
743 |
EXPORT_C void NTimerQ::Tick() |
|
744 |
{ |
|
745 |
TInt irq = iTimerSpinLock.LockIrqSave(); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
746 |
TInt i = TInt(__e32_atomic_add_rlx64(&iMsCount64, 1)) & ETimerQMask; |
0 | 747 |
STimerQ* pQ=iTickQ+i; |
748 |
iPresent &= ~(1<<i); |
|
749 |
TBool doDfc=FALSE; |
|
750 |
if (!pQ->iDfcQ.IsEmpty()) |
|
751 |
{ |
|
752 |
// transfer DFC completions to completed queue and queue DFC |
|
753 |
iCompletedQ.MoveFrom(&pQ->iDfcQ); |
|
754 |
doDfc=TRUE; |
|
755 |
} |
|
756 |
if ((i&(ETimerQMask>>1))==0) |
|
757 |
{ |
|
758 |
// Every 16 ticks we check if a DFC is required. |
|
759 |
// This allows a DFC latency of up to 16 ticks before timers are missed. |
|
760 |
if (!iHoldingQ.IsEmpty()) |
|
761 |
{ |
|
762 |
doDfc=TRUE; // if holding queue nonempty, queue DFC to sort |
|
763 |
} |
|
764 |
else if (!iOrderedQ.IsEmpty()) |
|
765 |
{ |
|
766 |
// if first ordered queue entry expires in <32ms, queue the DFC to transfer |
|
767 |
NTimer* pC=(NTimer*)iOrderedQ.First(); |
|
768 |
TUint x = pC->iTriggerTime - iMsCount; |
|
769 |
if (x < (TUint)ENumTimerQueues) |
|
770 |
{ |
|
771 |
doDfc=TRUE; |
|
772 |
} |
|
773 |
} |
|
774 |
} |
|
775 |
if (!pQ->iIntQ.IsEmpty()) |
|
776 |
{ |
|
777 |
// transfer ISR completions to a temporary queue |
|
778 |
// careful here - other CPUs could dequeue timers! |
|
779 |
SDblQue q(&pQ->iIntQ,0); |
|
780 |
for (; !q.IsEmpty(); iTimerSpinLock.LockIrqSave()) |
|
781 |
{ |
|
782 |
NTimer* pC=(NTimer*)q.First(); |
|
783 |
pC->Deque(); |
|
784 |
if (pC->IsMutating()) |
|
785 |
{ |
|
786 |
pC->AddAsDFC(); //mutate NTimer into TDfc and Add() it |
|
787 |
iTimerSpinLock.UnlockIrqRestore(irq); |
|
788 |
continue; |
|
789 |
} |
|
790 |
if (!pC->iFn) |
|
791 |
{ |
|
792 |
pC->i_NTimer_iState=NTimer::EIdle; |
|
793 |
iTimerSpinLock.UnlockIrqRestore(irq); |
|
794 |
((TDfc*)(pC->iPtr))->Add(); |
|
795 |
continue; |
|
796 |
} |
|
797 |
NSchedulable* tied = pC->iTied; |
|
798 |
if (tied) |
|
799 |
{ |
|
800 |
TInt cpu = tied->BeginTiedEvent(); |
|
801 |
if (cpu != NKern::CurrentCpu()) |
|
802 |
{ |
|
803 |
pC->i_NTimer_iState = TUint8(NTimer::EEventQ + cpu); |
|
804 |
TSubScheduler* ss = TheSubSchedulers + cpu; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
805 |
TInt kick = ss->QueueEvent(pC); |
0 | 806 |
iTimerSpinLock.UnlockIrqRestore(irq); |
807 |
if (kick) |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
808 |
send_irq_ipi(ss, kick); |
0 | 809 |
continue; |
810 |
} |
|
811 |
} |
|
812 |
pC->i_NTimer_iState=NTimer::EIdle; |
|
813 |
TAny* p = pC->iPtr; |
|
814 |
NTimerFn f = pC->iFn; |
|
815 |
iTimerSpinLock.UnlockIrqRestore(irq); |
|
816 |
(*f)(p); |
|
817 |
if (tied) |
|
818 |
tied->EndTiedEvent(); |
|
819 |
} |
|
820 |
} |
|
821 |
iTimerSpinLock.UnlockIrqRestore(irq); |
|
822 |
if (doDfc) |
|
823 |
iDfc.Add(); |
|
824 |
} |
|
825 |
||
826 |
||
827 |
/** Mutate an NTimer into a DFC and Add() it |
|
828 |
||
829 |
If NTimer state is EFinal, change to DFC state 008n and add to endogenous IDFC |
|
830 |
queue for this CPU. |
|
831 |
||
832 |
Enter and return with IRQs disabled and timer spin lock held |
|
833 |
No need to worry about Cancel()s since timer spin lock is held |
|
834 |
Don't touch iHState0 |
|
835 |
||
836 |
@internalComponent |
|
837 |
*/ |
|
838 |
void NTimer::AddAsDFC() |
|
839 |
{ |
|
840 |
TSubScheduler& ss = SubScheduler(); |
|
841 |
i8816.iHState16 = (TUint16)(0x80|ss.iCpuNum); |
|
842 |
ss.iDfcs.Add(this); |
|
843 |
ss.iDfcPendingFlag = 1; |
|
844 |
} |
|
845 |
||
846 |
||
847 |
/** Check if a nanokernel timer is pending or not |
|
848 |
||
849 |
@return TRUE if the timer is pending (OneShot() etc. would return KErrInUse) |
|
850 |
@return FALSE if the timer is idle (OneShot() etc. would succeed) |
|
851 |
@pre Any context |
|
852 |
||
853 |
@publishedPartner |
|
854 |
@prototype |
|
855 |
*/ |
|
856 |
EXPORT_C TBool NTimer::IsPending() |
|
857 |
{ |
|
858 |
TUint16 state = i8816.iHState16; |
|
859 |
return state != EIdle; |
|
860 |
} |
|
861 |
||
862 |
||
863 |
/** Return the number of ticks before the next nanokernel timer expiry. |
|
864 |
May on occasion return a pessimistic estimate (i.e. too low). |
|
865 |
Used by base port to disable the system tick interrupt when the system |
|
866 |
is idle. |
|
867 |
||
868 |
@return The number of ticks before the next nanokernel timer expiry. |
|
869 |
||
870 |
@pre Interrupts must be disabled. |
|
871 |
||
872 |
@post Interrupts are disabled. |
|
873 |
*/ |
|
874 |
EXPORT_C TInt NTimerQ::IdleTime() |
|
875 |
{ |
|
876 |
CHECK_PRECONDITIONS(MASK_INTERRUPTS_DISABLED,"NTimerQ::IdleTime"); |
|
877 |
NTimerQ& m=TheTimerQ; |
|
878 |
TUint32 next=m.iMsCount; // number of next tick |
|
879 |
TUint32 p=m.iPresent; |
|
880 |
TInt r=KMaxTInt; |
|
881 |
if (p) |
|
882 |
{ |
|
883 |
// Final queues nonempty |
|
884 |
TInt nx=next&0x1f; // number of next tick modulo 32 |
|
885 |
p=(p>>nx)|(p<<(32-nx)); // rotate p right by nx (so lsb corresponds to next tick) |
|
886 |
r=__e32_find_ls1_32(p); // find number of zeros before LS 1 |
|
887 |
} |
|
888 |
if (!m.iHoldingQ.IsEmpty()) |
|
889 |
{ |
|
890 |
// Sort operation required - need to process next tick divisible by 16 |
|
891 |
TInt nx=next&0x0f; // number of next tick modulo 16 |
|
892 |
TInt r2=nx?(16-nx):0; // number of ticks before next divisible by 16 |
|
893 |
if (r2<r) |
|
894 |
r=r2; |
|
895 |
} |
|
896 |
if (!m.iOrderedQ.IsEmpty()) |
|
897 |
{ |
|
898 |
// Timers present on ordered queue |
|
899 |
NTimer* pC=(NTimer*)m.iOrderedQ.First(); |
|
900 |
TUint32 tt=pC->iTriggerTime; |
|
901 |
tt=(tt&~0x0f)-16; // time at which transfer to final queue would occur |
|
902 |
TInt r3=(TInt)(tt-next); |
|
903 |
if (r3<r) |
|
904 |
r=r3; |
|
905 |
} |
|
906 |
return r; |
|
907 |
} |
|
908 |
#endif |
|
909 |
||
910 |
||
911 |
/** Advance the nanokernel timer queue by the specified number of ticks. |
|
912 |
It is assumed that no timers expire as a result of this. |
|
913 |
Used by base port when system comes out of idle mode after disabling the |
|
914 |
system tick interrupt to bring the timer queue up to date. |
|
915 |
||
916 |
@param aTicks Number of ticks skipped due to tick suppression |
|
917 |
||
918 |
@pre Interrupts must be disabled. |
|
919 |
||
920 |
@post Interrupts are disabled. |
|
921 |
*/ |
|
922 |
EXPORT_C void NTimerQ::Advance(TInt aTicks) |
|
923 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
924 |
CHECK_PRECONDITIONS(MASK_INTERRUPTS_DISABLED,"NTimerQ::Advance"); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
925 |
__e32_atomic_add_rlx64(&TheTimerQ.iMsCount64, TUint64(TUint32(aTicks))); |
0 | 926 |
} |
927 |
||
928 |
||
929 |
/** Returns the period of the nanokernel timer. |
|
930 |
@return Period in microseconds |
|
931 |
@pre any context |
|
932 |
@see NTimer |
|
933 |
*/ |
|
934 |
EXPORT_C TInt NKern::TickPeriod() |
|
935 |
{ |
|
936 |
return TheTimerQ.iTickPeriod; |
|
937 |
} |
|
938 |
||
939 |
||
940 |
/** Converts a time interval to timer ticks. |
|
941 |
||
942 |
@param aMilliseconds time interval in milliseconds. |
|
943 |
@return Number of nanokernel timer ticks. Non-integral results are rounded up. |
|
944 |
||
945 |
@pre aMilliseconds should be <=2147483 to avoid integer overflow. |
|
946 |
@pre any context |
|
947 |
*/ |
|
948 |
EXPORT_C TInt NKern::TimerTicks(TInt aMilliseconds) |
|
949 |
{ |
|
950 |
__ASSERT_WITH_MESSAGE_DEBUG(aMilliseconds<=2147483,"aMilliseconds should be <=2147483","NKern::TimerTicks"); |
|
951 |
TUint32 msp=TheTimerQ.iTickPeriod; |
|
952 |
if (msp==1000) // will be true except on pathological hardware |
|
953 |
return aMilliseconds; |
|
954 |
TUint32 us=(TUint32)aMilliseconds*1000; |
|
955 |
return (us+msp-1)/msp; |
|
956 |
} |
|
957 |