os/ossrv/genericopenlibs/posixrealtimeextensions/src/timerserver.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description:
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "sysif.h"
sl@0
    20
#include "timerserver.h"
sl@0
    21
#include "timerhandler.h"
sl@0
    22
#include "timer.h"
sl@0
    23
#include <pthread.h>
sl@0
    24
/* Implementation of the CShutDown class
sl@0
    25
 * Implements a timer
sl@0
    26
 */
sl@0
    27
sl@0
    28
// -------------------------------------------------------------------------------
sl@0
    29
// CShutdown::ConstructL
sl@0
    30
// -------------------------------------------------------------------------------
sl@0
    31
void CShutdown::ConstructL()
sl@0
    32
	{
sl@0
    33
	CTimer::ConstructL();
sl@0
    34
	}
sl@0
    35
sl@0
    36
// -------------------------------------------------------------------------------
sl@0
    37
// CShutdown::Start
sl@0
    38
// Starts the timer for the specified time given
sl@0
    39
// -------------------------------------------------------------------------------
sl@0
    40
void CShutdown::Start()
sl@0
    41
	{
sl@0
    42
	CActiveScheduler::Add(this);
sl@0
    43
	After(KTimerServerShutdownDelay);
sl@0
    44
	}
sl@0
    45
sl@0
    46
// -------------------------------------------------------------------------------
sl@0
    47
// CShutdown::RunL
sl@0
    48
// Initiate server exit when timer expires
sl@0
    49
// -------------------------------------------------------------------------------
sl@0
    50
void CShutdown::RunL()
sl@0
    51
    {
sl@0
    52
    getTimerHandler()->ResetClientFlag();
sl@0
    53
    getTimerHandler()->ResetServerFlag();
sl@0
    54
    CActiveScheduler::Stop();
sl@0
    55
    }
sl@0
    56
sl@0
    57
// -------------------------------------------------------------------------------
sl@0
    58
// CTimerServerSession::CreateL
sl@0
    59
// 2nd phase construction for sessions. Invoked by the CServer2 framework
sl@0
    60
// -------------------------------------------------------------------------------
sl@0
    61
void CTimerServerSession::CreateL()
sl@0
    62
    {
sl@0
    63
    Server().AddSession();
sl@0
    64
    }
sl@0
    65
sl@0
    66
//---------------------------------------------------------------------
sl@0
    67
//CTimerServerSession::~CTimerServerSession
sl@0
    68
//Destructor of the server side session
sl@0
    69
//---------------------------------------------------------------------
sl@0
    70
CTimerServerSession::~CTimerServerSession()
sl@0
    71
	{
sl@0
    72
	Server().DropSession();
sl@0
    73
	}
sl@0
    74
sl@0
    75
//---------------------------------------------------------------------
sl@0
    76
// Handle a client request.
sl@0
    77
// Leaving is handled by CMyServer::ServiceError() which reports
sl@0
    78
// the error code to the client
sl@0
    79
//---------------------------------------------------------------------
sl@0
    80
void CTimerServerSession::ServiceL(const RMessage2& aMessage)
sl@0
    81
	{
sl@0
    82
	switch (aMessage.Function())
sl@0
    83
		{
sl@0
    84
	case EDeleteTimer:
sl@0
    85
		{
sl@0
    86
		TInt timerid = aMessage.Int0();
sl@0
    87
		TInt ret = Server().RemoveInternal(timerid);
sl@0
    88
		aMessage.Complete(ret);
sl@0
    89
		break;
sl@0
    90
		}
sl@0
    91
	case ESetTimer:
sl@0
    92
			{
sl@0
    93
			TInt timerid = aMessage.Int0();
sl@0
    94
			TInt ret = Server().SetTimer(timerid);
sl@0
    95
			aMessage.Complete(ret);
sl@0
    96
			break;
sl@0
    97
			}
sl@0
    98
	default:
sl@0
    99
		PanicClient(aMessage,EPanicIllegalFunction);
sl@0
   100
		break;
sl@0
   101
		}
sl@0
   102
	}
sl@0
   103
