serialserver/c32serialserver/SCOMM/CS_TIME.CPP
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 1997-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 #include <e32def.h>
       
    18 #include "CS_STD.H"
       
    19 #include "C32LOG.H"
       
    20 #include "cs_glob.h"
       
    21 /** @file
       
    22  *
       
    23  * implements the static utility class CommTimer
       
    24  */
       
    25 
       
    26 EXPORT_C void CommTimer::Queue(TTimeIntervalMicroSeconds32 aTimeInMicroSeconds, TDeltaTimerEntry& aHandle)
       
    27 /**
       
    28  Queue a timer on the global timer.
       
    29  
       
    30  This class is only suitable for CSYs since it currently has no direct mechanism through which to release
       
    31  the Thread Local Storage and heap resources it allocates.
       
    32  In the case of CSYs these resources are managed separately by C32.
       
    33  
       
    34  
       
    35  Note:
       
    36  
       
    37  1. The Thread Local Storage of the calling thread must be available for use.
       
    38  
       
    39  2. The first time this is called for a thread, a small amount of heap memory is required. If
       
    40     no memory is available, a "c32-fault" panic of type 7 is raised. Subsequent calls are not affected since
       
    41     they reuse this memory.
       
    42  
       
    43  @param aTimeInMicroSeconds the timeout value in micro seconds
       
    44  @param aHandle handle to the delta timer entry
       
    45  */
       
    46  // extra note: C32 does not leak this memory when a CSY uses the CommTimer since they will fetch the 
       
    47  // tls of the C32.DLL for their particular player thread. This same tls is then used by the Player
       
    48  // during shutdown to find and release the CommTimer that the CSYs in the Player have been using.
       
    49  // However, when a client calls this API, they will in affect be getting CommTimer to allocate
       
    50  // the CDeltaTimer and CTLSRedirector but the client has no means to interact with the unpublished
       
    51  // CTLSRedirector class in order to delete the CDeltaTimer class that it refers to.
       
    52 	{
       
    53 	C32_STATIC_LOG(KC32Detail,_L8("CommTimer::Queue()"));
       
    54 	aTimeInMicroSeconds = aTimeInMicroSeconds.Int() + (KCommTimerGranularity>>2);
       
    55 
       
    56 	if(aTimeInMicroSeconds.Int() < KDeltaTimerInterval)
       
    57 		{
       
    58 		aTimeInMicroSeconds = aTimeInMicroSeconds.Int() + KCommTimerGranularity;
       
    59 		}
       
    60 
       
    61 	CDeltaTimer* timer = GetTimer();
       
    62 	if (timer) // can't do much if the allocation of the CDeltaTimer failed.
       
    63 		{      // There is no return value
       
    64 		timer->Queue(aTimeInMicroSeconds, aHandle);
       
    65 		}
       
    66 	}
       
    67 
       
    68 
       
    69 EXPORT_C void CommTimer::Remove(TDeltaTimerEntry& aTimer)
       
    70 /**
       
    71  * Call cancel on the global timer
       
    72  *
       
    73  * Note: This does not free the Thread Local Storage for the calling thread.
       
    74  *
       
    75  * @param aTimer the timer to cancel
       
    76  */
       
    77 	{
       
    78 	C32_STATIC_LOG(KC32Detail,_L8("CommTimer::Remove()"));
       
    79 	CDeltaTimer* timer = GetTimer();
       
    80  	if (timer)
       
    81 		{
       
    82  		timer->Remove(aTimer);
       
    83 		}
       
    84 	}
       
    85 
       
    86 
       
    87 CDeltaTimer* CommTimer::GetTimer()
       
    88 /**
       
    89   Get the pointer to the global timer. If it does not
       
    90   exist, create a new timer and store the pointer in the TLS.
       
    91  
       
    92  Only clients within CSYs should use this API, since this function has a legacy behaviour
       
    93  of panicking in the case of no memory. This is due to the API not containing a means
       
    94   to communicate this result back to the caller.
       
    95  In the case where GetTimer is used by a CSY this is not a problem since the CSY will be running in a thread
       
    96  that C32 has already allocated the CommTimer memory for.
       
    97  
       
    98   @return pointer to the timer
       
    99  
       
   100   @note This function is using TLS (Thread Local Storage) and may
       
   101         reduce performance.
       
   102  */
       
   103 	{
       
   104 	// Previously it was planned to move away from using the TLS,
       
   105 	// but CommTimer is published now so no current means by which we can change this.
       
   106 	TAny* d = Dll::Tls();
       
   107 	CTLSRedirector* tls = NULL;
       
   108 	if (d == NULL)
       
   109 		{
       
   110 		C32_STATIC_LOG(KC32Detail,_L8("CommTimer::GetTimer() Client TLS is NULL, initializing"));
       
   111 		TRAPD(ret,tls = CTLSRedirector::NewL());
       
   112 		
       
   113 #ifdef	__FLOG_ACTIVE
       
   114 		if (ret != KErrNone)
       
   115 			{
       
   116 			C32_STATIC_LOG2(KC32Detail,_L8("CommTimer::GetTimer()-creation of redirector failed with %d. Will panic"),ret);
       
   117 			}
       
   118 #endif
       
   119 		__ASSERT_ALWAYS(ret==KErrNone, Fault(EDTimerAllocFailure));
       
   120 		Dll::SetTls(tls);
       
   121 		}
       
   122 	else
       
   123 		{
       
   124 		tls = static_cast<CTLSRedirector*>(d);
       
   125 		}
       
   126 		
       
   127 		
       
   128 	if (tls->DeltaTimer() == NULL)
       
   129 		{
       
   130 		C32_STATIC_LOG(KC32Detail,_L8("CommTimer::GetTimer() CDeltaTimer is NULL, initializing"));
       
   131 		
       
   132 		CDeltaTimer* timer = NULL;
       
   133 		TRAPD(ret, timer = CDeltaTimer::NewL(CActive::EPriorityHigh, KCommTimerGranularity));
       
   134 #ifdef	__FLOG_ACTIVE
       
   135 		if (ret != KErrNone)
       
   136 			{
       
   137 			C32_STATIC_LOG2(KC32Detail,_L8("CommTimer::GetTimer()-creation of CDeltaTimer failed with %d. Will panic"),ret);
       
   138 			}
       
   139 #endif
       
   140 		__ASSERT_ALWAYS(ret==KErrNone, Fault(EDTimerAllocFailure));
       
   141 		tls->SetDeltaTimer(timer); //transfer ownership
       
   142 		
       
   143 		}
       
   144 	else
       
   145 		{
       
   146 		C32_STATIC_LOG(KC32Detail,_L8("CommTimer::GetTimer() returning already initialized client TLS"));
       
   147 		}
       
   148 		
       
   149 	return tls->DeltaTimer();
       
   150 	}
       
   151 
       
   152 // EOF - CS_TIME.CPP