0
|
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 "msgqueue.h" // For DMsgQueue
|
|
21 |
#include "p2proutertrace.h" // For C_TRACE..
|
|
22 |
|
|
23 |
// Fault identifiers
|
|
24 |
enum TDMsgQueueFaults
|
|
25 |
{
|
|
26 |
EDMsgQueueMemAllocFail = 0x00,
|
|
27 |
EDMsgQueueMemAllocFail2,
|
|
28 |
EDMsgQueueInvalidQueueSize,
|
|
29 |
EDMsgQueueNotEmpty,
|
|
30 |
EDMsgQueueNotInSync,
|
|
31 |
EDMsgQueueNotInSync2,
|
|
32 |
EDMsgQueueNotInSync3,
|
|
33 |
EDMsgQueueNotInSync4,
|
|
34 |
EDMsgQueueNotInSync5,
|
|
35 |
};
|
|
36 |
|
|
37 |
DMsgQueue::DMsgQueue(
|
|
38 |
const TUint16 aSize
|
|
39 |
)
|
|
40 |
{
|
|
41 |
|
|
42 |
C_TRACE( ( _T( "DMsgQueue::DMsgQueue 0x%x %d>" ), this, aSize ) );
|
|
43 |
iQueueMutex = new NFastMutex();
|
|
44 |
ASSERT_RESET_ALWAYS( iQueueMutex, ( EDMsgQueueMemAllocFail | EDMsgQueueTraceId << KClassIdentifierShift ) );
|
|
45 |
iSize = aSize;
|
|
46 |
ASSERT_RESET_ALWAYS( ( iSize > 0 ), ( EDMsgQueueInvalidQueueSize | EDMsgQueueTraceId << KClassIdentifierShift ) );
|
|
47 |
iShInputIndex = 0;
|
|
48 |
iShOutputIndex = 0;
|
|
49 |
iShCount = 0;
|
|
50 |
// Reserve memory for the buffer.
|
|
51 |
iShRingBuffer = new TDes8*[ aSize ];
|
|
52 |
C_TRACE( ( _T( "DMsgQueue::DMsgQueue 0x%x" ), iShRingBuffer ) );
|
|
53 |
ASSERT_RESET_ALWAYS( iShRingBuffer, ( EDMsgQueueMemAllocFail2 | EDMsgQueueTraceId << KClassIdentifierShift ) );
|
|
54 |
for( TInt i( 0 ); i < iSize; ++i )
|
|
55 |
{
|
|
56 |
iShRingBuffer[ i ] = NULL;
|
|
57 |
}
|
|
58 |
C_TRACE( ( _T( "DMsgQueue::DMsgQueue 0x%x<" ), this ) );
|
|
59 |
|
|
60 |
}
|
|
61 |
|
|
62 |
DMsgQueue::~DMsgQueue(
|
|
63 |
// None
|
|
64 |
)
|
|
65 |
{
|
|
66 |
|
|
67 |
C_TRACE( ( _T( "DMsgQueue::~DMsgQueue 0x%x %d>" ), this, iShCount ) );
|
|
68 |
ASSERT_RESET_ALWAYS( iShCount == 0, ( EDMsgQueueNotEmpty | EDMsgQueueTraceId << KClassIdentifierShift ) );
|
|
69 |
// NOTE! This does not deallocate the blocks from the allocated memory just the pointers!
|
|
70 |
for( TInt i( 0 ); i < iSize; ++i )
|
|
71 |
{
|
|
72 |
TDes8* temp = iShRingBuffer[ i ];
|
|
73 |
if( temp )
|
|
74 |
{
|
|
75 |
C_TRACE( ( _T( "DMsgQueue::~DMsgQueue ptr 0x%x" ), temp ) );
|
|
76 |
delete temp;
|
|
77 |
temp = NULL;
|
|
78 |
}
|
|
79 |
}
|
|
80 |
if( iShRingBuffer )
|
|
81 |
{
|
|
82 |
C_TRACE( ( _T( "DMsgQueue::~DMsgQueue iShRingBuffer 0x%x iShRingBuffer[0] 0x%x" ), iShRingBuffer, iShRingBuffer[0] ) );
|
|
83 |
delete [] iShRingBuffer;
|
|
84 |
iShRingBuffer = NULL;
|
|
85 |
}
|
|
86 |
|
|
87 |
iSize = 0;
|
|
88 |
iShInputIndex = 0;
|
|
89 |
iShOutputIndex = 0;
|
|
90 |
iShCount = 0;
|
|
91 |
|
|
92 |
C_TRACE( ( _T( "DMsgQueue::~DMsgQueue iQueueMutex" ) ) );
|
|
93 |
// Only modified in constructor if allocated failed, already reseted.
|
|
94 |
delete iQueueMutex;
|
|
95 |
iQueueMutex = NULL;
|
|
96 |
C_TRACE( ( _T( "DMsgQueue::~DMsgQueue 0x%x<" ), this ) );
|
|
97 |
|
|
98 |
}
|
|
99 |
|
|
100 |
void DMsgQueue::Add(
|
|
101 |
const TDesC8& aMessage
|
|
102 |
)
|
|
103 |
{
|
|
104 |
|
|
105 |
C_TRACE( ( _T( "DMsgQueue::Add 0x%x %d %d %d %d 0x%x>" ), this, iSize, iShCount, iShInputIndex, iShOutputIndex, &aMessage ) );
|
|
106 |
NKern::FMWait( iQueueMutex );
|
|
107 |
// If queue get's overfilled throw kernel fault.
|
|
108 |
ASSERT_RESET_ALWAYS( ( iShCount < iSize ), ( EDMsgQueueNotInSync | EDMsgQueueTraceId << KClassIdentifierShift ) );
|
|
109 |
// Place the buffer into the queue.
|
|
110 |
iShRingBuffer[ iShInputIndex ] = static_cast<TDes8*>( &const_cast<TDesC8&>( aMessage ) );
|
|
111 |
// Adjust input pointer.
|
|
112 |
iShInputIndex = ( ( iShInputIndex + 1 ) % iSize );
|
|
113 |
// Remember the amount of the requests in the queue.
|
|
114 |
iShCount++;
|
|
115 |
NKern::FMSignal( iQueueMutex );
|
|
116 |
C_TRACE( ( _T( "DMsgQueue::Add 0x%x %d %d %d %d 0x%x<" ), this, iSize, iShCount, iShInputIndex, iShOutputIndex, &aMessage ) );
|
|
117 |
|
|
118 |
}
|
|
119 |
|
|
120 |
TUint16 DMsgQueue::Count(
|
|
121 |
// None
|
|
122 |
)
|
|
123 |
{
|
|
124 |
|
|
125 |
C_TRACE( ( _T( "DMsgQueue::Count 0x%x %d>" ), this, iShCount ) );
|
|
126 |
TUint16 count( KErrNone );
|
|
127 |
NKern::FMWait( iQueueMutex );
|
|
128 |
// If count is too big.
|
|
129 |
ASSERT_RESET_ALWAYS( ( iShCount <= iSize ), ( EDMsgQueueNotInSync2 | EDMsgQueueTraceId << KClassIdentifierShift ) );
|
|
130 |
count = iShCount;
|
|
131 |
NKern::FMSignal( iQueueMutex );
|
|
132 |
C_TRACE( ( _T( "DMsgQueue::Count 0x%x %d %d<" ), this, iShCount, count ) );
|
|
133 |
return count;
|
|
134 |
|
|
135 |
}
|
|
136 |
|
|
137 |
TDes8& DMsgQueue::Get(
|
|
138 |
// None
|
|
139 |
)
|
|
140 |
{
|
|
141 |
|
|
142 |
C_TRACE( ( _T( "DMsgQueue::Get 0x%x %d %d %d %d>" ), this, iSize, iShCount, iShInputIndex, iShOutputIndex ) );
|
|
143 |
NKern::FMWait( iQueueMutex );
|
|
144 |
// If queue is empty.
|
|
145 |
ASSERT_RESET_ALWAYS( ( iShCount > 0 ), ( EDMsgQueueNotInSync3 | EDMsgQueueTraceId << KClassIdentifierShift ) );
|
|
146 |
// Get the buffer from the queue.
|
|
147 |
TDes8* temp = iShRingBuffer[ iShOutputIndex ];
|
|
148 |
// Set buffer location to NULL.
|
|
149 |
iShRingBuffer[ iShOutputIndex ] = NULL;
|
|
150 |
// Adjust output pointer.
|
|
151 |
iShOutputIndex = ( ( iShOutputIndex + 1 ) % iSize );
|
|
152 |
// Remember the amount of the requests in the queue.
|
|
153 |
iShCount--;
|
|
154 |
NKern::FMSignal( iQueueMutex );
|
|
155 |
ASSERT_RESET_ALWAYS( temp, ( EDMsgQueueNotInSync4 | EDMsgQueueTraceId << KClassIdentifierShift ) );
|
|
156 |
C_TRACE( ( _T( "DMsgQueue::Get 0x%x %d %d %d %d 0x%x<" ), this, iSize, iShCount, iShInputIndex, iShOutputIndex, temp ) );
|
|
157 |
return *temp;
|
|
158 |
|
|
159 |
}
|
|
160 |
|
|
161 |
void DMsgQueue::RollBack(
|
|
162 |
const TDesC8& aMsgToRoll
|
|
163 |
)
|
|
164 |
{
|
|
165 |
|
|
166 |
C_TRACE( ( _T( "DMsgQueue::RollBack 0x%x %d %d %d %d 0x%x>" ), this, iSize, iShCount, iShInputIndex, iShOutputIndex, &aMsgToRoll ) );
|
|
167 |
NKern::FMWait( iQueueMutex );
|
|
168 |
ASSERT_RESET_ALWAYS( ( iShCount < iSize ), ( EDMsgQueueNotInSync5 | EDMsgQueueTraceId << KClassIdentifierShift ) );
|
|
169 |
// If needs to rollback from 0 to iSize -1 index.
|
|
170 |
iShOutputIndex = ( iShOutputIndex == 0 ) ? iSize : iShOutputIndex;
|
|
171 |
// Adjust output pointer.
|
|
172 |
iShOutputIndex = ( ( iShOutputIndex - 1 ) % iSize );
|
|
173 |
// Get the descriptor from queue where the rollback is made.
|
|
174 |
iShRingBuffer[ iShOutputIndex ] = static_cast<TDes8*>( &const_cast<TDesC8&>( aMsgToRoll ) );
|
|
175 |
// Remember the amount of the requests in the queue.
|
|
176 |
iShCount++;
|
|
177 |
NKern::FMSignal( iQueueMutex );
|
|
178 |
C_TRACE( ( _T( "DMsgQueue::RollBack 0x%x %d %d %d %d 0x%x<" ), this, iSize, iShCount, iShInputIndex, iShOutputIndex, &aMsgToRoll ) );
|
|
179 |
|
|
180 |
}
|
|
181 |
|
|
182 |
// End of file.
|