os/kernelhwsrv/kernel/eka/euser/cbase/ub_tim.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) 1995-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_tim.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "ub_std.h"
sl@0
    19
sl@0
    20
EXPORT_C CTimer::CTimer(TInt aPriority)
sl@0
    21
	: CActive(aPriority)
sl@0
    22
/**
sl@0
    23
Protected constructor with priority.
sl@0
    24
sl@0
    25
Use this constructor to set the priority of the active object.
sl@0
    26
sl@0
    27
Classes derived from CTimer must define and provide a constructor through 
sl@0
    28
which the priority of the active object can be passed. Such a constructor 
sl@0
    29
can call CTimer's constructor in its constructor initialisation list.
sl@0
    30
sl@0
    31
@param aPriority The priority of the timer.
sl@0
    32
*/
sl@0
    33
	{
sl@0
    34
	}
sl@0
    35
sl@0
    36
sl@0
    37
sl@0
    38
sl@0
    39
EXPORT_C CTimer::~CTimer()
sl@0
    40
/**
sl@0
    41
Destructor.
sl@0
    42
sl@0
    43
Frees resources prior to destruction. Specifically, it cancels any outstanding 
sl@0
    44
request and closes the RTimer handle.
sl@0
    45
*/
sl@0
    46
	{
sl@0
    47
sl@0
    48
	Cancel();
sl@0
    49
	iTimer.Close();
sl@0
    50
	}
sl@0
    51
sl@0
    52
sl@0
    53
sl@0
    54
sl@0
    55
EXPORT_C void CTimer::At(const TTime &aTime)
sl@0
    56
/**
sl@0
    57
Requests an event at a given local time.
sl@0
    58
sl@0
    59
This timer completes at the specified time - if the machine is in a 
sl@0
    60
turned off state at that time, the machine will be turned on again.
sl@0
    61
sl@0
    62
Notes:
sl@0
    63
sl@0
    64
1. The CTimer' RunL() function will be run as soon as possible after the
sl@0
    65
   specified  system time.
sl@0
    66
sl@0
    67
2. The RunL() may be delayed because the RunL() of another active object, with 
sl@0
    68
   the deepest nesting-level active scheduler on the same thread, is running 
sl@0
    69
   when the event occurs: this cannot be avoided, but can be minimised by
sl@0
    70
   making  all RunL()s of short duration.
sl@0
    71
sl@0
    72
3. The RunL() may be delayed because other, higher-priority, active objects are 
sl@0
    73
   scheduled instead. This can be avoided by making CTimers very high-priority.
sl@0
    74
   
sl@0
    75
4. The TTime object should be set to the home time.
sl@0
    76
sl@0
    77
@param aTime The local time at which the event is to occur.
sl@0
    78
sl@0
    79
@see TTime::HomeTime
sl@0
    80
*/
sl@0
    81
	{
sl@0
    82
sl@0
    83
	__ASSERT_ALWAYS(IsAdded(),Panic(ETimNotAdded));
sl@0
    84
	iTimer.At(iStatus,aTime);
sl@0
    85
	SetActive();
sl@0
    86
	}
sl@0
    87
sl@0
    88
sl@0
    89
sl@0
    90
sl@0
    91
EXPORT_C void CTimer::AtUTC(const TTime &aTimeInUTC)
sl@0
    92
