sysstatemgmt/systemstarter/sysmonsrc/timerlist.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2007-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 // timerlist.h
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalComponent 
       
    21 */
       
    22 
       
    23 #include "timerlist.h"
       
    24 #include "../src/SysStartDebug.h"
       
    25 
       
    26 CTimerList* CTimerList::NewL(TInt aPriority)
       
    27 	{
       
    28 	CTimerList* self = new(ELeave) CTimerList(aPriority);
       
    29 	CleanupStack::PushL(self);
       
    30 	self->ConstructL();
       
    31 	CleanupStack::Pop();
       
    32 	return self;
       
    33 	}
       
    34 
       
    35 CTimerList::CTimerList(TInt aPriority) : CTimer(aPriority)
       
    36 	{ 
       
    37 	CActiveScheduler::Add(this);		
       
    38 	}
       
    39 
       
    40 CTimerList::~CTimerList()
       
    41 	{
       
    42 	Cancel();
       
    43 	iTimerList.ResetAndDestroy();
       
    44 	}
       
    45 
       
    46 void CTimerList::ConstructL()
       
    47 	{
       
    48 	CTimer::ConstructL();
       
    49 	}
       
    50 
       
    51 TInt32 CTimerList::AddL(const TTimeIntervalMicroSeconds32& aInterval, const TCallBack& aCallBack)
       
    52 	{
       
    53 	TTimerInfo* info = new(ELeave) TTimerInfo;
       
    54 	
       
    55 	info->iCallBack = aCallBack;
       
    56 	info->iTime.UniversalTime();
       
    57 	info->iTime += TTimeIntervalMicroSeconds32(aInterval);
       
    58 	
       
    59 	AddL(info);
       
    60 	
       
    61 	return reinterpret_cast<TInt32>(info);
       
    62 	}
       
    63 
       
    64 TInt32 CTimerList::AddL(const TTime& aTime, const TCallBack& aCallBack)
       
    65 	{
       
    66 	TTimerInfo* info = new(ELeave) TTimerInfo;
       
    67 	
       
    68 	info->iCallBack = aCallBack;
       
    69 	info->iTime = aTime;
       
    70 
       
    71 	AddL(info);
       
    72 	
       
    73 	return reinterpret_cast<TInt32>(info);
       
    74 	}
       
    75 
       
    76 void CTimerList::AddL(const TTimerInfo* aInfo)
       
    77 	{
       
    78 	TInt64 last = 0;
       
    79 	
       
    80 	TInt count = iTimerList.Count();
       
    81 	if (count > 0)
       
    82 		{
       
    83 		last = iTimerList[count-1]->iTime.Int64();
       
    84 		}
       
    85 	
       
    86 	TLinearOrder<TTimerInfo> timerSortOrder(CTimerList::TimerSortOrder);
       
    87 	
       
    88 	iTimerList.InsertInOrderAllowRepeatsL(aInfo, timerSortOrder);
       
    89 	
       
    90 	// Only reset timer if necessary.
       
    91 	count++;
       
    92 	if (last != iTimerList[count-1]->iTime.Int64())
       
    93 		{
       
    94 		// reset timer to the new last-of-the-list
       
    95 		Cancel();
       
    96 		AtUTC(iTimerList[count-1]->iTime);
       
    97 		}
       
    98 	}
       
    99 	
       
   100 void CTimerList::Remove(TInt32 aTimer)
       
   101 	{
       
   102 	TInt index = iTimerList.FindInAddressOrder(reinterpret_cast<TTimerInfo*>(aTimer));
       
   103 	
       
   104 	if (index != KErrNotFound)
       
   105 		{
       
   106 		delete reinterpret_cast<TTimerInfo*>(aTimer);
       
   107 		iTimerList.Remove(index);
       
   108 		}
       
   109 	}
       
   110 
       
   111 void CTimerList::RunL()
       
   112 	{ // Leavescan, this implementation of this method does not leave but further derived implementations may if they choose
       
   113 	TTime now;
       
   114 
       
   115 	now.UniversalTime();
       
   116 	
       
   117 	TInt count;
       
   118 
       
   119 	for (count = iTimerList.Count(); 
       
   120 		((count > 0) && (iTimerList[count-1]->iTime <= now));
       
   121 		count--)
       
   122 		{
       
   123 		// Always remove from the end of the sorted list
       
   124 		
       
   125 		iTimerList[count-1]->iCallBack.CallBack();
       
   126 		delete iTimerList[count-1];
       
   127 		iTimerList.Remove(count-1);
       
   128 		};
       
   129 		
       
   130 	// reschedule to run again at the expiry date of next repository on the list, if any
       
   131 	count = iTimerList.Count();
       
   132 	if (count > 0)
       
   133 		{
       
   134 		AtUTC(iTimerList[count-1]->iTime);
       
   135 		}
       
   136 	}
       
   137 
       
   138 TInt CTimerList::TimerSortOrder(const TTimerInfo &aInfo1, const TTimerInfo &aInfo2)
       
   139 	{
       
   140 	return static_cast<TInt>(aInfo2.iTime.Int64() - aInfo1.iTime.Int64());
       
   141 	}
       
   142 
       
   143 #ifdef _DEBUG
       
   144 TInt CTimerList::RunError(TInt aError)
       
   145 #else
       
   146 TInt CTimerList::RunError(TInt /*aError*/)
       
   147 #endif
       
   148 	{
       
   149 	DEBUGPRINT2(_L("CTimerList: RunError called with error=%d"), aError);
       
   150 
       
   151 	return KErrNone;
       
   152 	}