omxil_generic/omxilcomplib/src/primsgqueue.inl
changeset 0 0e4a32b9112d
equal deleted inserted replaced
-1:000000000000 0:0e4a32b9112d
       
     1 // Copyright (c) 2008-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 /**
       
    18    @file
       
    19    @internalComponent
       
    20 */
       
    21 
       
    22 
       
    23 template <typename T>
       
    24 inline RPriMsgQueue<T>::TFrontQueueElement::TFrontQueueElement(
       
    25 	const TAny* apInfo)
       
    26 	:
       
    27 	ipInfo(apInfo)
       
    28 	{
       
    29 	iLink.iPriority = 0;
       
    30 	}
       
    31 
       
    32 template <typename T>
       
    33 inline RPriMsgQueue<T>::TFrontQueueElement::TFrontQueueElement(
       
    34 	const TAny* apInfo, TInt aPriority)
       
    35 	:
       
    36 	ipInfo(apInfo)
       
    37 	{
       
    38 	iLink.iPriority = aPriority;
       
    39 	}
       
    40 
       
    41 template <typename T>
       
    42 inline TInt RPriMsgQueue<T>::CreateLocal(TInt aSize, TOwnerType aOwner)
       
    43     {
       
    44 	iFrontQueue.SetOffset(_FOFF(TFrontQueueElement, iLink));
       
    45 	return iBackQueue.CreateLocal(aSize, sizeof(T), aOwner);
       
    46 	}
       
    47 
       
    48 template <typename T>
       
    49 inline TInt RPriMsgQueue<T>::Handle() const
       
    50 	{
       
    51 	return iBackQueue.Handle();
       
    52 	}
       
    53 
       
    54 template <typename T>
       
    55 inline TInt RPriMsgQueue<T>::Send(const T& aMsg)
       
    56     {
       
    57 	ASSERT(aMsg);
       
    58 	return iBackQueue.Send(&aMsg, sizeof(T));
       
    59 	}
       
    60 
       
    61 template <typename T>
       
    62 inline TInt RPriMsgQueue<T>::Receive(T& aMsg)
       
    63     {
       
    64 	TInt err = DrainBackQueue();
       
    65 	if (KErrNone == err)
       
    66 		{
       
    67 		if (iFrontQueue.IsEmpty())
       
    68 			{
       
    69 			return KErrUnderflow;
       
    70 			}
       
    71 
       
    72 		TFrontQueueElement* pElement = iFrontQueue.First();
       
    73 		__ASSERT_DEBUG(pElement != 0,
       
    74 					   User::Panic(RPriMsgQueuePanicCategory, 1));
       
    75 		pElement->iLink.Deque();
       
    76 		aMsg = reinterpret_cast<T>(const_cast<TAny*>(pElement->ipInfo));
       
    77 		delete pElement;
       
    78 		}
       
    79 
       
    80 	return err;
       
    81 
       
    82 	}
       
    83 
       
    84 template <typename T>
       
    85 inline void RPriMsgQueue<T>::Close()
       
    86 	{
       
    87 
       
    88 	// Back queue should be empty by now, but just in case...
       
    89 
       
    90 	// Here we expect a pointer type!...
       
    91 	T pElement;
       
    92 	while (iBackQueue.Receive(&pElement, sizeof(T)) == KErrNone)
       
    93 		{
       
    94 		delete pElement;
       
    95 		pElement = 0;
       
    96 		}
       
    97 	iBackQueue.Close();
       
    98 
       
    99 	// Front queue should be empty by now, but just in case...
       
   100 	TFrontQueueElement* pFQElement = 0;
       
   101 	while (!iFrontQueue.IsEmpty())
       
   102 		{
       
   103 		pFQElement = iFrontQueue.First();
       
   104 		__ASSERT_DEBUG(pFQElement != 0,
       
   105 					   User::Panic(RPriMsgQueuePanicCategory, 1));
       
   106 		pFQElement->iLink.Deque();
       
   107 		pElement = reinterpret_cast<T>(const_cast<TAny*>(pFQElement->ipInfo));
       
   108 		delete pElement;
       
   109 		delete pFQElement;
       
   110 		}
       
   111 
       
   112 	}
       
   113 
       
   114 template <typename T>
       
   115 inline void RPriMsgQueue<T>::NotifyDataAvailable(TRequestStatus& aStatus)
       
   116 	{
       
   117 	iBackQueue.NotifyDataAvailable(aStatus);
       
   118 	}
       
   119 
       
   120 template <typename T>
       
   121 inline void RPriMsgQueue<T>::CancelDataAvailable()
       
   122 	{
       
   123 	iBackQueue.CancelDataAvailable();
       
   124 	}
       
   125 
       
   126 template <typename T>
       
   127 TInt RPriMsgQueue<T>::DrainBackQueue()
       
   128 	{
       
   129 
       
   130 	// Here we expect a pointer type!...
       
   131 	T pElement;
       
   132 	while (iBackQueue.Receive(&pElement, sizeof(T)) == KErrNone)
       
   133 		{
       
   134 		TFrontQueueElement* pElem =
       
   135 			new TFrontQueueElement(pElement, pElement->Priority());
       
   136 
       
   137 		// This double attempt at allocation is done in order to allow OOM
       
   138 		// tests to pass, as there is no way to report an error to the client
       
   139 		// if we lose a RegisterCallbacks message in an OOM situation
       
   140 		if (!pElem)
       
   141 			{
       
   142 			pElem = new TFrontQueueElement(pElement, pElement->Priority());
       
   143 			}
       
   144 
       
   145 		if (!pElem)
       
   146 			{
       
   147 			// This command will get lost, but at least it won't be leaked
       
   148 			delete pElement;
       
   149 			return KErrNoMemory;
       
   150 			}
       
   151 		iFrontQueue.Add(*pElem);
       
   152 		}
       
   153 
       
   154 	return KErrNone;
       
   155 
       
   156 	}