0
|
1 |
// Copyright (c) 1996-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\notifier.cpp
|
|
15 |
// DChangeNotifier & DUndertaker code
|
|
16 |
// Note that both change notifier and undertaker handles can be thread or process relative
|
|
17 |
//
|
|
18 |
//
|
|
19 |
|
|
20 |
#include <kernel/kern_priv.h>
|
|
21 |
#include "execs.h"
|
|
22 |
|
|
23 |
#define iMState iWaitLink.iSpare1
|
|
24 |
|
|
25 |
DChangeNotifier::DChangeNotifier()
|
|
26 |
//
|
|
27 |
// Constructor
|
|
28 |
//
|
|
29 |
{
|
|
30 |
|
|
31 |
/**************************************************************************
|
|
32 |
** CHANGE THE NEXT LINE IF YOU ADD A NEW ECHANGES... TO THE NOTIFIER **
|
|
33 |
** But not for EChangesLowMemory! **
|
|
34 |
**************************************************************************/
|
|
35 |
iChanges=EChangesLocale|EChangesMidnightCrossover|EChangesThreadDeath|EChangesPowerStatus|EChangesSystemTime|EChangesFreeMemory|EChangesOutOfMemory|EChangesThrashLevel;
|
|
36 |
//iRequest=NULL;
|
|
37 |
//iThread=NULL;
|
|
38 |
}
|
|
39 |
|
|
40 |
TInt DChangeNotifier::Create()
|
|
41 |
{
|
|
42 |
return Kern::CreateClientRequest(iRequest);
|
|
43 |
}
|
|
44 |
|
|
45 |
DChangeNotifier::~DChangeNotifier()
|
|
46 |
//
|
|
47 |
// Destructor
|
|
48 |
// Enter and leave with system unlocked.
|
|
49 |
//
|
|
50 |
{
|
|
51 |
NKern::LockSystem();
|
|
52 |
if (iThread)
|
|
53 |
Complete(KErrGeneral);
|
|
54 |
else
|
|
55 |
NKern::UnlockSystem();
|
|
56 |
Kern::DestroyClientRequest(iRequest);
|
|
57 |
}
|
|
58 |
|
|
59 |
|
|
60 |
void DChangeNotifier::Complete(TInt aResult)
|
|
61 |
//
|
|
62 |
// Complete the client the result code specified
|
|
63 |
// Enter with system locked, leave with system unlocked.
|
|
64 |
//
|
|
65 |
{
|
|
66 |
DThread* t = iThread;
|
|
67 |
iChanges = 0;
|
|
68 |
iThread = NULL;
|
|
69 |
NKern::ThreadEnterCS();
|
|
70 |
Kern::QueueRequestComplete(t, iRequest,aResult);
|
|
71 |
NKern::UnlockSystem();
|
|
72 |
t->Close(NULL);
|
|
73 |
NKern::ThreadLeaveCS();
|
|
74 |
}
|
|
75 |
|
|
76 |
|
|
77 |
void DChangeNotifier::Notify(TUint aChanges)
|
|
78 |
//
|
|
79 |
// Complete logged on status, or save up change.
|
|
80 |
// Enter with system locked, leave with system unlocked.
|
|
81 |
//
|
|
82 |
{
|
|
83 |
__KTRACE_OPT(KEXEC,Kern::Printf("DChangeNotifier::Notify %08x %x",this,aChanges));
|
|
84 |
iChanges|=aChanges;
|
|
85 |
if (iRequest->IsReady())
|
|
86 |
Complete(iChanges);
|
|
87 |
else
|
|
88 |
NKern::UnlockSystem();
|
|
89 |
}
|
|
90 |
|
|
91 |
|
|
92 |
TInt DChangeNotifier::Logon(TRequestStatus& aStatus, DThread* aThread)
|
|
93 |
//
|
|
94 |
// Logon to notifier.
|
|
95 |
// Enter with system locked, return with system unlocked.
|
|
96 |
//
|
|
97 |
{
|
|
98 |
if (iRequest->SetStatus(&aStatus) != KErrNone)
|
|
99 |
return KErrInUse;
|
|
100 |
iThread=aThread;
|
|
101 |
iThread->CheckedOpen();
|
|
102 |
if (iChanges!=0)
|
|
103 |
Complete(iChanges);
|
|
104 |
else
|
|
105 |
NKern::UnlockSystem();
|
|
106 |
return KErrNone;
|
|
107 |
}
|
|
108 |
|
|
109 |
TInt DChangeNotifier::LogonCancel(DThread* aThread)
|
|
110 |
//
|
|
111 |
// Logoff from notifier.
|
|
112 |
// Enter with system locked, return with system unlocked.
|
|
113 |
//
|
|
114 |
{
|
|
115 |
if (iRequest->IsReady() && iThread==aThread)
|
|
116 |
{
|
|
117 |
Complete(KErrCancel);
|
|
118 |
return KErrNone;
|
|
119 |
}
|
|
120 |
NKern::UnlockSystem();
|
|
121 |
return KErrGeneral;
|
|
122 |
}
|
|
123 |
|
|
124 |
TInt ExecHandler::ChangeNotifierCreate(TOwnerType aType)
|
|
125 |
//
|
|
126 |
// Create a change notifier
|
|
127 |
//
|
|
128 |
{
|
|
129 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ChangeNotifierCreate"));
|
|
130 |
NKern::ThreadEnterCS();
|
|
131 |
DChangeNotifier* pN=new DChangeNotifier;
|
|
132 |
TInt r=KErrNoMemory;
|
|
133 |
if (pN)
|
|
134 |
{
|
|
135 |
r=pN->Create();
|
|
136 |
if (r==KErrNone)
|
|
137 |
r=K::AddObject(pN,EChangeNotifier);
|
|
138 |
if (r==KErrNone)
|
|
139 |
r=K::MakeHandle(aType,pN);
|
|
140 |
if (r<KErrNone)
|
|
141 |
pN->Close(NULL);
|
|
142 |
}
|
|
143 |
NKern::ThreadLeaveCS();
|
|
144 |
return r;
|
|
145 |
}
|
|
146 |
|
|
147 |
TInt ExecHandler::ChangeNotifierLogon(DChangeNotifier* aChangeNotifier, TRequestStatus& aStatus)
|
|
148 |
//
|
|
149 |
// Enter with system locked, return with system unlocked.
|
|
150 |
//
|
|
151 |
{
|
|
152 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ChangeNotifierLogon"));
|
|
153 |
return aChangeNotifier->Logon(aStatus, TheCurrentThread);
|
|
154 |
}
|
|
155 |
|
|
156 |
TInt ExecHandler::ChangeNotifierLogoff(DChangeNotifier* aChangeNotifier)
|
|
157 |
//
|
|
158 |
// Enter with system locked, return with system unlocked.
|
|
159 |
//
|
|
160 |
{
|
|
161 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ChangeNotifierLogoff"));
|
|
162 |
return aChangeNotifier->LogonCancel(TheCurrentThread);
|
|
163 |
}
|
|
164 |
|
|
165 |
|
|
166 |
/** Synchronously notifies all change notifiers of a set of events.
|
|
167 |
|
|
168 |
This call causes all DChangeNotifier objects to record the events indicated
|
|
169 |
and to signal their owning thread if it is currently logged on.
|
|
170 |
|
|
171 |
@param aChanges The mask of events to be notified (TChanges enumeration)
|
|
172 |
|
|
173 |
@pre No fast mutex can be held
|
|
174 |
@pre Calling thread must be in a critical section
|
|
175 |
@pre Call in a thread context
|
|
176 |
@pre Kernel must be unlocked
|
|
177 |
@pre interrupts enabled
|
|
178 |
|
|
179 |
@see Kern::AsyncNotifyChanges()
|
|
180 |
@see TChanges
|
|
181 |
*/
|
|
182 |
EXPORT_C void Kern::NotifyChanges(TUint aChanges)
|
|
183 |
{
|
|
184 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::NotifyChanges");
|
|
185 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Kern::NotifyChanges %x",aChanges));
|
|
186 |
DObjectCon& chnot=*K::Containers[EChangeNotifier];
|
|
187 |
chnot.Wait();
|
|
188 |
TInt c=chnot.Count();
|
|
189 |
TInt i;
|
|
190 |
for(i=0; i<c; i++)
|
|
191 |
{
|
|
192 |
DChangeNotifier* pN=(DChangeNotifier*)chnot[i];
|
|
193 |
if (pN->Open() == KErrNone)
|
|
194 |
{
|
|
195 |
NKern::LockSystem();
|
|
196 |
pN->Notify(aChanges);
|
|
197 |
pN->AsyncClose();
|
|
198 |
}
|
|
199 |
}
|
|
200 |
chnot.Signal();
|
|
201 |
}
|
|
202 |
|
|
203 |
//
|
|
204 |
// Thread Watchers
|
|
205 |
//
|
|
206 |
|
|
207 |
TInt DUndertaker::Create()
|
|
208 |
{
|
|
209 |
return Kern::CreateClientDataRequest(iRequest);
|
|
210 |
}
|
|
211 |
|
|
212 |
DUndertaker::~DUndertaker()
|
|
213 |
//
|
|
214 |
// Destructor
|
|
215 |
// Enter and return with system unlocked
|
|
216 |
//
|
|
217 |
{
|
|
218 |
if (iRequest)
|
|
219 |
{
|
|
220 |
NKern::LockSystem();
|
|
221 |
Complete(KErrGeneral,NULL);
|
|
222 |
Kern::DestroyClientRequest(iRequest);
|
|
223 |
}
|
|
224 |
}
|
|
225 |
|
|
226 |
void DUndertaker::Complete(TInt aValue, DThread* aDeadThread)
|
|
227 |
//
|
|
228 |
// Complete and delete from queue
|
|
229 |
// Enter with system locked and, if aDeadThread!=NULL, calling thread in critical section
|
|
230 |
// Return with system unlocked.
|
|
231 |
// Must be careful to ensure only one completion can occur
|
|
232 |
//
|
|
233 |
{
|
|
234 |
if (!iRequest->IsReady())
|
|
235 |
{
|
|
236 |
NKern::UnlockSystem();
|
|
237 |
return;
|
|
238 |
}
|
|
239 |
if (iOwningThread->iMState==DThread::EDead)
|
|
240 |
{
|
|
241 |
iOwningThread=NULL;
|
|
242 |
iRequest->Reset();
|
|
243 |
NKern::UnlockSystem();
|
|
244 |
return;
|
|
245 |
}
|
|
246 |
iRequest->Data()=0;
|
|
247 |
if (aDeadThread)
|
|
248 |
{
|
|
249 |
NKern::UnlockSystem();
|
|
250 |
// At this point, the undertaker could be logged on again here by another thread in the same
|
|
251 |
// process, but this will result in KErrInUse, as that thread can't know that the undertaker
|
|
252 |
// has been completed
|
|
253 |
iOwningThread->MakeHandleAndOpen(EOwnerThread,aDeadThread,iRequest->Data());
|
|
254 |
NKern::LockSystem();
|
|
255 |
}
|
|
256 |
DThread* t = iOwningThread;
|
|
257 |
iOwningThread = NULL;
|
|
258 |
Kern::QueueRequestComplete(t, iRequest,aValue);
|
|
259 |
NKern::UnlockSystem();
|
|
260 |
}
|
|
261 |
|
|
262 |
void DUndertaker::Notify(DThread* aDeadThread)
|
|
263 |
//
|
|
264 |
// Complete the watcher if someone's logged on to it
|
|
265 |
// Enter and return with system unlocked
|
|
266 |
//
|
|
267 |
{
|
|
268 |
__KTRACE_OPT(KEXEC,Kern::Printf("DUndertaker::Notify %08x",this));
|
|
269 |
NKern::LockSystem();
|
|
270 |
Complete(KErrDied,aDeadThread);
|
|
271 |
}
|
|
272 |
|
|
273 |
TInt DUndertaker::Logon(TRequestStatus* aStatus, TInt* aDeadThreadHandle)
|
|
274 |
//
|
|
275 |
// Logon to thread watcher
|
|
276 |
// Enter and return with system locked
|
|
277 |
//
|
|
278 |
{
|
|
279 |
if (iRequest->SetStatus(aStatus) != KErrNone)
|
|
280 |
return KErrInUse;
|
|
281 |
iRequest->SetDestPtr(aDeadThreadHandle);
|
|
282 |
iOwningThread=TheCurrentThread;
|
|
283 |
return KErrNone;
|
|
284 |
}
|
|
285 |
|
|
286 |
TInt DUndertaker::LogonCancel()
|
|
287 |
//
|
|
288 |
// Logoff from thread watcher
|
|
289 |
// Enter with system locked, return with system unlocked.
|
|
290 |
//
|
|
291 |
{
|
|
292 |
if (iOwningThread==TheCurrentThread)
|
|
293 |
{
|
|
294 |
Complete(KErrCancel,NULL);
|
|
295 |
return KErrNone;
|
|
296 |
}
|
|
297 |
NKern::UnlockSystem();
|
|
298 |
return KErrGeneral;
|
|
299 |
}
|
|
300 |
|
|
301 |
TInt ExecHandler::UndertakerCreate(TOwnerType aType)
|
|
302 |
//
|
|
303 |
// Create a thread watcher
|
|
304 |
//
|
|
305 |
{
|
|
306 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::UndertakerCreate"));
|
|
307 |
NKern::ThreadEnterCS();
|
|
308 |
DUndertaker* pU=new DUndertaker;
|
|
309 |
TInt r=KErrNoMemory;
|
|
310 |
if (pU)
|
|
311 |
{
|
|
312 |
r=pU->Create();
|
|
313 |
if (r==KErrNone)
|
|
314 |
r=K::AddObject(pU,EUndertaker);
|
|
315 |
if (r==KErrNone)
|
|
316 |
r=K::MakeHandle(aType,pU);
|
|
317 |
if (r<KErrNone)
|
|
318 |
pU->Close(NULL);
|
|
319 |
}
|
|
320 |
NKern::ThreadLeaveCS();
|
|
321 |
return r;
|
|
322 |
}
|
|
323 |
|
|
324 |
TInt ExecHandler::UndertakerLogon(DUndertaker *anUndertaker, TRequestStatus& aStatus, TInt* aDeadThreadHandle)
|
|
325 |
//
|
|
326 |
// Enter and return with system locked.
|
|
327 |
//
|
|
328 |
{
|
|
329 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::UndertakerLogon"));
|
|
330 |
return anUndertaker->Logon(&aStatus,aDeadThreadHandle);
|
|
331 |
}
|
|
332 |
|
|
333 |
TInt ExecHandler::UndertakerLogonCancel(DUndertaker* anUndertaker)
|
|
334 |
//
|
|
335 |
// Enter with system locked, return with system unlocked.
|
|
336 |
//
|
|
337 |
{
|
|
338 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::UndertakerLogonCancel"));
|
|
339 |
return anUndertaker->LogonCancel();
|
|
340 |
}
|
|
341 |
|
|
342 |
|
|
343 |
/** Notifies all undertakers of a thread death.
|
|
344 |
|
|
345 |
This call causes all DUndertaker objects to signal their owning thread
|
|
346 |
if it is currently logged on. A handle to the dead thread will be created
|
|
347 |
for each owning thread signalled.
|
|
348 |
|
|
349 |
@param aDeadThread Pointer to the deceased thread.
|
|
350 |
|
|
351 |
@pre No fast mutex can be held.
|
|
352 |
@pre Calling thread must be in a critical section.
|
|
353 |
@pre Call in a thread context.
|
|
354 |
@pre Kernel must be unlocked
|
|
355 |
@pre interrupts enabled
|
|
356 |
|
|
357 |
@internalTechnology
|
|
358 |
*/
|
|
359 |
EXPORT_C void Kern::NotifyThreadDeath(DThread* aDeadThread)
|
|
360 |
{
|
|
361 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::NotifyThreadDeath");
|
|
362 |
__KTRACE_OPT(KTHREAD,Kern::Printf("Kern::NotifyThreadDeath %O",aDeadThread));
|
|
363 |
DObjectCon& und=*K::Containers[EUndertaker];
|
|
364 |
RObjectIx::Wait();
|
|
365 |
und.Wait();
|
|
366 |
TInt c=und.Count();
|
|
367 |
TInt i;
|
|
368 |
for (i=0; i<c; i++)
|
|
369 |
{
|
|
370 |
DUndertaker* pU=(DUndertaker*)und[i];
|
|
371 |
if (pU->Open() == KErrNone)
|
|
372 |
{
|
|
373 |
pU->Notify(aDeadThread);
|
|
374 |
pU->AsyncClose();
|
|
375 |
}
|
|
376 |
}
|
|
377 |
und.Signal();
|
|
378 |
RObjectIx::Signal();
|
|
379 |
}
|
|
380 |
|
|
381 |
|
|
382 |
|