Develop and Download Open Source Software

Browse Subversion Repository

Contents of /WcsAPI/Plugin.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (show annotations) (download) (as text)
Wed Feb 10 18:21:00 2010 UTC (14 years, 2 months ago) by sho1get
File MIME type: text/x-c++src
File size: 6093 byte(s)


1 #include "StdAfx.h"
2 #include "WcsAPI.h"
3
4 //////////////////////////////////////////////////////////////////////////
5 // Common
6
7 CPlugin::CPlugin() :
8 m_pAPI(NULL),
9 m_fMaster(FALSE),
10 m_fSystem(FALSE),
11 m_fRunning(FALSE)
12 {
13 ZeroMemory(&m_SlavePlugin, sizeof(m_SlavePlugin));
14 ZeroMemory(&m_CalcSize, sizeof(m_CalcSize));
15 }
16
17 CPlugin::~CPlugin()
18 {
19 }
20
21 void CPlugin::GetPluginInfo(LPPLUGIN_INFO lpInfo)
22 {
23 CopyMemory(lpInfo, &m_PlgInfo, sizeof(PLUGIN_INFO));
24 }
25
26 BOOL CPlugin::StartPlugin(const CByteArray &arg)
27 {
28 CSingleLock sl(&m_ThdLock, TRUE);
29 DWORD dwCount;
30
31 m_Argument.RemoveAll();
32 m_Argument.Copy(arg);
33 m_fRunning = TRUE;
34
35 if (m_fMaster)
36 {
37 m_MasterPlugin.wNodeID = m_MasterPlugin.list[0].wNodeID;
38 m_MasterPlugin.dwAddress = m_MasterPlugin.list[0].dwAddress;
39 m_MasterPlugin.dwProcessors = m_fSystem ? 1 : m_MasterPlugin.list[0].dwProcessors;
40 dwCount = m_MasterPlugin.dwProcessors;
41
42 if (!m_fSystem)
43 {
44 m_Reportlist.RemoveAll();
45 m_Reportlist.SetSize(dwCount);
46 }
47 }
48 else
49 {
50 dwCount = m_SlavePlugin.dwProcessors;
51 }
52
53 m_ThdCount.dwCount = dwCount;
54 m_ThdCount.dwFinish = 0;
55
56 m_ThdList.SetSize(dwCount);
57 m_ThdIDList.SetSize(dwCount);
58 m_PlgPrmList.SetSize(dwCount);
59
60 for (UINT i = 0; i < dwCount; i++)
61 {
62 m_PlgPrmList[i].obj = this;
63 m_PlgPrmList[i].dwIndex = i;
64 m_PlgPrmList[i].dwAffinity = (0x0001 << i);
65 m_PlgPrmList[i].dwThdCount = dwCount;
66 m_PlgPrmList[i].fAlive = TRUE;
67 m_PlgPrmList[i].fSystem = m_fSystem;
68 m_PlgPrmList[i].arg.Copy(arg);
69
70 m_ThdList[i] = MC_BEGINTHREADEX(NULL,
71 0,
72 (m_fMaster ? doMasterTransaction : doSlaveTransaction),
73 &m_PlgPrmList[i],
74 CREATE_SUSPENDED,
75 &m_ThdIDList[i]);
76 }
77
78 // Start thread
79 for (UINT i = 0; i < dwCount; i++)
80 {
81 ResumeThread(m_ThdList[i]);
82 }
83
84 for (UINT i = 0; i < dwCount; i++)
85 {
86 if (m_ThdList[i] == NULL)
87 {
88 return FALSE;
89 }
90 }
91
92 return TRUE;
93 }
94
95 void CPlugin::ThreadFinish()
96 {
97 ++m_ThdCount.dwFinish;
98
99 if (m_ThdCount.dwCount == m_ThdCount.dwFinish)
100 {
101 if (m_fMaster)
102 {
103 MasterFinalize();
104 }
105 else
106 {
107 SlaveFinalize();
108 }
109
110 CloseHandleList();
111 m_fRunning = FALSE;
112 }
113 }
114
115 BOOL CPlugin::CancelPlugin(DWORD dwTimeout)
116 {
117 CSingleLock sl(&m_ThdLock, TRUE);
118 UINT nSize = m_ThdIDList.GetSize();
119 DWORD dwRet;
120
121 if (nSize > 0)
122 {
123 m_fRunning = FALSE;
124 dwRet = WaitForMultipleObjects(nSize, m_ThdList.GetData(), TRUE, dwTimeout);
125
126 if (dwRet != WAIT_FAILED)
127 {
128 m_ThdList.RemoveAll();
129 m_ThdIDList.RemoveAll();
130 m_PlgPrmList.RemoveAll();
131 return TRUE;
132 }
133 }
134
135 return FALSE;
136 }
137
138 //////////////////////////////////////////////////////////////////////////
139 // Master
140
141 DWORD CPlugin::MasterTransaction(LPVOID lpParams)
142 {
143 if (!m_fSystem)
144 {
145 // Single mode
146 ThreadFinish();
147 }
148
149 return 0;
150 }
151
152 BOOL CPlugin::doCreateWindow(const MASTER_PLUGIN &init)
153 {
154 m_CalcCount.dwAllCount = 0;
155 m_CalcCount.dwFinCount = 0;
156 m_MasterPlugin.csWndName = init.csWndName;
157 m_MasterPlugin.bFront = init.bFront;
158 m_MasterPlugin.list.Copy(init.list);
159 m_fMaster = TRUE;
160 m_fSystem = init.bSystem;
161 m_NodeInitList.RemoveAll();
162 CloseHandleList();
163
164 CString csName = m_MasterPlugin.csWndName;
165 BOOL bFront = m_MasterPlugin.bFront;
166 int cx = m_CalcSize.nWidth;
167 int cy = m_CalcSize.nHeight;
168
169 if (API_CreateGraphWindow(csName, cx, cy, bFront))
170 {
171 API_SetCenterWindow();
172 return TRUE;
173 }
174
175 return FALSE;
176 }
177
178 DWORD WINAPI CPlugin::doMasterTransaction(LPVOID lpParams)
179 {
180 // Master single mode
181 LPPLUGIN_PARAMS p = (LPPLUGIN_PARAMS)lpParams;
182 HANDLE hCurrent = GetCurrentThread();
183
184 // Set Affinity
185 if (SetThreadAffinityMask(hCurrent, p->dwAffinity) == 0)
186 {
187 TRACE2("***** ERROR: SetThreadAffinityMask(%d) Processor(%d)", GetLastError(), p->dwIndex);
188 return 0;
189 }
190 /*if (SetThreadIdealProcessor(hCurrent, p->dwAffinity) == -1)
191 {
192 TRACE2("***** ERROR: SetThreadIdealProcessor(%d) Processor(%d)", GetLastError(), p->dwIndex);
193 return 0;
194 }*/
195
196 return p->obj->MasterTransaction(lpParams);
197 }
198
199 //////////////////////////////////////////////////////////////////////////
200 // Slave
201
202 DWORD CPlugin::SlaveTransaction(LPVOID lpParams)
203 {
204 return ThreadFinish(), 0;
205 }
206
207 BOOL CPlugin::SlaveInitialize(const SLAVE_PLUGIN &init)
208 {
209 m_SlavePlugin = init;
210 m_fMaster = FALSE;
211 m_SlavePlugin.dbRuntime = 0.0;
212 CloseHandleList();
213 return TRUE;
214 }
215
216 DWORD WINAPI CPlugin::doSlaveTransaction(LPVOID lpParams)
217 {
218 LPPLUGIN_PARAMS p = (LPPLUGIN_PARAMS)lpParams;
219 HANDLE hCurrent = GetCurrentThread();
220
221 // Set Affinity
222 if (SetThreadAffinityMask(hCurrent, p->dwAffinity) == 0)
223 {
224 TRACE2("***** ERROR: SetThreadAffinityMask(%d) Processor(%d)", GetLastError(), p->dwIndex);
225 return 0;
226 }
227 /*if (SetThreadIdealProcessor(hCurrent, p->dwAffinity) == -1)
228 {
229 TRACE2("***** ERROR: SetThreadIdealProcessor(%d) Processor(%d)", GetLastError(), p->dwIndex);
230 return 0;
231 }*/
232
233 return p->obj->SlaveTransaction(lpParams);
234 }
235
236 void CPlugin::CloseHandleList(BOOL fWait, DWORD dwMilliseconds)
237 {
238 CSingleLock sl(&m_ThdLock, TRUE);
239 UINT nSize = m_ThdList.GetSize();
240
241 if (nSize > 0)
242 {
243 if (fWait)
244 {
245 for (UINT i = 0; i < nSize; i++)
246 {
247 m_PlgPrmList[i].fAlive = FALSE;
248 }
249
250 WaitForMultipleObjects(nSize, m_ThdList.GetData(), TRUE, dwMilliseconds);
251 }
252
253 for (UINT i = 0; i < nSize; i++)
254 {
255 CloseHandle(m_ThdList[i]);
256 }
257
258 m_ThdList.RemoveAll();
259 m_ThdIDList.RemoveAll();
260 m_PlgPrmList.RemoveAll();
261 }
262 }
263
264 void CPlugin::SetCalcReport(DWORD dwIndex, double dbRuntime)
265 {
266 UINT nSize = m_Reportlist.GetSize();
267
268 if (m_fMaster && !m_fSystem && (nSize > dwIndex))
269 {
270 m_Reportlist[dwIndex].wNodeID = m_MasterPlugin.wNodeID;
271 m_Reportlist[dwIndex].wSubID = static_cast<WORD>(dwIndex);
272 m_Reportlist[dwIndex].dwAddress = m_MasterPlugin.dwAddress;
273 m_Reportlist[dwIndex].dbRuntime = dbRuntime;
274 }
275 }
276
277 //////////////////////////////////////////////////////////////////////////

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26