/**
sl@0
    93
Requests an event at a given UTC time.
sl@0
    94
sl@0
    95
This timer completes at the specified time - if the machine is in a 
sl@0
    96
turned off state at that time, the machine will be turned on again.
sl@0
    97
sl@0
    98
Notes:
sl@0
    99
sl@0
   100
1. The CTimer' RunL() function will be run as soon as possible after the
sl@0
   101
   specified  system time.
sl@0
   102
sl@0
   103
2. The RunL() may be delayed because the RunL() of another active object, with 
sl@0
   104
   the deepest nesting-level active scheduler on the same thread, is running 
sl@0
   105
   when the event occurs: this cannot be avoided, but can be minimised by
sl@0
   106
   making  all RunL()s of short duration.
sl@0
   107
sl@0
   108
3. The RunL() may be delayed because other, higher-priority, active objects are 
sl@0
   109
   scheduled instead. This can be avoided by making CTimers very high-priority.
sl@0
   110
   
sl@0
   111
4. The TTime object should be set to the universal time.
sl@0
   112
sl@0
   113
@param aTime The UTC time at which the event is to occur.
sl@0
   114
sl@0
   115
@see TTime::UniversalTime
sl@0
   116
*/
sl@0
   117
	{
sl@0
   118
sl@0
   119
	__ASSERT_ALWAYS(IsAdded(),Panic(ETimNotAdded));
sl@0
   120
	iTimer.AtUTC(iStatus,aTimeInUTC);
sl@0
   121
	SetActive();
sl@0
   122
	}
sl@0
   123
sl@0
   124
sl@0
   125
sl@0
   126
sl@0
   127
EXPORT_C void CTimer::After(TTimeIntervalMicroSeconds32 anInterval)
sl@0
   128
/**
sl@0
   129
Requests an event after an interval.
sl@0
   130
sl@0
   131
This timer completes after the specified number of microseconds. The 
sl@0
   132
"after timer" counter stops during power-down. Therefore, a 5-second timer 
sl@0
   133
will complete late if the machine is turned off 2 seconds after the request 
sl@0
   134
is made.
sl@0
   135
sl@0
   136
Notes:
sl@0
   137
sl@0
   138
1. The CTimer's RunL() function will be run as soon as possible after the
sl@0
   139
   specified interval.
sl@0
   140
sl@0
   141
2. The RunL() may be delayed because the RunL() of another active object, with 
sl@0
   142
   the deepest nesting-level active scheduler on the same thread, is running 
sl@0
   143
   when the event occurs: this cannot be avoided, but can be minimised by
sl@0
   144
   making  all RunL()s of short duration.
sl@0
   145
sl@0
   146
3. The RunL() may be delayed because other, higher-priority, active objects are 
sl@0
   147
   scheduled instead. This can be avoided by making CTimers very high-priority.
sl@0
   148
sl@0
   149
@param anInterval Interval after which event is to occur, in microseconds.
sl@0
   150
sl@0
   151
@panic USER 87, if anInterval is negative. This is raised by the
sl@0
   152
       underlying RTimer.
sl@0
   153
@panic E32USER-CBase 51, if the active object has not been added to an
sl@0
   154
       active scheduler.
sl@0
   155
       
sl@0
   156
@see RTimer
sl@0
   157
*/
sl@0
   158
	{
sl@0
   159
sl@0
   160
	__ASSERT_ALWAYS(IsAdded(),Panic(ETimNotAdded));
sl@0
   161
	iTimer.After(iStatus,anInterval);
sl@0
   162
	SetActive();
sl@0
   163
	}
sl@0
   164
sl@0
   165
sl@0
   166
sl@0
   167
sl@0
   168
EXPORT_C void CTimer::Lock(TTimerLockSpec aLock)
sl@0
   169
/** 
sl@0
   170
Requests an event on a specified second fraction.
sl@0
   171
sl@0
   172
Note that the RunL() function is run exactly on the specified second fraction.
sl@0
   173
sl@0
   174
@param aLock The fraction of a second at which the timer completes.
sl@0
   175
*/
sl@0
   176
	{
sl@0
   177
sl@0
   178
	__ASSERT_ALWAYS(IsAdded(),Panic(ETimNotAdded));
sl@0
   179
	iTimer.Lock(iStatus,aLock);
sl@0
   180
	SetActive();
sl@0
   181
	}
sl@0
   182
sl@0
   183
sl@0
   184
sl@0
   185
sl@0
   186
EXPORT_C void CTimer::Inactivity(TTimeIntervalSeconds aSeconds)
sl@0
   187
