os/kernelhwsrv/kernel/eka/euser/cbase/ub_dtim.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32\euser\cbase\ub_dtim.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "ub_std.h"
sl@0
    19
sl@0
    20
/**
sl@0
    21
Creates a new timed event queue with the specified active object priority.
sl@0
    22
sl@0
    23
@param aPriority The priority of this active object.
sl@0
    24
sl@0
    25
@return On successful return, a pointer to the queue of timed events.
sl@0
    26
sl@0
    27
@publishedAll
sl@0
    28
@released
sl@0
    29
*/
sl@0
    30
EXPORT_C CDeltaTimer* CDeltaTimer::NewL(TInt aPriority)
sl@0
    31
	{
sl@0
    32
	TTimeIntervalMicroSeconds32 tickPeriod;
sl@0
    33
	UserHal::TickPeriod(tickPeriod);
sl@0
    34
sl@0
    35
	CDeltaTimer* timer = new(ELeave) CDeltaTimer(aPriority, tickPeriod.Int());
sl@0
    36
sl@0
    37
	TInt err = timer->iTimer.CreateLocal();
sl@0
    38
sl@0
    39
	if (err)
sl@0
    40
		{
sl@0
    41
		delete timer;
sl@0
    42
		User::Leave(err);
sl@0
    43
		}
sl@0
    44
sl@0
    45
	CActiveScheduler::Add(timer);
sl@0
    46
sl@0
    47
	return timer;
sl@0
    48
	}
sl@0
    49
sl@0
    50
/**
sl@0
    51
Creates a new timed event queue with the specified active object priority, and 
sl@0
    52
the specified timer granularity.
sl@0
    53
sl@0
    54
@param aPriority    The priority of this active object.
sl@0
    55
@param aGranularity Ignored.  The resolution of the timer is the tick period.
sl@0
    56
sl@0
    57
@return On successful return, a pointer to the queue of timed events.
sl@0
    58
sl@0
    59
@publishedAll
sl@0
    60
@deprecated
sl@0
    61
*/
sl@0
    62
EXPORT_C CDeltaTimer* CDeltaTimer::NewL(TInt aPriority, TTimeIntervalMicroSeconds32 /*aGranularity*/)
sl@0
    63
	{
sl@0
    64
	return CDeltaTimer::NewL(aPriority);
sl@0
    65
	}
sl@0
    66
sl@0
    67
/**
sl@0
    68
Constructor taking an active object priority value.
sl@0
    69
sl@0
    70
The constructor sets this active object's priority value through a call to 
sl@0
    71
the base class constructor in its c'tor list.
sl@0
    72
sl@0
    73
@param aPriority    The priority of this active object.
sl@0
    74
@param aTickPeriod  The period of a tick on the system.
sl@0
    75
sl@0
    76
@internalComponent
sl@0
    77
@released
sl@0
    78
*/
sl@0
    79
CDeltaTimer::CDeltaTimer(TInt aPriority, TInt aTickPeriod)
sl@0
    80
	: CActive(aPriority), iTickPeriod(aTickPeriod)
sl@0
    81
	{
sl@0
    82
	}
sl@0
    83
sl@0
    84
/**
sl@0
    85
Adds a new timed event entry into the timed event queue.
sl@0
    86
	
sl@0
    87
@param aTimeInMicroSeconds The interval from the present time when the timed 
sl@0
    88
	                       event entry is to expire.
sl@0
    89
@param aEntry              The timed event entry encapsulating the call back that
sl@0
    90
                           is to be called when this timed event entry expires.
sl@0
    91
sl@0
    92
@publishedAll
sl@0
    93
@released
sl@0
    94
*/
sl@0
    95
EXPORT_C void CDeltaTimer::Queue(TTimeIntervalMicroSeconds32 aTimeInMicroSeconds, TDeltaTimerEntry& aEntry)
sl@0
    96
	{
sl@0
    97
	QueueLong(TTimeIntervalMicroSeconds(MAKE_TINT64(0, aTimeInMicroSeconds.Int())), aEntry);
sl@0
    98
	}
sl@0
    99
sl@0
   100
