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