connectivitylayer/isce/isirouter_dll/src/isithreadcontainer.cpp
changeset 0 63b37f68c1ce
child 9 8486d82aef45
equal deleted inserted replaced
-1:000000000000 0:63b37f68c1ce
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <kernel.h>               // For Kern
       
    21 #include "isiroutertrace.h"       // For C_TRACE, ASSERT_RESET.. and fault codes
       
    22 #include "isiinternaldefs.h"      // For KAmountOfKernelThreads...
       
    23 #include "isithreadcontainer.h"   // For DISIThreadContainer
       
    24 
       
    25 // Faults
       
    26 enum TISIThreadContainerFaults
       
    27     {
       
    28     EISIThreadContainerDfcQAllocFailure = 0x01,
       
    29     EISIThreadContainerDfcQAllocFailure1,
       
    30     EISIThreadContainerUnknownThreadType,
       
    31     EISIThreadContainerNULLThreadPtr,
       
    32     EISIThreadContainerThreadNotFound,
       
    33     EISIThreadContainerOverflow,
       
    34     EISIThreadContainerOverflow1,
       
    35     EISIThreadContainerUnderflow,
       
    36     EISIThreadContainerUnderflow1,
       
    37     };
       
    38 
       
    39 
       
    40 _LIT8( KISIKernelClientThread, "ISIKernelClientThread" );
       
    41 _LIT8( KISIUserClientThread, "ISIUserClientThread" );
       
    42 
       
    43 const TInt KDefaultDfcThreadPriority( 27 );
       
    44 const TUint8 KMaxThreadUsers( 255 );
       
    45 const TUint8 KMaxThreadNameLength( 25 );
       
    46 
       
    47 DISIThreadContainer::DISIThreadContainer()
       
    48     {
       
    49     C_TRACE( ( _T( "DISIThreadContainer::DISIThreadContainer>" ) ) );
       
    50     TUint8 nameIndex = 0;
       
    51     for( TUint8 i( 0 ); i < KAmountOfKernelThreads; i++ )
       
    52         {
       
    53         iNameTable[ nameIndex ] = HBuf8::New( KMaxThreadNameLength );
       
    54         iNameTable[ nameIndex ]->Append( (&KISIKernelClientThread)->Ptr(), (&KISIKernelClientThread)->Length() );
       
    55         iNameTable[ nameIndex ]->AppendNum( i, EDecimal );
       
    56         Kern::DfcQCreate( iKClientDfcQueList[ i ], KDefaultDfcThreadPriority, iNameTable[ nameIndex ] );
       
    57         ASSERT_RESET_ALWAYS( iKClientDfcQueList[ i ], ( EISIThreadContainerDfcQAllocFailure | EDISIThreadContainerTraceId << KClassIdentifierShift ) );
       
    58         nameIndex++;
       
    59         }
       
    60     for( TUint8 i( 0 ); i < KAmountOfUserThreads; i++ )
       
    61         {
       
    62         iNameTable[ nameIndex ] = HBuf8::New( KMaxThreadNameLength );
       
    63         iNameTable[ nameIndex ]->Append( (&KISIUserClientThread)->Ptr(), (&KISIUserClientThread)->Length() );
       
    64         iNameTable[ nameIndex ]->AppendNum( i, EDecimal );
       
    65         Kern::DfcQCreate( iUClientDfcQueList[ i ], KDefaultDfcThreadPriority, iNameTable[ nameIndex ] );
       
    66         ASSERT_RESET_ALWAYS( iUClientDfcQueList[ i ], ( EISIThreadContainerDfcQAllocFailure1 | EDISIThreadContainerTraceId << KClassIdentifierShift ) );
       
    67         nameIndex++;
       
    68         }
       
    69     C_TRACE( ( _T( "DISIThreadContainer::DISIThreadContainer<" ) ) );
       
    70     }
       
    71 
       
    72 DISIThreadContainer::~DISIThreadContainer()
       
    73     {
       
    74     C_TRACE( ( _T( "DISIThreadContainer::~DISIThreadContainer>" ) ) );
       
    75     for( TUint8 i( 0 ); i < ( KAmountOfKernelThreads + KAmountOfUserThreads ); i++ )
       
    76         {
       
    77         if( iNameTable[ i ] )
       
    78             {
       
    79             delete iNameTable[ i ];
       
    80             iNameTable[ i ] = NULL;
       
    81             }
       
    82         }
       
    83     for( TUint8 i( 0 ); i < KAmountOfKernelThreads; i++ )
       
    84         {
       
    85         if( iKClientDfcQueList[ i ] )
       
    86             {
       
    87             delete iKClientDfcQueList[ i ];
       
    88             iKClientDfcQueList[ i ] = NULL;
       
    89             }
       
    90         }
       
    91     for( TUint8 i( 0 ); i < KAmountOfUserThreads; i++ )
       
    92         {
       
    93         if( iUClientDfcQueList[ i ] )
       
    94             {
       
    95             delete iUClientDfcQueList[ i ];
       
    96             iUClientDfcQueList[ i ] = NULL;
       
    97             }
       
    98         }
       
    99     C_TRACE( ( _T( "DISIThreadContainer::~DISIThreadContainer<" ) ) );
       
   100     }
       
   101 
       
   102 TDfcQue* DISIThreadContainer::AllocateThread( const MISIChannelRouterIf::TISIDfcQThreadType aType )
       
   103     {
       
   104     C_TRACE( ( _T( "DISIThreadContainer::AllocateThread %d>" ), aType ) );
       
   105     TDfcQue* tmpPtr( NULL );
       
   106     switch( aType )
       
   107         {
       
   108         case MISIChannelRouterIf::EISIKernelMainThread:  
       
   109         case MISIChannelRouterIf::EISIKernelRequestCompletionThread:
       
   110             {
       
   111             tmpPtr = ReserveKernelThread();
       
   112             break;
       
   113             }
       
   114         case MISIChannelRouterIf::EISIUserMainThread:
       
   115         case MISIChannelRouterIf::EISIUserRequestCompletionThread:
       
   116             {
       
   117             tmpPtr = ReserveUserThread();
       
   118             break;
       
   119             }
       
   120         default:
       
   121             {
       
   122             ASSERT_RESET_ALWAYS( 0, ( EISIThreadContainerUnknownThreadType | EDISIThreadContainerTraceId << KClassIdentifierShift ) );
       
   123             break;
       
   124             }
       
   125         }
       
   126     ASSERT_RESET_ALWAYS( tmpPtr, ( EISIThreadContainerNULLThreadPtr | EDISIThreadContainerTraceId << KClassIdentifierShift ) );
       
   127     C_TRACE( ( _T( "DISIThreadContainer::AllocateThread %d 0x%x<" ), aType, tmpPtr ) );
       
   128     return tmpPtr;
       
   129     }
       
   130 
       
   131 TDfcQue* DISIThreadContainer::ReserveKernelThread()
       
   132     {
       
   133     C_TRACE( ( _T( "DISIThreadContainer::ReservekernelThread>" ) ) );
       
   134     TUint8 clientCount = iKThreadOccupation[ 0 ];
       
   135     TUint8 index = 0;
       
   136     for( TUint8 i( 0 ) ; i < KAmountOfKernelThreads; i++ )
       
   137         {
       
   138         if( clientCount > iKThreadOccupation[ i ] )
       
   139             {
       
   140             clientCount = iKThreadOccupation[ i ];
       
   141             index = i;
       
   142             }
       
   143         }
       
   144     ++iKThreadOccupation[ index ];
       
   145     ASSERT_RESET_ALWAYS( iKThreadOccupation[ index ] <= KMaxThreadUsers, ( EISIThreadContainerOverflow | EDISIThreadContainerTraceId << KClassIdentifierShift ) );
       
   146     C_TRACE( ( _T( "DISIThreadContainer::ReserveKernelThread 0x%x %d %d<" ), iKClientDfcQueList[ index ], iKThreadOccupation[ index ], index ) );
       
   147     return iKClientDfcQueList[ index ];
       
   148     }
       
   149 
       
   150 TDfcQue* DISIThreadContainer::ReserveUserThread()
       
   151     {
       
   152     C_TRACE( ( _T( "DISIThreadContainer::ReserveUserThread>" ) ) );
       
   153     TUint8 clientCount = iUThreadOccupation[ 0 ];
       
   154     TUint8 index = 0;
       
   155     for( TUint8 i( 0 ); i < KAmountOfUserThreads; i++ )
       
   156         {
       
   157         if( clientCount > iUThreadOccupation[ i ] )
       
   158             {
       
   159             clientCount = iUThreadOccupation[ i ];
       
   160             index = i;
       
   161             }
       
   162         }
       
   163     ++iUThreadOccupation[ index ];
       
   164     ASSERT_RESET_ALWAYS( iUThreadOccupation[ index ] <= KMaxThreadUsers, ( EISIThreadContainerOverflow1 | EDISIThreadContainerTraceId << KClassIdentifierShift ) );
       
   165     C_TRACE( ( _T( "DISIThreadContainer::ReserveUserThread 0x%x %d %d<" ), iUClientDfcQueList[ index ], iUThreadOccupation[ index ], index ) );
       
   166     return iUClientDfcQueList[ index ];
       
   167     }
       
   168 
       
   169 void DISIThreadContainer::DeallocateThread( TDfcQue* aThread )
       
   170     {
       
   171     C_TRACE( ( _T( "DISIThreadContainer::DeallocateThread 0x%x>" ), aThread ) );
       
   172     TBool found( EFalse );
       
   173     for( TUint8 i( 0 ) ; i < KAmountOfKernelThreads; i++ )
       
   174         {
       
   175         if( aThread == iKClientDfcQueList[ i ] )
       
   176             {
       
   177             --iKThreadOccupation[ i ];
       
   178             ASSERT_RESET_ALWAYS( iKThreadOccupation[ i ] != KMaxThreadUsers, ( EISIThreadContainerUnderflow | EDISIThreadContainerTraceId << KClassIdentifierShift ) );
       
   179             C_TRACE( ( _T( "DISIThreadContainer DeallocKThread 0x%x %d %d>" ), aThread, iKThreadOccupation[ i ], i ) );
       
   180             found = ETrue;
       
   181             break;
       
   182             }
       
   183         }
       
   184     if( !found )
       
   185         {
       
   186         for( TUint8 i( 0 ) ; i < KAmountOfUserThreads; i++ )
       
   187             {
       
   188             if( aThread == iUClientDfcQueList[ i ] )
       
   189                 {
       
   190                 --iUThreadOccupation[ i ];
       
   191                 ASSERT_RESET_ALWAYS( iUThreadOccupation[ i ] != KMaxThreadUsers, ( EISIThreadContainerUnderflow1 | EDISIThreadContainerTraceId << KClassIdentifierShift ) );
       
   192                 C_TRACE( ( _T( "DISIThreadContainer DeallocUThread 0x%x %d %d>" ), aThread, iUThreadOccupation[ i ], i ) );
       
   193                 found = ETrue;
       
   194                 break;
       
   195                 }
       
   196             }
       
   197         }
       
   198     ASSERT_RESET_ALWAYS( found, ( EISIThreadContainerThreadNotFound | EDISIThreadContainerTraceId << KClassIdentifierShift ) );
       
   199     C_TRACE( ( _T( "DISIThreadContainer::DeallocateThread 0x%x<" ), aThread ) );
       
   200     }
       
   201