/**
sl@0
   101
Adds a new timed event entry into the timed event queue.
sl@0
   102
	
sl@0
   103
@param aTimeInMicroSeconds The interval from the present time when the timed 
sl@0
   104
	                       event entry is to expire.
sl@0
   105
@param aEntry              The timed event entry encapsulating the call back that
sl@0
   106
                           is to be called when this timed event entry expires.
sl@0
   107
sl@0
   108
@return KErrNone if sucessful, KErrOverflow if the interval is too great or negative.
sl@0
   109
sl@0
   110
@publishedAll
sl@0
   111
@released
sl@0
   112
*/
sl@0
   113
EXPORT_C TInt CDeltaTimer::QueueLong(TTimeIntervalMicroSeconds aTimeInMicroSeconds, TDeltaTimerEntry& aEntry)
sl@0
   114
	{
sl@0
   115
	const TInt64 timeInTicks = (aTimeInMicroSeconds.Int64() + iTickPeriod - 1) / iTickPeriod;
sl@0
   116
sl@0
   117
	TInt timeInTicks32 = I64LOW(timeInTicks);
sl@0
   118
sl@0
   119
	// We are using deltas on tick values, hence using maximum signed number of ticks
sl@0
   120
	if (I64HIGH(timeInTicks) || (timeInTicks32 < 0))
sl@0
   121
		{
sl@0
   122
		return KErrOverflow;
sl@0
   123
		}
sl@0
   124
sl@0
   125
	// Make sure we queue for at least one tick
sl@0
   126
	if (timeInTicks32 == 0)
sl@0
   127
		{
sl@0
   128
		timeInTicks32 = 1;
sl@0
   129
		}
sl@0
   130
	
sl@0
   131
	// Calculate tick count for new entry
sl@0
   132
	aEntry.iLink.iTickCount = Exec::TickCount() + timeInTicks32;
sl@0
   133
sl@0
   134
	// Add this entry at the right spot
sl@0
   135
	iQueue.Add(aEntry.iLink);
sl@0
   136
sl@0
   137
	// Queue the timer, re-queuing if we've added to the head of the queue
sl@0
   138
	Activate(&aEntry.iLink == iQueue.First());
sl@0
   139
	
sl@0
   140
	return KErrNone;
sl@0
   141
	}
sl@0
   142
sl@0
   143
/**
sl@0
   144
Issues a new RTimer request, if there is no outstanding request and the queue 
sl@0
   145
is not empty.
sl@0
   146
sl@0
   147
@internalComponent
sl@0
   148
@released
sl@0
   149
*/
sl@0
   150
void CDeltaTimer::Activate(TBool aRequeueTimer)
sl@0
   151
//
sl@0
   152
// Queue a request on the timer.
sl@0
   153
//
sl@0
   154
	{
sl@0
   155
	if (IsActive())
sl@0
   156
		{
sl@0
   157
		if (aRequeueTimer)
sl@0
   158
			{
sl@0
   159
			Cancel();
sl@0
   160
			}
sl@0
   161
		else
sl@0
   162
			{
sl@0
   163
			return;
sl@0
   164
			}		
sl@0
   165
		}
sl@0
   166
sl@0
   167
	if (!iQueue.IsEmpty() && !iQueueBusy)
sl@0
   168
		{
sl@0
   169
		SetActive();
sl@0
   170
sl@0
   171
		const TInt ticksToWait = iQueue.First()->iTickCount - Exec::TickCount();
sl@0
   172
sl@0
   173
		if (ticksToWait > 0)
sl@0
   174
			{
sl@0
   175
			iTimer.AfterTicks(iStatus, ticksToWait);
sl@0
   176
			}
sl@0
   177
		else
sl@0
   178
			{
sl@0
   179
			TRequestStatus* status = &iStatus;
sl@0
   180
			User::RequestComplete(status, KErrNone);
sl@0
   181
			}
sl@0
   182
		}
sl@0
   183
	}
sl@0
   184
sl@0
   185
/**
sl@0
   186
Deals with an RTimer completion event.
sl@0
   187
sl@0
   188
The function inspects the timed event entry at the head of the queue, and 
sl@0
   189
reduces its timer value by the appropriate amount. If this timed event is 
sl@0
   190
now found to have expired, the call back function is called, and the timed 
sl@0
   191
event entry removed from the queue.
sl@0
   192
sl@0
   193
If the timed event entry has not expired, it remains at the head of the queue.
sl@0
   194
sl@0
   195
The function issues a new RTimer request, using the timer granularity value 
sl@0
   196
as the time interval.
sl@0
   197
sl@0
   198
@see RTimer
sl@0
   199
@see CActive
sl@0
   200
sl@0
   201
@internalComponent
sl@0
   202
@released
sl@0
   203
*/
sl@0
   204