sl@0
   104
CTimerServer& CTimerServerSession::Server()
sl@0
   105
	{
sl@0
   106
	return *static_cast<CTimerServer*>(const_cast<CServer2*>(CSession2::Server()));
sl@0
   107
	}
sl@0
   108
sl@0
   109
//------------------------------------------------------------------------------
sl@0
   110
// Handle an error from CTimerServerSession::ServiceL()
sl@0
   111
// A bad descriptor error implies a badly programmed client, so panic it;
sl@0
   112
// otherwise use the default handling (report the error to the client)
sl@0
   113
//------------------------------------------------------------------------------
sl@0
   114
sl@0
   115
void CTimerServerSession::ServiceError(const RMessage2& aMessage,TInt aError)
sl@0
   116
	{
sl@0
   117
	PanicClient(aMessage,EPanicIllegalFunction);
sl@0
   118
	CSession2::ServiceError(aMessage,aError);
sl@0
   119
	}
sl@0
   120
sl@0
   121
//-----------------------------------------------------------------------------------------
sl@0
   122
// Implementation for CTimerServer class
sl@0
   123
// Implements the functionalities of the timer server
sl@0
   124
//------------------------------------------------------------------------------------------
sl@0
   125
CTimerServer* CTimerServer::NewLC()
sl@0
   126
	{
sl@0
   127
	CTimerServer* self=new(ELeave) CTimerServer;
sl@0
   128
	CleanupStack::PushL(self);
sl@0
   129
	self->ConstructL();
sl@0
   130
	return self;
sl@0
   131
	}
sl@0
   132
sl@0
   133
// -------------------------------------------------------------------------------
sl@0
   134
// CTimerServer::ConstructL
sl@0
   135
// Second phase construction. Create the shutdown timer object and the semaphore
sl@0
   136
// -------------------------------------------------------------------------------
sl@0
   137
void CTimerServer::ConstructL()
sl@0
   138
    {
sl@0
   139
    CShutdown* shutdown = new(ELeave) CShutdown;
sl@0
   140
    CleanupStack::PushL(shutdown);
sl@0
   141
    shutdown->ConstructL();
sl@0
   142
	CleanupStack::Pop();
sl@0
   143
	iShutdown = shutdown;
sl@0
   144
	shutdown = 0;
sl@0
   145
    }
sl@0
   146
sl@0
   147
// -------------------------------------------------------------------------------
sl@0
   148
// CTimerServer::AddToScheduler
sl@0
   149
// Add both the server and the shutdown timer objects to the active scheduler
sl@0
   150
// -------------------------------------------------------------------------------
sl@0
   151
TInt CTimerServer::AddToScheduler()
sl@0
   152
	{
sl@0
   153
	iShutdown->Start();
sl@0
   154
	return CServer2::Start(KNullDesC);
sl@0
   155
	}
sl@0
   156
sl@0
   157
// -------------------------------------------------------------------------------
sl@0
   158
// CFileDesTransferServer::RemoveFromScheduler
sl@0
   159
// Remove both the server and the shutdown timer objects from the active scheduler
sl@0
   160
// -------------------------------------------------------------------------------
sl@0
   161
void CTimerServer::RemoveFromScheduler()
sl@0
   162
	{
sl@0
   163
	if (iShutdown->IsAdded())
sl@0
   164
		{
sl@0
   165
		iShutdown->Deque();
sl@0
   166
		}
sl@0
   167
	Deque();
sl@0
   168
	}
sl@0
   169
sl@0
   170
// -------------------------------------------------------------------------------
sl@0
   171
// CFileDesTransferServer::NewSessionL
sl@0
   172
// Create a new client session.
sl@0
   173
// -------------------------------------------------------------------------------
sl@0
   174
CSession2* CTimerServer::NewSessionL(const TVersion&, const RMessage2&) const
sl@0
   175
    {
sl@0
   176
    return new(ELeave) CTimerServerSession;
sl@0
   177
    }
sl@0
   178
sl@0
   179
// -------------------------------------------------------------------------------
sl@0
   180
