Update contrib.
1 // Copyright (c) 2008-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 "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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Name : timerhandler.cpp
15 // Part of : librt-timer specific cpp file
16 // This is a project specific source file for building the
17 // timer related functions as part of librt library.
23 #include "timerhandler.h"
24 #include "timerclient.h"
25 #include "timerserver.h"
26 #include "timermessage.h"
31 #include <pls.h> // For emulator WSD API
32 const TUid KLibrtUid3 = {0x2001E553};
33 #elif defined __X86GCC__
34 CTimerReqHandler gTimerHandler;
38 CTimerReqHandler* getTimerHandler()
41 // Access the PLS of this process
42 CTimerReqHandler* lTimerHandlerPtr = Pls<CTimerReqHandler>(KLibrtUid3);
43 return lTimerHandlerPtr;
44 #elif defined __X86GCC__
45 return &gTimerHandler;
47 static CTimerReqHandler sgTimerHandler;
48 return &sgTimerHandler;
53 CTimerReqHandler::CTimerReqHandler() : iTimers(CTimerReqHandler::KTimersGran)
55 iTimersLock.CreateLocal();
56 iServConnectLock.CreateLocal();
57 iTimerSemaphore.CreateLocal(0);
61 CTimerReqHandler::~CTimerReqHandler()
64 iTimers.ResetAndDestroy();
65 iTimerSemaphore.Close();
67 iServConnectLock.Close();
70 //method to create a timer
71 TInt CTimerReqHandler::CreateTimer(TInt& aTimerId, struct sigevent *aSig)
74 if(iTimers.Count() >= MAXTIMERLIMIT)
76 lRet = KErrWouldBlock;
83 switch(aSig->sigev_notify)
86 #if (!defined SYMBIAN_OE_POSIX_SIGNALS || !defined SYMBIAN_OE_LIBRT)
89 lRet = KErrNotSupported;
94 if(aSig->sigev_signo < 1 || aSig->sigev_signo > SIGRTMAX)
113 RHeap* oldHeap = User::SwitchHeap(Backend()->Heap());
114 CRtTimer *lTimer = CRtTimer::New(aSig);
119 User::SwitchHeap(oldHeap);
123 aTimerId = lTimer->iTimerId;
125 TRAP(lRet, iTimers.AppendL(lTimer));
126 User::SwitchHeap(oldHeap);
127 iTimersLock.Signal();
135 //method to delete a timer
136 TInt CTimerReqHandler::RemoveTimer(const TInt& aTimerId)
138 if(FindTimer(aTimerId)== NULL)
142 TInt lRet = Connect();
145 session.OnDemandConnect(iServer);
146 lRet = session.DeleteTimer(aTimerId);
151 //method to set a new timeout value for a timer
152 TInt CTimerReqHandler::SetTime(const TInt& aTimerId, TInt aFlag, const struct itimerspec *aIpTime,
153 struct itimerspec *aOpTime)
155 TInt lRet = Connect();
158 CRtTimer* lTimer = FindTimer(aTimerId);
160 lRet = lTimer->SetTime(aFlag, aIpTime, aOpTime);
168 //method to makesure that the timer server is started only once on demand.
169 TInt CTimerReqHandler::Connect()
171 TInt lRet = KErrNone;
173 iServConnectLock.Wait();
176 lRet = StartTimerServer();
178 iIsServStarted = ETrue;
180 iServConnectLock.Signal();
184 //start up function for the timer server.
185 static TInt sTimerServer(TAny*)
187 RHeap *oldHeap = User::SwitchHeap(Backend()->Heap());
188 CTrapCleanup* cleanup = CTrapCleanup::New();
189 TRAPD(ret, CTimerServer::NewTimerServerL());
191 User::SwitchHeap(oldHeap);
195 //method to start the timer server
196 TInt CTimerReqHandler::StartTimerServer()
198 TRequestStatus status;
199 TInt lRet = iServ.Create(KNullDesC, sTimerServer,
200 KDefaultStackSize, NULL, (TAny*)NULL);
203 iServ.SetPriority(EPriorityAbsoluteHigh);// match the signal server priority.
204 iServ.Rendezvous(status);
205 iServ.Resume(); //get this ready
206 User::WaitForRequest(status);
211 //method to get the "time to expiration" of a timer
212 TInt CTimerReqHandler::Time(const TInt& aTimerId, struct itimerspec *aTime)
214 CRtTimer* lTimer = FindTimer(aTimerId);
217 return lTimer->Time(aTime);
220 //method to get the overruns for a timer
221 TInt CTimerReqHandler::OverrunCount(const TInt& aTimerId, TInt& aOverrunCount)
223 CRtTimer* lTimer = FindTimer(aTimerId);
226 return lTimer->OverrunCount(aOverrunCount);
229 //find the timer with the given timer id
230 CRtTimer* CTimerReqHandler::FindTimer(const TInt& aTimerId)
232 CRtTimer* lRtTimerP = NULL;
234 TInt lTimerCount = iTimers.Count();
235 for(TInt lIdx =0; lIdx < lTimerCount; lIdx++)
237 if(iTimers[lIdx]->iTimerId == aTimerId)
239 lRtTimerP = (iTimers[lIdx]);
243 iTimersLock.Signal();