realtimenetprots/sipfw/SIP/LightWeightTimer/src/TimerManager.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2005-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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Name          : timermanager.cpp
       
    15 // Part of       : LightWeightTimer
       
    16 // Version       : SIP/4.0
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #include "SipAssert.h"
       
    22 #include "TimerManager.h"
       
    23 #include "timerrequest.h"
       
    24 #include "timerstore.h"
       
    25 #include "singletimer.h"
       
    26 #include "timerlog.h"
       
    27 
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CTimerManager::NewL
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 CTimerManager* CTimerManager::NewL()
       
    34 	{
       
    35 	CTimerManager* self = CTimerManager::NewLC();	
       
    36 	CleanupStack::Pop(self);
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CTimerManager::NewLC
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 CTimerManager* CTimerManager::NewLC()
       
    45 	{
       
    46 	CTimerManager* self = new (ELeave) CTimerManager();
       
    47 	CleanupStack::PushL(self);	
       
    48 	self->ConstructL();
       
    49 	return self;
       
    50 	}
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CTimerManager::CTimerManager
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 CTimerManager::CTimerManager() :
       
    57 	iTimerIdCounter(KReservedTimer),
       
    58 	iNowExpiring(EFalse)
       
    59 	{
       
    60 	}
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CTimerManager::ConstructL
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 void CTimerManager::ConstructL()
       
    67 	{
       
    68 	iTimerStore = CTimerStore::NewL();
       
    69 	iTimer = CSingleTimer::NewL(*this);
       
    70 	}
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CTimerManager::~CTimerManager
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 CTimerManager::~CTimerManager()
       
    77 	{
       
    78 	delete iTimerStore;
       
    79 	delete iTimer;
       
    80 	}
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CTimerManager::StartL
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 TTimerId
       
    87 CTimerManager::StartL(MExpirationHandler* aObserver, TUint aMilliseconds)
       
    88 	{
       
    89 	__TEST_INVARIANT;
       
    90 	__ASSERT_ALWAYS(aObserver, User::Leave(KErrArgument));
       
    91 
       
    92 	return StartL(aObserver, aMilliseconds, NULL);
       
    93 	}
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CTimerManager::StartL
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 TTimerId CTimerManager::StartL(MExpirationHandler* aObserver,
       
   100 							   TUint aMilliseconds,
       
   101 							   TAny* aTimerParam)
       
   102 	{
       
   103 	__TEST_INVARIANT;
       
   104 	__ASSERT_ALWAYS(aObserver, User::Leave(KErrArgument));
       
   105 	
       
   106     TTimerId newTimerId(NewTimerId());
       
   107 
       
   108 #if defined(WRITE_TIMER_LOG)
       
   109 	__SIP_INT_LOG2( "LwTimer StartL (id,duration)", newTimerId, aMilliseconds )
       
   110 #endif
       
   111 
       
   112 	iTimerStore->AddL(aObserver,
       
   113 					  MillisecToTTime(aMilliseconds),
       
   114 					  newTimerId,
       
   115 					  aTimerParam);		
       
   116 
       
   117 	//If iNowExpiring is true, timer can't be set now, as execution is inside
       
   118 	//MExpirationHandler::TimerExpiredL. Timer will be set when the control
       
   119 	//returns from callback to CTimerManager::TimerExpiredL.
       
   120 	if (!iNowExpiring)
       
   121 		{
       
   122 		CheckIsNewTimerShortestL(newTimerId, aMilliseconds);
       
   123 		}
       
   124 
       
   125 	__TEST_INVARIANT;
       
   126 	return newTimerId;
       
   127 	}
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CTimerManager::Stop
       
   131 // User is not allowed to stop the internal timer
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 TInt CTimerManager::Stop(TTimerId aTimerId)
       
   135 	{
       
   136 	__TEST_INVARIANT;
       
   137 
       
   138 #if defined(WRITE_TIMER_LOG)
       
   139 	__SIP_INT_LOG1( "LwTimer Stop, timer id", aTimerId)
       
   140 #endif
       
   141 
       
   142 	if (aTimerId == KReservedTimer)
       
   143 		{
       
   144 		return KErrNotFound;
       
   145 		}
       
   146 
       
   147 	TInt index(0);
       
   148 	const CTimerRequest* timerReq = iTimerStore->SearchById(aTimerId, index);
       
   149 	if (!timerReq)
       
   150 		{
       
   151 		return KErrNotFound;
       
   152 		}
       
   153 
       
   154 	if (!iTimerStore->Remove(index))
       
   155 		{
       
   156 		return KErrNotFound;
       
   157 		}
       
   158 
       
   159 	//If Stop is called from MExpirationHandler::TimerExpiredL, this condition
       
   160 	//isn't true, as iTimer has TimerId of the expiring timer which was removed
       
   161 	//from iTimerStore. So no need to check iNowExpiring flag now.
       
   162 	if (aTimerId == iTimer->TimerId())
       
   163 		{
       
   164 		//The currently running timer was stopped.
       
   165 		iTimer->Cancel();
       
   166 
       
   167 		TRAPD(err, SetTimerWithShortestValueL());
       
   168 		if (err != KErrNone)
       
   169 			{
       
   170 			return err;
       
   171 			}
       
   172 		}
       
   173 
       
   174 	__TEST_INVARIANT;
       
   175 	return KErrNone;
       
   176 	}
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CTimerManager::IsRunning
       
   180 // Don't reveal the internal timer to user.
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 TBool CTimerManager::IsRunning(TTimerId aTimerId) const
       
   184 	{
       
   185 	__TEST_INVARIANT;
       
   186 
       
   187 	if (aTimerId == KReservedTimer)
       
   188 		{
       
   189 		return EFalse;
       
   190 		}
       
   191 
       
   192 	TInt dummyIndex(0);
       
   193 	return (iTimerStore->SearchById(aTimerId, dummyIndex) != NULL);
       
   194 	}
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CTimerManager::ExpiresAfter
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 TInt CTimerManager::ExpiresAfter(
       
   201     TTimerId aTimerId, 
       
   202     TUint& aExpiresAfterInMillisecs) const
       
   203     {
       
   204     __TEST_INVARIANT;
       
   205     
       
   206     aExpiresAfterInMillisecs = 0;
       
   207 
       
   208     TInt index(0);
       
   209 	const CTimerRequest* timerReq = iTimerStore->SearchById(aTimerId, index);
       
   210 	if (!timerReq)
       
   211 		{
       
   212 		return KErrNotFound;
       
   213 		}
       
   214 	
       
   215 	TTimeToMillisec(timerReq->ExpirationTime(), aExpiresAfterInMillisecs);
       
   216 		
       
   217     return KErrNone;
       
   218     }
       
   219     
       
   220 // -----------------------------------------------------------------------------
       
   221 // CTimerManager::TimerExpiredL
       
   222 // SearchById can return NULL, e.g. in a following case:
       
   223 // - User has set two timers with very similar durations and they are the only
       
   224 //   currently running timers.
       
   225 // - User stops the shorter timer, CTimerManager::Stop uses
       
   226 //   SetTimerWithShortestValueL but as the remaining timer's expiration is so
       
   227 //   close to the first timer, CSingleTimer uses CSingleTimer::ExpireImmediately
       
   228 //-  Before CSingleTimer::RunL has chance to be executed, user stops also the
       
   229 //   remaining timer, which is removed from CTimerStore.
       
   230 //-  Soon CSingleTimer::RunL is called and leads here but the expiring timer has
       
   231 //   already been removed and CTimerStore::SearchById returns NULL.
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 void CTimerManager::TimerExpiredL(TTimerId aTimerId)
       
   235 	{
       
   236 	__TEST_INVARIANT;
       
   237     __SIP_ASSERT_LEAVE(aTimerId != KNoSuchTimer, KErrArgument);
       
   238 
       
   239 	TInt index(0);
       
   240 	CTimerRequest* timerReq = iTimerStore->SearchById(aTimerId, index);
       
   241 
       
   242 	if (timerReq)
       
   243 		{
       
   244 #if defined(WRITE_TIMER_LOG)
       
   245     __SIP_INT_LOG1( "LwTimer TimerExpiredL id", aTimerId )
       
   246 #endif
       
   247 		//Store callback and parameter before freeing timerReq
       
   248 		MExpirationHandler* notifier = timerReq->Observer();
       
   249 		TAny* timerParam = timerReq->TimerParam();
       
   250 
       
   251 		//Remove expired item BEFORE calling TimerExpiredL.
       
   252 		//If user stops the expiring timer withing TimerExpiredL, would crash
       
   253 		//as the timer would be used here after TimerExpiredL.
       
   254 		iTimerStore->Remove(index);
       
   255 
       
   256 		//Notifier can be NULL for the internal timer
       
   257 		if (notifier)
       
   258 			{
       
   259 			iNowExpiring = ETrue;
       
   260 			notifier->TimerExpiredL(aTimerId, timerParam);
       
   261 			iNowExpiring = EFalse;
       
   262 			}		
       
   263 		}
       
   264 
       
   265 	SetTimerWithShortestValueL();
       
   266 
       
   267 	__TEST_INVARIANT;
       
   268 	}
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // CTimerManager::LeaveOccurred
       
   272 // Do things that were not done in CTimerManager::TimerExpiredL due to leave.
       
   273 // Clear iNowExpiring flag and start the next timer.
       
   274 // -----------------------------------------------------------------------------
       
   275 //
       
   276 TInt CTimerManager::LeaveOccurred()
       
   277 	{
       
   278     __TEST_INVARIANT;
       
   279 
       
   280 	iNowExpiring = EFalse;
       
   281 	TRAPD(err, SetTimerWithShortestValueL());
       
   282 
       
   283     __TEST_INVARIANT;
       
   284 	return err;
       
   285 	}
       
   286 
       
   287 // -----------------------------------------------------------------------------
       
   288 // CTimerManager::NewTimerId
       
   289 // -----------------------------------------------------------------------------
       
   290 //
       
   291 TTimerId CTimerManager::NewTimerId()
       
   292 	{
       
   293 	__TEST_INVARIANT;
       
   294 
       
   295 	iTimerIdCounter++;
       
   296 	if (iTimerIdCounter == KNoSuchTimer || iTimerIdCounter == KReservedTimer)
       
   297 		{
       
   298 		iTimerIdCounter = KReservedTimer + 1;
       
   299 		}
       
   300 
       
   301 	__TEST_INVARIANT;
       
   302 	return iTimerIdCounter;
       
   303 	}
       
   304 
       
   305 // -----------------------------------------------------------------------------
       
   306 // CTimerManager::MillisecToTTime
       
   307 // -----------------------------------------------------------------------------
       
   308 //
       
   309 TTime CTimerManager::MillisecToTTime(TUint aMilliseconds) const
       
   310 	{
       
   311 	__TEST_INVARIANT;
       
   312 
       
   313 	TTime expirationTime;
       
   314 	expirationTime.HomeTime();
       
   315 
       
   316 	TTimeIntervalSeconds sec = aMilliseconds/1000;
       
   317 	expirationTime += sec;
       
   318 
       
   319 	TTimeIntervalMicroSeconds us = (TInt64)(aMilliseconds % 1000) * 1000;
       
   320 	expirationTime += us;
       
   321 
       
   322 	return expirationTime;
       
   323 	}
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 // CTimerManager::TTimeToMillisec
       
   327 // -----------------------------------------------------------------------------
       
   328 //
       
   329 TBool CTimerManager::TTimeToMillisec(TTime aTime, TUint& aMilliseconds) const
       
   330 	{
       
   331 	__TEST_INVARIANT;
       
   332 
       
   333 	TTime now;
       
   334 	now.HomeTime();
       
   335 
       
   336 	TTimeIntervalSeconds deltaSeconds(0);
       
   337 	if (aTime.SecondsFrom(now, deltaSeconds) != KErrNone)
       
   338 		{
       
   339 		return EFalse;
       
   340 		}
       
   341 
       
   342 	TTimeIntervalMicroSeconds deltaMicrosecs = aTime.MicroSecondsFrom(now);
       
   343 
       
   344 	if (deltaSeconds.Int() < 0 || deltaMicrosecs.Int64() < 0)
       
   345 		{
       
   346 		//Timer should've already expired. Expire now. Can happen in debugger or
       
   347 		//if there are so many short timers they can't be handled fast enough.
       
   348 		aMilliseconds = 0;
       
   349 		return ETrue;
       
   350 		}
       
   351 
       
   352     // Set a value anyway but it should not be used for timer if EFalse is 
       
   353     // returned
       
   354     aMilliseconds = deltaSeconds.Int() * 1000;
       
   355     
       
   356 	//"+ 1" is the worst case for the millisecond part
       
   357     if ((deltaSeconds.Int() + 1) > CSingleTimer::KMaxTimerAfterDuration / 1000)
       
   358 		{
       
   359 		return EFalse;
       
   360 		}
       
   361 
       
   362 	TInt64 milliseconds = (deltaMicrosecs.Int64()/1000) % 1000;
       
   363 	//As milliseconds is always 0..999, the low 32 bits are enough
       
   364 	aMilliseconds += I64LOW(milliseconds);
       
   365 
       
   366 	return ETrue;
       
   367 	}
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // CTimerManager::CheckIsNewTimerShortestL
       
   371 // If duration is too long for CSingleTimer::SetAfter, simulate it by chaining
       
   372 // many shorter timers together. RTimer::At is avoided as it may end with
       
   373 // KErrAbort ~23 minutes after setting it.
       
   374 // -----------------------------------------------------------------------------
       
   375 //
       
   376 void CTimerManager::CheckIsNewTimerShortestL(TTimerId aTimerId,
       
   377 											 TUint aMilliseconds)
       
   378 	{
       
   379 	__TEST_INVARIANT;
       
   380 
       
   381 	TTimerId shortestTimerId(0);
       
   382 	TTime shortestExpirationTime;
       
   383 
       
   384 	if (iTimerStore->ShortestTimer(shortestTimerId, shortestExpirationTime))
       
   385 		{
       
   386 		if (shortestTimerId == aTimerId)
       
   387 			{
       
   388 			//The new timer is shorter than any of the existing, stop iTimer and
       
   389 			//start it with the new expiration time.
       
   390 			iTimer->Cancel();
       
   391 
       
   392             if (aMilliseconds <= CSingleTimer::KMaxTimerAfterDuration)
       
   393 				{
       
   394 				iTimer->SetAfter(aMilliseconds, aTimerId);
       
   395 				}
       
   396 			else
       
   397 				{
       
   398 				SetInternalTimerL();
       
   399 				}
       
   400 			}
       
   401 		}
       
   402 	else
       
   403 		{
       
   404 		//There must be at least one timer (the one just created).
       
   405 		__ASSERT_DEBUG(EFalse,
       
   406 			User::Panic(_L("TimerMgr:CheckIsNewTimerShortestL no timers"),
       
   407 						KErrNotFound));
       
   408 		}
       
   409 
       
   410 	__TEST_INVARIANT;
       
   411 	}
       
   412 
       
   413 // -----------------------------------------------------------------------------
       
   414 // CTimerManager::SetTimerWithShortestValueL
       
   415 // -----------------------------------------------------------------------------
       
   416 //
       
   417 void CTimerManager::SetTimerWithShortestValueL()
       
   418 	{
       
   419 	__TEST_INVARIANT;
       
   420     __SIP_ASSERT_LEAVE(!iTimer->IsActive(), KErrNotReady);
       
   421 
       
   422 	TTimerId timerId(0);
       
   423 	TTime expirationTime;
       
   424 	
       
   425 	if (iTimerStore->ShortestTimer(timerId, expirationTime))
       
   426 		{
       
   427 		TUint milliseconds(0);
       
   428 
       
   429 		if (TTimeToMillisec(expirationTime, milliseconds))
       
   430 			{
       
   431 			iTimer->SetAfter(milliseconds, timerId);
       
   432 			}
       
   433 		else
       
   434 			{
       
   435 			SetInternalTimerL();
       
   436 			}
       
   437 		}
       
   438 
       
   439 #if defined(WRITE_TIMER_LOG)
       
   440 	__SIP_INT_LOG1( "LwTimer SetTimerWithShortestValueL id", timerId )
       
   441 #endif
       
   442 
       
   443 	__TEST_INVARIANT;
       
   444 	}
       
   445 
       
   446 // -----------------------------------------------------------------------------
       
   447 // CTimerManager::SetInternalTimerL
       
   448 // -----------------------------------------------------------------------------
       
   449 //
       
   450 void CTimerManager::SetInternalTimerL() const
       
   451 	{
       
   452     __TEST_INVARIANT;
       
   453 
       
   454 	iTimerStore->AddL(NULL,
       
   455                       MillisecToTTime(CSingleTimer::KInternalTimerDuration),
       
   456 					  KReservedTimer,
       
   457 					  NULL);
       
   458     iTimer->SetAfter(CSingleTimer::KInternalTimerDuration, KReservedTimer);
       
   459 	}
       
   460 
       
   461 // -----------------------------------------------------------------------------
       
   462 // CTimerManager::__DbgTestInvariant
       
   463 // -----------------------------------------------------------------------------
       
   464 //
       
   465 
       
   466 void CTimerManager::__DbgTestInvariant() const
       
   467 	{
       
   468 	if (!iTimerStore || !iTimer)
       
   469 		{
       
   470 		User::Invariant();
       
   471 		}
       
   472 	}