/**
sl@0
   188
Requests an event if no activity occurs within the specified interval.
sl@0
   189
sl@0
   190
@param aSeconds The time interval.
sl@0
   191
*/
sl@0
   192
	{
sl@0
   193
sl@0
   194
	__ASSERT_ALWAYS(IsAdded(),Panic(ETimNotAdded));
sl@0
   195
	iTimer.Inactivity(iStatus, aSeconds);
sl@0
   196
	SetActive();
sl@0
   197
	}
sl@0
   198
sl@0
   199
sl@0
   200
sl@0
   201
EXPORT_C void CTimer::HighRes(TTimeIntervalMicroSeconds32 aInterval)
sl@0
   202
/**
sl@0
   203
Requests an event after the specified interval to a resolution of 1ms. 
sl@0
   204
The "HighRes timer" counter stops during power-down (the same as "after timer"). 
sl@0
   205
sl@0
   206
@param aInterval  The time interval, in microseconds, after which an event
sl@0
   207
                  is to occur.
sl@0
   208
@panic USER 87, if anInterval is negative. This is raised by the
sl@0
   209
       underlying RTimer.
sl@0
   210
@panic KERN-EXEC 15, if this function is called while a request for a timer
sl@0
   211
       event is still outstanding.
sl@0
   212
*/
sl@0
   213
	{
sl@0
   214
sl@0
   215
	__ASSERT_ALWAYS(IsAdded(),Panic(ETimNotAdded));
sl@0
   216
	iTimer.HighRes(iStatus, aInterval);
sl@0
   217
	SetActive();
sl@0
   218
	}
sl@0
   219
sl@0
   220
sl@0
   221
sl@0
   222
sl@0
   223
EXPORT_C void CTimer::ConstructL()
sl@0
   224
/**
sl@0
   225
Constructs a new asynchronous timer.
sl@0
   226
sl@0
   227
The function must be called before any timer requests (i.e. calls to
sl@0
   228
RTimer::After() or RTimer::At()) can be made.
sl@0
   229
sl@0
   230
Since it is protected, it cannot be called directly by clients of CTimer
sl@0
   231
derived classes. Typically, a derived class makes a base call to this function
sl@0
   232
in the second phase of two-phase construction; i.e. the derived class defines 
sl@0
   233
and implements its own ConstructL() function within which it makes a base 
sl@0
   234
call to CTimer::ConstructL().
sl@0
   235
*/
sl@0
   236
	{
sl@0
   237
sl@0
   238
	TInt r=iTimer.CreateLocal();
sl@0
   239
	if (r!=KErrNone)
sl@0
   240
		User::Leave(r);
sl@0
   241
	}
sl@0
   242
sl@0
   243
sl@0
   244
sl@0
   245
sl@0
   246
EXPORT_C void CTimer::DoCancel()
sl@0
   247
//
sl@0
   248
// Cancel the timer.
sl@0
   249
//
sl@0
   250
	{
sl@0
   251
sl@0
   252
	iTimer.Cancel();
sl@0
   253
	}
sl@0
   254
sl@0
   255
sl@0
   256
sl@0
   257
sl@0
   258
EXPORT_C CPeriodic *CPeriodic::New(TInt aPriority)
sl@0
   259
/**
sl@0
   260
Allocates and constructs a CPeriodic object - non-leaving.
sl@0
   261
sl@0
   262
Specify a high priority so the callback function is scheduled as soon as
sl@0
   263
possible after the timer events complete.
sl@0
   264
sl@0
   265
@param aPriority The priority of the active object. If timing is critical, 
sl@0
   266
                 it should be higher than that of all other active objects
sl@0
   267
                 owned by the scheduler.
sl@0
   268
                 
sl@0
   269
@return Pointer to new CPeriodic object. The object is initialised and added 
sl@0
   270
        to the active scheduler. This value is NULL if there is insufficient
sl@0
   271
        memory.
sl@0
   272
*/
sl@0
   273
	{
sl@0
   274
sl@0
   275
	CPeriodic *pP=new CPeriodic(aPriority);
sl@0
   276
	if (pP)
sl@0
   277
		{
sl@0
   278
		TRAPD(r,pP->ConstructL());
sl@0
   279
		if (r==KErrNone)
sl@0
   280
			CActiveScheduler::Add(pP);
sl@0
   281
		else
sl@0
   282
			{
sl@0
   283
			delete pP;
sl@0
   284
			pP=NULL;
sl@0
   285
			}
sl@0
   286
		}
sl@0
   287
	return pP;
sl@0
   288
	}
