srsf/vcommandhandler/src/asyncworker.cpp
branchRCL_3
changeset 19 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
18:cad71a31b7fc 19:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2006 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 "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:  Class that is able to pause the execution until the 
       
    15 *                asynchronous call resumes it. Knows how to leave the
       
    16 *                resumed function with the error code supplied by the
       
    17 *                resuming asynchronous call
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 #include "rubydebug.h"
       
    23 #include "asyncworker.h"
       
    24 
       
    25 // async worker panic code
       
    26 _LIT( KAsyncWorkerPanic, "AWRK" );
       
    27 
       
    28 CAsyncWorker* CAsyncWorker::NewLC()
       
    29 	{
       
    30 	CAsyncWorker* self = new (ELeave) CAsyncWorker;
       
    31 	CleanupStack::PushL( self );
       
    32 	return self;
       
    33 	}
       
    34 	
       
    35 CAsyncWorker* CAsyncWorker::NewL()
       
    36 	{
       
    37 	CAsyncWorker* self = CAsyncWorker::NewLC();
       
    38 	CleanupStack::Pop( self );
       
    39 	return self;
       
    40 	}
       
    41 	
       
    42 CAsyncWorker::~CAsyncWorker()
       
    43 	{
       
    44 	delete iWaiter;  // if any
       
    45 	}
       
    46 
       
    47 /**
       
    48 * Pauses the execution until StopAsyncWaitingL is called
       
    49 * Leaves if StopAsyncWaitingL is called with the negative error code
       
    50 * @leave KErrNotReady if another waiting is still in progress
       
    51 * @leave Leaves with the error code passed to StopAsyncWaitingL
       
    52 */
       
    53 void CAsyncWorker::WaitForAsyncCallbackL()
       
    54 	{
       
    55 	/** @todo Change to RUBY_ASSERT_ALWAYS */
       
    56 	RUBY_DEBUG_BLOCKL( "CAsyncWorker::WaitForAsyncCallbackL" );
       
    57 	if( iWaiter )
       
    58 		{
       
    59 		RUBY_ERROR0( "CAsyncWorker::WaitForAsyncCallbackL Already waiting" );
       
    60 		}
       
    61 	
       
    62 	__ASSERT_ALWAYS( !iWaiter, User::Leave( KErrNotReady ) );
       
    63 	iErr = KErrNone;
       
    64     iWaiter = new (ELeave) CActiveSchedulerWait;
       
    65     iWaiter->Start();
       
    66     delete iWaiter;
       
    67     iWaiter = NULL;
       
    68     User::LeaveIfError( iErr );
       
    69 	}
       
    70 
       
    71 /** 
       
    72 * Resumes the process paused by WaitForAsyncCallbackL
       
    73 * @param aErr Result code to pass to the 
       
    74 *        WaitForAsyncCallbackL error handling. Note, that 
       
    75 *        WaitForAsyncCallbackL leaves only if aErr is negative
       
    76 * @return KErrNotReady if no waiting is in progress. Otherwise KErrNone
       
    77 */
       
    78 TInt CAsyncWorker::StopAsyncWaiting( TInt aErr )
       
    79 	{
       
    80     RUBY_DEBUG1( "CAsyncWorker::StopAsyncWaitingL aErr [%d] ", aErr ); 
       
    81     iErr = aErr;   
       
    82     /** @todo Change to RUBY_ASSERT_ALWAYS */
       
    83 	if( !iWaiter )
       
    84 		{
       
    85 		RUBY_ERROR0( "CAsyncWorker::WaitForAsyncCallbackL Not waiting" );
       
    86 		}
       
    87 	if( !iWaiter)
       
    88 		{
       
    89 		return KErrNotReady;
       
    90 		}
       
    91 	
       
    92     iWaiter->AsyncStop();
       
    93     return KErrNone;
       
    94 	}
       
    95 	
       
    96 /**
       
    97 * Same as StopAsyncWaiting, but panics with AWRK <error code> instead 
       
    98 * of returning the error code
       
    99 * @see StopAsyncWaiting
       
   100 */
       
   101 void CAsyncWorker::StopAsyncWaitingOrPanic( TInt aErr )
       
   102 	{
       
   103 	TInt err = StopAsyncWaiting( aErr );
       
   104 	if( err != KErrNone ) 
       
   105 		{
       
   106 		User::Panic( KAsyncWorkerPanic, err );
       
   107 		}
       
   108 	}
       
   109 	
       
   110 //End of file