|
1 // wsp_scheduler.cpp |
|
2 // |
|
3 // Copyright (c) 2002 - 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "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 // Accenture - Initial contribution |
|
11 // |
|
12 |
|
13 |
|
14 #include "wsp_scheduler.h" |
|
15 #include "wsp_panic.h" |
|
16 #include <windows.h> |
|
17 #include <emulator.h> |
|
18 |
|
19 CWin32Scheduler* CWin32Scheduler::NewL() |
|
20 { |
|
21 return new(ELeave) CWin32Scheduler(); |
|
22 } |
|
23 |
|
24 CWin32Scheduler::~CWin32Scheduler() |
|
25 { |
|
26 __ASSERT_DEBUG(iActiveQue.IsEmpty(), Panic(EWinSockPrtActiveQueueNotEmpty)); |
|
27 } |
|
28 |
|
29 void CWin32Scheduler::Add(CWin32ActiveObject& aActiveObject) |
|
30 { |
|
31 iActiveQue.AddLast(aActiveObject); |
|
32 RebuildEventList(); |
|
33 } |
|
34 |
|
35 void CWin32Scheduler::Remove(CWin32ActiveObject& aActiveObject) |
|
36 { |
|
37 iActiveQue.Remove(aActiveObject); |
|
38 RebuildEventList(); |
|
39 } |
|
40 |
|
41 TBool CWin32Scheduler::IsPresent(CWin32ActiveObject& aActiveObject) const |
|
42 { |
|
43 TSglQueIter<CWin32ActiveObject> iterator(const_cast<CWin32Scheduler*>(this)->iActiveQue); |
|
44 iterator.SetToFirst(); |
|
45 CWin32ActiveObject* thisActiveObject = NULL; |
|
46 while ((thisActiveObject = iterator++) != NULL) |
|
47 { |
|
48 if (thisActiveObject == &aActiveObject) |
|
49 { |
|
50 return ETrue; |
|
51 } |
|
52 } |
|
53 return EFalse; |
|
54 } |
|
55 |
|
56 void CWin32Scheduler::Start() |
|
57 { |
|
58 while (!iStopping) |
|
59 { |
|
60 Emulator::Escape(); |
|
61 DWORD ret = WaitForMultipleObjectsEx(iNumEvents, iEventList, FALSE, INFINITE, TRUE); |
|
62 Emulator::Reenter(); |
|
63 if ((ret >= WAIT_OBJECT_0) && (ret < (WAIT_OBJECT_0 + iNumEvents))) |
|
64 { |
|
65 iActiveObjectList[ret - WAIT_OBJECT_0]->Run(); |
|
66 } |
|
67 else if (ret == WAIT_IO_COMPLETION) |
|
68 { |
|
69 // TODO: Safe to ignore? |
|
70 } |
|
71 else |
|
72 { |
|
73 __ASSERT_DEBUG(EFalse, Panic(EWinSockPrtUnexpectedWaitError)); |
|
74 } |
|
75 } |
|
76 } |
|
77 |
|
78 void CWin32Scheduler::Stop() |
|
79 { |
|
80 iStopping = ETrue; |
|
81 } |
|
82 |
|
83 CWin32Scheduler::CWin32Scheduler() |
|
84 : iActiveQue(_FOFF(CWin32ActiveObject, iLink)) |
|
85 { |
|
86 } |
|
87 |
|
88 void CWin32Scheduler::RebuildEventList() |
|
89 { |
|
90 iNumEvents = 0; |
|
91 TSglQueIter<CWin32ActiveObject> iterator(iActiveQue); |
|
92 iterator.SetToFirst(); |
|
93 CWin32ActiveObject* thisActiveObject = NULL; |
|
94 while ((thisActiveObject = iterator++) != NULL) |
|
95 { |
|
96 iActiveObjectList[iNumEvents] = thisActiveObject; |
|
97 iEventList[iNumEvents++] = thisActiveObject->iEvent; |
|
98 __ASSERT_DEBUG(iNumEvents <= MAXIMUM_WAIT_OBJECTS, Panic(EWinSockPrtTooManyWaitObjects)); |
|
99 } |
|
100 } |