sl@0
   289
sl@0
   290
sl@0
   291
sl@0
   292
sl@0
   293
EXPORT_C CPeriodic *CPeriodic::NewL(TInt aPriority)
sl@0
   294
/**
sl@0
   295
Allocates and constructs a CPeriodic object - leaving.
sl@0
   296
sl@0
   297
Specify a high priority so the callback function is scheduled as soon as
sl@0
   298
possible after the timer events complete.
sl@0
   299
sl@0
   300
@param aPriority The priority of the active object. If timing is critical, 
sl@0
   301
                 it should be higher than that of all other active objects
sl@0
   302
                 owned by the scheduler.
sl@0
   303
sl@0
   304
@return Pointer to new CPeriodic object. The object is initialised and added 
sl@0
   305
        to the active scheduler.
sl@0
   306
                          
sl@0
   307
@leave KErrNoMemory There is insufficient memory to create the object.
sl@0
   308
*/
sl@0
   309
	{
sl@0
   310
sl@0
   311
	return((CPeriodic *)User::LeaveIfNull(New(aPriority)));
sl@0
   312
	}
sl@0
   313
sl@0
   314
sl@0
   315
sl@0
   316
sl@0
   317
EXPORT_C CPeriodic::CPeriodic(TInt aPriority)
sl@0
   318
	: CTimer(aPriority)
sl@0
   319
/**
sl@0
   320
Protected constructor with priority.
sl@0
   321
sl@0
   322
Use this constructor to set the priority of the active object.
sl@0
   323
sl@0
   324
Classes derived from CPeriodic must define and provide a constructor through 
sl@0
   325
which the priority of the active object can be passed. Such a constructor 
sl@0
   326
can call CPeriodic's constructor in its constructor initialisation list.
sl@0
   327
sl@0
   328
@param aPriority The priority of the timer.
sl@0
   329
*/
sl@0
   330
	{
sl@0
   331
	}
sl@0
   332
sl@0
   333
sl@0
   334
sl@0
   335
sl@0
   336
EXPORT_C CPeriodic::~CPeriodic()
sl@0
   337
/**
sl@0
   338
Destructor.
sl@0
   339
sl@0
   340
Frees resources prior to destruction.
sl@0
   341
*/
sl@0
   342
	{
sl@0
   343
	}
sl@0
   344
sl@0
   345
sl@0
   346
sl@0
   347
sl@0
   348
EXPORT_C void CPeriodic::Start(TTimeIntervalMicroSeconds32 aDelay,TTimeIntervalMicroSeconds32 anInterval,TCallBack aCallBack)
sl@0
   349
