serialserver/c32serialserver/SCOMM/cs_glob.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     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 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include "cs_glob.h"
       
    22 #include <e32std.h>
       
    23 #include "C32LOG.H"
       
    24 #include "cs_roles.h"
       
    25 #include "cs_thread.h"
       
    26 
       
    27 
       
    28 CC32Data* CC32Data::NewL()
       
    29 		{
       
    30 		return new(ELeave) CC32Data();
       
    31 		}
       
    32 
       
    33 
       
    34 	
       
    35 CC32Data::CC32Data()
       
    36 	{
       
    37 	}
       
    38 
       
    39 
       
    40 CC32WorkerThread* CC32Data::SelfWorker() const
       
    41 	{ 
       
    42 	return iWorkerThread;
       
    43 	}
       
    44 
       
    45 void C32GlobalUtil::InitC32GlobalsL(CC32WorkerThread* aWorker)
       
    46 	{
       
    47 	CTLSRedirector* tls = CRedirectorInTls();
       
    48 	if(tls)
       
    49 		{
       
    50 		Fault(EInitTwice);
       
    51 		}
       
    52 
       
    53 	// Allocate global storage to be held in tls.
       
    54 	// only transfer ownership to tls at end since if we fail, the thread is never started
       
    55 	CC32Data *globals=CC32Data::NewL();
       
    56 
       
    57 	CleanupStack::PushL(globals);
       
    58 
       
    59 	// Pre allocate the shutdown timer so that we can always shut down.
       
    60 	globals->iShutdownWatchDog=CC32ShutdownWatchDog::NewL();
       
    61 #ifdef _DEBUG
       
    62 	globals->iShutdownWatchDog->SetWorkerId(aWorker->WorkerId());
       
    63 #endif
       
    64 	CleanupStack::PushL(globals->iShutdownWatchDog);
       
    65 
       
    66 	globals->iWorkerThread = aWorker;
       
    67 	
       
    68 	tls = CTLSRedirector::NewL();
       
    69 	
       
    70 	CleanupStack::PushL(tls);
       
    71 	
       
    72 	CDeltaTimer* timer = CDeltaTimer::NewL(CActive::EPriorityHigh, KCommTimerGranularity);
       
    73 
       
    74 	tls->SetDeltaTimer(timer); // ownership	transferred
       
    75 		
       
    76 	tls->SetC32GlobalData(globals); //ownership transferred
       
    77 
       
    78 	
       
    79 	Dll::SetTls(tls);  // owned now indirectly by the worker thread which must now remove it when shutting down
       
    80 
       
    81 	CleanupStack::Pop(3); //watchdog, globals, tls
       
    82 
       
    83 	C32LOG1(KC32Bootup, _L("C32GlobalUtil::InitL() Done!"));
       
    84 	}
       
    85 
       
    86 void C32GlobalUtil::ShutDown()
       
    87 /**
       
    88 free data structures
       
    89 */
       
    90 	{
       
    91 	// Mark ourself as on the way out
       
    92 	CC32Data* globals = CC32DataInTls();
       
    93 	CTLSRedirector* tls = CRedirectorInTls();
       
    94 	// The CC32Data destructor does not own so does not delete the data it points
       
    95 	// This is since don't want to delete the Watchdog - it is left to delete itself.
       
    96 	// So we delete everything else in CC32Data explicitly.
       
    97 
       
    98 	C32LOG2(KC32Shutdown, _L("--------------- C32Server W%d starting shutdown ---------------"), globals->iWorkerThread->WorkerId());
       
    99 
       
   100 	delete globals->iWorkerThread;
       
   101 	delete tls;
       
   102 	Dll::SetTls(NULL); // so that logger sees it is gone.
       
   103 	}
       
   104 
       
   105 void C32GlobalUtil::NewSession()
       
   106 /**
       
   107 Increment the session counter - if we were about to shutdown, stop.
       
   108 */
       
   109 	{
       
   110 	CC32Data* globals = CC32DataInTls();
       
   111 	globals->iNumSessions++;
       
   112 	C32LOG2(KC32Detail, _L8("C32GlobalUtil::NewSession() iNumSessions= %d "), globals->iNumSessions );
       
   113 	}
       
   114 
       
   115 void C32GlobalUtil::SessionClosing()
       
   116 /**
       
   117 A session is closing, if it's the last one take our stumps home.
       
   118 */
       
   119 	{
       
   120 	CC32Data* globals = CC32DataInTls();
       
   121 	globals->iNumSessions--;
       
   122 	C32LOG2(KC32Detail, _L8("C32GlobalUtil::SessionClosing() iNumSessions= %d "), globals->iNumSessions );
       
   123 
       
   124 	CC32WorkerThread* selfWorker = globals->SelfWorker();
       
   125 	if (selfWorker->ShuttingDown())
       
   126 		{
       
   127 		C32LOG2(KC32Shutdown, _L8("Shutdown requested: %d sessions"), globals->iNumSessions );
       
   128 		if (globals->iNumSessions <= 0)
       
   129 			{
       
   130 			if(selfWorker->IsMainThread())
       
   131 				{
       
   132 				selfWorker->DealerByRef().SessionShutdownComplete();
       
   133 				}
       
   134 			else
       
   135 				{
       
   136 				selfWorker->MaybeTriggerThreadShutdownCallback();
       
   137 				}
       
   138 			}
       
   139 		}
       
   140 	}
       
   141 	
       
   142 	
       
   143 	
       
   144 CTLSRedirector* CTLSRedirector::NewL()
       
   145 		{
       
   146 		return new(ELeave) CTLSRedirector();
       
   147 		}
       
   148 
       
   149 
       
   150 CTLSRedirector::~CTLSRedirector()
       
   151 	{
       
   152 	delete iTimer;
       
   153 	delete iC32Data;
       
   154 	}
       
   155 
       
   156 
       
   157 CC32ShutdownWatchDog::CC32ShutdownWatchDog()
       
   158 	:CTimer(EPriorityLow)
       
   159 /**
       
   160 Constructor
       
   161 */
       
   162 	{
       
   163 	}
       
   164 
       
   165 void CC32ShutdownWatchDog::Shutdown()
       
   166 /**
       
   167 Que ourselves to shut the C32 server down after a suitable delay.
       
   168 */
       
   169 	{
       
   170 	C32LOG1(KC32Shutdown, _L("CC32ShutdownWatchDog::Shutdown()"));
       
   171 
       
   172 	if (!IsAdded())
       
   173 		{
       
   174 		CActiveScheduler::Add(this);
       
   175 		}
       
   176 
       
   177 	if(!IsActive())
       
   178 		{
       
   179 		After(KCommServerShutdownLatencyMicroSeconds);
       
   180 		}
       
   181 	}
       
   182 
       
   183 void CC32ShutdownWatchDog::RunL()
       
   184 	{
       
   185 	C32LOG1(KC32Shutdown, _L("CC32ShutdownWatchDog::RunL()"));	
       
   186 	ASSERT(CC32DataInTls()->iNumSessions == 0);
       
   187 	// End of the line
       
   188 	C32GlobalUtil::ShutDown();
       
   189 	CActiveScheduler::Stop();
       
   190 	// tls is now deleted, so we mimmick our workerId description on the log line:
       
   191     C32LOG2(KC32Shutdown, _L("W%d: CC32ShutdownWatchDog::RunL() - about to delete self, which is last step of shutdown"),iWorkerId);
       
   192 	delete this;
       
   193 	}
       
   194 
       
   195 CC32ShutdownWatchDog* CC32ShutdownWatchDog::NewL()
       
   196 	{
       
   197 	CC32ShutdownWatchDog* t=new (ELeave) CC32ShutdownWatchDog();
       
   198 	CleanupStack::PushL(t);
       
   199 	t->ConstructL();
       
   200 	CleanupStack::Pop(t);
       
   201 	return t;
       
   202 	}
       
   203 
       
   204 #ifdef _DEBUG
       
   205 void CC32ShutdownWatchDog::SetWorkerId(TWorkerId aWorkerId)
       
   206 	{
       
   207 	iWorkerId = aWorkerId;
       
   208 	}
       
   209 #endif
       
   210 
       
   211 
       
   212 // EOF - CS_GLOB.CPP
       
   213 
       
   214