|
1 // Copyright (c) 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 #include "systemsharedbufs.h" |
|
17 #include "systemsharedasyncalloc.h" |
|
18 #include "commsbufasyncreqinternal.h" |
|
19 |
|
20 |
|
21 CSystemSharedPoolAsyncAlloc::CSystemSharedPoolAsyncAlloc(CSystemSharedBufPool& aPool, MAsyncAllocNotifier& aNotifier) |
|
22 : CActive(EPriorityStandard), |
|
23 iPool(aPool), |
|
24 iNotifier(aNotifier) |
|
25 { |
|
26 CActiveScheduler::Add(this); |
|
27 } |
|
28 |
|
29 CSystemSharedPoolAsyncAlloc::~CSystemSharedPoolAsyncAlloc() |
|
30 { |
|
31 Deactivate(); |
|
32 } |
|
33 |
|
34 void CSystemSharedPoolAsyncAlloc::Activate(TInt aFreeBufs) |
|
35 { |
|
36 if(!IsActive()) |
|
37 { |
|
38 iStatus = KRequestPending; |
|
39 iPool.Pool().RequestFreeSpaceNotification(aFreeBufs, iStatus); |
|
40 SetActive(); |
|
41 } |
|
42 } |
|
43 |
|
44 void CSystemSharedPoolAsyncAlloc::Deactivate() |
|
45 { |
|
46 Cancel(); |
|
47 } |
|
48 |
|
49 void CSystemSharedPoolAsyncAlloc::RunL() |
|
50 { |
|
51 User::LeaveIfError(iStatus.Int()); |
|
52 iNotifier.OnCompletion(); |
|
53 } |
|
54 |
|
55 TInt CSystemSharedPoolAsyncAlloc::RunError(TInt aError) |
|
56 { |
|
57 iNotifier.OnError(aError); |
|
58 return KErrNone; |
|
59 } |
|
60 |
|
61 void CSystemSharedPoolAsyncAlloc::DoCancel() |
|
62 { |
|
63 iPool.Pool().CancelFreeSpaceNotification(iStatus); |
|
64 } |
|
65 |
|
66 // ------------------------------------------------------------------------- |
|
67 |
|
68 CSystemSharedAsyncAlloc* CSystemSharedAsyncAlloc::New(const RPointerArray<CSystemSharedBufPool>& aPools) |
|
69 { |
|
70 CSystemSharedAsyncAlloc* self = new CSystemSharedAsyncAlloc(); |
|
71 if(self) |
|
72 { |
|
73 if (self->Construct(aPools) == KErrNone) |
|
74 { |
|
75 return self; |
|
76 } |
|
77 else |
|
78 { |
|
79 delete self; |
|
80 } |
|
81 } |
|
82 return NULL; |
|
83 } |
|
84 |
|
85 CSystemSharedAsyncAlloc::CSystemSharedAsyncAlloc() |
|
86 : CActive(EPriorityStandard), |
|
87 iRequestState(EMakeRequest), |
|
88 iSignalled(ETrue) |
|
89 { |
|
90 CActiveScheduler::Add(this); |
|
91 } |
|
92 |
|
93 CSystemSharedAsyncAlloc::~CSystemSharedAsyncAlloc() |
|
94 { |
|
95 __ASSERT_DEBUG(iAllocsPending.IsEmpty(), User::Invariant()); |
|
96 Cancel(); |
|
97 iAsyncAllocPools.ResetAndDestroy(); |
|
98 iAsyncAllocPools.Close(); |
|
99 iAsynAllocLock.Close(); |
|
100 iOwnerThread.Close(); |
|
101 } |
|
102 |
|
103 TInt CSystemSharedAsyncAlloc::Construct(const RPointerArray<CSystemSharedBufPool>& aPools) |
|
104 { |
|
105 TInt err = KErrNone; |
|
106 for(TInt i = 0; i < aPools.Count(); ++i) |
|
107 { |
|
108 CSystemSharedPoolAsyncAlloc* asyncPool = new CSystemSharedPoolAsyncAlloc(*(aPools[i]), *this); |
|
109 if(asyncPool) |
|
110 { |
|
111 if(iAsyncAllocPools.Append(asyncPool) != KErrNone) |
|
112 { |
|
113 delete asyncPool; |
|
114 err = KErrNoMemory; |
|
115 break; |
|
116 } |
|
117 } |
|
118 else |
|
119 { |
|
120 err = KErrNoMemory; |
|
121 break; |
|
122 } |
|
123 } |
|
124 if(err == KErrNone) |
|
125 { |
|
126 iAllocsPending.SetOffset(_FOFF(CCommsBufAsyncRequest, iLink)); |
|
127 err = iAsynAllocLock.CreateLocal(); |
|
128 if(err == KErrNone) |
|
129 { |
|
130 TThreadId id = RThread().Id(); |
|
131 err = iOwnerThread.Open(id); |
|
132 if(err == KErrNone) |
|
133 { |
|
134 // Activate the AO. |
|
135 ActivateSelf(); |
|
136 } |
|
137 } |
|
138 } |
|
139 return err; |
|
140 } |
|
141 |
|
142 void CSystemSharedAsyncAlloc::StartRequest(CCommsBufAsyncRequest& aRequest) |
|
143 { |
|
144 iAsynAllocLock.Wait(); |
|
145 if(iAllocsPending.IsEmpty()) |
|
146 { |
|
147 // We do not have anything in the pending alloc queue. |
|
148 // Kick off the AO. |
|
149 CompleteSelf(); |
|
150 } |
|
151 iAllocsPending.AddLast(aRequest); |
|
152 iAsynAllocLock.Signal(); |
|
153 } |
|
154 |
|
155 void CSystemSharedAsyncAlloc::CancelRequest(CCommsBufAsyncRequest& aRequest) |
|
156 { |
|
157 iAsynAllocLock.Wait(); |
|
158 aRequest.iLink.Deque(); |
|
159 aRequest.Complete(KErrCancel); |
|
160 CompleteSelf(); |
|
161 iAsynAllocLock.Signal(); |
|
162 } |
|
163 |
|
164 void CSystemSharedAsyncAlloc::MakePoolAsyncRequest() |
|
165 { |
|
166 iAsynAllocLock.Wait(); |
|
167 MakePoolRequest(); |
|
168 ActivateSelf(); |
|
169 iAsynAllocLock.Signal(); |
|
170 } |
|
171 |
|
172 void CSystemSharedAsyncAlloc::MakePoolRequest() |
|
173 { |
|
174 if(!iAllocsPending.IsEmpty()) |
|
175 { |
|
176 CCommsBufAsyncRequest* req = iAllocsPending.First(); |
|
177 for(TInt i = 0; i < iAsyncAllocPools.Count(); ++i) |
|
178 { |
|
179 CSystemSharedPoolAsyncAlloc* asyncPool = iAsyncAllocPools[i]; |
|
180 TInt poolBufSize = asyncPool->iPool.BufSize(); |
|
181 if(poolBufSize >= req->iMinSize && poolBufSize <= req->iMaxSize) |
|
182 { |
|
183 TInt freeBufs = req->iSize / poolBufSize; |
|
184 freeBufs = ((req->iSize % asyncPool->iPool.BufSize()) == 0) ? freeBufs : (freeBufs + 1); |
|
185 asyncPool->Activate(freeBufs); |
|
186 } |
|
187 } |
|
188 } |
|
189 } |
|
190 |
|
191 void CSystemSharedAsyncAlloc::CancelAllPoolRequest() |
|
192 { |
|
193 for(TInt i = 0; i < iAsyncAllocPools.Count(); ++i) |
|
194 { |
|
195 iAsyncAllocPools[i]->Deactivate(); |
|
196 } |
|
197 } |
|
198 |
|
199 void CSystemSharedAsyncAlloc::CancelPoolAsyncRequest() |
|
200 { |
|
201 iAsynAllocLock.Wait(); |
|
202 CancelAllPoolRequest(); |
|
203 ActivateSelf(); |
|
204 if(!iAllocsPending.IsEmpty()) |
|
205 { |
|
206 CompleteSelf(); |
|
207 } |
|
208 iAsynAllocLock.Signal(); |
|
209 } |
|
210 |
|
211 void CSystemSharedAsyncAlloc::RunL() |
|
212 { |
|
213 // |
|
214 switch(iRequestState) |
|
215 { |
|
216 case EMakeRequest: |
|
217 MakePoolAsyncRequest(); |
|
218 break; |
|
219 |
|
220 case ECancelRequest: |
|
221 CancelPoolAsyncRequest(); |
|
222 iRequestState = EMakeRequest; |
|
223 break; |
|
224 |
|
225 default: |
|
226 // We should never reach here |
|
227 CommsBuf::Panic(EMBuf_AsyncAllocInvalidState); |
|
228 break; |
|
229 } |
|
230 |
|
231 } |
|
232 |
|
233 void CSystemSharedAsyncAlloc::DoCancel() |
|
234 { |
|
235 TRequestStatus* requestStatusPtr = &iStatus; |
|
236 iOwnerThread.RequestComplete(requestStatusPtr, KErrCancel); |
|
237 } |
|
238 |
|
239 void CSystemSharedAsyncAlloc::ActivateSelf() |
|
240 { |
|
241 if(iSignalled) |
|
242 { |
|
243 iStatus = KRequestPending; |
|
244 SetActive(); |
|
245 iSignalled = EFalse; |
|
246 } |
|
247 } |
|
248 |
|
249 void CSystemSharedAsyncAlloc::CompleteSelf() |
|
250 { |
|
251 if(!iSignalled) |
|
252 { |
|
253 TRequestStatus* requestStatusPtr = &iStatus; |
|
254 iOwnerThread.RequestComplete(requestStatusPtr, KErrNone); |
|
255 iSignalled = ETrue; |
|
256 } |
|
257 } |
|
258 |
|
259 void CSystemSharedAsyncAlloc::OnCompletion() |
|
260 { |
|
261 CompleteAsyncAllocs(); |
|
262 } |
|
263 |
|
264 void CSystemSharedAsyncAlloc::OnError(TInt aError) |
|
265 { |
|
266 // We hit with an error from the system pool. |
|
267 // complete all alloc request with that error. |
|
268 CCommsBufAsyncRequest* req; |
|
269 iAsynAllocLock.Wait(); |
|
270 if(!iAllocsPending.IsEmpty()) |
|
271 { |
|
272 TDblQueIter<CCommsBufAsyncRequest> iter(iAllocsPending); |
|
273 while (req = iter++, req != NULL) |
|
274 { |
|
275 req->Complete(aError); |
|
276 } |
|
277 } |
|
278 iAsynAllocLock.Signal(); |
|
279 } |
|
280 |
|
281 void CSystemSharedAsyncAlloc::CompleteAsyncAllocs() |
|
282 { |
|
283 CCommsBufAsyncRequest *req; |
|
284 |
|
285 iAsynAllocLock.Wait(); |
|
286 TDblQueIter<CCommsBufAsyncRequest> iter(iAllocsPending); |
|
287 while (req = iter++, req != NULL) |
|
288 { |
|
289 RCommsBufChain chain; |
|
290 chain.Alloc(req->iSize, req->iMinSize, req->iMaxSize, req->iAllocator); |
|
291 if(!chain.IsEmpty()) |
|
292 { |
|
293 req->iBufQ.Assign(chain); |
|
294 req->Complete(KErrNone); |
|
295 } |
|
296 } |
|
297 // We are in the same thread. Cancel the current pool requests. |
|
298 CancelAllPoolRequest(); |
|
299 // Make the pool request if needed. |
|
300 MakePoolRequest(); |
|
301 iAsynAllocLock.Signal(); |
|
302 } |
|
303 |