/**
sl@0
   350
Starts generating periodic events.
sl@0
   351
sl@0
   352
The event calls the protected RunL() function, 
sl@0
   353
which in turn calls the function specified by aCallBack. The first event is 
sl@0
   354
generated after aDelay microseconds; subsequent events are generated regularly 
sl@0
   355
thereafter at intervals of anInterval microseconds.
sl@0
   356
sl@0
   357
The TCallBack contains a function pointer and a TAny* pointer. The function 
sl@0
   358
will be repeatedly called with the pointer as a parameter.
sl@0
   359
sl@0
   360
Once started, periodic events are generated until the CPeriodic object is 
sl@0
   361
destroyed.
sl@0
   362
sl@0
   363
Notes:
sl@0
   364
sl@0
   365
1. The callback function will be run as soon as possible after the initial delay, 
sl@0
   366
   and after each period.
sl@0
   367
sl@0
   368
2. The callback may be delayed because the RunL() of another active object, with 
sl@0
   369
   the deepest nesting-level active scheduler on the same thread, is running 
sl@0
   370
   when the event occurs: this cannot be avoided, but can be minimised by making 
sl@0
   371
   all RunL()s of short duration.
sl@0
   372
sl@0
   373
3. The callback may be delayed because other, higher-priority, active objects 
sl@0
   374
   are scheduled instead. This can be avoided by giving the CPeriodic a very 
sl@0
   375
   high priority.
sl@0
   376
sl@0
   377
@param aDelay     The delay from the Start() function to the generation of the 
sl@0
   378
                  first event, in microseconds.
sl@0
   379
@param anInterval The interval between events generated after the initial
sl@0
   380
                  delay, in microseconds.
sl@0
   381
@param aCallBack  A callback specifying a function to be called when the CPeriodic 
sl@0
   382
                  is scheduled after a timer event.
sl@0
   383
                  
sl@0
   384
@panic E32USER-CBase 52, if anInterval is negative.
sl@0
   385
@panic E32USER-CBase 53, if aDelay is negative.
sl@0
   386
*/
sl@0
   387
	{
sl@0
   388
sl@0
   389
	__ASSERT_ALWAYS(anInterval.Int()>=0,Panic(ETimIntervalNegativeOrZero));
sl@0
   390
	__ASSERT_ALWAYS(aDelay.Int()>=0,Panic(ETimDelayNegative));
sl@0
   391
	iInterval=anInterval.Int();
sl@0
   392
	iCallBack=aCallBack;
sl@0
   393
	After(aDelay);
sl@0
   394
	}
sl@0
   395
sl@0
   396
EXPORT_C void CPeriodic::RunL()
sl@0
   397
//
sl@0
   398
// Handle completion by issuing the next request and then calling back.
sl@0
   399
//
sl@0
   400
	{
sl@0
   401
sl@0
   402
	After(iInterval);
sl@0
   403
	iCallBack.CallBack();
sl@0
   404
	}
sl@0
   405
sl@0
   406
sl@0
   407
sl@0
   408
sl@0
   409
EXPORT_C CHeartbeat::CHeartbeat(TInt aPriority)
sl@0
   410
	: CTimer(aPriority)
sl@0
   411
/**
sl@0
   412
Protected constructor with a priority. Use this constructor to set the priority 
sl@0
   413
of the active object.
sl@0
   414
sl@0
   415
Classes derived from CHeartbeat must define and provide a constructor through 
sl@0
   416
which the priority of the active object can be passed. Such a constructor 
sl@0
   417
can call CHeartbeat's constructor in its constructor initialisation list.
sl@0
   418
sl@0
   419
@param aPriority The priority of the timer.
sl@0
   420
*/
sl@0
   421
	{}
sl@0
   422
sl@0
   423
sl@0
   424
sl@0
   425
sl@0
   426
EXPORT_C CHeartbeat *CHeartbeat::New(TInt aPriority)
sl@0
   427
/**
sl@0
   428
Allocates and constructs a CHeartbeat object - non-leaving.
sl@0
   429
sl@0
   430
Specify a high priority so the callback function is scheduled as soon as
sl@0
   431
possible after the timer events complete.
sl@0
   432
sl@0
   433
@param aPriority The priority of the active object. If timing is critical, 
sl@0
   434
                 it should be higher than that of all other active objects
sl@0
   435
                 owned by the scheduler.
sl@0
   436
                 
sl@0
   437
@return Pointer to new CHeartbeat object. The object is initialised and added 
sl@0
   438
        to the active scheduler. This value is NULL if insufficient memory was
sl@0
   439
        available.
sl@0
   440
*/
sl@0
   441
	{
sl@0
   442
sl@0
   443
	CHeartbeat *pP=new CHeartbeat(aPriority);
sl@0
   444
	if (pP)
sl@0
   445
		{
sl@0
   446
		TRAPD(r,pP->ConstructL());
sl@0
   447
		if (r==KErrNone)
sl@0
   448
			CActiveScheduler::Add(pP);
sl@0
   449
		else
sl@0
   450
			{
sl@0
   451
			delete pP;
sl@0
   452
			pP=NULL;
sl@0
   453
			}
sl@0
   454
		}
sl@0
   455
	return pP;
sl@0
   456
	}
