serialserver/c32serialserver/INC/cs_glob.h
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  @internalTechnology 
       
    19 */
       
    20 
       
    21 #ifndef CS_GLOB_H
       
    22 #define CS_GLOB_H
       
    23 
       
    24 #include "cs_thread.h"
       
    25 #include "CS_STD.H"
       
    26 #include <e32base.h>
       
    27 
       
    28 
       
    29 class C32GlobalUtil
       
    30 	{
       
    31 public:
       
    32 	static void InitC32GlobalsL(CC32WorkerThread* aWorker);
       
    33 	static void ShutDown(void);
       
    34 	static void NewSession();
       
    35 	static void SessionClosing();
       
    36 	};
       
    37 
       
    38 
       
    39 
       
    40 class CC32ShutdownWatchDog;
       
    41 
       
    42 
       
    43 class CC32Data : public CBase
       
    44 // owned by CTLSRedirector and meant to be referenced via TLS. Holds various pointers
       
    45 // to facilitate tricky scenarios such as shutdown and static logging with workerthread id
       
    46 	{
       
    47 public:
       
    48 	static CC32Data* NewL();
       
    49 	CC32WorkerThread* SelfWorker() const;
       
    50 
       
    51 public:		
       
    52 	CC32WorkerThread* iWorkerThread;           // pointer to the main class of the thread, not owned
       
    53 	CC32ShutdownWatchDog* iShutdownWatchDog;   // loosely owned by workerthread: normally watchdog deletes itself when it fires
       
    54 	TInt iNumSessions;						//< Keep a counter of the sessions
       
    55 private:
       
    56 	CC32Data();
       
    57 	};
       
    58 
       
    59 
       
    60 
       
    61 NONSHARABLE_CLASS(CTLSRedirector) : public CBase
       
    62 // stored at TLS pointer and handles the two separate situations where TLS is used in C32:
       
    63 // 1. inside C32, TLS is used for global data and CommTimer
       
    64 // 2. outside C32, clients may call CommTimer which uses client's TLS
       
    65 	{
       
    66 public:
       
    67 	static CTLSRedirector* NewL();
       
    68 	inline CDeltaTimer* DeltaTimer() const;
       
    69 	
       
    70 	
       
    71 	inline void SetDeltaTimer(CDeltaTimer* aTimer);
       
    72 
       
    73 		
       
    74 	inline CC32Data* C32GlobalData() const;
       
    75 	
       
    76 	inline void SetC32GlobalData(CC32Data* aC32Data);
       
    77 		
       
    78 	inline TBool IsC32GlobalDataSet() const;
       
    79 	
       
    80 	~CTLSRedirector();
       
    81 
       
    82 private:
       
    83 	CDeltaTimer* iTimer;	// The global delta timer for this thread. owned directly by this class
       
    84 	CC32Data*    iC32Data;	// always owned by CWorkerThread. Never created in client thread situation.
       
    85 	};
       
    86 
       
    87 
       
    88 
       
    89 
       
    90 NONSHARABLE_CLASS(CC32ShutdownWatchDog) : public CTimer
       
    91 /**
       
    92 @internalComponent
       
    93 */
       
    94 // There is some unclarity with regards to this watchdog, since it was copied from Esock along with most
       
    95 // of the shutdown ideas. It appears it allows 
       
    96 // the workerthread to delay the destruction of data structures until both the dealer and player classes
       
    97 // have had a chance to shutdown - without which the destruction of the transport would panic due
       
    98 // to commsfw seeing open channels.
       
    99 // In practice the watchdog is often triggered for each thread when they run the shutdown for the
       
   100 // dealer of the main thread (done by whoever gets there first). The watchdog allows the thread to yield
       
   101 // and then it runs the player class shutdown, after which the watchdog runs.
       
   102 // It is not known why a timer has been used rather than simply commence the data destruction after both
       
   103 // dealer and player classes have shut down.
       
   104 // The watchdog does not seem to be used to mitigate any inter-thread races.
       
   105 //
       
   106 	{
       
   107 public:
       
   108 	void Shutdown();
       
   109 	static CC32ShutdownWatchDog* NewL();
       
   110 #ifdef _DEBUG
       
   111 	void SetWorkerId(TUint16 aId);
       
   112 #endif
       
   113 private:
       
   114 	CC32ShutdownWatchDog();
       
   115 	void RunL();
       
   116 private:
       
   117 #ifdef _DEBUG
       
   118 	TInt iWorkerId;
       
   119 #endif
       
   120 	};
       
   121 	
       
   122 	
       
   123 	
       
   124 inline CDeltaTimer* CTLSRedirector::DeltaTimer() const
       
   125 	{
       
   126 	// non=-null not guaranteed, but this function is hardly used
       
   127 	return iTimer;
       
   128 	}
       
   129 
       
   130 
       
   131 inline void CTLSRedirector::SetDeltaTimer(CDeltaTimer* aTimer)
       
   132 	{
       
   133 	iTimer = aTimer;
       
   134 	}
       
   135 
       
   136 	
       
   137 inline CC32Data* CTLSRedirector::C32GlobalData() const
       
   138 	{
       
   139 	__ASSERT_DEBUG(iC32Data!=NULL,Fault(EBadState,_L8("CTLSRedirector: access of null C32GlobalData attempted")));
       
   140 	return iC32Data;
       
   141 	}
       
   142 inline void CTLSRedirector::SetC32GlobalData(CC32Data* aC32Data)
       
   143 	{
       
   144 	__ASSERT_DEBUG(iC32Data==NULL,Fault(EBadState,_L8("CTLSRedirector: attempt to set already-set GlobalData")));
       
   145 	iC32Data=aC32Data;
       
   146 	}
       
   147 	
       
   148 inline TBool CTLSRedirector::IsC32GlobalDataSet() const
       
   149 	{
       
   150 	return iC32Data!=NULL;
       
   151 	}
       
   152 
       
   153 
       
   154 inline CTLSRedirector* CRedirectorInTls()
       
   155 // provide a convenient way to access the tls redirector.
       
   156 	{
       
   157 	return static_cast<CTLSRedirector *>(Dll::Tls());
       
   158 	}
       
   159 
       
   160 inline CC32Data* CC32DataInTls()
       
   161 // provide a convenient way to access the global data.
       
   162 // Panics if the global data is not set
       
   163 	{
       
   164 	return CRedirectorInTls()->C32GlobalData();
       
   165 	}	
       
   166 
       
   167 
       
   168 #endif // CS_GLOB_H
       
   169 
       
   170 
       
   171 
       
   172 
       
   173 
       
   174