1 /*****************************************************************************
4 ** Name: BackgroundSupr.cpp
5 ** Purpose: Background Supervisor classes
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: February 2001
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2001 Kevin Rosenberg
12 ** $Id: backgroundsupr.cpp,v 1.12 2001/03/04 22:30:19 kevin Exp $
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
28 #include "wx/wxprec.h"
38 #include "backgroundsupr.h"
39 #include "backgroundmgr.h"
43 ////////////////////////////////////////////////////////////////////////////
45 // Class BackgroundSupervisor -- An event handler run by a SupervisorThread
47 ////////////////////////////////////////////////////////////////////////////
49 IMPLEMENT_DYNAMIC_CLASS(BackgroundSupervisor, wxEvtHandler)
50 BEGIN_EVENT_TABLE(BackgroundSupervisor, BackgroundSupervisor)
55 BackgroundSupervisor::BackgroundSupervisor (SupervisorThread* pMyThread, wxFrame* pParentFrame, wxDocument* pDocument, const char* const pszProcessTitle, int iTotalUnits)
56 : wxEvtHandler(), m_pMyThread(pMyThread), m_pParentFrame(pParentFrame), m_pDocument(pDocument), m_strProcessTitle(pszProcessTitle),
57 m_iTotalUnits(iTotalUnits), m_iNumThreads(0), m_bDone(false), m_bFail(false), m_bCancelled(false), m_iRunning(0),
58 m_pTimer(NULL), m_bBackgroundTaskAdded(false), m_bWorkersDeleted(false)
60 m_iNumThreads = theApp->getNumberCPU();
63 m_vecpThreads.reserve (m_iNumThreads);
64 for (int iThread = 0; iThread < m_iNumThreads; iThread++)
65 m_vecpThreads[iThread] = NULL;
69 BackgroundSupervisor::~BackgroundSupervisor()
71 if (m_bBackgroundTaskAdded) {
72 wxCommandEvent doneEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_REMOVE);
73 doneEvent.SetClientData (this);
74 wxPostEvent (theApp->getBackgroundManager(), doneEvent);
75 wxPostEvent (m_pDocument, doneEvent);
78 while (m_bBackgroundTaskAdded) {
79 m_pMyThread->Sleep(50);
86 BackgroundSupervisor::deleteWorkers()
88 wxCriticalSectionLocker lock (m_critsectThreads);
89 if (m_bWorkersDeleted)
92 for (int i = 0; i < m_iNumThreads; i++)
93 if (m_vecpThreads[i]) {
94 m_vecpThreads[i]->Delete(); // sends Destroy message to workers
97 while (m_iRunning > 0) {
98 m_pMyThread->Sleep(50);
101 m_bWorkersDeleted = true;
105 BackgroundSupervisor::start()
107 int iBaseUnits = m_iTotalUnits / m_iNumThreads;
108 int iExtraUnits = m_iTotalUnits % m_iNumThreads;
110 for (int iThread = 0; iThread < m_iNumThreads; iThread++) {
111 int iNumUnits = iBaseUnits;
112 if (iThread < iExtraUnits)
114 m_vecpThreads[iThread] = createWorker (iThread, iStartUnit, iNumUnits);
115 if (! m_vecpThreads[iThread]) {
117 m_strFailMessage = "createWorker returned NULL [BackgroundSupervisor]";
120 if (m_vecpThreads[iThread]->Create () != wxTHREAD_NO_ERROR) {
122 m_strFailMessage = "Thread creation failed [BackgroundSupervisor]";
125 m_vecpThreads[iThread]->SetPriority (40);
126 iStartUnit += iNumUnits;
131 m_pTimer = new Timer;
133 std::string strLabel (m_strProcessTitle);
135 strLabel += m_pParentFrame->GetTitle();
136 wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_ADD);
137 addTaskEvent.SetString (strLabel.c_str());
138 addTaskEvent.SetInt (m_iTotalUnits);
139 addTaskEvent.SetClientData (this);
140 wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
141 wxPostEvent (m_pDocument, addTaskEvent);
142 m_bBackgroundTaskAdded = true;
144 m_iRunning = m_iNumThreads;
147 for (int i = 0; i < m_iNumThreads; i++)
148 m_vecpThreads[i]->Run();
154 BackgroundSupervisor::onCancel()
161 BackgroundSupervisor::onAckDocumentRemove()
163 m_bBackgroundTaskAdded = false;
168 BackgroundSupervisor::onWorkerUnitTick ()
173 if (theApp->getVerboseLogging())
174 *theApp->getLog() << "Units done: " << static_cast<int>(m_iUnitsDone) <<"\n";
177 wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_UNIT_TICK);
178 addTaskEvent.SetInt (m_iUnitsDone - 1);
179 addTaskEvent.SetClientData (this);
180 wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
184 BackgroundSupervisor::onWorkerDone (int iThread)
186 wxCriticalSection critsectDone;
187 critsectDone.Enter();
190 wxASSERT (m_iRunning >= 0);
193 if (theApp->getVerboseLogging()) {
195 msg.Printf("Background Supervisor: Thread finished. Remaining threads: %d\n", m_iRunning);
196 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
197 eventLog.SetString( msg );
198 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
202 critsectDone.Leave();
206 BackgroundSupervisor::onWorkerFail (int iThread, std::string strFailMessage)
209 wxCommandEvent eventLog( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
210 eventLog.SetString( strFailMessage.c_str() );
211 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
216 #endif // HAVE_WXTHREADS