|
1 /* |
|
2 * Copyright (c) 2009 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 "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 "sysif.h" |
|
20 #include "timerserver.h" |
|
21 #include "timerhandler.h" |
|
22 #include "timer.h" |
|
23 #include <pthread.h> |
|
24 /* Implementation of the CShutDown class |
|
25 * Implements a timer |
|
26 */ |
|
27 |
|
28 // ------------------------------------------------------------------------------- |
|
29 // CShutdown::ConstructL |
|
30 // ------------------------------------------------------------------------------- |
|
31 void CShutdown::ConstructL() |
|
32 { |
|
33 CTimer::ConstructL(); |
|
34 } |
|
35 |
|
36 // ------------------------------------------------------------------------------- |
|
37 // CShutdown::Start |
|
38 // Starts the timer for the specified time given |
|
39 // ------------------------------------------------------------------------------- |
|
40 void CShutdown::Start() |
|
41 { |
|
42 CActiveScheduler::Add(this); |
|
43 After(KTimerServerShutdownDelay); |
|
44 } |
|
45 |
|
46 // ------------------------------------------------------------------------------- |
|
47 // CShutdown::RunL |
|
48 // Initiate server exit when timer expires |
|
49 // ------------------------------------------------------------------------------- |
|
50 void CShutdown::RunL() |
|
51 { |
|
52 getTimerHandler()->ResetClientFlag(); |
|
53 getTimerHandler()->ResetServerFlag(); |
|
54 CActiveScheduler::Stop(); |
|
55 } |
|
56 |
|
57 // ------------------------------------------------------------------------------- |
|
58 // CTimerServerSession::CreateL |
|
59 // 2nd phase construction for sessions. Invoked by the CServer2 framework |
|
60 // ------------------------------------------------------------------------------- |
|
61 void CTimerServerSession::CreateL() |
|
62 { |
|
63 Server().AddSession(); |
|
64 } |
|
65 |
|
66 //--------------------------------------------------------------------- |
|
67 //CTimerServerSession::~CTimerServerSession |
|
68 //Destructor of the server side session |
|
69 //--------------------------------------------------------------------- |
|
70 CTimerServerSession::~CTimerServerSession() |
|
71 { |
|
72 Server().DropSession(); |
|
73 } |
|
74 |
|
75 //--------------------------------------------------------------------- |
|
76 // Handle a client request. |
|
77 // Leaving is handled by CMyServer::ServiceError() which reports |
|
78 // the error code to the client |
|
79 //--------------------------------------------------------------------- |
|
80 void CTimerServerSession::ServiceL(const RMessage2& aMessage) |
|
81 { |
|
82 switch (aMessage.Function()) |
|
83 { |
|
84 case EDeleteTimer: |
|
85 { |
|
86 TInt timerid = aMessage.Int0(); |
|
87 TInt ret = Server().RemoveInternal(timerid); |
|
88 aMessage.Complete(ret); |
|
89 break; |
|
90 } |
|
91 case ESetTimer: |
|
92 { |
|
93 TInt timerid = aMessage.Int0(); |
|
94 TInt ret = Server().SetTimer(timerid); |
|
95 aMessage.Complete(ret); |
|
96 break; |
|
97 } |
|
98 default: |
|
99 PanicClient(aMessage,EPanicIllegalFunction); |
|
100 break; |
|
101 } |
|
102 } |
|
103 |
|
104 CTimerServer& CTimerServerSession::Server() |
|
105 { |
|
106 return *static_cast<CTimerServer*>(const_cast<CServer2*>(CSession2::Server())); |
|
107 } |
|
108 |
|
109 //------------------------------------------------------------------------------ |
|
110 // Handle an error from CTimerServerSession::ServiceL() |
|
111 // A bad descriptor error implies a badly programmed client, so panic it; |
|
112 // otherwise use the default handling (report the error to the client) |
|
113 //------------------------------------------------------------------------------ |
|
114 |
|
115 void CTimerServerSession::ServiceError(const RMessage2& aMessage,TInt aError) |
|
116 { |
|
117 PanicClient(aMessage,EPanicIllegalFunction); |
|
118 CSession2::ServiceError(aMessage,aError); |
|
119 } |
|
120 |
|
121 //----------------------------------------------------------------------------------------- |
|
122 // Implementation for CTimerServer class |
|
123 // Implements the functionalities of the timer server |
|
124 //------------------------------------------------------------------------------------------ |
|
125 CTimerServer* CTimerServer::NewLC() |
|
126 { |
|
127 CTimerServer* self=new(ELeave) CTimerServer; |
|
128 CleanupStack::PushL(self); |
|
129 self->ConstructL(); |
|
130 return self; |
|
131 } |
|
132 |
|
133 // ------------------------------------------------------------------------------- |
|
134 // CTimerServer::ConstructL |
|
135 // Second phase construction. Create the shutdown timer object and the semaphore |
|
136 // ------------------------------------------------------------------------------- |
|
137 void CTimerServer::ConstructL() |
|
138 { |
|
139 iShutdown = new(ELeave) CShutdown; |
|
140 CleanupStack::PushL(iShutdown); |
|
141 iShutdown->ConstructL(); |
|
142 CleanupStack::Pop(); |
|
143 } |
|
144 |
|
145 // ------------------------------------------------------------------------------- |
|
146 // CTimerServer::AddToScheduler |
|
147 // Add both the server and the shutdown timer objects to the active scheduler |
|
148 // ------------------------------------------------------------------------------- |
|
149 TInt CTimerServer::AddToScheduler() |
|
150 { |
|
151 iShutdown->Start(); |
|
152 return CServer2::Start(KNullDesC); |
|
153 } |
|
154 |
|
155 // ------------------------------------------------------------------------------- |
|
156 // CFileDesTransferServer::RemoveFromScheduler |
|
157 // Remove both the server and the shutdown timer objects from the active scheduler |
|
158 // ------------------------------------------------------------------------------- |
|
159 void CTimerServer::RemoveFromScheduler() |
|
160 { |
|
161 if (iShutdown->IsAdded()) |
|
162 { |
|
163 iShutdown->Deque(); |
|
164 } |
|
165 Deque(); |
|
166 } |
|
167 |
|
168 // ------------------------------------------------------------------------------- |
|
169 // CFileDesTransferServer::NewSessionL |
|
170 // Create a new client session. |
|
171 // ------------------------------------------------------------------------------- |
|
172 CSession2* CTimerServer::NewSessionL(const TVersion&, const RMessage2&) const |
|
173 { |
|
174 return new(ELeave) CTimerServerSession; |
|
175 } |
|
176 |
|
177 // ------------------------------------------------------------------------------- |
|
178 // CFileDesTransferServer::AddSession |
|
179 // A new session is created. Cancel the shutdown timer if it was running. |
|
180 // ------------------------------------------------------------------------------- |
|
181 inline void CTimerServer::AddSession() |
|
182 { |
|
183 if (iShutdown->IsAdded()) |
|
184 { |
|
185 iShutdown->Deque(); |
|
186 } |
|
187 ++iSessionCount; |
|
188 |
|
189 } |
|
190 |
|
191 // ------------------------------------------------------------------------------- |
|
192 // CTimerServer::DropSession |
|
193 // The session is destroyed. Stop the ActiveScheduler |
|
194 // ------------------------------------------------------------------------------- |
|
195 void CTimerServer::DropSession() |
|
196 { |
|
197 if (--iSessionCount==0) |
|
198 { |
|
199 CTimerReqHandler* handler = getTimerHandler(); |
|
200 handler->ResetClientFlag(); |
|
201 if ((handler->iTimers.Count()) == 0 ) |
|
202 { |
|
203 getTimerHandler()->ResetServerFlag(); |
|
204 CActiveScheduler::Stop(); |
|
205 } |
|
206 } |
|
207 } |
|
208 |
|
209 //------------------------------------------------------------------------- |
|
210 // CTimerServer::NewTimerServerL |
|
211 // creates an active scheduler and installs it. |
|
212 //------------------------------------------------------------------------- |
|
213 TInt CTimerServer::NewTimerServerL() |
|
214 { |
|
215 CActiveScheduler* pScheduler = new(ELeave) CActiveScheduler; |
|
216 CleanupStack::PushL(pScheduler); |
|
217 |
|
218 CTimerServer* pServer = CTimerServer::NewLC(); |
|
219 |
|
220 CActiveScheduler::Install(pScheduler); |
|
221 |
|
222 TInt err = pServer->AddToScheduler(); |
|
223 if (err != KErrNone) |
|
224 { |
|
225 pServer->RemoveFromScheduler(); |
|
226 CActiveScheduler::Install(NULL); |
|
227 User::Leave(err); |
|
228 } |
|
229 getTimerHandler()->SetServerHandle(pServer->Server()); |
|
230 // pop both pScheduler and pServer from the cleanup stack |
|
231 // Start the scheduler |
|
232 RThread().Rendezvous(KErrNone); |
|
233 CActiveScheduler::Start(); |
|
234 CActiveScheduler::Install(NULL); |
|
235 |
|
236 CleanupStack::PopAndDestroy(2); |
|
237 return KErrNone; |
|
238 } |
|
239 // ------------------------------------------------------------------------------- |
|
240 // CFileDesTransferServer::~CFileDesTransferServer |
|
241 // ------------------------------------------------------------------------------- |
|
242 CTimerServer::~CTimerServer() |
|
243 { |
|
244 if(iShutdown) |
|
245 { |
|
246 delete iShutdown; |
|
247 iShutdown = 0; |
|
248 } |
|
249 } |
|
250 |
|
251 //--------------------------------------------------------------------------------- |
|
252 // CTimerServer:: RemoveInternal |
|
253 // Removing the timer object from the list |
|
254 //--------------------------------------------------------------------------------- |
|
255 TInt CTimerServer:: RemoveInternal(const TInt aTimerId) |
|
256 { |
|
257 TInt lTimerIdx = 0; |
|
258 TInt lRet = KErrArgument; |
|
259 TBool lSigReq = EFalse; |
|
260 CTimerReqHandler* handler = getTimerHandler(); |
|
261 handler->iTimersLock.Wait(); |
|
262 int lTimerCnt = handler->iTimers.Count(); |
|
263 |
|
264 for(lTimerIdx = 0; lTimerIdx != lTimerCnt; lTimerIdx++) |
|
265 { |
|
266 if(handler->iTimers[lTimerIdx]->iTimerId == aTimerId) |
|
267 { |
|
268 if (handler->iTimers[lTimerIdx]->iSigEvent.sigev_notify == SIGEV_SIGNAL) |
|
269 lSigReq = ETrue; |
|
270 RHeap* oldHeap = User::SwitchHeap(Backend()->Heap()); |
|
271 delete handler->iTimers[lTimerIdx]; |
|
272 handler->iTimers.Delete(lTimerIdx); |
|
273 User::SwitchHeap(oldHeap); |
|
274 lRet = KErrNone; |
|
275 break; |
|
276 } |
|
277 } |
|
278 if(lRet == KErrNone && lSigReq) |
|
279 { |
|
280 RHeap* oldHeap = User::SwitchHeap(Backend()->Heap()); |
|
281 handler->iTimers.Compress(); |
|
282 User::SwitchHeap(oldHeap); |
|
283 #if (defined SYMBIAN_OE_POSIX_SIGNALS && defined SYMBIAN_OE_LIBRT) |
|
284 Backend()->DeleteTimer(aTimerId); |
|
285 #endif |
|
286 } |
|
287 handler->iTimersLock.Signal(); |
|
288 if(handler->iTimers.Count() == 0) |
|
289 { |
|
290 DropSession(); |
|
291 } |
|
292 return lRet; |
|
293 } |
|
294 |
|
295 //thread startup function for SIGEV_THREAD |
|
296 static TAny* sThreadFunc(TAny* aArgPtr) |
|
297 { |
|
298 struct sigevent* lTimerP = reinterpret_cast<struct sigevent *> (aArgPtr); |
|
299 lTimerP->sigev_notify_function(lTimerP->sigev_value); |
|
300 return NULL; |
|
301 } |
|
302 |
|
303 //--------------------------------------------------------------------------------- |
|
304 // CTimerServer:: SetTimer |
|
305 // Server sets a new value for the timeout. |
|
306 //--------------------------------------------------------------------------------- |
|
307 TInt CTimerServer:: SetTimer(const TInt aTimerId) |
|
308 { |
|
309 CRtTimer* lTimer = getTimerHandler()->FindTimer(aTimerId); |
|
310 if(lTimer) |
|
311 { |
|
312 TTime lSetTime(MAKE_TINT64 (0x00dcddb3 ,0x0f2f8000)); |
|
313 if (lTimer->iIsArmed) |
|
314 { |
|
315 lTimer->iTimer.Cancel(); |
|
316 if(lTimer->iIsTimerReset) |
|
317 { |
|
318 lTimer->iTimer.Deque(); |
|
319 lTimer->iIsArmed = EFalse; |
|
320 return 0; |
|
321 } |
|
322 } |
|
323 else |
|
324 lTimer->iTimer.ConstructL(); |
|
325 |
|
326 lSetTime+=(TTimeIntervalSeconds) lTimer->iEndTime.tv_sec; |
|
327 lSetTime+=(TTimeIntervalMicroSeconds)(lTimer->iEndTime.tv_nsec/1000); |
|
328 |
|
329 lTimer->iTimer.AddToAS(); |
|
330 lTimer->iTimer.At(lSetTime); //Before requesting a timer event, add this active object to AS. |
|
331 lTimer->iIsArmed = ETrue; |
|
332 } |
|
333 return KErrNone; |
|
334 } |
|
335 |
|
336 void CLibRtTimer::AddToAS() |
|
337 { |
|
338 if(!IsAdded()) |
|
339 CActiveScheduler::Add(this); |
|
340 else return; |
|
341 } |
|
342 |
|
343 void CLibRtTimer::RunL() |
|
344 { |
|
345 CLocalSystemInterface* lClsiPtr = Backend(); |
|
346 |
|
347 unsigned long lPeriodicTimeout = (1000000 * GetRtPtr()->iStartTime.it_interval.tv_sec) + |
|
348 (GetRtPtr()->iStartTime.it_interval.tv_nsec/1000); |
|
349 |
|
350 if(lPeriodicTimeout) |
|
351 { |
|
352 this->HighRes(lPeriodicTimeout); |
|
353 } |
|
354 else |
|
355 { |
|
356 GetRtPtr()->iIsArmed = EFalse; |
|
357 } |
|
358 int val = GetRtPtr()->iSigEvent.sigev_notify; |
|
359 //run the handler here |
|
360 switch(GetRtPtr()->iSigEvent.sigev_notify) |
|
361 { |
|
362 #if (defined SYMBIAN_OE_POSIX_SIGNALS && defined SYMBIAN_OE_LIBRT) |
|
363 case SIGEV_SIGNAL: |
|
364 { |
|
365 lClsiPtr->AddTimer(GetRtPtr()->iTimerId); |
|
366 |
|
367 if(lClsiPtr->IncrementOverrun(GetRtPtr()->iTimerId) == 1) |
|
368 { |
|
369 lClsiPtr->RaiseTimerSignal(GetRtPtr()->iSigEvent.sigev_signo,\ |
|
370 GetRtPtr()->iTimerId); |
|
371 } |
|
372 } |
|
373 break; |
|
374 #endif |
|
375 case SIGEV_THREAD: |
|
376 { |
|
377 pthread_t lthread = 0; |
|
378 pthread_create( <hread, |
|
379 (pthread_attr_t*)GetRtPtr()->iSigEvent.sigev_notify_attributes, |
|
380 sThreadFunc, |
|
381 &GetRtPtr()->iSigEvent ); |
|
382 } |
|
383 break; |
|
384 |
|
385 case SIGEV_NONE: |
|
386 default: |
|
387 break; |
|
388 } |
|
389 } |
|
390 |
|
391 // --------------------------------------------------------------------------- |
|
392 // RMessage::Panic() also completes the message. This is: |
|
393 // (a) important for efficient cleanup within the kernel |
|
394 // (b) a problem if the message is completed a second time |
|
395 // --------------------------------------------------------------------------- |
|
396 void PanicClient(const RMessagePtr2& aMessage,TMyPanic aPanic) |
|
397 { |
|
398 _LIT(KPanic,"TimerServer"); |
|
399 aMessage.Panic(KPanic,aPanic); |
|
400 } |
|
401 |