void CDeltaTimer::RunL()
sl@0
   205
//
sl@0
   206
// Call all zero delta callbacks
sl@0
   207
	{
sl@0
   208
	// Queue busy
sl@0
   209
	iQueueBusy = ETrue;
sl@0
   210
sl@0
   211
	// Whilst the list of expired timers is being processed, time will pass and
sl@0
   212
	// the tick count may have increased such that there are now more expired
sl@0
   213
	// timers.  Loop until we have either emptied the queue or can wait for a
sl@0
   214
	// timer exipration in the future.
sl@0
   215
	while (!iQueue.IsEmpty())
sl@0
   216
		{
sl@0
   217
		// Calculate how long till first timer expires
sl@0
   218
		const TUint tickCount = Exec::TickCount();
sl@0
   219
sl@0
   220
		// If the first timer is yet to expire, wait some more
sl@0
   221
		if (((TInt)(iQueue.First()->iTickCount - tickCount)) > 0)
sl@0
   222
			{
sl@0
   223
			break;
sl@0
   224
			}
sl@0
   225
sl@0
   226
		// Remove entry before callback to prevent re-entrancy issues
sl@0
   227
		TTickCountQueLink* entry = iQueue.RemoveFirst();
sl@0
   228
sl@0
   229
		// Iterate through the timers we know have expired based on the
sl@0
   230
		// last calculation of delta
sl@0
   231
		while (entry)
sl@0
   232
			{
sl@0
   233
			// Make callback.  This could go reentrant on Queue[Long]() or Remove().
sl@0
   234
			reinterpret_cast<TDeltaTimerEntry*>(
sl@0
   235
				PtrSub(
sl@0
   236
					entry,
sl@0
   237
					_FOFF(TDeltaTimerEntry, iLink)
sl@0
   238
				))
sl@0
   239
			->iCallBack.CallBack();
sl@0
   240
sl@0
   241
			// Remove the next expired entry, if any
sl@0
   242
			entry = iQueue.RemoveFirst(tickCount);
sl@0
   243
			}
sl@0
   244
		}
sl@0
   245
sl@0
   246
	// Queue idle
sl@0
   247
	iQueueBusy = EFalse;
sl@0
   248
sl@0
   249
	// Requeue timer
sl@0
   250
	Activate();
sl@0
   251
	}
sl@0
   252
	
sl@0
   253
/**
sl@0
   254
Implements cancellation of an oustanding RTimer request.
sl@0
   255
sl@0
   256
@internalComponent
sl@0
   257
@released
sl@0
   258
*/
sl@0
   259
void CDeltaTimer::DoCancel()
sl@0
   260
	{
sl@0
   261
	iTimer.Cancel();
sl@0
   262
	}
sl@0
   263
sl@0
   264
/**
sl@0
   265
Removes the specified timed event entry from the timer queue.
sl@0
   266
sl@0
   267
@param aEntry The timed event entry.
sl@0
   268
sl@0
   269
@publishedAll
sl@0
   270
@released
sl@0
   271
*/
sl@0
   272
EXPORT_C void CDeltaTimer::Remove(TDeltaTimerEntry& aEntry)
sl@0
   273
	{
sl@0
   274
	// Remove the specified entry from the list
sl@0
   275
	aEntry.iLink.Deque();
sl@0
   276
	}
sl@0
   277
sl@0
   278
/**
sl@0
   279
Destructor.
sl@0
   280
sl@0
   281
Frees all resources before destruction of the object. Specifically, it cancels 
sl@0
   282
any outstanding timer requests generated by the RTimer object and then deletes 
sl@0
   283
all timed event entries from the timed event queue.
sl@0
   284
sl@0
   285
@see RTimer
sl@0
   286
sl@0
   287
@publishedAll
sl@0
   288
@released
sl@0
   289
*/
sl@0
   290
CDeltaTimer::~CDeltaTimer()
sl@0
   291
	{
sl@0
   292
	Cancel();
sl@0
   293
sl@0
   294
	while (!iQueue.IsEmpty())
sl@0
   295
		{
sl@0
   296
		iQueue.First()->Deque();
sl@0
   297
		}
sl@0
   298
sl@0
   299
	iTimer.Close();
sl@0
   300
	}