serialserver/c32serialserver/INC/cs_thread.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 // defines the workerThread class and assoicates. These operate at a level below the roles.h
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalTechnology
       
    21 */
       
    22 
       
    23 #ifndef CS_THREAD_H
       
    24 #define CS_THREAD_H
       
    25 
       
    26 #include <cfutil.h>
       
    27 #include <cfextras.h>
       
    28 #include <elements/cftransport.h>
       
    29 #include <cs_panic.h>
       
    30 #include "cs_common.h"
       
    31 
       
    32 
       
    33 class CCommChannelHandler;
       
    34 
       
    35 typedef CommsFW::CWorkerThreadRegister<TC32WorkerThreadRegister, KMaxWorkerThreadId> CC32WorkerRegister;
       
    36 
       
    37 
       
    38 /**
       
    39 @class
       
    40 CC32WorkerThread
       
    41 The Worker Thread is the holder of the relevant C32 objects in the CPM instance,
       
    42 e.g. the Dealer and the Player (depending on configuration). It is also
       
    43 in charge of inter-thread communication as it holds the object enabling communication
       
    44 with the RootServer (CCommChannelHandler) and a list of objects for
       
    45 communication with other Worker threads (CCommsTransport).
       
    46 
       
    47 @see CCommChannelHandler
       
    48 @see CCommsTransport
       
    49 */
       
    50 NONSHARABLE_CLASS(CC32WorkerThread) : public CBase, public CommsFW::MLegacyMessageReceiver
       
    51 	{
       
    52 public:
       
    53 	static CC32WorkerThread* NewL(CommsFW::TCFModuleInfo* aModuleInfo);
       
    54 	~CC32WorkerThread();
       
    55 
       
    56 	inline CC32Dealer* Dealer() const;
       
    57 	inline CC32Player* Player() const;
       
    58 	inline CommsFW::TWorkerId WorkerId() const;
       
    59 	inline CC32Dealer& DealerByRef() const;
       
    60 
       
    61 	/**
       
    62 	Use this to discover whether this Worker Thread is EMainThread ("C32_Main") which is
       
    63 	the main Dealer.
       
    64 	@see TWorkerThreadInfo
       
    65 	*/
       
    66 	TBool IsMainThread() const
       
    67 	    {
       
    68 	    return WorkerId()==TC32WorkerThreadRegister::EMainThread;
       
    69 	    }
       
    70 
       
    71 	/**
       
    72 	This is the main thread function used by the RootServer when creating a new C32 thread.
       
    73 	The RootServer will know the DLL ordinal for it, as specified in the .CMI file, and this is where
       
    74 	an C32 instance starts and ends operation (unless it PANICs).
       
    75 	@param aArg Will be the module info structure from the RootServer.
       
    76 	@see CommsFW::TCFModuleInfo
       
    77 	*/
       
    78 	IMPORT_C static TInt ThreadEntryPoint(TAny* aArg);
       
    79 	IMPORT_C static TInt RunC32Thread(CommsFW::TCFModuleInfo* aModuleInfo);
       
    80 
       
    81 	void SetDealerShutdownComplete(TBool aComplete);
       
    82 	inline TBool DealerShutdownComplete() const;
       
    83 	void SetPlayerShutdownComplete(TBool aComplete);
       
    84 	inline TBool PlayerShutdownComplete() const;
       
    85 
       
    86 	TBool ShuttingDown() const;
       
    87 	void SetShuttingDown();
       
    88 	void SessionShutdownComplete();
       
    89 	void MaybeTriggerThreadShutdownCallback();
       
    90 	void TriggerThreadShutdownCallback();
       
    91 
       
    92 	// RS control binding processing & message forwarding
       
    93 	void CFBindMessageReceived(const CommsFW::TCFBindMsg& aMsg);
       
    94 	void CFUnbindMessageReceived(const CommsFW::TCFUnbindMsg& aMsg);
       
    95 	void CFShutdownMessageReceived(const CommsFW::TCFShutdownMsg& aMsg);
       
    96 
       
    97 
       
    98 	// Test whether a message
       
    99 	TBool PeerReachable(CommsFW::TWorkerId aPeerId) const
       
   100 		{
       
   101 		return iTransport->PeerReachable(aPeerId);
       
   102 		}
       
   103 	// Send a message to a peer
       
   104     void PostMessage(CommsFW::TWorkerId aWorkerId, CommsFW::TCFMessage& aMessage);
       
   105 
       
   106 	// From MLegacyMessageReceiver
       
   107 	void DispatchL(const CommsFW::TCFMessage& aMessage, CommsFW::TWorkerId aSenderId);
       
   108 	void OnDispatchLeave(const CommsFW::TCFMessage& aMessage, CommsFW::TWorkerId aSenderId, TInt aFirstDispatchLeaveReason);
       
   109 
       
   110 #ifdef _DEBUG
       
   111   	inline RAllocator::TAllocFail AllocFailType() const;
       
   112   	inline TInt AllocFailRate() const;
       
   113 #endif
       
   114 
       
   115 protected:
       
   116 	void ConstructL(CommsFW::TCFModuleInfo* aModuleInfo);
       
   117 	CC32WorkerThread();
       
   118 private:
       
   119 	void DetermineRoleL(const TDesC8& aIniData, TBool& aIsDealer, TBool& aIsPlayer);
       
   120 	void ProcessIniDataL();
       
   121 	TInt DecodePeerId(const CommsFW::TCFSubModuleAddress* aSubModule1, const CommsFW::TCFSubModuleAddress* aSubModule2, CommsFW::TWorkerId& aPeerId);
       
   122 	void MaybeCompleteUnbindings();
       
   123 	void MaybeCompleteUnbinding(CommsFW::TWorkerId aWorker);
       
   124 	static void DeleteHBufC8(TAny* aHBufC);
       
   125 
       
   126 	/**
       
   127 	@see ::iProlongBindingLife
       
   128 	*/
       
   129 	void IncProlongBindingLife()
       
   130 		{
       
   131 		++iProlongBindingLife;
       
   132 		}
       
   133 
       
   134 	/**
       
   135 	@see ::iProlongBindingLife
       
   136 	*/
       
   137 	void DecProlongBindingLife()
       
   138 		{
       
   139 		--iProlongBindingLife;
       
   140 		}
       
   141 
       
   142 private:
       
   143 
       
   144 	/** This is the handler used for bi-directional communication with the Root Server. */
       
   145 	CCommChannelHandler* iChannelHandler;
       
   146 
       
   147 	CC32WorkerRegister* iWorkerRegister;    // allows transport to find other threads
       
   148 	CommsFW::CCommsTransport* iTransport;
       
   149 	/**
       
   150 	Indentification of this thread. No other instance must have the same id.
       
   151 	@see TWorkerThreadInfo
       
   152 	*/
       
   153 	CommsFW::TWorkerId iWorkerId;
       
   154 
       
   155 	CC32Dealer* iDealer;
       
   156 
       
   157 	CC32Player* iPlayer;
       
   158 
       
   159 	/** Set when a CommsFW::TCFShutdownMsg is received from the Root Server. */
       
   160 	TBool iWorkerShuttingDown;
       
   161 
       
   162 	/**
       
   163 	Set by the Dealer instance to signal to the Worker Thread that it has
       
   164 	finished with the shutdown bookkeeping and is ready to be deleted.
       
   165 	This essentially means no sessions remain.
       
   166 	*/
       
   167 	TBool iDealerShutdownComplete;
       
   168 
       
   169 	/**
       
   170 	Set by the Player instance to signal to the Worker Thread that it has
       
   171 	finished with the shutdown bookkeeping and is ready to be deleted.
       
   172 	*/
       
   173 	TBool iPlayerShutdownComplete;
       
   174 
       
   175 	/**
       
   176 	If the value of this TInt is larger than 0 any unbind requests will not be served,
       
   177 	but postponed until iProlongBindingLife is 0. This is to ensure that if e.g. a
       
   178 	TWorkerMsg::ECleanupDeadPeer or TPlayerMsg::ESessionClose is received they will be fully
       
   179 	served before completing any unbind and thus deleting channel handlers.
       
   180 	*/
       
   181 	TInt iProlongBindingLife;
       
   182 
       
   183 #ifdef _DEBUG
       
   184   	RAllocator::TAllocFail iFailType;
       
   185   	TInt iFailRate;
       
   186 #endif
       
   187 
       
   188 	};
       
   189 
       
   190 
       
   191 
       
   192 NONSHARABLE_CLASS (CCommChannelHandler) : public CommsFW::CCFModuleChannelHandler
       
   193 /** Main Comms Channel traffic handler for the serialserver.
       
   194 The adapter responsible for communicating with the Root Server over the channel given with the
       
   195 CommsFW::TCFModuleInfo structure delivered via the parameter to the main thread function.
       
   196 @internalComponent
       
   197 */
       
   198 	{
       
   199 	typedef CommsFW::CCFModuleChannelHandler inherited;
       
   200 public:
       
   201 	static CCommChannelHandler* NewL(CommsFW::RCFChannel::TMsgQueues aRxQueues,
       
   202 									 CommsFW::RCFChannel::TMsgQueues aTxQueues, CC32WorkerThread* aWorkerThread);
       
   203 	TInt Send(const CommsFW::TCFMessage& aMessage);
       
   204 private:
       
   205 	CCommChannelHandler(CC32WorkerThread* aWorkerThread);
       
   206 	virtual void CFMessageShutdown(const CommsFW::TCFShutdownMsg& aMessage);
       
   207 	virtual void CFMessageDiscover(const CommsFW::TCFDiscoverMsg& aMessage);
       
   208 	virtual void CFMessageBind(const CommsFW::TCFBindMsg& aMessage);
       
   209 	virtual void CFMessageUnbind(const CommsFW::TCFUnbindMsg& aMessage);
       
   210 private:
       
   211 	/** Pointer back to the worker thread owning this instance. */
       
   212 	CC32WorkerThread* iWorkerThread;
       
   213 
       
   214 	};
       
   215 
       
   216 #include "cs_thread.inl"
       
   217 
       
   218 #endif // CS_THREAD_H
       
   219 
       
   220