sl@0
   457
sl@0
   458
sl@0
   459
sl@0
   460
sl@0
   461
EXPORT_C CHeartbeat *CHeartbeat::NewL(TInt aPriority)
sl@0
   462
/**
sl@0
   463
Allocates and constructs a CHeartbeat object - leaving.
sl@0
   464
sl@0
   465
Specify a high priority so the callback function is scheduled as soon as
sl@0
   466
possible after the timer events complete.
sl@0
   467
sl@0
   468
@param aPriority The priority of the active object. If timing is critical, 
sl@0
   469
                 it should be higher than that of all other active objects
sl@0
   470
                 owned by the scheduler.
sl@0
   471
                 
sl@0
   472
@return Pointer to new CHeartbeat object. The object is initialised and added 
sl@0
   473
        to the active scheduler.
sl@0
   474
*/
sl@0
   475
	{
sl@0
   476
sl@0
   477
	return((CHeartbeat *)User::LeaveIfNull(New(aPriority)));
sl@0
   478
	}
sl@0
   479
sl@0
   480
sl@0
   481
sl@0
   482
sl@0
   483
EXPORT_C CHeartbeat::~CHeartbeat()
sl@0
   484
/**
sl@0
   485
Destructor.
sl@0
   486
sl@0
   487
Frees resources prior to destruction.
sl@0
   488
*/
sl@0
   489
	{}
sl@0
   490
sl@0
   491
sl@0
   492
sl@0
   493
sl@0
   494
EXPORT_C void CHeartbeat::Start(TTimerLockSpec aLock, MBeating *aBeating)
sl@0
   495
/**
sl@0
   496
Starts generating heartbeat events. The event results in calls to the Beat() 
sl@0
   497
and Synchronize() functions specified by aBeating.
sl@0
   498
sl@0
   499
The first event is generated on the first fraction of a second corresponding 
sl@0
   500
to aLock that occurs after Start() has returned; subsequent events are generated 
sl@0
   501
regularly thereafter at one second intervals on the second fraction specified 
sl@0
   502
by aLock.
sl@0
   503
sl@0
   504
The aBeating mixin must be written by the user. Most of the time, its Beat() 
sl@0
   505
function is called which trivially updates the tick count. Occasionally, synchronisation 
sl@0
   506
is lost, and the Synchronize() function is called instead: this must find 
sl@0
   507
out from the system time how many ticks should have been counted, and update 
sl@0
   508
things accordingly.
sl@0
   509
sl@0
   510
Once started, heartbeat events are generated until the CHeartbeat object is 
sl@0
   511
destroyed.
sl@0
   512
sl@0
   513
@param aLock    The fraction of a second at which the timer completes.
sl@0
   514
@param aBeating Provides the Beat() and Synchronize() functions.
sl@0
   515
*/
sl@0
   516
	{
sl@0
   517
sl@0
   518
	iBeating=aBeating;
sl@0
   519
	iLock=aLock;
sl@0
   520
	Lock(aLock);
sl@0
   521
	}
sl@0
   522
sl@0
   523
sl@0
   524
sl@0
   525
sl@0
   526
EXPORT_C void CHeartbeat::RunL()
sl@0
   527
//
sl@0
   528
// Handle completion
sl@0
   529
//
sl@0
   530
	{
sl@0
   531
	
sl@0
   532
	TRequestStatus stat=iStatus;
sl@0
   533
	Lock(iLock);
sl@0
   534
	if (stat==KErrNone)
sl@0
   535
		iBeating->Beat();
sl@0
   536
	else
sl@0
   537
		iBeating->Synchronize();
sl@0
   538
	}
sl@0
   539