0
|
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 the License "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 |
// e32\kernel\smqueue.cpp
|
|
15 |
//
|
|
16 |
//
|
|
17 |
|
|
18 |
#include <kernel/kern_priv.h>
|
|
19 |
#include "execs.h"
|
|
20 |
#include "msgqueue.h"
|
|
21 |
|
|
22 |
/********************************************
|
|
23 |
* Asynchronous message queues
|
|
24 |
********************************************/
|
|
25 |
|
|
26 |
TInt ExecHandler::MsgQueueCreate(const TDesC8* aName, TInt aSize, TInt aLength, TOwnerType aType)
|
|
27 |
{
|
|
28 |
|
|
29 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MsgQueueCreate"));
|
|
30 |
|
|
31 |
//validate params
|
|
32 |
//length must be multiple of 4, greater than 0 and < kmaxlength
|
|
33 |
if ((aLength & 3) || (aLength > DMsgQueue::KMaxLength) || (aLength < 4))
|
|
34 |
K::PanicKernExec(EMsgQueueInvalidLength);
|
|
35 |
|
|
36 |
//size is number of message slots in the queue, it must be > 0
|
|
37 |
if (aSize <= 0)
|
|
38 |
K::PanicKernExec(EMsgQueueInvalidSlots);
|
|
39 |
|
|
40 |
TKName name;
|
|
41 |
DObject* pOwner = NULL;
|
|
42 |
const TDesC* pName = NULL;
|
|
43 |
if (aName)
|
|
44 |
{
|
|
45 |
Kern::KUDesGet(name, *aName);
|
|
46 |
pName = &name;
|
|
47 |
}
|
|
48 |
else if (aType == EOwnerThread)
|
|
49 |
pOwner = TheCurrentThread;
|
|
50 |
else
|
|
51 |
pOwner = TheCurrentThread->iOwningProcess;
|
|
52 |
|
|
53 |
|
|
54 |
NKern::ThreadEnterCS();
|
|
55 |
|
|
56 |
TInt ret = KErrNoMemory;
|
|
57 |
DMsgQueue* pMQ=new DMsgQueue;
|
|
58 |
if (pMQ)
|
|
59 |
{
|
|
60 |
ret = pMQ->Create(pOwner, pName, aLength, aSize);
|
|
61 |
|
|
62 |
if (KErrNone == ret)
|
|
63 |
{
|
|
64 |
if(aName)
|
|
65 |
pMQ->SetProtection(name.Length() ? DObject::EGlobal : DObject::EProtected);
|
|
66 |
ret = K::MakeHandle(aType, pMQ);
|
|
67 |
}
|
|
68 |
if (ret < KErrNone)
|
|
69 |
pMQ->Close(NULL);
|
|
70 |
|
|
71 |
}
|
|
72 |
NKern::ThreadLeaveCS();
|
|
73 |
return ret;
|
|
74 |
}
|
|
75 |
|
|
76 |
TInt ExecHandler::MsgQueueSend(TInt aMsgQueueHandle, const TAny* aPtr, TInt aLength)
|
|
77 |
{
|
|
78 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MsgQueueSend"));
|
|
79 |
|
|
80 |
TUint8 sbuf[DMsgQueue::KMaxLength];
|
|
81 |
if(TUint(aLength)<=TUint(DMsgQueue::KMaxLength))
|
|
82 |
kumemget(sbuf,aPtr,aLength);
|
|
83 |
|
|
84 |
NKern::LockSystem();
|
|
85 |
DMsgQueue* msgQueue = (DMsgQueue*)K::ObjectFromHandle(aMsgQueueHandle,EMsgQueue);
|
|
86 |
return msgQueue->Send(sbuf, aLength);
|
|
87 |
}
|
|
88 |
|
|
89 |
|
|
90 |
TInt ExecHandler::MsgQueueReceive(DMsgQueue* aMsgQueue, TAny* aPtr, TInt aLength)
|
|
91 |
{
|
|
92 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MsgQueueReceive"));
|
|
93 |
TUint8 sbuf[DMsgQueue::KMaxLength];
|
|
94 |
TInt r = aMsgQueue->Receive(sbuf, aLength);
|
|
95 |
if(r==KErrNone)
|
|
96 |
kumemput(aPtr,sbuf,aLength);
|
|
97 |
return r;
|
|
98 |
}
|
|
99 |
|
|
100 |
|
|
101 |
void ExecHandler::MsgQueueNotifySpaceAvailable(DMsgQueue* aMsgQueue, TRequestStatus& aStatus)
|
|
102 |
{
|
|
103 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MsgQueueNotifySpaceAvailable"));
|
|
104 |
aMsgQueue->NotifySpaceAvailable(&aStatus);
|
|
105 |
}
|
|
106 |
|
|
107 |
void ExecHandler::MsgQueueCancelSpaceAvailable(DMsgQueue* aMsgQueue)
|
|
108 |
{
|
|
109 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MsgQueueCancelSpaceAvailable"));
|
|
110 |
aMsgQueue->CancelSpaceAvailable();
|
|
111 |
}
|
|
112 |
|
|
113 |
void ExecHandler::MsgQueueNotifyDataAvailable(DMsgQueue* aMsgQueue, TRequestStatus& aStatus)
|
|
114 |
{
|
|
115 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MsgQueueNotifyDataAvailable"));
|
|
116 |
aMsgQueue->NotifyDataAvailable(&aStatus);
|
|
117 |
}
|
|
118 |
|
|
119 |
void ExecHandler::MsgQueueCancelDataAvailable(DMsgQueue* aMsgQueue)
|
|
120 |
{
|
|
121 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MsgQueueCancelDataAvailable"));
|
|
122 |
aMsgQueue->CancelDataAvailable();
|
|
123 |
}
|
|
124 |
|
|
125 |
TInt ExecHandler::MsgQueueSize(DMsgQueue* aMsgQueue)
|
|
126 |
{
|
|
127 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MsgQueueSize"));
|
|
128 |
return aMsgQueue->MessageSize();
|
|
129 |
}
|
|
130 |
|
|
131 |
|
|
132 |
TInt DMsgQueue::Create(DObject* aOwner, const TDesC* aName, TInt aMsgLength, TInt aSlotCount, TBool aVisible)
|
|
133 |
// Enter and leave with system unlocked
|
|
134 |
{
|
|
135 |
|
|
136 |
SetOwner(aOwner);
|
|
137 |
TInt ret = KErrNone;
|
|
138 |
if (aName && (aName->Length() > 0))
|
|
139 |
{
|
|
140 |
ret = SetName(aName);
|
|
141 |
if (ret != KErrNone)
|
|
142 |
return ret;
|
|
143 |
}
|
|
144 |
|
|
145 |
ret = Kern::CreateClientRequest(iDataAvailRequest);
|
|
146 |
if (ret != KErrNone)
|
|
147 |
return ret;
|
|
148 |
|
|
149 |
ret = Kern::CreateClientRequest(iSpaceAvailRequest);
|
|
150 |
if (ret != KErrNone)
|
|
151 |
return ret;
|
|
152 |
|
|
153 |
//Kern::Alloc asserts if the size is > KMaxTint/2 so guard against this
|
|
154 |
if (aSlotCount > (KMaxTInt/2) / aMsgLength)
|
|
155 |
return KErrNoMemory;
|
|
156 |
|
|
157 |
iMsgPool = static_cast<TUint8*>(Kern::Alloc(aSlotCount * aMsgLength));
|
|
158 |
|
|
159 |
if (!iMsgPool)
|
|
160 |
return KErrNoMemory;
|
|
161 |
|
|
162 |
iMaxMsgLength = (TUint16)aMsgLength;
|
|
163 |
iFirstFreeSlot = iMsgPool;
|
|
164 |
iFirstFullSlot = iMsgPool;
|
|
165 |
iEndOfPool = iMsgPool + iMaxMsgLength * aSlotCount;
|
|
166 |
|
|
167 |
if (aVisible)
|
|
168 |
ret = K::AddObject(this,EMsgQueue);
|
|
169 |
|
|
170 |
return ret;
|
|
171 |
}
|
|
172 |
|
|
173 |
DMsgQueue::~DMsgQueue()
|
|
174 |
{
|
|
175 |
//no problem with race condition here, don't need temporary copy of thread ptrs
|
|
176 |
if (iDataAvailRequest->IsReady())
|
|
177 |
{
|
|
178 |
Kern::QueueRequestComplete(iThreadWaitingOnDataAvail, iDataAvailRequest, KErrCancel);
|
|
179 |
iThreadWaitingOnDataAvail->Close(NULL);
|
|
180 |
}
|
|
181 |
if (iSpaceAvailRequest->IsReady())
|
|
182 |
{
|
|
183 |
Kern::QueueRequestComplete(iThreadWaitingOnSpaceAvail, iSpaceAvailRequest, KErrCancel);
|
|
184 |
iThreadWaitingOnSpaceAvail->Close(NULL);
|
|
185 |
}
|
|
186 |
Kern::Free(iMsgPool);
|
|
187 |
Kern::DestroyClientRequest(iDataAvailRequest);
|
|
188 |
Kern::DestroyClientRequest(iSpaceAvailRequest);
|
|
189 |
}
|
|
190 |
|
|
191 |
|
|
192 |
TInt DMsgQueue::Send(const TAny* aPtr, TInt aLength)
|
|
193 |
// Enter with system locked, leave with system unlocked
|
|
194 |
{
|
|
195 |
if (aLength > iMaxMsgLength || aLength <= 0)
|
|
196 |
K::PanicCurrentThread(EMsgQueueInvalidLength);
|
|
197 |
|
|
198 |
if (iState == EFull)
|
|
199 |
{
|
|
200 |
NKern::UnlockSystem();
|
|
201 |
return KErrOverflow;
|
|
202 |
}
|
|
203 |
|
|
204 |
memcpy(iFirstFreeSlot, aPtr, aLength);
|
|
205 |
|
|
206 |
iFirstFreeSlot += iMaxMsgLength;
|
|
207 |
if (iFirstFreeSlot == iEndOfPool)
|
|
208 |
iFirstFreeSlot = iMsgPool;
|
|
209 |
|
|
210 |
iState = static_cast<TUint8>((iFirstFreeSlot == iFirstFullSlot) ? EFull : EPartial);
|
|
211 |
|
|
212 |
//see if anyone is waiting on data available
|
|
213 |
CompleteRequestIfPending(iThreadWaitingOnDataAvail, iDataAvailRequest, KErrNone);
|
|
214 |
|
|
215 |
return KErrNone;
|
|
216 |
}
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
TInt DMsgQueue::Receive(TAny* aPtr, TInt aLength)
|
|
221 |
// Enter with system locked, leave with system unlocked
|
|
222 |
{
|
|
223 |
if (aLength != iMaxMsgLength)
|
|
224 |
K::PanicCurrentThread(EMsgQueueInvalidLength);
|
|
225 |
|
|
226 |
if (iState == EEmpty)
|
|
227 |
{
|
|
228 |
NKern::UnlockSystem();
|
|
229 |
return KErrUnderflow;
|
|
230 |
}
|
|
231 |
|
|
232 |
memcpy(aPtr, iFirstFullSlot, iMaxMsgLength);
|
|
233 |
|
|
234 |
iFirstFullSlot += iMaxMsgLength;
|
|
235 |
if (iFirstFullSlot == iEndOfPool)
|
|
236 |
iFirstFullSlot = iMsgPool;
|
|
237 |
|
|
238 |
iState = static_cast<TUint8>((iFirstFreeSlot == iFirstFullSlot) ? EEmpty : EPartial);
|
|
239 |
|
|
240 |
//see if anyone is waiting on space available
|
|
241 |
CompleteRequestIfPending(iThreadWaitingOnSpaceAvail, iSpaceAvailRequest, KErrNone);
|
|
242 |
|
|
243 |
return KErrNone;
|
|
244 |
}
|
|
245 |
|
|
246 |
|
|
247 |
void DMsgQueue::NotifySpaceAvailable(TRequestStatus* aStatus)
|
|
248 |
// Enter with system locked, leave with system unlocked
|
|
249 |
{
|
|
250 |
RequestNotification(ESpaceAvailable, aStatus, iThreadWaitingOnSpaceAvail, iSpaceAvailRequest);
|
|
251 |
}
|
|
252 |
|
|
253 |
void DMsgQueue::NotifyDataAvailable(TRequestStatus* aStatus)
|
|
254 |
// Enter with system locked, leave with system unlocked
|
|
255 |
{
|
|
256 |
RequestNotification(EDataAvailable, aStatus, iThreadWaitingOnDataAvail, iDataAvailRequest);
|
|
257 |
}
|
|
258 |
|
|
259 |
void DMsgQueue::RequestNotification(TNotification aNotification, TRequestStatus* aStatus, DThread*& aThread, TClientRequest* aRequest)
|
|
260 |
// Enter with system locked, leave with system unlocked
|
|
261 |
{
|
|
262 |
TInt r = KErrNone;
|
|
263 |
DThread* previousThread = NULL;
|
|
264 |
if (aRequest->IsReady())
|
|
265 |
{
|
|
266 |
//someone already waiting...
|
|
267 |
if (aThread->iExitType == EExitPending)
|
|
268 |
K::PanicCurrentThread(EMsgQueueRequestPending); //...and is still alive. Panic the current thread.
|
|
269 |
//The thread that was previously waitning on available data has died.
|
|
270 |
//As iThreadWaitingOnDataAvail is not cleaned up automatically when the waiting
|
|
271 |
//thread dies, we have to do it here.
|
|
272 |
previousThread = aThread;
|
|
273 |
NKern::ThreadEnterCS();
|
|
274 |
aThread = NULL;
|
|
275 |
aRequest->Reset();
|
|
276 |
}
|
|
277 |
if ((aNotification == ESpaceAvailable && iState != EFull) ||
|
|
278 |
(aNotification == EDataAvailable && iState != EEmpty))
|
|
279 |
Kern::RequestComplete(aStatus, KErrNone);
|
|
280 |
else
|
|
281 |
{
|
|
282 |
DThread* thread = TheCurrentThread;
|
|
283 |
thread->CheckedOpen();
|
|
284 |
aThread = thread;
|
|
285 |
r = aRequest->SetStatus(aStatus);
|
|
286 |
}
|
|
287 |
NKern::UnlockSystem();
|
|
288 |
|
|
289 |
if (previousThread)
|
|
290 |
{
|
|
291 |
//Complete the clean up of the dying thread which was previously waiting on data-available event.
|
|
292 |
previousThread->AsyncClose();
|
|
293 |
NKern::ThreadLeaveCS();
|
|
294 |
}
|
|
295 |
|
|
296 |
if (r != KErrNone)
|
|
297 |
K::PanicCurrentThread(EMsgQueueRequestPending);
|
|
298 |
}
|
|
299 |
|
|
300 |
|
|
301 |
void DMsgQueue::CancelSpaceAvailable()
|
|
302 |
// Enter with system locked, leave with system unlocked
|
|
303 |
{
|
|
304 |
CompleteRequestIfPending(iThreadWaitingOnSpaceAvail, iSpaceAvailRequest, KErrCancel);
|
|
305 |
}
|
|
306 |
|
|
307 |
void DMsgQueue::CancelDataAvailable()
|
|
308 |
// Enter with system locked, leave with system unlocked
|
|
309 |
{
|
|
310 |
CompleteRequestIfPending(iThreadWaitingOnDataAvail, iDataAvailRequest, KErrCancel);
|
|
311 |
}
|
|
312 |
|
|
313 |
void DMsgQueue::CompleteRequestIfPending(DThread*& aThread, TClientRequest* aRequest, TInt aCompletionVal)
|
|
314 |
// Enter with system locked, leave with system unlocked
|
|
315 |
{
|
|
316 |
DThread* thread = aThread;
|
|
317 |
aThread = NULL;
|
|
318 |
if (aRequest->IsReady())
|
|
319 |
{
|
|
320 |
TInt c = thread->Dec();
|
|
321 |
if (c==1)
|
|
322 |
{
|
|
323 |
// todo: why do we need to do this?
|
|
324 |
aRequest->Reset();
|
|
325 |
NKern::ThreadEnterCS();
|
|
326 |
NKern::UnlockSystem();
|
|
327 |
thread->AsyncDelete();
|
|
328 |
NKern::ThreadLeaveCS();
|
|
329 |
return;
|
|
330 |
}
|
|
331 |
Kern::QueueRequestComplete(thread, aRequest, aCompletionVal);
|
|
332 |
}
|
|
333 |
NKern::UnlockSystem();
|
|
334 |
}
|
|
335 |
|
|
336 |
TInt DMsgQueue::MessageSize() const
|
|
337 |
{
|
|
338 |
// Enter with system locked, leave with system locked
|
|
339 |
return iMaxMsgLength;
|
|
340 |
}
|