|
1 /* |
|
2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "config.h" |
|
20 #include "SharedTimer.h" |
|
21 #include <wtf/Assertions.h> |
|
22 #include <e32std.h> |
|
23 #include <e32base.h> |
|
24 |
|
25 static TInt64 remainingMicro = 0; |
|
26 static bool shutdownInProgress = false; |
|
27 |
|
28 namespace WebCore { |
|
29 |
|
30 static CPeriodic* sharedTimer; |
|
31 static void (*sharedTimerFiredFunction)(); |
|
32 |
|
33 void setSharedTimerFiredFunction(void (*f)()) |
|
34 { |
|
35 ASSERT(!sharedTimerFiredFunction || sharedTimerFiredFunction == f); |
|
36 |
|
37 sharedTimerFiredFunction = f; |
|
38 } |
|
39 |
|
40 static TInt timerFired( TAny* /*aPtr*/ ) |
|
41 { |
|
42 if (shutdownInProgress) |
|
43 { |
|
44 return KErrNone; |
|
45 } |
|
46 if( remainingMicro == 0 ) |
|
47 { |
|
48 sharedTimerFiredFunction(); |
|
49 } |
|
50 else |
|
51 { |
|
52 setSharedTimerFireTime( -1 ); |
|
53 } |
|
54 return KErrNone; |
|
55 } |
|
56 |
|
57 void setSharedTimerFireTime(double fireTime) |
|
58 { |
|
59 if (shutdownInProgress) |
|
60 { |
|
61 return; |
|
62 } |
|
63 ASSERT(sharedTimerFiredFunction); |
|
64 |
|
65 if (sharedTimer) |
|
66 { |
|
67 sharedTimer->Cancel(); |
|
68 delete sharedTimer; |
|
69 sharedTimer = NULL; |
|
70 } |
|
71 if (fireTime != -1) |
|
72 remainingMicro = 0; |
|
73 |
|
74 sharedTimer = CPeriodic::New( CActive::EPriorityStandard ); |
|
75 if( sharedTimer ) |
|
76 { |
|
77 TInt64 interval( remainingMicro ); |
|
78 if( remainingMicro == 0 ) |
|
79 { |
|
80 // fireTime comes in second resolution |
|
81 TTime fireDate( TTime(fireTime * 1000000 ).Int64() ); |
|
82 |
|
83 TTime time; |
|
84 time.HomeTime(); |
|
85 interval = fireDate.Int64() - time.Int64(); |
|
86 } |
|
87 interval = interval < 0 ? 0 : interval; |
|
88 // |
|
89 TInt t; |
|
90 if (interval<(TInt)(KMaxTInt32)) |
|
91 { |
|
92 t = interval; |
|
93 remainingMicro = 0; |
|
94 } |
|
95 else |
|
96 { |
|
97 t = KMaxTInt32; |
|
98 remainingMicro = interval - KMaxTInt32; |
|
99 } |
|
100 sharedTimer->Start( t, 0, timerFired); |
|
101 } |
|
102 } |
|
103 |
|
104 void stopSharedTimer() |
|
105 { |
|
106 if (sharedTimer) |
|
107 { |
|
108 sharedTimer->Cancel(); |
|
109 delete sharedTimer; |
|
110 sharedTimer = NULL; |
|
111 } |
|
112 remainingMicro = 0; |
|
113 } |
|
114 |
|
115 void shutdownSharedTimer() |
|
116 { |
|
117 shutdownInProgress = true; |
|
118 stopSharedTimer(); |
|
119 } |
|
120 |
|
121 } |