// CFileDesTransferServer::AddSession
sl@0
   181
// A new session is created. Cancel the shutdown timer if it was running.
sl@0
   182
// -------------------------------------------------------------------------------
sl@0
   183
inline void CTimerServer::AddSession()
sl@0
   184
    {
sl@0
   185
    if (iShutdown->IsAdded())
sl@0
   186
        {
sl@0
   187
        iShutdown->Deque();
sl@0
   188
        }
sl@0
   189
    ++iSessionCount;
sl@0
   190
    
sl@0
   191
    }
sl@0
   192
sl@0
   193
// -------------------------------------------------------------------------------
sl@0
   194
// CTimerServer::DropSession
sl@0
   195
// The session is destroyed. Stop the ActiveScheduler
sl@0
   196
// -------------------------------------------------------------------------------
sl@0
   197
void CTimerServer::DropSession()
sl@0
   198
    {
sl@0
   199
    if (--iSessionCount==0)
sl@0
   200
    	{
sl@0
   201
    	CTimerReqHandler* handler = getTimerHandler();
sl@0
   202
    	handler->ResetClientFlag();
sl@0
   203
    	if ((handler->iTimers.Count()) == 0 )
sl@0
   204
    		{
sl@0
   205
    	    getTimerHandler()->ResetServerFlag();
sl@0
   206
    	    CActiveScheduler::Stop();
sl@0
   207
    	    }
sl@0
   208
    	}
sl@0
   209
    }
sl@0
   210
sl@0
   211
//-------------------------------------------------------------------------
sl@0
   212
// CTimerServer::NewTimerServerL
sl@0
   213
// creates an active scheduler and installs it.
sl@0
   214
//-------------------------------------------------------------------------
sl@0
   215
TInt CTimerServer::NewTimerServerL()
sl@0
   216
	{
sl@0
   217
	CActiveScheduler* pScheduler = new(ELeave) CActiveScheduler;
sl@0
   218
	CleanupStack::PushL(pScheduler);
sl@0
   219
	
sl@0
   220
	CTimerServer* pServer = CTimerServer::NewLC();
sl@0
   221
	
sl@0
   222
	CActiveScheduler::Install(pScheduler);
sl@0
   223
sl@0
   224
	TInt err = pServer->AddToScheduler();
sl@0
   225
    	if (err != KErrNone)
sl@0
   226
    		{
sl@0
   227
    		pServer->RemoveFromScheduler();
sl@0
   228
    		CActiveScheduler::Install(NULL);
sl@0
   229
    		User::Leave(err);
sl@0
   230
    		}
sl@0
   231
    	getTimerHandler()->SetServerHandle(pServer->Server());
sl@0
   232
	// pop both pScheduler and pServer from the cleanup stack
sl@0
   233
    	// Start the scheduler
sl@0
   234
    	RThread().Rendezvous(KErrNone);
sl@0
   235
    	CActiveScheduler::Start();
sl@0
   236
	CActiveScheduler::Install(NULL);
sl@0
   237
	
sl@0
   238
	CleanupStack::PopAndDestroy(2);
sl@0
   239
	return KErrNone;
sl@0
   240
	}
sl@0
   241
// -------------------------------------------------------------------------------
sl@0
   242
// CFileDesTransferServer::~CFileDesTransferServer
sl@0
   243
// -------------------------------------------------------------------------------
sl@0
   244
CTimerServer::~CTimerServer()
sl@0
   245
	{
sl@0
   246
	if(iShutdown)
sl@0
   247
		{
sl@0
   248
		delete iShutdown;
sl@0
   249
		iShutdown = 0;
sl@0
   250
		}
sl@0
   251
	}
sl@0
   252
sl@0
   253
//---------------------------------------------------------------------------------
sl@0
   254
// CTimerServer:: RemoveInternal
sl@0
   255
// Removing the timer object from the list 
sl@0
   256
//---------------------------------------------------------------------------------
sl@0
   257
