author | hgs |
Mon, 24 May 2010 18:38:55 +0100 | |
changeset 134 | 95847726fe57 |
parent 90 | 947f0dc9f7a8 |
child 257 | 3e88ff8f41d5 |
child 286 | 48e57fb1237e |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies). |
2 |
// All rights reserved. |
|
3 |
// This component and the accompanying materials are made available |
|
4 |
// under the terms of the License "Eclipse Public License v1.0" |
|
5 |
// which accompanies this distribution, and is available |
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// e32\kernel\stimer.cpp |
|
15 |
// Note that timer handles are always thread relative |
|
16 |
// |
|
17 |
// |
|
18 |
||
19 |
#include <kernel/kern_priv.h> |
|
20 |
#include "execs.h" |
|
21 |
||
22 |
_LIT(KTimerThreadName,"TimerThread"); |
|
23 |
_LIT(KLitTimerMutex,"TimerMutex"); |
|
24 |
||
25 |
DMutex* TTickQ::Mutex; |
|
26 |
||
27 |
const TInt KTimerThreadPriority=27; |
|
28 |
||
29 |
const TInt KDaysFrom0ADTo2000AD=730497; // See US_TIME.CPP to verify this |
|
30 |
const TInt KSecondsPerDay=86400; |
|
31 |
||
32 |
const TInt KMaxSkippedTicks=65536; |
|
33 |
const TInt KMsDeltaLimit=2000000; |
|
34 |
||
35 |
#define __CHECK_LAST_DELTA if (TUint32(iLastDelta)>TUint32(KMaxSkippedTicks)) {NKern::Lock(); *(TInt*)0xfeedface=iLastDelta;} |
|
36 |
||
37 |
||
38 |
/** Gets the address of the tick timer DFC queue. |
|
39 |
||
40 |
@return The tick timer DFC queue. |
|
41 |
*/ |
|
42 |
EXPORT_C TDfcQue* Kern::TimerDfcQ() |
|
43 |
{ |
|
44 |
return K::TimerDfcQ; |
|
45 |
} |
|
46 |
||
47 |
TInt K::StartTickQueue() |
|
48 |
{ |
|
49 |
TInt r=Kern::MutexCreate(TTickQ::Mutex, KLitTimerMutex, KMutexOrdTimer); |
|
50 |
if (r!=KErrNone) |
|
51 |
return r; |
|
52 |
r=Kern::DfcQCreate(K::TimerDfcQ,KTimerThreadPriority,&KTimerThreadName); |
|
53 |
if (r!=KErrNone) |
|
54 |
return r; |
|
55 |
DThread* pT = _LOFF(K::TimerDfcQ->iThread, DThread, iNThread); |
|
56 |
pT->iFlags |= KThreadFlagSystemPermanent; |
|
57 |
K::SecondQ=new TSecondQ; |
|
58 |
if (!K::SecondQ) |
|
59 |
return KErrNoMemory; |
|
60 |
K::InactivityQ=new TInactivityQ; |
|
61 |
if (!K::InactivityQ) |
|
62 |
return KErrNoMemory; |
|
63 |
r=TTickQ::Init(); |
|
64 |
if (r!=KErrNone) |
|
65 |
return r; |
|
66 |
return KErrNone; |
|
67 |
} |
|
68 |
||
69 |
void AbortTimers(TBool aAbortAbsolute) |
|
70 |
{ |
|
71 |
DObjectCon& timers=*K::Containers[ETimer]; |
|
72 |
DObjectCon& threads=*K::Containers[EThread]; |
|
73 |
timers.Wait(); |
|
74 |
TInt c=timers.Count(); |
|
75 |
TInt i; |
|
76 |
for (i=0; i<c; i++) |
|
77 |
{ |
|
78 |
DTimer* pT=(DTimer*)timers[i]; |
|
79 |
pT->Abort(aAbortAbsolute); |
|
80 |
} |
|
81 |
timers.Signal(); |
|
82 |
threads.Wait(); |
|
83 |
c=threads.Count(); |
|
84 |
for (i=0; i<c; i++) |
|
85 |
{ |
|
86 |
DThread* pT=(DThread*)threads[i]; |
|
87 |
pT->AbortTimer(aAbortAbsolute); |
|
88 |
} |
|
89 |
threads.Signal(); |
|
90 |
} |
|
91 |
||
92 |
/******************************************** |
|
93 |
* Tick-based relative timer queue |
|
94 |
********************************************/ |
|
95 |
void msCallBack(TAny* aPtr) |
|
96 |
{ |
|
97 |
// __KTRACE_OPT(KTIMING,Kern::Printf("!")); |
|
98 |
TTickQ& q=*(TTickQ*)aPtr; |
|
99 |
q.iTickDfc.Add(); |
|
100 |
} |
|
101 |
||
102 |
void tickDfc(TAny* aPtr) |
|
103 |
{ |
|
104 |
TTickQ& q=*(TTickQ*)aPtr; |
|
105 |
__ASSERT_DEBUG(q.iMsTimer.i_NTimer_iState==NTimer::EIdle, *(TInt*)0xdfc01bad=0); |
|
106 |
__ASSERT_DEBUG(!q.iTickDfc.Queued(), *(TInt*)0xdfc02bad=0); |
|
107 |
__ASSERT_DEBUG(NTickCount()-q.iMsTimer.iTriggerTime<0x80000000u, *(TInt*)0xdfc03bad=0); |
|
108 |
q.Tick(); |
|
109 |
} |
|
110 |
||
111 |
TTickQ::TTickQ() |
|
112 |
: iLastDelta(0), iLastTicks(0), iRtc(0), iInTick(0), |
|
113 |
iRounding(0), |
|
114 |
iPrevRounding(0), |
|
115 |
iLastMs(0), |
|
116 |
iTickDfc(tickDfc,this,1), |
|
117 |
iMsTimer(msCallBack,this) |
|
118 |
{ |
|
119 |
// Careful with the constants here or GCC will get it wrong |
|
120 |
K::Year2000InSeconds=Int64(KDaysFrom0ADTo2000AD)*Int64(KSecondsPerDay); |
|
121 |
} |
|
122 |
||
123 |
#ifdef _DEBUG |
|
124 |
void TTickQ::Check() |
|
125 |
{ |
|
126 |
__ASSERT_DEBUG(iMsTimer.i_NTimer_iState==NTimer::EIdle, *(TInt*)0xdfc03bad=0); |
|
127 |
__ASSERT_DEBUG(!iTickDfc.Queued(), *(TInt*)0xdfc04bad=0); |
|
128 |
} |
|
129 |
#endif |
|
130 |
||
131 |
TInt TTickQ::Init() |
|
132 |
{ |
|
133 |
TTickQ* pQ=new TTickQ; |
|
134 |
if (!pQ) |
|
135 |
return KErrNoMemory; |
|
136 |
K::TickQ=pQ; |
|
137 |
pQ->iTickDfc.SetDfcQ(K::TimerDfcQ); |
|
138 |
pQ->iMsTickPeriod=NTickPeriod(); |
|
139 |
_LOFF(K::TimerDfcQ->iThread,DThread,iNThread)->iDebugMask=0x80000000; |
|
140 |
||
141 |
// These lines are tick-period dependent |
|
142 |
// Assume we can always set the tick period to 64Hz |
|
143 |
pQ->iTickPeriod=15625; |
|
144 |
pQ->iNominalTickPeriod=15625; |
|
145 |
pQ->iTicksPerSecond=64; |
|
146 |
K::SecondQ->iTicksPerDay=86400*64; |
|
147 |
||
148 |
TInt s2000=P::InitSystemTime(); |
|
149 |
__KTRACE_OPT(KBOOT,Kern::Printf("s2000=%d",s2000)); |
|
150 |
pQ->iMsTimer.iTriggerTime=NTickCount(); |
|
151 |
pQ->iLastMs=pQ->iMsTimer.iTriggerTime; |
|
152 |
TUint temp = 0; |
|
153 |
K::SetSystemTime(s2000,0,temp,ETimeSetTime|ETimeSetAllowTimeReversal); |
|
154 |
// Int64 now=s2000; |
|
155 |
// now*=1000000; |
|
156 |
// now+=K::Year2000; |
|
157 |
// now/=pQ->iNominalTickPeriod; |
|
158 |
// pQ->iRtc=now; |
|
159 |
||
160 |
// TInt seconds=s2000%86400; |
|
161 |
// __KTRACE_OPT(KBOOT,Kern::Printf("seconds=%d",seconds)); |
|
162 |
// now+=(86400-seconds)*pQ->iTicksPerSecond; |
|
163 |
// K::SecondQ->iMidnight=now; |
|
164 |
||
165 |
pQ->iMsTimer.i_NTimer_iUserFlags = ETrue; // used as start flag |
|
166 |
TTickQ::Wait(); |
|
167 |
K::SecondQ->Tick(); // start the second queue |
|
168 |
K::TickQ->Tick(); // start the tick queue |
|
169 |
TTickQ::Signal(); |
|
170 |
||
171 |
return KErrNone; |
|
172 |
} |
|
173 |
||
174 |
void TTickQ::Wait() |
|
175 |
{ |
|
176 |
if (Mutex) |
|
177 |
Kern::MutexWait(*Mutex); |
|
178 |
} |
|
179 |
||
180 |
void TTickQ::Signal() |
|
181 |
{ |
|
182 |
if (Mutex) |
|
183 |
Kern::MutexSignal(*Mutex); |
|
184 |
} |
|
185 |
||
186 |
void TTickQ::Tick() |
|
187 |
{ |
|
188 |
Wait(); |
|
189 |
__KTRACE_OPT(KTIMING,Kern::Printf("Tick!: l%d e%d",iLastDelta,IsEmpty())); |
|
190 |
||
191 |
// Make sure this DFC hasn't been queued again - this can happen if someone calls Add() |
|
192 |
// between this DFC being dispatched and executing the Wait() above; priority inheritance |
|
193 |
// can make this happen more often than you might expect. |
|
194 |
if (iTickDfc.Queued()) |
|
195 |
iTickDfc.Cancel(); |
|
196 |
||
197 |
if (K::SecondQ->iWakeUpDfc.Queued()) |
|
198 |
{ |
|
199 |
Signal(); |
|
200 |
return; |
|
201 |
} |
|
202 |
iInTick=ETrue; |
|
203 |
Update(); |
|
204 |
if (!IsEmpty() && CountDown(iLastDelta)) // Anything ready to be completed ? |
|
205 |
{ |
|
206 |
__KTRACE_OPT(KTIMING,Kern::Printf("Tick!: f%d",FirstDelta())); |
|
207 |
TTickLink* pT; |
|
208 |
while (!IsEmpty() && (pT=(TTickLink*)RemoveFirst())!=NULL) |
|
209 |
{ |
|
210 |
pT->iNext=NULL; // In case Complete() calls TTickLink::Cancel() |
|
211 |
(*pT->iCallBack)(pT->iPtr); // Dispatch the timer completion |
|
212 |
#ifdef _DEBUG |
|
213 |
Check(); |
|
214 |
#endif |
|
215 |
if (pT->iNext==NULL && pT->iPeriod!=0) // Periodic timer |
|
216 |
Add(pT,pT->iPeriod); |
|
217 |
#ifdef _DEBUG |
|
218 |
Check(); |
|
219 |
#endif |
|
220 |
} |
|
221 |
} |
|
222 |
StartTimer(); |
|
223 |
iInTick=EFalse; |
|
224 |
Signal(); |
|
225 |
} |
|
226 |
||
227 |
void TTickQ::Update() |
|
228 |
{ |
|
229 |
TInt irq = __SPIN_LOCK_IRQSAVE(TheTimerQ.iTimerSpinLock); |
|
230 |
iLastMs=iMsTimer.iTriggerTime; |
|
231 |
iLastTicks+=iLastDelta; |
|
232 |
iRtc+=iLastDelta; |
|
233 |
__SPIN_UNLOCK_IRQRESTORE(TheTimerQ.iTimerSpinLock,irq); |
|
234 |
iPrevRounding=iRounding; |
|
235 |
} |
|
236 |
||
237 |
void TTickQ::StartTimer() |
|
238 |
{ |
|
239 |
TInt delta=KMaxTInt; |
|
240 |
if (!IsEmpty()) |
|
241 |
delta=FirstDelta(); |
|
242 |
if (delta>KMaxSkippedTicks) // so that microseconds don't overflow |
|
243 |
delta=KMaxSkippedTicks; |
|
244 |
TInt delta_us=delta*iTickPeriod; // number of microseconds to next timer expiry |
|
245 |
__KTRACE_OPT(KTIMING,Kern::Printf("Tick:delta_us=%d, prdg=%d",delta_us,iPrevRounding)); |
|
246 |
delta_us-=iPrevRounding; // subtract rounding error on last completed ms timer |
|
247 |
TInt msp=iMsTickPeriod; |
|
248 |
TInt delta_ms=(delta_us+(msp>>1))/msp; // round to milliseconds |
|
249 |
iRounding=msp*delta_ms-delta_us; // save rounding error on this timer |
|
250 |
iLastDelta=delta; // save number of ticks |
|
251 |
__CHECK_LAST_DELTA; |
|
252 |
__ASSERT_DEBUG(!iTickDfc.Queued(), *(TInt*)0xdfc00bad=0); |
|
253 |
TInt r=KErrNone; |
|
254 |
if (iMsTimer.i_NTimer_iUserFlags) |
|
255 |
{ |
|
256 |
iMsTimer.i_NTimer_iUserFlags = EFalse; |
|
257 |
iMsTimer.OneShot(delta_ms); // start timer for the first time |
|
258 |
} |
|
259 |
else |
|
260 |
r=iMsTimer.Again(delta_ms); // start timer |
|
261 |
__KTRACE_OPT(KTIMING,Kern::Printf("Tick:delta=%d, r=%d, lastdelta=%d, rdg=%d",delta_ms,r,iLastDelta,iRounding)); |
|
262 |
if (r!=KErrNone) |
|
263 |
{ |
|
264 |
__ASSERT_ALWAYS(r==KErrArgument, *(TInt*)0xbad0beef=r); |
|
265 |
// requested time has already passed so manually requeue the DFC |
|
266 |
iMsTimer.iTriggerTime+=delta_ms; |
|
267 |
iTickDfc.Enque(); |
|
268 |
} |
|
269 |
} |
|
270 |
||
271 |
// Wait on mutex before calling this |
|
272 |
void TTickQ::Add(TTickLink* aLink, TInt aPeriod) |
|
273 |
{ |
|
274 |
__KTRACE_OPT(KTIMING,Kern::Printf("Tick:Add p%d i%d ld%d",aPeriod, iInTick, iLastDelta)); |
|
275 |
SDeltaQue::Add(aLink,aPeriod); |
|
276 |
if (!iInTick && aPeriod<iLastDelta) |
|
277 |
{ |
|
278 |
// need to reset millisecond timer |
|
279 |
if (iMsTimer.Cancel()) |
|
280 |
{ |
|
281 |
// the timer was actually cancelled - so it hadn't expired |
|
282 |
iMsTimer.iTriggerTime=iLastMs; |
|
283 |
StartTimer(); |
|
284 |
} |
|
285 |
} |
|
286 |
} |
|
287 |
||
288 |
void TTickQ::Synchronise() |
|
289 |
// |
|
290 |
// Update everything as if a tick had just occurred |
|
291 |
// Call with system unlocked and timer mutex held |
|
292 |
// |
|
293 |
{ |
|
294 |
TInt ms_delta=(TInt)(NTickCount()-iLastMs-1); |
|
295 |
__ASSERT_ALWAYS(ms_delta<KMsDeltaLimit,K::Fault(K::ESynchroniseMsDeltaTooBig)); |
|
296 |
TInt msp=iMsTickPeriod; |
|
297 |
TInt us_delta=ms_delta*msp+iPrevRounding; |
|
298 |
TInt tick_delta=us_delta/iTickPeriod; |
|
299 |
if (tick_delta>iLastDelta) |
|
300 |
tick_delta=iLastDelta; |
|
301 |
us_delta=tick_delta*iTickPeriod-iPrevRounding; |
|
302 |
ms_delta=(us_delta+(msp>>1))/msp; |
|
303 |
iPrevRounding=msp*ms_delta-us_delta; |
|
304 |
iLastDelta-=tick_delta; |
|
305 |
TInt irq = __SPIN_LOCK_IRQSAVE(TheTimerQ.iTimerSpinLock); |
|
306 |
iRtc+=tick_delta; |
|
307 |
iLastTicks+=tick_delta; |
|
308 |
iLastMs+=ms_delta; |
|
309 |
__SPIN_UNLOCK_IRQRESTORE(TheTimerQ.iTimerSpinLock,irq); |
|
310 |
__CHECK_LAST_DELTA; |
|
311 |
if (!IsEmpty()) |
|
312 |
{ |
|
313 |
CountDown(tick_delta); |
|
314 |
__ASSERT_ALWAYS(FirstDelta()>=0, *(TInt*)0xfacefeed=FirstDelta()); |
|
315 |
} |
|
316 |
} |
|
317 |
||
318 |
||
319 |
/** |
|
320 |
Rounds up microseconds into the number of Kernel Ticks(1/64Hz). |
|
321 |
@param a if >=0, the number of microseconds; |
|
322 |
if < 0, ABS(a) is the number of Kernel Ticks. |
|
323 |
@par aAdjust if true, the elapsed time from the last Kernel Tick is also taken into account. |
|
324 |
*/ |
|
325 |
TInt MicroSecondsToTicks(TInt a, TBool aAdjust) |
|
326 |
{ |
|
327 |
TUint32 p=K::TickQ->iTickPeriod; |
|
328 |
||
329 |
if (a<0) |
|
330 |
{ |
|
331 |
TUint32 b=(TUint32)(-a); |
|
332 |
if (aAdjust) |
|
333 |
b+=((NTickCount()-K::TickQ->iLastMs-1)* K::TickQ->iMsTickPeriod + p - 1)/p; |
|
334 |
if (b>(TUint32)KMaxTInt) b=KMaxTInt; |
|
335 |
return b; |
|
336 |
} |
|
337 |
if (a==0) |
|
338 |
a=1; |
|
339 |
TUint32 b=(TUint32)a; |
|
340 |
if (aAdjust) |
|
341 |
b += (TInt)(NTickCount()-K::TickQ->iLastMs-1)*K::TickQ->iMsTickPeriod; |
|
342 |
return (TInt)((b+p-1)/p); |
|
343 |
} |
|
344 |
||
345 |
||
346 |
||
347 |
/** |
|
348 |
Constructor for a tick timer. |
|
349 |
||
350 |
@pre Calling thread must be in a critical section. |
|
351 |
@pre Call in a thread context. |
|
352 |
@pre Kernel must be unlocked |
|
353 |
@pre interrupts enabled |
|
354 |
*/ |
|
355 |
EXPORT_C TTickLink::TTickLink() |
|
356 |
{ |
|
357 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC|MASK_CRITICAL,"TTickLink::TTickLink"); |
|
358 |
iNext=NULL; |
|
359 |
iLastLock=-1; |
|
360 |
} |
|
361 |
||
362 |
/** @internalComponent */ |
|
363 |
void TTickLink::DoCancel() |
|
364 |
{ |
|
365 |
iPeriod=0; |
|
366 |
if (iNext) |
|
367 |
{ |
|
368 |
K::TickQ->Remove(this); |
|
369 |
iNext=NULL; |
|
370 |
} |
|
371 |
} |
|
372 |
||
373 |
||
374 |
||
375 |
||
376 |
/** |
|
377 |
Adds this periodic tick timer to the tick timer queue. |
|
378 |
||
379 |
On expiry, the timer is put back onto the tick timer queue. |
|
380 |
||
381 |
@param aPeriod The timer interval in microseconds. |
|
382 |
@param aCallBack The callback function that is called every time |
|
383 |
this periodic timer expires. |
|
384 |
@param aPtr An argument that is passed to the callback function. |
|
385 |
||
386 |
@pre Calling thread must be in a critical section. |
|
387 |
@pre No fast mutex can be held. |
|
388 |
@pre Call in a thread context. |
|
389 |
@pre Kernel must be unlocked |
|
390 |
@pre interrupts enabled |
|
391 |
*/ |
|
392 |
EXPORT_C void TTickLink::Periodic(TInt aPeriod, TTickCallBack aCallBack, TAny* aPtr) |
|
393 |
{ |
|
394 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"TTickLink::Periodic"); |
|
395 |
TTickQ::Wait(); |
|
396 |
DoCancel(); |
|
397 |
iPeriod=MicroSecondsToTicks(aPeriod, EFalse); |
|
398 |
iCallBack=aCallBack; |
|
399 |
iPtr=aPtr; |
|
400 |
K::TickQ->Add(this,MicroSecondsToTicks(aPeriod, ETrue)); |
|
401 |
TTickQ::Signal(); |
|
402 |
} |
|
403 |
||
404 |
||
405 |
||
406 |
||
407 |
/** |
|
408 |
Adds this one-off tick timer to the tick timer queue. |
|
409 |
||
410 |
@param aTime The timer interval in microseconds. |
|
411 |
@param aCallBack The callback function that is called when this timer expires. |
|
412 |
@param aPtr An argument that is passed to the callback function. |
|
413 |
||
414 |
@pre Calling thread must be in a critical section. |
|
415 |
@pre No fast mutex can be held. |
|
416 |
@pre Call in a thread context. |
|
417 |
@pre Kernel must be unlocked |
|
418 |
@pre interrupts enabled |
|
419 |
*/ |
|
420 |
EXPORT_C void TTickLink::OneShot(TInt aTime, TTickCallBack aCallBack, TAny* aPtr) |
|
421 |
{ |
|
422 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"TTickLink::OneShot"); |
|
423 |
TTickQ::Wait(); |
|
424 |
DoCancel(); |
|
425 |
iCallBack=aCallBack; |
|
426 |
iPtr=aPtr; |
|
427 |
K::TickQ->Add(this,MicroSecondsToTicks(aTime, ETrue)); |
|
428 |
TTickQ::Signal(); |
|
429 |
} |
|
430 |
||
431 |
||
432 |
||
433 |
||
434 |
/** |
|
435 |
Cancels this tick timer. |
|
436 |
||
437 |
This timer object is removed from the tick timer queue. |
|
438 |
||
439 |
@pre Calling thread must be in a critical section. |
|
440 |
@pre No fast mutex can be held. |
|
441 |
@pre Call in a thread context. |
|
442 |
@pre Kernel must be unlocked |
|
443 |
@pre interrupts enabled |
|
444 |
*/ |
|
445 |
EXPORT_C void TTickLink::Cancel() |
|
446 |
{ |
|
447 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"TTickLink::Cancel"); |
|
448 |
TTickQ::Wait(); |
|
449 |
DoCancel(); |
|
450 |
TTickQ::Signal(); |
|
451 |
} |
|
452 |
||
453 |
||
454 |
||
455 |
||
456 |
/** |
|
457 |
Adds this tick timer to the tick timer queue. |
|
458 |
||
459 |
@param aTicks The timer interval in ticks. This is expected to be |
|
460 |
the tick count for the next lock. |
|
461 |
@param aCallBack The callback function that is called when this timer expires. |
|
462 |
@param aPtr An argument that is passed to the callback function. |
|
463 |
||
464 |
@pre Calling thread must be in a critical section. |
|
465 |
@pre No fast mutex can be held. |
|
466 |
@pre Call in a thread context. |
|
467 |
@pre Kernel must be unlocked |
|
468 |
@pre interrupts enabled |
|
469 |
*/ |
|
470 |
EXPORT_C void TTickLink::Lock(TInt aTicks, TTickCallBack aCallBack, TAny* aPtr) |
|
471 |
{ |
|
472 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"TTickLink::Lock"); |
|
473 |
TTickQ::Wait(); |
|
474 |
DoCancel(); |
|
475 |
iCallBack=aCallBack; |
|
476 |
iPtr=aPtr; |
|
477 |
K::TickQ->Add(this, aTicks); |
|
478 |
TTickQ::Signal(); |
|
479 |
} |
|
480 |
||
481 |
||
482 |
||
483 |
||
484 |
/** |
|
485 |
Get the tick count for the next lock. |
|
486 |
THIS FUNCTION ASSUMES 64 TICKS PER SECOND |
|
487 |
||
488 |
@return KErrGeneral with aTickCount = number of ticks until ETwelveOClock if the |
|
489 |
requested lock is more than 1 second from the last completed lock. |
|
490 |
KErrNone with aTickCount = number of ticks until the next aMark otherwise. |
|
491 |
@pre Wait on TimerMutex before calling this. |
|
492 |
||
493 |
@internalComponent |
|
494 |
*/ |
|
495 |
TInt TTickLink::GetNextLock(TTimerLockSpec aLock, TInt& aTickCount) const |
|
496 |
{ |
|
497 |
__ASSERT_WITH_MESSAGE_MUTEX(TTickQ::Mutex,"Wait on TimerMutex before calling this","TTickLink::GetNextLock"); |
|
498 |
TTickQ& tq=*K::TickQ; |
|
499 |
tq.Synchronise(); |
|
500 |
TUint odd_ticks=TUint(tq.iRtc)&63; |
|
501 |
TUint lock_ticks=((aLock+1)*64+6)/12; |
|
502 |
Int64 second=tq.iRtc-odd_ticks; |
|
503 |
Int64 next_lock=second+lock_ticks; |
|
504 |
if (next_lock<=tq.iRtc) |
|
505 |
next_lock+=64; |
|
506 |
if (next_lock-iLastLock>64) |
|
507 |
{ |
|
508 |
// Lock on ETwelveOClock |
|
509 |
aTickCount=64-odd_ticks; |
|
510 |
__KTRACE_OPT(KTIMING,Kern::Printf("GS%x",aTickCount)); |
|
511 |
return KErrGeneral; |
|
512 |
} |
|
513 |
// Lock on aMark |
|
514 |
aTickCount=TInt(next_lock-tq.iRtc); |
|
515 |
__KTRACE_OPT(KTIMING,Kern::Printf("GL%x",aTickCount)); |
|
516 |
return KErrNone; |
|
517 |
} |
|
518 |
||
519 |
/******************************************** |
|
520 |
* Absolute timer queue |
|
521 |
********************************************/ |
|
522 |
void TSecondQ::TickCallBack(TAny* aPtr) |
|
523 |
{ |
|
524 |
((TSecondQ*)aPtr)->Tick(); |
|
525 |
} |
|
526 |
||
527 |
void TSecondQ::WakeUpDfc(TAny* aPtr) |
|
528 |
{ |
|
529 |
TSecondQ& sq=*(TSecondQ*)aPtr; |
|
530 |
TTickQ& tq=*K::TickQ; |
|
531 |
TTickQ::Wait(); |
|
532 |
__ASSERT_ALWAYS(tq.iInTick,K::Fault(K::ETickQNotLocked)); |
|
533 |
sq.iTimer.DoCancel(); // make sure second queue timer is stopped |
|
534 |
sq.Tick(); // call back any timers which have already expired and restart second queue |
|
535 |
if (!tq.IsEmpty() && tq.FirstDelta()<=0) // Anything ready to be completed now? |
|
536 |
tq.Tick(); // restart tick queue |
|
537 |
else |
|
538 |
{ |
|
539 |
tq.StartTimer(); // restart tick queue |
|
540 |
tq.iInTick=EFalse; |
|
541 |
} |
|
542 |
TTickQ::Signal(); |
|
543 |
} |
|
544 |
||
545 |
TSecondQ::TSecondQ() |
|
546 |
: iInTick(0), iWakeUpDfc(WakeUpDfc,this,K::TimerDfcQ,2) |
|
547 |
{ |
|
548 |
iTimer.iPtr=this; |
|
549 |
iTimer.iCallBack=TickCallBack; |
|
550 |
} |
|
551 |
||
552 |
void TSecondQ::Tick() |
|
553 |
{ |
|
554 |
// Called in tick timer callback, so mutex already held |
|
555 |
// RTC value already incremented |
|
556 |
Int64 rtc=K::TickQ->iRtc; |
|
557 |
iInTick=ETrue; |
|
558 |
if (rtc==iMidnight) |
|
559 |
{ |
|
560 |
iMidnight+=iTicksPerDay; |
|
561 |
Kern::AsyncNotifyChanges(EChangesMidnightCrossover); |
|
562 |
} |
|
563 |
while (!IsEmpty()) |
|
564 |
{ |
|
565 |
TSecondLink* pS=(TSecondLink*)First(); |
|
566 |
if (pS->iTime>rtc) |
|
567 |
break; |
|
568 |
if (K::PowerModel) |
|
569 |
K::PowerModel->AbsoluteTimerExpired(); |
|
570 |
pS->Deque(); |
|
571 |
pS->iNext=NULL; |
|
572 |
(*pS->iCallBack)(pS->iPtr); |
|
573 |
} |
|
574 |
StartTimer(); |
|
575 |
iInTick=EFalse; |
|
576 |
} |
|
577 |
||
578 |
void TSecondQ::StartTimer() |
|
579 |
{ |
|
580 |
TSecondLink* pS=(TSecondLink*)First(); |
|
581 |
Int64 rtc=K::TickQ->iRtc; |
|
582 |
TInt delta=(TInt)(iMidnight-rtc); // ticks before midnight |
|
583 |
if (pS!=&iA) |
|
584 |
{ |
|
585 |
Int64 delta64=pS->iTime-rtc; // ticks before next timer |
|
586 |
if (delta64<delta) |
|
587 |
delta=(TInt)delta64; |
|
588 |
} |
|
589 |
iNextTrigger=rtc+delta; |
|
590 |
K::TickQ->Add(&iTimer, delta); |
|
591 |
} |
|
592 |
||
593 |
// Wait on mutex before calling this |
|
594 |
void TSecondQ::Add(TSecondLink* aLink) |
|
595 |
{ |
|
596 |
Int64 time=aLink->iTime; |
|
597 |
TSecondLink* pS=(TSecondLink*)First(); |
|
598 |
SDblQueLink* anchor=&iA; |
|
599 |
while(pS!=anchor && pS->iTime<=time) |
|
600 |
pS=(TSecondLink*)pS->iNext; |
|
601 |
aLink->InsertBefore(pS); |
|
602 |
if (!iInTick && time<iNextTrigger) |
|
603 |
{ |
|
604 |
iTimer.DoCancel(); |
|
605 |
StartTimer(); |
|
606 |
} |
|
607 |
} |
|
608 |
||
609 |
TInt TSecondQ::FirstDelta() |
|
610 |
{ |
|
611 |
if (IsEmpty()) |
|
612 |
return KMaxTInt; |
|
613 |
TSecondLink* pS=(TSecondLink*)First(); |
|
614 |
TTimeK first=pS->iTime*K::TickQ->iNominalTickPeriod; |
|
615 |
TTimeK now=Kern::SystemTime(); |
|
616 |
Int64 delta=first-now; |
|
617 |
delta/=1000000; |
|
618 |
if (delta>KMaxTInt) |
|
619 |
return KMaxTInt; |
|
620 |
return (TInt)delta; |
|
621 |
} |
|
622 |
||
623 |
TTimeK TSecondQ::WakeupTime() |
|
624 |
// |
|
625 |
// Called machine goes to standby |
|
626 |
// Timer mutex is already held |
|
627 |
// |
|
628 |
{ |
|
629 |
if (IsEmpty()) |
|
630 |
return 0; |
|
631 |
TSecondLink* pS=(TSecondLink*)First(); |
|
632 |
return pS->iTime*K::TickQ->iNominalTickPeriod; |
|
633 |
} |
|
634 |
||
635 |
void TSecondQ::WakeUp() |
|
636 |
// |
|
637 |
// Called after machine wakes up |
|
638 |
// Timer mutex is already held |
|
639 |
// |
|
640 |
{ |
|
641 |
TInt secs; |
|
642 |
AbortTimers(EFalse); // abort any locked timers |
|
643 |
TInt r=A::SystemTimeInSecondsFrom2000(secs); // get hardware RTC value |
|
644 |
TUint changes = 0; |
|
645 |
if (r==KErrNone) |
|
646 |
{ |
|
647 |
// Apply nonsecure offset if secure clock in use |
|
648 |
if (K::SecureClockStatus == ESecureClockOk) |
|
649 |
{ |
|
650 |
secs += K::NonSecureOffsetSeconds; |
|
651 |
} |
|
652 |
K::TickQ->Synchronise(); |
|
653 |
r=K::SetSystemTime(secs,0,changes,ETimeSetTime); // update K::SecondQ->iRtc, don't allow it to go backwards |
|
654 |
__KTRACE_OPT(KPOWER,Kern::Printf("new time=%d, r=%d",secs,r)); |
|
655 |
} |
|
656 |
K::InactivityQ->Reset(); |
|
657 |
if (r<0) |
|
658 |
return; // time has not changed |
|
659 |
||
660 |
if (!K::TickQ->iInTick) // if iInTick is already set, wake up DFC is already queued |
|
661 |
{ |
|
662 |
K::TickQ->iInTick=ETrue; // stop anyone else restarting timer queues |
|
663 |
iWakeUpDfc.Enque(); // this will restart the timer queues |
|
664 |
} |
|
665 |
if (changes & EChangesMidnightCrossover) |
|
666 |
Kern::AsyncNotifyChanges(EChangesMidnightCrossover); |
|
667 |
} |
|
668 |
||
669 |
||
670 |
||
671 |
||
672 |
/** |
|
673 |
Constructor for a second timer. |
|
674 |
||
675 |
@pre Calling thread must be in a critical section. |
|
676 |
@pre Call in a thread context. |
|
677 |
@pre Kernel must be unlocked |
|
678 |
@pre interrupts enabled |
|
679 |
*/ |
|
680 |
EXPORT_C TSecondLink::TSecondLink() |
|
681 |
{ |
|
682 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC|MASK_CRITICAL,"TSecondLink::TSecondLink"); |
|
683 |
iNext=NULL; |
|
684 |
} |
|
685 |
||
686 |
||
687 |
||
688 |
||
689 |
/** |
|
690 |
Adds this timer to the second timer queue. |
|
691 |
||
692 |
@param aUTCTime The absolute date and time when the timer is to expire, in UTC. |
|
693 |
@param aCallBack The callback function that is called when this timer expires. |
|
694 |
@param aPtr An argument that is passed to the callback function. |
|
695 |
||
696 |
@return KErrNone, if successful; |
|
697 |
KErrUnderflow, if the specified time is earlier than the system time. |
|
698 |
KErrOverflow, if the specified time is too big. |
|
699 |
||
700 |
@pre Calling thread must be in a critical section. |
|
701 |
@pre No fast mutex can be held. |
|
702 |
@pre Call in a thread context. |
|
703 |
@pre Kernel must be unlocked |
|
704 |
@pre interrupts enabled |
|
705 |
*/ |
|
706 |
EXPORT_C TInt TSecondLink::At(const TTimeK& aUTCTime, TSecondCallBack aCallBack, TAny* aPtr) |
|
707 |
{ |
|
708 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"TSecondLink::At"); |
|
709 |
TTickQ::Wait(); |
|
710 |
iTime=aUTCTime; |
|
711 |
iCallBack=aCallBack; |
|
712 |
iPtr=aPtr; |
|
713 |
if (iTime<=Kern::SystemTime()) |
|
714 |
{ |
|
715 |
TTickQ::Signal(); |
|
716 |
return KErrUnderflow; |
|
717 |
} |
|
718 |
iTime=(iTime+999999)/1000000; // seconds from 00:00:00 01-01-0000 UTC, rounded up |
|
719 |
Int64 y2k=Int64(KDaysFrom0ADTo2000AD)*Int64(KSecondsPerDay); |
|
720 |
Int64 delta2k=iTime-y2k; |
|
721 |
if (delta2k>KMaxTInt) |
|
722 |
{ |
|
723 |
TTickQ::Signal(); |
|
724 |
return KErrOverflow; |
|
725 |
} |
|
726 |
iTime*=K::TickQ->iTicksPerSecond; |
|
727 |
K::SecondQ->Add(this); |
|
728 |
TTickQ::Signal(); |
|
729 |
return KErrNone; |
|
730 |
} |
|
731 |
||
732 |
||
733 |
||
734 |
||
735 |
/** |
|
736 |
Cancels this timer. |
|
737 |
||
738 |
This timer object is removed from the second timer queue. |
|
739 |
||
740 |
@pre Calling thread must be in a critical section. |
|
741 |
@pre No fast mutex can be held. |
|
742 |
@pre Call in a thread context. |
|
743 |
@pre Kernel must be unlocked |
|
744 |
@pre interrupts enabled |
|
745 |
*/ |
|
746 |
EXPORT_C void TSecondLink::Cancel() |
|
747 |
// |
|
748 |
// Cancel a pending timer. |
|
749 |
// |
|
750 |
{ |
|
751 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"TSecondLink::Cancel"); |
|
752 |
TTickQ::Wait(); |
|
753 |
if (iNext) |
|
754 |
{ |
|
755 |
Deque(); |
|
756 |
iNext=NULL; |
|
757 |
} |
|
758 |
TTickQ::Signal(); |
|
759 |
} |
|
760 |
||
761 |
/******************************************** |
|
762 |
* Inactivity timer queue |
|
763 |
********************************************/ |
|
764 |
/** |
|
765 |
Constructor for an inactivity timer. |
|
766 |
||
767 |
@pre Calling thread must be in a critical section. |
|
768 |
@pre Call in a thread context. |
|
769 |
@pre Kernel must be unlocked |
|
770 |
@pre interrupts enabled |
|
771 |
*/ |
|
772 |
EXPORT_C TInactivityLink::TInactivityLink() |
|
773 |
{ |
|
774 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED|MASK_INTERRUPTS_ENABLED|MASK_NOT_ISR|MASK_NOT_IDFC|MASK_CRITICAL,"TInactivityLink::TInactivityLink"); |
|
775 |
iNext=NULL; |
|
776 |
} |
|
777 |
||
778 |
||
779 |
||
780 |
||
781 |
/** |
|
782 |
Adds this inactivity timer to the inactivity timer queue. |
|
783 |
||
784 |
@param aSeconds The period of inactivity needed to trigger this timer, in seconds. |
|
785 |
@param aCallBack The callback function that is called when this timer expires. |
|
786 |
@param aPtr An argument that is passed to the callback function. |
|
787 |
||
788 |
@return KErrNone, if successful; |
|
789 |
KErrArgument, if aSeconds is negative; |
|
790 |
KErrOverflow, if the number of ticks corresponding to aSeconds |
|
791 |
overflows a 32 bit signed integer. |
|
792 |
||
793 |
@pre Calling thread must be in a critical section. |
|
794 |
@pre No fast mutex can be held. |
|
795 |
@pre Call in a thread context. |
|
796 |
@pre Kernel must be unlocked |
|
797 |
@pre interrupts enabled |
|
798 |
*/ |
|
799 |
EXPORT_C TInt TInactivityLink::Start(TInt aSeconds, TInactivityCallBack aCallBack, TAny* aPtr) |
|
800 |
{ |
|
801 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"TInactivityLink::Start"); |
|
802 |
// Time period can't be more than 2^31 ticks (just over 1 year at 64Hz) |
|
803 |
TTickQ& tq=*K::TickQ; |
|
804 |
TInactivityQ& iq=*K::InactivityQ; |
|
805 |
if (aSeconds<0) |
|
806 |
return KErrArgument; |
|
807 |
Int64 ticks(aSeconds); |
|
808 |
ticks*=Int64(tq.iTicksPerSecond); |
|
809 |
if (ticks>KMaxTInt) |
|
810 |
return KErrOverflow; |
|
811 |
TTickQ::Wait(); |
|
812 |
if (iNext) |
|
813 |
{ |
|
814 |
Deque(); |
|
815 |
iNext=NULL; |
|
816 |
} |
|
817 |
iTime=(TUint32)ticks; |
|
818 |
iPtr=aPtr; |
|
819 |
iCallBack=aCallBack; |
|
820 |
TUint32 tc=Kern::TickCount(); |
|
821 |
NKern::LockSystem(); |
|
822 |
TUint32 lev = iq.iLastEventTime; |
|
823 |
SDblQue* pQ=&iq; |
|
824 |
if (tc-lev>=iTime) |
|
825 |
{ |
|
826 |
pQ=&iq.iPending; |
|
827 |
if (pQ->IsEmpty()) |
|
828 |
{ |
|
829 |
// adding to empty pending queue, do with system lock held |
|
830 |
// to prevent race with AddEvent |
|
831 |
pQ->Add(this); |
|
832 |
pQ=NULL; |
|
833 |
} |
|
834 |
} |
|
835 |
NKern::UnlockSystem(); |
|
836 |
if (pQ) |
|
837 |
{ |
|
838 |
TInactivityLink* pL=(TInactivityLink*)pQ->First(); |
|
839 |
while (pL!=&pQ->iA && pL->iTime<=iTime) |
|
840 |
pL=(TInactivityLink*)pL->iNext; |
|
841 |
InsertBefore(pL); |
|
842 |
if (pQ==&iq && iq.First()==this && !iq.iInTick) |
|
843 |
{ |
|
844 |
// need to restart tick timer |
|
845 |
iq.iTimer.DoCancel(); |
|
846 |
tq.Add(&iq.iTimer, iTime+lev-tq.iLastTicks); |
|
847 |
} |
|
848 |
} |
|
849 |
TTickQ::Signal(); |
|
850 |
return KErrNone; |
|
851 |
} |
|
852 |
||
853 |
||
854 |
||
855 |
||
856 |
/** |
|
857 |
Cancels this inactivity timer. |
|
858 |
||
859 |
This timer object is removed from the inactivity timer queue. |
|
860 |
||
861 |
@pre Calling thread must be in a critical section. |
|
862 |
@pre No fast mutex can be held. |
|
863 |
@pre Call in a thread context. |
|
864 |
@pre Kernel must be unlocked |
|
865 |
@pre interrupts enabled |
|
866 |
*/ |
|
867 |
EXPORT_C void TInactivityLink::Cancel() |
|
868 |
{ |
|
869 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"TInactivityLink::Cancel"); |
|
870 |
TTickQ::Wait(); |
|
871 |
if (iNext) |
|
872 |
{ |
|
873 |
NKern::LockSystem(); |
|
874 |
Deque(); |
|
875 |
NKern::UnlockSystem(); |
|
876 |
iNext=NULL; |
|
877 |
} |
|
878 |
TTickQ::Signal(); |
|
879 |
} |
|
880 |
||
881 |
void TInactivityQ::TimerCallBack(TAny* aPtr) |
|
882 |
{ |
|
883 |
((TInactivityQ*)aPtr)->Expired(ETrue); |
|
884 |
} |
|
885 |
||
886 |
void TInactivityQ::EventDfcFn(TAny* aPtr) |
|
887 |
{ |
|
888 |
((TInactivityQ*)aPtr)->EventDfc(); |
|
889 |
} |
|
890 |
||
891 |
TInactivityQ::TInactivityQ() |
|
892 |
: iLastEventTime(0), |
|
893 |
iInTick(EFalse), |
|
894 |
iEventDfc(EventDfcFn,this,K::TimerDfcQ,1) |
|
895 |
{ |
|
896 |
iTimer.iPtr=this; |
|
897 |
iTimer.iCallBack=TimerCallBack; |
|
898 |
} |
|
899 |
||
900 |
void TInactivityQ::Reset() |
|
901 |
{ |
|
902 |
TUint32 tc=Kern::TickCount(); |
|
903 |
NKern::LockSystem(); |
|
904 |
iLastEventTime=tc; |
|
905 |
if (!iPending.IsEmpty()) |
|
906 |
iEventDfc.Enque(SYSTEM_LOCK); |
|
907 |
else |
|
908 |
NKern::UnlockSystem(); |
|
909 |
} |
|
910 |
||
911 |
TInt TInactivityQ::InactiveTime() |
|
912 |
{ |
|
913 |
TUint32 lev=iLastEventTime; |
|
914 |
TUint32 tc=Kern::TickCount(); |
|
915 |
tc-=lev; |
|
916 |
if (tc>0x80000000u) |
|
917 |
tc=0x80000000u; |
|
918 |
tc/=K::TickQ->iTicksPerSecond; |
|
919 |
return (TInt)tc; |
|
920 |
} |
|
921 |
||
922 |
void TInactivityQ::Expired(TBool aTicksUpdated) |
|
923 |
{ |
|
924 |
// called in tick timer call back, so timer mutex held |
|
925 |
TTickQ& tq=*K::TickQ; |
|
926 |
TUint32 tc; |
|
927 |
if (aTicksUpdated) |
|
928 |
tc=tq.iLastTicks-iLastEventTime; |
|
929 |
else |
|
930 |
tc=Kern::TickCount()-iLastEventTime; |
|
931 |
iInTick=ETrue; |
|
932 |
while (!IsEmpty()) |
|
933 |
{ |
|
934 |
TInactivityLink* pI=(TInactivityLink*)First(); |
|
935 |
if (tc<pI->iTime) |
|
936 |
{ |
|
937 |
tq.Add(&iTimer, pI->iTime-tc); |
|
938 |
break; |
|
939 |
} |
|
940 |
pI->Deque(); |
|
941 |
pI->iNext=NULL; |
|
942 |
(*pI->iCallBack)(pI->iPtr); |
|
943 |
} |
|
944 |
iInTick=EFalse; |
|
945 |
} |
|
946 |
||
947 |
void TInactivityQ::EventDfc() |
|
948 |
{ |
|
949 |
// called after an event to transfer pending timers to active queue |
|
950 |
TTickQ::Wait(); |
|
951 |
||
952 |
SDblQueLink* anchor=&iA; |
|
953 |
TInactivityLink* pActL=(TInactivityLink*)iA.iNext; // first active timer |
|
954 |
TInactivityLink* pOldFirstActive=pActL; |
|
955 |
||
956 |
while (!iPending.IsEmpty()) |
|
957 |
{ |
|
958 |
TInactivityLink* pPendL=(TInactivityLink*)iPending.First(); |
|
959 |
pPendL->Deque(); |
|
960 |
TUint32 pending_time=pPendL->iTime; |
|
961 |
while (pActL!=anchor && pending_time>=pActL->iTime) |
|
962 |
pActL=(TInactivityLink*)pActL->iNext; // loop until pActL expires after pPendL |
|
963 |
pPendL->InsertBefore(pActL); // add pending one before first later one |
|
964 |
// leave pActL where it is - OK since pending Q is ordered |
|
965 |
} |
|
966 |
||
967 |
// prevent double calling of this DFC |
|
968 |
// pending queue can only become nonempty by adding a TInactivityLink |
|
969 |
// which would require TTickQ::Wait() to be called |
|
970 |
iEventDfc.Cancel(); |
|
971 |
||
972 |
pActL=(TInactivityLink*)iA.iNext; // first active timer |
|
973 |
if (pActL!=pOldFirstActive) |
|
974 |
{ |
|
975 |
// need to restart tick timer |
|
976 |
iTimer.DoCancel(); |
|
977 |
Expired(EFalse); |
|
978 |
} |
|
979 |
||
980 |
TTickQ::Signal(); |
|
981 |
} |
|
982 |
||
983 |
/******************************************** |
|
984 |
* DTimer class |
|
985 |
********************************************/ |
|
986 |
DTimer::DTimer() |
|
987 |
{} |
|
988 |
||
989 |
DTimer::~DTimer() |
|
990 |
{ |
|
991 |
// cancel timer before destroying |
|
992 |
Cancel(); |
|
993 |
} |
|
994 |
||
995 |
TInt DTimer::Create(DThread *aThread) |
|
996 |
// |
|
997 |
// Create a Timer. Always owned by a thread. |
|
998 |
// |
|
999 |
{ |
|
1000 |
__KTRACE_OPT(KTHREAD,Kern::Printf("DTimer::Create thread %O",aThread)); |
|
1001 |
SetOwner(aThread); |
|
1002 |
return iTimer.Create(); |
|
1003 |
} |
|
1004 |
||
1005 |
TInt DTimer::RequestUserHandle(DThread* aThread, TOwnerType aType) |
|
1006 |
{ |
|
1007 |
if (aThread!=Owner() || aType!=EOwnerThread) |
|
1008 |
return KErrPermissionDenied; |
|
1009 |
return KErrNone; |
|
1010 |
} |
|
1011 |
||
1012 |
void DTimer::TimerComplete(TAny* aPtr) |
|
1013 |
// |
|
1014 |
// Called when the relative timer completes. |
|
1015 |
// |
|
1016 |
{ |
|
1017 |
DTimer* pT=FromPtr(aPtr); |
|
1018 |
if (pT->iTimer.iState == TTimer::EWaiting) |
|
1019 |
{ |
|
1020 |
Kern::QueueRequestComplete(pT->Owner(),pT->iTimer.iRequest,KErrNone); |
|
1021 |
pT->iTimer.iState = (TUint8)TTimer::EIdle; |
|
1022 |
} |
|
1023 |
} |
|
1024 |
||
1025 |
void DTimer::SecondComplete(TAny* aPtr) |
|
1026 |
// |
|
1027 |
// Called when the absolute timer completes. |
|
1028 |
// |
|
1029 |
{ |
|
1030 |
DTimer* pT=FromPtr(aPtr); |
|
1031 |
if (pT->iTimer.iState == TTimer::EWaiting) |
|
1032 |
{ |
|
1033 |
Kern::QueueRequestComplete(pT->Owner(),pT->iTimer.iRequest,KErrNone); |
|
1034 |
pT->iTimer.iState = (TUint8)TTimer::EIdle; |
|
1035 |
} |
|
1036 |
} |
|
1037 |
||
1038 |
void DTimer::LockComplete(TAny* aPtr) |
|
1039 |
// |
|
1040 |
// Called when the locked timer completes. |
|
1041 |
// |
|
1042 |
{ |
|
1043 |
DTimer* pT=(DTimer*)aPtr; |
|
1044 |
if (pT->iTimer.iState == TTimer::EWaiting) |
|
1045 |
{ |
|
1046 |
pT->iTimer.TickLink().iLastLock=K::TickQ->iRtc; |
|
1047 |
__KTRACE_OPT(KTIMING,Kern::Printf("LC%lx",pT->iTimer.TickLink().iLastLock)); |
|
1048 |
Kern::QueueRequestComplete(pT->Owner(),pT->iTimer.iRequest,KErrNone); |
|
1049 |
pT->iTimer.iState = (TUint8)TTimer::EIdle; |
|
1050 |
} |
|
1051 |
} |
|
1052 |
||
1053 |
void DTimer::LockSynchronize(TAny* aPtr) |
|
1054 |
// |
|
1055 |
// Called when the locked timer synchronises |
|
1056 |
// |
|
1057 |
{ |
|
1058 |
DTimer* pT=(DTimer*)aPtr; |
|
1059 |
if (pT->iTimer.iState == TTimer::EWaiting) |
|
1060 |
{ |
|
1061 |
pT->iTimer.TickLink().iLastLock=K::TickQ->iRtc; |
|
1062 |
__KTRACE_OPT(KTIMING,Kern::Printf("LS%lx",pT->iTimer.TickLink().iLastLock)); |
|
1063 |
Kern::QueueRequestComplete(pT->Owner(),pT->iTimer.iRequest,KErrGeneral); |
|
1064 |
pT->iTimer.iState = (TUint8)TTimer::EIdle; |
|
1065 |
} |
|
1066 |
} |
|
1067 |
||
1068 |
void DTimer::MsComplete(TAny* aTimer) |
|
1069 |
{ |
|
1070 |
// called in DFC, system unlocked |
|
1071 |
DTimer* pT=FromPtr(aTimer); |
|
1072 |
NKern::LockSystem(); |
|
1073 |
pT->iTimer.iState = (TUint8)TTimer::EIdle; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1074 |
Kern::QueueRequestComplete(pT->Owner(), pT->iTimer.iRequest, KErrNone); |
0 | 1075 |
NKern::UnlockSystem(); |
1076 |
} |
|
1077 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1078 |
void DTimer::HighRes(TRequestStatus& aStatus, TInt aInterval) |
0 | 1079 |
{ |
1080 |
// enter and return with system locked |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1081 |
TInt r=iTimer.AfterHighRes(aInterval,MsComplete,aStatus); |
0 | 1082 |
if (r!=KErrNone) |
1083 |
K::PanicCurrentThread(ETimerAlreadyPending); |
|
1084 |
} |
|
1085 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1086 |
void DTimer::AgainHighRes(TRequestStatus& aStatus, TInt aInterval) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1087 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1088 |
// enter and return with system locked |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1089 |
TInt r=iTimer.AgainHighRes(aInterval,MsComplete,aStatus); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1090 |
if (r==KErrInUse) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1091 |
K::PanicCurrentThread(ETimerAlreadyPending); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1092 |
else if (r!=KErrNone) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1093 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1094 |
TRequestStatus* status = &aStatus; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1095 |
Kern::RequestComplete(status, r); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1096 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1097 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1098 |
|
0 | 1099 |
void DTimer::Cancel() |
1100 |
// |
|
1101 |
// Cancel an outstanding request. |
|
1102 |
// system unlocked here |
|
1103 |
// |
|
1104 |
{ |
|
1105 |
iTimer.Cancel(Owner()); |
|
1106 |
} |
|
1107 |
||
1108 |
void DTimer::Abort(TBool aAbortAbsolute) |
|
1109 |
// |
|
1110 |
// Abort an outstanding request. |
|
1111 |
// TimerMutex is already held here. |
|
1112 |
// |
|
1113 |
{ |
|
1114 |
TInt typeMask=aAbortAbsolute?(TTimer::ELocked|TTimer::EAbsolute):(TTimer::ELocked); |
|
1115 |
iTimer.Abort(Owner(),typeMask); |
|
1116 |
} |
|
1117 |
||
1118 |
TInt DTimer::After(TRequestStatus& aStatus, TInt anInterval) |
|
1119 |
// |
|
1120 |
// Request a relative time. |
|
1121 |
// System is unlocked here |
|
1122 |
// |
|
1123 |
{ |
|
1124 |
return iTimer.After(anInterval,TimerComplete,aStatus); |
|
1125 |
} |
|
1126 |
||
1127 |
TInt DTimer::At(TRequestStatus& aStatus, const TTimeK& aTime) |
|
1128 |
// |
|
1129 |
// Request an absolute time. |
|
1130 |
// |
|
1131 |
{ |
|
1132 |
return iTimer.At(aTime,SecondComplete,aStatus); |
|
1133 |
} |
|
1134 |
||
1135 |
TInt DTimer::Inactivity(TRequestStatus& aStatus, TInt aSeconds) |
|
1136 |
// |
|
1137 |
// Request an inactivity time. |
|
1138 |
// |
|
1139 |
{ |
|
1140 |
return iTimer.Inactivity(aSeconds,TimerComplete,aStatus); |
|
1141 |
} |
|
1142 |
||
1143 |
TInt DTimer::Lock(TRequestStatus& aStatus, TTimerLockSpec aLock) |
|
1144 |
// |
|
1145 |
// Request a synchronisation lock to a fraction of a second. |
|
1146 |
// |
|
1147 |
{ |
|
1148 |
TTickQ::Wait(); |
|
1149 |
NKern::LockSystem(); |
|
1150 |
if (iTimer.iState != TTimer::EIdle || iTimer.iRequest->SetStatus(&aStatus) != KErrNone) |
|
1151 |
{ |
|
1152 |
NKern::UnlockSystem(); |
|
1153 |
TTickQ::Signal(); |
|
1154 |
return KErrInUse; |
|
1155 |
} |
|
1156 |
iTimer.iState = (TUint8)TTimer::EWaiting; |
|
1157 |
if (iTimer.Type()!=TTimer::ELocked) |
|
1158 |
iTimer.SetType(TTimer::ELocked); |
|
1159 |
NKern::UnlockSystem(); |
|
1160 |
TInt ticks; |
|
1161 |
TInt r=iTimer.TickLink().GetNextLock(aLock, ticks); |
|
1162 |
if (r==KErrNone) |
|
1163 |
iTimer.TickLink().Lock(ticks,LockComplete,this); |
|
1164 |
else |
|
1165 |
iTimer.TickLink().Lock(ticks,LockSynchronize,this); |
|
1166 |
TTickQ::Signal(); |
|
1167 |
return KErrNone; |
|
1168 |
} |
|
1169 |
||
1170 |
||
1171 |
/******************************************** |
|
1172 |
* Multipurpose timer |
|
1173 |
********************************************/ |
|
1174 |
||
1175 |
TInt TTimer::Create() |
|
1176 |
{ |
|
1177 |
return Kern::CreateClientRequest(iRequest); |
|
1178 |
} |
|
1179 |
||
1180 |
TTimer::~TTimer() |
|
1181 |
{ |
|
1182 |
Kern::DestroyClientRequest(iRequest); |
|
1183 |
} |
|
1184 |
||
1185 |
void TTimer::SetType(TTimerType aType) |
|
1186 |
{ |
|
1187 |
switch (aType) |
|
1188 |
{ |
|
1189 |
case ERelative: |
|
1190 |
case ELocked: |
|
1191 |
new (&TickLink()) TTickLink; |
|
1192 |
break; |
|
1193 |
case EAbsolute: |
|
1194 |
new (&SecondLink()) TSecondLink; |
|
1195 |
break; |
|
1196 |
case EHighRes: |
|
1197 |
break; |
|
1198 |
case EInactivity: |
|
1199 |
new (&Inact()) TInactivityLink; |
|
1200 |
break; |
|
1201 |
default: |
|
1202 |
K::Fault(K::EBadTimerType); |
|
1203 |
} |
|
1204 |
iType=(TUint8)aType; |
|
1205 |
} |
|
1206 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1207 |
TInt TTimer::AfterHighRes(TInt aInterval, NTimerFn aFunction, TRequestStatus& aStatus) |
0 | 1208 |
{ |
1209 |
// enter and return with system locked |
|
1210 |
if (iState!=EIdle || iRequest->SetStatus(&aStatus) != KErrNone) |
|
1211 |
return KErrInUse; |
|
1212 |
iState = (TUint8)EWaitHighRes; |
|
1213 |
SetType(EHighRes); |
|
1214 |
TInt msp=NTickPeriod(); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1215 |
TInt t=(TInt)(((TUint)aInterval+msp-1)/msp); // convert microseconds to milliseconds, rounding up |
0 | 1216 |
new (&Ms()) NTimer(aFunction,this); |
1217 |
Ms().OneShot(t,ETrue); // start millisecond timer, complete in DFC |
|
1218 |
return KErrNone; |
|
1219 |
} |
|
1220 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1221 |
TInt TTimer::AgainHighRes(TInt aInterval, NTimerFn aFunction, TRequestStatus& aStatus) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1222 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1223 |
// enter and return with system locked |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1224 |
if (iState!=EIdle) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1225 |
return KErrInUse; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1226 |
if (iType!=EHighRes) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1227 |
return AfterHighRes(aInterval, aFunction, aStatus); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1228 |
if (iRequest->SetStatus(&aStatus) != KErrNone) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1229 |
return KErrInUse; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1230 |
iState = (TUint8)EWaitHighRes; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1231 |
TInt msp=NTickPeriod(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1232 |
TInt t; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1233 |
if (aInterval>=0) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1234 |
t = (TInt)(((TUint)aInterval+msp-1)/msp); // convert microseconds to milliseconds, rounding up |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1235 |
else |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1236 |
t = aInterval/msp; // convert microseconds to milliseconds, rounding up |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1237 |
TInt r = Ms().Again(t); // start millisecond timer, complete in DFC |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1238 |
if (r != KErrNone) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1239 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1240 |
iState=(TUint8)EIdle; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1241 |
iRequest->Reset(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1242 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1243 |
return r; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1244 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1245 |
|
0 | 1246 |
void TTimer::Cancel(DThread* aThread) |
1247 |
// |
|
1248 |
// Cancel an outstanding request. |
|
1249 |
// Enter and return with system unlocked |
|
1250 |
// |
|
1251 |
{ |
|
1252 |
NKern::LockSystem(); |
|
1253 |
TTimerState s = (TTimerState)iState; |
|
1254 |
if (s==EWaitHighRes) |
|
1255 |
{ |
|
1256 |
Ms().Cancel(); |
|
1257 |
iState=(TUint8)EIdle; |
|
1258 |
s=EIdle; |
|
1259 |
if (aThread) |
|
1260 |
Kern::QueueRequestComplete(aThread, iRequest,KErrCancel); |
|
1261 |
NKern::UnlockSystem(); |
|
1262 |
} |
|
1263 |
else |
|
1264 |
NKern::UnlockSystem(); |
|
1265 |
if (s==EIdle) |
|
1266 |
return; |
|
1267 |
TTickQ::Wait(); |
|
1268 |
if (iState==EWaiting) |
|
1269 |
{ |
|
1270 |
if (iType==EAbsolute) |
|
1271 |
SecondLink().Cancel(); |
|
1272 |
else if (iType==EInactivity) |
|
1273 |
Inact().Cancel(); |
|
1274 |
else |
|
1275 |
TickLink().Cancel(); |
|
1276 |
if (aThread) |
|
1277 |
Kern::QueueRequestComplete(aThread,iRequest,KErrCancel); |
|
1278 |
iState=(TUint8)EIdle; |
|
1279 |
} |
|
1280 |
TTickQ::Signal(); |
|
1281 |
} |
|
1282 |
||
1283 |
TInt TTimer::After(TInt anInterval, TTickCallBack aFunction, TRequestStatus& aStatus) |
|
1284 |
// |
|
1285 |
// Request a relative time. |
|
1286 |
// Enter and return with system unlocked |
|
1287 |
// |
|
1288 |
{ |
|
1289 |
TTickQ::Wait(); |
|
1290 |
NKern::LockSystem(); |
|
1291 |
if (iState!=EIdle || iRequest->SetStatus(&aStatus) != KErrNone) |
|
1292 |
{ |
|
1293 |
NKern::UnlockSystem(); |
|
1294 |
TTickQ::Signal(); |
|
1295 |
return KErrInUse; |
|
1296 |
} |
|
1297 |
iState=(TUint8)EWaiting; |
|
1298 |
SetType(ERelative); |
|
1299 |
NKern::UnlockSystem(); |
|
1300 |
TickLink().OneShot(anInterval,aFunction,this); |
|
1301 |
TTickQ::Signal(); |
|
1302 |
return KErrNone; |
|
1303 |
} |
|
1304 |
||
1305 |
TInt TTimer::At(const TTimeK& aTime, TSecondCallBack aFunction, TRequestStatus& aStatus) |
|
1306 |
// |
|
1307 |
// Request an absolute time. |
|
1308 |
// Enter and return with system unlocked |
|
1309 |
// |
|
1310 |
{ |
|
1311 |
TTickQ::Wait(); |
|
1312 |
NKern::LockSystem(); |
|
1313 |
if (iState!=EIdle || iRequest->SetStatus(&aStatus) != KErrNone) |
|
1314 |
{ |
|
1315 |
NKern::UnlockSystem(); |
|
1316 |
TTickQ::Signal(); |
|
1317 |
return KErrInUse; |
|
1318 |
} |
|
1319 |
iState=(TUint8)EWaiting; |
|
1320 |
SetType(EAbsolute); |
|
1321 |
NKern::UnlockSystem(); |
|
1322 |
TInt r=SecondLink().At(aTime,aFunction,this); |
|
1323 |
if (r!=KErrNone) |
|
1324 |
{ |
|
1325 |
iState=(TUint8)EIdle; |
|
1326 |
iRequest->Reset(); |
|
1327 |
} |
|
1328 |
TTickQ::Signal(); |
|
1329 |
return r; |
|
1330 |
} |
|
1331 |
||
1332 |
TInt TTimer::Inactivity(TInt aSeconds, TInactivityCallBack aFunction, TRequestStatus& aStatus) |
|
1333 |
// |
|
1334 |
// Request an inactivity time. |
|
1335 |
// Enter and return with system unlocked |
|
1336 |
// |
|
1337 |
{ |
|
1338 |
TTickQ::Wait(); |
|
1339 |
NKern::LockSystem(); |
|
1340 |
if (iState!=EIdle || iRequest->SetStatus(&aStatus) != KErrNone) |
|
1341 |
{ |
|
1342 |
NKern::UnlockSystem(); |
|
1343 |
TTickQ::Signal(); |
|
1344 |
return KErrInUse; |
|
1345 |
} |
|
1346 |
iState=(TUint8)EWaiting; |
|
1347 |
SetType(EInactivity); |
|
1348 |
NKern::UnlockSystem(); |
|
1349 |
TInt r=Inact().Start(aSeconds,aFunction,this); |
|
1350 |
if (r!=KErrNone) |
|
1351 |
{ |
|
1352 |
iState=(TUint8)EIdle; |
|
1353 |
iRequest->Reset(); |
|
1354 |
} |
|
1355 |
TTickQ::Signal(); |
|
1356 |
return r; |
|
1357 |
} |
|
1358 |
||
1359 |
void TTimer::Abort(DThread* aThread, TInt aTypeMask) |
|
1360 |
// |
|
1361 |
// Abort an outstanding request. |
|
1362 |
// TimerMutex is already held here. |
|
1363 |
// |
|
1364 |
{ |
|
1365 |
NKern::LockSystem(); |
|
1366 |
if (iState!=EWaiting || (iType&aTypeMask)==0) |
|
1367 |
{ |
|
1368 |
NKern::UnlockSystem(); |
|
1369 |
return; |
|
1370 |
} |
|
1371 |
NKern::UnlockSystem(); |
|
1372 |
if (iType==EAbsolute) |
|
1373 |
SecondLink().Cancel(); |
|
1374 |
else |
|
1375 |
TickLink().Cancel(); |
|
1376 |
if (iType==ELocked) |
|
1377 |
TickLink().iLastLock=-1; |
|
1378 |
if (aThread) |
|
1379 |
Kern::QueueRequestComplete(aThread,iRequest,KErrAbort); |
|
1380 |
iState=(TUint8)EIdle; |
|
1381 |
} |
|
1382 |
||
1383 |
||
1384 |
/******************************************** |
|
1385 |
* Timer utilities |
|
1386 |
********************************************/ |
|
1387 |
TInt ExecHandler::TimerCreate() |
|
1388 |
// |
|
1389 |
// Create a Timer. |
|
1390 |
// |
|
1391 |
{ |
|
1392 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::TimerCreate")); |
|
1393 |
NKern::ThreadEnterCS(); |
|
1394 |
DTimer *pT=new DTimer; |
|
1395 |
TInt r=KErrNoMemory; |
|
1396 |
if (pT) |
|
1397 |
{ |
|
1398 |
r=pT->Create(TheCurrentThread); |
|
1399 |
if (r==KErrNone) |
|
1400 |
r=K::AddObject(pT,ETimer); |
|
1401 |
if (r==KErrNone) |
|
1402 |
r=K::MakeHandle(EOwnerThread,pT); |
|
1403 |
} |
|
1404 |
if (pT && r<KErrNone) |
|
1405 |
pT->Close(NULL); |
|
1406 |
NKern::ThreadLeaveCS(); |
|
1407 |
return r; |
|
1408 |
} |
|
1409 |
||
1410 |
void ExecHandler::TimerCancel(DTimer* aTimer) |
|
1411 |
{ |
|
1412 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::TimerCancel")); |
|
1413 |
K::ThreadEnterCS(); |
|
1414 |
aTimer->Cancel(); // OK since timer handles always thread relative |
|
1415 |
K::ThreadLeaveCS(); |
|
1416 |
} |
|
1417 |
||
1418 |
void ExecHandler::TimerAfter(DTimer* aTimer, TRequestStatus& aStatus, TInt aTime) |
|
1419 |
{ |
|
1420 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::TimerAfter")); |
|
1421 |
K::ThreadEnterCS(); |
|
1422 |
TInt r=aTimer->After(aStatus,aTime); // OK since timer handles always thread relative |
|
1423 |
K::ThreadLeaveCS(); |
|
1424 |
if (r!=KErrNone) |
|
1425 |
K::PanicCurrentThread(ETimerAlreadyPending); |
|
1426 |
} |
|
1427 |
||
1428 |
void ExecHandler::TimerHighRes(DTimer* aTimer, TRequestStatus& aStatus, TInt aTime) |
|
1429 |
{ |
|
1430 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::TimerHighRes")); |
|
1431 |
aTimer->HighRes(aStatus,aTime); |
|
1432 |
} |
|
1433 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1434 |
void ExecHandler::TimerAgainHighRes(DTimer* aTimer, TRequestStatus& aStatus, TInt aTime) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1435 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1436 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::TimerAgainHighRes")); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1437 |
aTimer->AgainHighRes(aStatus,aTime); |
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 |
void ExecHandler::TimerAt(DTimer* aTimer, TRequestStatus& aStatus, TUint32 aTimeLo, TUint32 aTimeHi) |
1441 |
{ |
|
1442 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::TimerAt")); |
|
1443 |
TTimeK time = MAKE_TINT64(aTimeHi,aTimeLo); |
|
1444 |
K::ThreadEnterCS(); |
|
1445 |
TInt r=aTimer->At(aStatus,time); // OK since timer handles always thread relative |
|
1446 |
K::ThreadLeaveCS(); |
|
1447 |
if (r==KErrInUse) |
|
1448 |
K::PanicCurrentThread(ETimerAlreadyPending); |
|
1449 |
else if (r!=KErrNone) |
|
1450 |
{ |
|
1451 |
TRequestStatus* status = &aStatus; |
|
1452 |
Kern::RequestComplete(status, r); |
|
1453 |
} |
|
1454 |
} |
|
1455 |
||
1456 |
void ExecHandler::TimerLock(DTimer* aTimer, TRequestStatus& aStatus, TTimerLockSpec aLock) |
|
1457 |
{ |
|
1458 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::TimerLock")); |
|
1459 |
K::ThreadEnterCS(); |
|
1460 |
TInt r=aTimer->Lock(aStatus,aLock); // OK since timer handles always thread relative |
|
1461 |
K::ThreadLeaveCS(); |
|
1462 |
if (r!=KErrNone) |
|
1463 |
K::PanicCurrentThread(ETimerAlreadyPending); |
|
1464 |
} |
|
1465 |
||
1466 |
void ExecHandler::TimerInactivity(DTimer* aTimer, TRequestStatus& aStatus, TInt aSeconds) |
|
1467 |
{ |
|
1468 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::TimerInactivity")); |
|
1469 |
K::ThreadEnterCS(); |
|
1470 |
TInt r=aTimer->Inactivity(aStatus,aSeconds); // OK since timer handles always thread relative |
|
1471 |
K::ThreadLeaveCS(); |
|
1472 |
if (r==KErrInUse) |
|
1473 |
K::PanicCurrentThread(ETimerAlreadyPending); |
|
1474 |
else if (r!=KErrNone) |
|
1475 |
{ |
|
1476 |
TRequestStatus* status = &aStatus; |
|
1477 |
Kern::RequestComplete(status, r); |
|
1478 |
} |
|
1479 |
} |
|
1480 |
||
1481 |
/******************************************** |
|
1482 |
* Miscellaneous |
|
1483 |
********************************************/ |
|
1484 |
TInt ExecHandler::UserInactivityTime() |
|
1485 |
{ |
|
1486 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::UserInactivityTime")); |
|
1487 |
return K::InactivityQ->InactiveTime(); |
|
1488 |
} |
|
1489 |
||
1490 |
void ExecHandler::ResetInactivityTime() |
|
1491 |
{ |
|
1492 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ResetInactivityTime")); |
|
1493 |
NKern::ThreadEnterCS(); |
|
1494 |
K::InactivityQ->Reset(); |
|
1495 |
NKern::ThreadLeaveCS(); |
|
1496 |
} |
|
1497 |
||
1498 |
EXPORT_C TUint32 Kern::TickCount() |
|
1499 |
/** |
|
1500 |
Returns the number of system ticks since boot. |
|
1501 |
*/ |
|
1502 |
{ |
|
1503 |
TTickQ& q=*K::TickQ; |
|
1504 |
TInt irq = __SPIN_LOCK_IRQSAVE(TheTimerQ.iTimerSpinLock); |
|
1505 |
TUint32 last_ticks=q.iLastTicks; |
|
1506 |
TUint32 lastms=q.iLastMs; |
|
1507 |
TUint32 ms=NTickCount(); |
|
1508 |
TUint32 p=(TUint32)q.iTickPeriod; |
|
1509 |
__SPIN_UNLOCK_IRQRESTORE(TheTimerQ.iTimerSpinLock,irq); |
|
1510 |
return last_ticks+((ms-lastms-1)*q.iMsTickPeriod)/p; |
|
1511 |
} |
|
1512 |
||
1513 |
TInt K::SecondsFrom2000(const TTimeK& aTime, TInt& aSeconds) |
|
1514 |
{ |
|
1515 |
TInt64 diff=aTime/1000000; // convert microseconds to seconds |
|
1516 |
diff-=K::Year2000InSeconds; |
|
1517 |
if (diff>KMaxTInt || diff<KMinTInt) |
|
1518 |
return KErrOverflow; |
|
1519 |
aSeconds=(TInt)diff; |
|
1520 |
return KErrNone; |
|
1521 |
} |
|
1522 |
||
1523 |
EXPORT_C TTimeK Kern::SystemTimeSecure() |
|
1524 |
/** |
|
1525 |
Gets the current system time. |
|
1526 |
||
1527 |
@return The current time as the number of microseconds since midnight, January 1st, 0 AD nominal Gregorian. |
|
1528 |
*/ |
|
1529 |
{ |
|
1530 |
TTimeK r = Kern::SystemTime(); |
|
1531 |
if (K::SecureClockStatus==ESecureClockOk) |
|
1532 |
{ |
|
1533 |
/* |
|
1534 |
NB: We read the software clock and unapply the nonsecure offset, rather than |
|
1535 |
reading the hardware rtc directly, for two reasons: |
|
1536 |
1. The software clock ticks at 64Hz, whereas the variant API |
|
1537 |
for reading the rtc is a measly 1Hz. |
|
1538 |
2. Less code. |
|
1539 |
*/ |
|
1540 |
TInt64 nonsecure_offset=K::NonSecureOffsetSeconds; |
|
1541 |
nonsecure_offset *= 1000000; |
|
1542 |
r -= nonsecure_offset; |
|
1543 |
} |
|
1544 |
else |
|
1545 |
{ |
|
1546 |
// Driver/variant code mustn't try to read the secure system time before the HAL has |
|
1547 |
// loaded (i.e. the nonsecure offset has been read from permanent storage). If you hit the |
|
1548 |
// assert below you need to change your code to use the nonsecure time API. |
|
1549 |
__ASSERT_DEBUG(K::SecureClockStatus!=ESecureClockPresent, Kern::Printf("Secure clock read but no nonsecure offset is present")); |
|
1550 |
} |
|
1551 |
return r; |
|
1552 |
} |
|
1553 |
||
1554 |
EXPORT_C TTimeK Kern::SystemTime() |
|
1555 |
/** |
|
1556 |
Gets the current nonsecure (user-settable) system time. |
|
1557 |
||
1558 |
@return The current time as the number of microseconds since midnight, January 1st, 0 AD nominal Gregorian. |
|
1559 |
*/ |
|
1560 |
{ |
|
1561 |
TTickQ& q=*K::TickQ; |
|
1562 |
TInt irq = __SPIN_LOCK_IRQSAVE(TheTimerQ.iTimerSpinLock); |
|
1563 |
Int64 rtc = q.iRtc; |
|
1564 |
TUint32 lastms=q.iLastMs; |
|
1565 |
TUint32 ms=NTickCount(); |
|
1566 |
__SPIN_UNLOCK_IRQRESTORE(TheTimerQ.iTimerSpinLock,irq); |
|
1567 |
return rtc*q.iNominalTickPeriod+TInt(ms-lastms-1)*q.iMsTickPeriod; |
|
1568 |
} |
|
1569 |
||
1570 |
||
1571 |
static TInt ExecTimeNowImpl(TTimeK& aUniversalTime, TInt& aUniversalTimeOffset, TBool aSecure) |
|
1572 |
{ |
|
1573 |
if (aSecure && K::SecureClockStatus != ESecureClockOk) |
|
1574 |
return KErrNoSecureTime; |
|
1575 |
||
1576 |
TInt off=K::HomeTimeOffsetSeconds; |
|
1577 |
TTimeK time = aSecure ? Kern::SystemTimeSecure() : Kern::SystemTime(); |
|
1578 |
kumemput32(&aUniversalTime,&time,sizeof(time)); |
|
1579 |
kumemput32(&aUniversalTimeOffset,&off,sizeof(off)); |
|
1580 |
return KErrNone; |
|
1581 |
} |
|
1582 |
||
1583 |
TInt ExecHandler::TimeNowSecure(TTimeK& aUniversalTime, TInt& aUniversalTimeOffset) |
|
1584 |
// |
|
1585 |
// Get the secure system time and universal time offset. |
|
1586 |
// |
|
1587 |
{ |
|
1588 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::TimeNowSecure")); |
|
1589 |
return ExecTimeNowImpl(aUniversalTime, aUniversalTimeOffset, ETrue); |
|
1590 |
} |
|
1591 |
||
1592 |
TInt ExecHandler::TimeNow(TTimeK& aUniversalTime, TInt& aUniversalTimeOffset) |
|
1593 |
// |
|
1594 |
// Get the nonsecure system time and universal time offset. |
|
1595 |
// |
|
1596 |
{ |
|
1597 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::TimeNow")); |
|
1598 |
return ExecTimeNowImpl(aUniversalTime, aUniversalTimeOffset, EFalse); |
|
1599 |
} |
|
1600 |
||
1601 |
TInt ExecHandler::UTCOffset() |
|
1602 |
// |
|
1603 |
// Get the universal time offset in seconds. |
|
1604 |
// |
|
1605 |
{ |
|
1606 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::UTCOffset")); |
|
1607 |
return K::HomeTimeOffsetSeconds; |
|
1608 |
} |
|
1609 |
||
1610 |
// Call with system unlocked and timer mutex held |
|
1611 |
TInt K::SetSystemTime(TInt aSecondsFrom2000, TInt aUTCOffset, TUint& aChanges, TUint aMode) |
|
1612 |
{ |
|
1613 |
__KTRACE_OPT(KEXEC,Kern::Printf("K::SetSystemTime %d %d %d",aSecondsFrom2000,aUTCOffset,aMode)); |
|
1614 |
||
1615 |
TTickQ& tq=*K::TickQ; |
|
1616 |
TSecondQ& sq=*(TSecondQ*)K::SecondQ; |
|
1617 |
||
1618 |
// get current midnight time and UTC offset |
|
1619 |
Int64 mnt = sq.iMidnight; |
|
1620 |
TInt old_offset = K::HomeTimeOffsetSeconds; |
|
1621 |
||
1622 |
// set up new offset and tweak midnight for offset change |
|
1623 |
if (aMode & ETimeSetOffset) |
|
1624 |
{ |
|
1625 |
aUTCOffset %= 86400; // stop silly offsets crashing the system |
|
1626 |
mnt += (old_offset - aUTCOffset) * tq.iTicksPerSecond; |
|
1627 |
} |
|
1628 |
else |
|
1629 |
aUTCOffset = old_offset; |
|
1630 |
||
1631 |
// set up new time and check for time reversal |
|
1632 |
Int64 c; |
|
1633 |
if (aMode & ETimeSetTime) |
|
1634 |
{ |
|
1635 |
c=aSecondsFrom2000; |
|
1636 |
c+=K::Year2000InSeconds; |
|
1637 |
c*=tq.iTicksPerSecond; |
|
1638 |
if (c<tq.iRtc && (aMode & ETimeSetAllowTimeReversal) == 0) |
|
1639 |
return KErrArgument; |
|
1640 |
} |
|
1641 |
else |
|
1642 |
c = tq.iRtc; |
|
1643 |
||
1644 |
// check if we've crossed midnight |
|
1645 |
if (c>=mnt || c<mnt-sq.iTicksPerDay) |
|
1646 |
aChanges |= EChangesMidnightCrossover; |
|
1647 |
||
1648 |
// work out local time and round to the next midnight, then back to utc |
|
1649 |
mnt=(c + aUTCOffset*tq.iTicksPerSecond + sq.iTicksPerDay) / sq.iTicksPerDay * sq.iTicksPerDay - aUTCOffset*tq.iTicksPerSecond; |
|
1650 |
||
1651 |
// update time, next midnight, and offset atomically |
|
1652 |
TInt irq = __SPIN_LOCK_IRQSAVE(TheTimerQ.iTimerSpinLock); |
|
1653 |
tq.iRtc=c; |
|
1654 |
sq.iMidnight=mnt; |
|
1655 |
K::HomeTimeOffsetSeconds = aUTCOffset; |
|
1656 |
__SPIN_UNLOCK_IRQRESTORE(TheTimerQ.iTimerSpinLock,irq); |
|
1657 |
||
1658 |
// Cancel timers and note time change |
|
1659 |
if (aMode & ETimeSetTime) |
|
1660 |
{ |
|
1661 |
tq.iMsTimer.Cancel(); |
|
1662 |
tq.iMsTimer.iTriggerTime=tq.iLastMs; |
|
1663 |
tq.iTickDfc.Cancel(); |
|
1664 |
} |
|
1665 |
if ((aMode & ETimeSetTime) || (old_offset != aUTCOffset && (aMode & ETimeSetNoTimeUpdate) == 0)) |
|
1666 |
{ |
|
1667 |
aChanges |= EChangesSystemTime; |
|
1668 |
sq.iTimer.DoCancel(); |
|
1669 |
} |
|
1670 |
||
1671 |
return KErrNone; |
|
1672 |
} |
|
1673 |
||
1674 |
||
1675 |
TInt ExecHandler::SetUTCTimeAndOffset(const TTimeK& aHomeTime, TInt aOffsetInSeconds, TUint aMode, TUint aChanges) |
|
1676 |
{ |
|
1677 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SetUTCTimeAndOffset")); |
|
1678 |
||
1679 |
if(aChanges != EChangesLocale && !Kern::CurrentThreadHasCapability(ECapabilityWriteDeviceData,__PLATSEC_DIAGNOSTIC_STRING("Checked by User time set function"))) |
|
1680 |
K::UnlockedPlatformSecurityPanic(); |
|
1681 |
||
1682 |
// If caller wants to set the secure (hardware) clock then check the process has permission to do that. Also |
|
1683 |
// check that the nonsecure offset HAL attribute is present, or a secure time cannot be set. |
|
1684 |
TUint flags = Kern::ETimeSet_SetHwRtc; // all user-mode clock setting APIs need to use this flag. It's only optional for drivers calling Kern::SetSystemTime |
|
1685 |
if (aMode & ETimeSetSecure) |
|
1686 |
{ |
|
1687 |
if(!Kern::CurrentThreadHasCapability(ECapabilityTCB,__PLATSEC_DIAGNOSTIC_STRING("Checked by User time set function"))) |
|
1688 |
return KErrPermissionDenied; |
|
1689 |
if (0 == (K::SecureClockStatus & ESecureClockOffsetPresent)) |
|
1690 |
return KErrNoSecureTime; |
|
1691 |
aMode &= ~ETimeSetSecure; |
|
1692 |
flags |= Kern::ETimeSet_Secure; |
|
1693 |
} |
|
1694 |
||
1695 |
||
1696 |
TTimeK hometime; |
|
1697 |
kumemget32(&hometime, &aHomeTime, sizeof(hometime)); |
|
1698 |
||
1699 |
if (aMode & ETimeSetOffset) |
|
1700 |
{ |
|
1701 |
SLocaleData& ld = TheMachineConfig().iLocale; |
|
1702 |
NKern::LockSystem(); |
|
1703 |
ld.iUniversalTimeOffset = aOffsetInSeconds; |
|
1704 |
ld.iDaylightSaving = EDstNone; |
|
1705 |
NKern::ThreadEnterCS(); |
|
1706 |
NKern::UnlockSystem(); |
|
1707 |
} |
|
1708 |
else |
|
1709 |
NKern::ThreadEnterCS(); |
|
1710 |
TUint changes = 0; |
|
1711 |
TInt r = K::SetSystemTimeAndOffset(hometime, aOffsetInSeconds, flags, changes, aMode); |
|
1712 |
if (changes & EChangesSystemTime) |
|
1713 |
{ |
|
1714 |
if (aMode & ETimeSetOffset || aChanges & EChangesLocale) |
|
1715 |
changes |= EChangesLocale; |
|
1716 |
__KTRACE_OPT(KEXEC,Kern::Printf("changes=%x",changes)); |
|
1717 |
Kern::NotifyChanges(changes); |
|
1718 |
} |
|
1719 |
else if(aChanges & EChangesLocale) |
|
1720 |
Kern::NotifyChanges(EChangesLocale); |
|
1721 |
NKern::ThreadLeaveCS(); |
|
1722 |
return r; |
|
1723 |
} |
|
1724 |
||
1725 |
/** |
|
1726 |
Updates the software RTC and possibly the hardware RTC. |
|
1727 |
||
1728 |
@param aTime The new time to set, either universal or local |
|
1729 |
@param aMode Flags as follows (from enum Kern::TTimeSetMode): |
|
1730 |
ETimeSet_SetHwRtc - set HW as well as SW RTC |
|
1731 |
ETimeSet_LocalTime - aTime is local time rather than UTC |
|
1732 |
ETimeSet_SyncNotify - synchronously trigger change notifiers |
|
1733 |
ETimeSet_AsyncNotify - asynchronously trigger change notifiers |
|
1734 |
ETimeSet_Secure - set the secure clock |
|
1735 |
||
1736 |
@pre Calling thread must be in a critical section. |
|
1737 |
@pre Interrupts must be enabled. |
|
1738 |
@pre Kernel must be unlocked. |
|
1739 |
@pre No fast mutex can be held. |
|
1740 |
@pre Call in a thread context. |
|
1741 |
@pre Can be used in a device driver. |
|
1742 |
||
1743 |
@return KErrNone, if successful; |
|
1744 |
KErrOverflow, if aTime it outside the representable range, which is |
|
1745 |
-2^31 to +2^31-1 seconds relative to 00:00:00 01-01-2000 |
|
1746 |
*/ |
|
1747 |
EXPORT_C TInt Kern::SetSystemTime(const TTimeK& aTime, TUint aMode) |
|
1748 |
{ |
|
1749 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::SetSystemTime"); |
|
1750 |
TUint dummy = 0; |
|
1751 |
return K::SetSystemTimeAndOffset(aTime, 0, aMode, dummy, ETimeSetTime); |
|
1752 |
} |
|
1753 |
||
1754 |
||
1755 |
||
1756 |
TInt K::SetSystemTimeAndOffset(const TTimeK& aTime, TInt aOffset, TUint aTimeSetMode, TUint& aChanges, TUint aMode) |
|
1757 |
{ |
|
1758 |
TSecondQ& sq = *K::SecondQ; |
|
1759 |
TTickQ& tq = *K::TickQ; |
|
1760 |
TTickQ::Wait(); |
|
1761 |
tq.Synchronise(); |
|
1762 |
TInt s = 0; |
|
1763 |
TInt old = 0; |
|
1764 |
||
1765 |
if (aMode & ETimeSetTime) |
|
1766 |
{ |
|
1767 |
||
1768 |
// Convert the new time parameter to UTC seconds |
|
1769 |
Int64 oldrtc = tq.iRtc; |
|
1770 |
TTimeK newTime = aTime; |
|
1771 |
if ((aTimeSetMode & Kern::ETimeSet_LocalTime) || (aMode & ETimeSetLocalTime)) |
|
1772 |
{ |
|
1773 |
if (aMode & ETimeSetOffset) |
|
1774 |
newTime -= TTimeK(aOffset)*1000000; |
|
1775 |
else |
|
1776 |
newTime -= TTimeK(K::HomeTimeOffsetSeconds)*1000000; |
|
1777 |
} |
|
1778 |
K::SecondsFrom2000(oldrtc*tq.iNominalTickPeriod, old); |
|
1779 |
TInt r = K::SecondsFrom2000(newTime, s); |
|
1780 |
if (r!=KErrNone) |
|
1781 |
{ |
|
1782 |
TTickQ::Signal(); |
|
1783 |
return r; |
|
1784 |
} |
|
1785 |
||
1786 |
||
1787 |
// Update secure clock |
|
1788 |
if (aTimeSetMode & Kern::ETimeSet_Secure) |
|
1789 |
{ |
|
1790 |
if (aTimeSetMode & Kern::ETimeSet_SetHwRtc) |
|
1791 |
{ |
|
1792 |
r = A::SetSystemTimeInSecondsFrom2000(s); // Update hardware RTC whether or not secure clock OK (HAL setting present) |
|
1793 |
if (r!=KErrNone) |
|
1794 |
{ |
|
1795 |
TTickQ::Signal(); |
|
1796 |
return r; |
|
1797 |
} |
|
1798 |
} |
|
1799 |
K::SecureClockStatus |= ESecureClockPresent; // Flag that a secure time has/is going to be set. |
|
1800 |
||
1801 |
if (K::SecureClockStatus == ESecureClockOk) // i.e. if Nonsecure offset present (if secure clock in use) |
|
1802 |
{ |
|
1803 |
TInt64 tmp = old; |
|
1804 |
tmp -= s; |
|
1805 |
K::NonSecureOffsetSeconds = static_cast<TInt32>(tmp); // i.e. Nonsecure time remains unchanged |
|
1806 |
s = old; // so the software RTC (set below) remains unchanged nonsecure time |
|
1807 |
} |
|
1808 |
} |
|
1809 |
// Update nonsecure clock |
|
1810 |
else |
|
1811 |
// If the secure clock is in working order (i.e. both a secure time source and the |
|
1812 |
// nonsecure offset are present) then all we have to do is update the nonsecure |
|
1813 |
// offset. Otherwise (i.e. either secure clock attribute is missing) all we can do |
|
1814 |
// is update the hardware clock. |
|
1815 |
if (K::SecureClockStatus == ESecureClockOk) |
|
1816 |
{ |
|
1817 |
TInt64 tmp = K::NonSecureOffsetSeconds; |
|
1818 |
tmp += s; |
|
1819 |
tmp -= old; |
|
1820 |
K::NonSecureOffsetSeconds = static_cast<TInt32>(tmp); |
|
1821 |
} |
|
1822 |
else |
|
1823 |
{ |
|
1824 |
if (aTimeSetMode & Kern::ETimeSet_SetHwRtc) |
|
1825 |
{ |
|
1826 |
r = A::SetSystemTimeInSecondsFrom2000(s); |
|
1827 |
if (r!=KErrNone) |
|
1828 |
{ |
|
1829 |
TTickQ::Signal(); |
|
1830 |
return r; |
|
1831 |
} |
|
1832 |
} |
|
1833 |
} |
|
1834 |
} |
|
1835 |
||
1836 |
// Update the software RTC and offset |
|
1837 |
K::SetSystemTime(s, aOffset, aChanges, aMode|ETimeSetAllowTimeReversal); |
|
1838 |
||
1839 |
// abort locked and absolute timers if the nonsecure time changed |
|
1840 |
if (aChanges & EChangesSystemTime && !(aTimeSetMode & Kern::ETimeSet_Secure)) |
|
1841 |
AbortTimers(ETrue); |
|
1842 |
||
1843 |
if (aMode & ETimeSetTime) |
|
1844 |
{ |
|
1845 |
// queue wakeup DFC to restart the timer queues |
|
1846 |
// if iInTick is already set, wake up DFC is already queued |
|
1847 |
if (!tq.iInTick) |
|
1848 |
{ |
|
1849 |
tq.iInTick = ETrue; |
|
1850 |
sq.iWakeUpDfc.Enque(); |
|
1851 |
} |
|
1852 |
||
1853 |
// tell the power manager |
|
1854 |
if (K::PowerModel) |
|
1855 |
K::PowerModel->SystemTimeChanged(old, s); |
|
1856 |
} |
|
1857 |
else if (aChanges & EChangesSystemTime) |
|
1858 |
sq.StartTimer(); |
|
1859 |
||
1860 |
TTickQ::Signal(); |
|
1861 |
||
1862 |
if (aChanges) |
|
1863 |
{ |
|
1864 |
if (aTimeSetMode & Kern::ETimeSet_SyncNotify) |
|
1865 |
Kern::NotifyChanges(aChanges); |
|
1866 |
else if (aTimeSetMode & Kern::ETimeSet_AsyncNotify) |
|
1867 |
Kern::AsyncNotifyChanges(aChanges); |
|
1868 |
} |
|
1869 |
return KErrNone; |
|
1870 |
} |
|
1871 |
||
1872 |
||
1873 |
TTimerLockSpec ExecHandler::LockPeriod() |
|
1874 |
{ |
|
1875 |
// NO ONE USES THIS, SO KILL IT |
|
1876 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::LockPeriod")); |
|
1877 |
return ETwelveOClock; |
|
1878 |
} |
|
1879 |
||
1880 |
/** Returns the period of the Symbian OS tick timer. |
|
1881 |
||
1882 |
@return Period in microseconds. |
|
1883 |
||
1884 |
@pre Call in any context. |
|
1885 |
||
1886 |
@see TTickLink |
|
1887 |
*/ |
|
1888 |
||
1889 |
EXPORT_C TInt Kern::TickPeriod() |
|
1890 |
{ |
|
1891 |
return K::TickQ->iNominalTickPeriod; |
|
1892 |
} |