TInt CTimerServer:: RemoveInternal(const TInt aTimerId)
sl@0
   258
	{
sl@0
   259
	TInt lTimerIdx = 0;
sl@0
   260
	TInt lRet = KErrArgument;
sl@0
   261
	TBool lSigReq = EFalse;
sl@0
   262
	CTimerReqHandler* handler = getTimerHandler();
sl@0
   263
	handler->iTimersLock.Wait();
sl@0
   264
	int lTimerCnt = handler->iTimers.Count();
sl@0
   265
sl@0
   266
	for(lTimerIdx = 0; lTimerIdx != lTimerCnt; lTimerIdx++)
sl@0
   267
		{
sl@0
   268
		if(handler->iTimers[lTimerIdx]->iTimerId == aTimerId)
sl@0
   269
			{
sl@0
   270
			if (handler->iTimers[lTimerIdx]->iSigEvent.sigev_notify == SIGEV_SIGNAL)
sl@0
   271
				lSigReq = ETrue;
sl@0
   272
			RHeap* oldHeap = User::SwitchHeap(Backend()->Heap());
sl@0
   273
			delete handler->iTimers[lTimerIdx];
sl@0
   274
			handler->iTimers.Delete(lTimerIdx);
sl@0
   275
			User::SwitchHeap(oldHeap);
sl@0
   276
			lRet = KErrNone;
sl@0
   277
			break;
sl@0
   278
			}
sl@0
   279
		}	
sl@0
   280
	if(lRet == KErrNone && lSigReq)
sl@0
   281
		{
sl@0
   282
		RHeap* oldHeap = User::SwitchHeap(Backend()->Heap());
sl@0
   283
		handler->iTimers.Compress();	
sl@0
   284
		User::SwitchHeap(oldHeap);
sl@0
   285
		#if (defined SYMBIAN_OE_POSIX_SIGNALS && defined SYMBIAN_OE_LIBRT)		
sl@0
   286
		Backend()->DeleteTimer(aTimerId);
sl@0
   287
		#endif		
sl@0
   288
		}
sl@0
   289
	handler->iTimersLock.Signal();
sl@0
   290
	if(handler->iTimers.Count() == 0)
sl@0
   291
		{
sl@0
   292
		DropSession();
sl@0
   293
		}
sl@0
   294
	return lRet;
sl@0
   295
	}
sl@0
   296
sl@0
   297
//thread startup function for SIGEV_THREAD
sl@0
   298
static TAny* sThreadFunc(TAny* aArgPtr)
sl@0
   299
	{
sl@0
   300
	struct sigevent* lTimerP = reinterpret_cast<struct sigevent *> (aArgPtr);
sl@0
   301
	lTimerP->sigev_notify_function(lTimerP->sigev_value);	
sl@0
   302
	return NULL;
sl@0
   303
	}
sl@0
   304
sl@0
   305
//---------------------------------------------------------------------------------
sl@0
   306
// CTimerServer:: SetTimer
sl@0
   307
// Server sets a new value for the timeout.
sl@0
   308
//---------------------------------------------------------------------------------
sl@0
   309
TInt CTimerServer:: SetTimer(const TInt aTimerId)
sl@0
   310
	{
sl@0
   311
	CRtTimer* lTimer = getTimerHandler()->FindTimer(aTimerId);
sl@0
   312
	if(lTimer)
sl@0
   313
		{
sl@0
   314
		TTime lSetTime(MAKE_TINT64 (0x00dcddb3 ,0x0f2f8000)); 
sl@0
   315
		if (lTimer->iIsArmed)
sl@0
   316
			{
sl@0
   317
			lTimer->iTimer.Cancel();	
sl@0
   318
			if(lTimer->iIsTimerReset)
sl@0
   319
				{
sl@0
   320
				lTimer->iTimer.Deque();
sl@0
   321
				lTimer->iIsArmed = EFalse;
sl@0
   322
				return 0;
sl@0
   323
				}
sl@0
   324
			}
sl@0
   325
		else
sl@0
   326
			lTimer->iTimer.ConstructL();
sl@0
   327
		
sl@0
   328
		lSetTime+=(TTimeIntervalSeconds) lTimer->iEndTime.tv_sec;
sl@0
   329
		lSetTime+=(TTimeIntervalMicroSeconds)(lTimer->iEndTime.tv_nsec/1000);
sl@0
   330
		
sl@0
   331
		lTimer->iTimer.AddToAS();
sl@0
   332
		lTimer->iTimer.At(lSetTime);	//Before requesting a timer event, add this active object to AS.					
sl@0
   333
		lTimer->iIsArmed = ETrue;										
sl@0
   334
		}		
sl@0
   335
	return KErrNone;
sl@0
   336
	}
sl@0
   337
sl@0
   338
void CLibRtTimer::AddToAS()
sl@0
   339
	{
sl@0
   340
	if(!IsAdded())
sl@0
   341
		CActiveScheduler::Add(this);
sl@0
   342
	else return;
sl@0
   343
	}
sl@0
   344
sl@0
   345
void CLibRtTimer::RunL()
sl@0
   346
	{
sl@0
   347
	CLocalSystemInterface* lClsiPtr = Backend();
sl@0
   348
		
sl@0
   349
		unsigned long lPeriodicTimeout = (1000000 * GetRtPtr()->iStartTime.it_interval.tv_sec) +
sl@0
   350
			  (GetRtPtr()->iStartTime.it_interval.tv_nsec/1000);
sl@0
   351
			  
sl@0
   352
		if(lPeriodicTimeout)
sl@0
   353
			{
sl@0
   354
			this->HighRes(lPeriodicTimeout);
sl@0
   355
			}
sl@0
   356
		else
sl@0
   357
			{
sl@0
   358
			GetRtPtr()->iIsArmed = EFalse;
sl@0
   359
			}						
sl@0
   360
		int val = GetRtPtr()->iSigEvent.sigev_notify;
sl@0
   361
		//run the handler here	
sl@0
   362
		switch(GetRtPtr()->iSigEvent.sigev_notify)
sl@0
   363
			{
sl@0
   364
			#if (defined SYMBIAN_OE_POSIX_SIGNALS && defined SYMBIAN_OE_LIBRT)
sl@0
   365
			case SIGEV_SIGNAL:
sl@0
   366
				{
sl@0
   367
				lClsiPtr->AddTimer(GetRtPtr()->iTimerId);	
sl@0
   368
				
sl@0
   369
				if(lClsiPtr->IncrementOverrun(GetRtPtr()->iTimerId) == 1)
sl@0
   370
					{
sl@0
   371
					lClsiPtr->RaiseTimerSignal(GetRtPtr()->iSigEvent.sigev_signo,\
sl@0
   372
							GetRtPtr()->iTimerId);
sl@0
   373
					}
sl@0
   374
				}
sl@0
   375
				break;
sl@0
   376
			#endif
sl@0
   377
			case SIGEV_THREAD:
sl@0
   378
				{
sl@0
   379
				pthread_t lthread = 0;
sl@0
   380
				pthread_create(	&lthread,
sl@0
   381
								(pthread_attr_t*)GetRtPtr()->iSigEvent.sigev_notify_attributes,
sl@0
   382
								sThreadFunc,
sl@0
   383
								&GetRtPtr()->iSigEvent );
sl@0
   384
				}
sl@0
   385
				break;
sl@0
   386
sl@0
   387
			case SIGEV_NONE:
sl@0
   388
			default:
sl@0
   389
				break;						
sl@0
   390
			}
sl@0
   391
	}
sl@0
   392
sl@0
   393
// --------------------------------------------------------------------------- 
sl@0
   394
// RMessage::Panic() also completes the message. This is:
sl@0
   395
// (a) important for efficient cleanup within the kernel
sl@0
   396
// (b) a problem if the message is completed a second time
sl@0
   397
// ---------------------------------------------------------------------------
sl@0
   398
void PanicClient(const RMessagePtr2& aMessage,TMyPanic aPanic)
sl@0
   399
	{
sl@0
   400
	_LIT(KPanic,"TimerServer");
sl@0
   401
	aMessage.Panic(KPanic,aPanic);
sl@0
   402
	}
sl@0
   403