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.13 2001/03/05 17:26:46 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, BackgroundProcessingDocument* 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_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 wxCommandEvent doneEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_REMOVE);
72 doneEvent.SetClientData (this);
73 wxPostEvent (theApp->getBackgroundManager(), doneEvent);
75 m_pDocument->removeBackgroundSupervisor (this);
81 BackgroundSupervisor::deleteWorkers()
83 wxCriticalSectionLocker lock (m_critsectThreads);
84 if (m_bWorkersDeleted)
87 for (int i = 0; i < m_iNumThreads; i++)
88 if (m_vecpThreads[i]) {
89 m_vecpThreads[i]->Delete(); // sends Destroy message to workers
92 while (m_iRunning > 0) {
93 m_pMyThread->Sleep(50);
96 m_bWorkersDeleted = true;
100 BackgroundSupervisor::start()
102 int iBaseUnits = m_iTotalUnits / m_iNumThreads;
103 int iExtraUnits = m_iTotalUnits % m_iNumThreads;
105 for (int iThread = 0; iThread < m_iNumThreads; iThread++) {
106 int iNumUnits = iBaseUnits;
107 if (iThread < iExtraUnits)
109 m_vecpThreads[iThread] = createWorker (iThread, iStartUnit, iNumUnits);
110 if (! m_vecpThreads[iThread]) {
112 m_strFailMessage = "createWorker returned NULL [BackgroundSupervisor]";
115 if (m_vecpThreads[iThread]->Create () != wxTHREAD_NO_ERROR) {
117 m_strFailMessage = "Thread creation failed [BackgroundSupervisor]";
120 m_vecpThreads[iThread]->SetPriority (40);
121 iStartUnit += iNumUnits;
126 m_pTimer = new Timer;
128 std::string strLabel (m_strProcessTitle);
130 strLabel += m_pParentFrame->GetTitle();
131 wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_ADD);
132 addTaskEvent.SetString (strLabel.c_str());
133 addTaskEvent.SetInt (m_iTotalUnits);
134 addTaskEvent.SetClientData (this);
135 wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
137 m_pDocument->addBackgroundSupervisor (this);
139 m_iRunning = m_iNumThreads;
142 for (int i = 0; i < m_iNumThreads; i++)
143 m_vecpThreads[i]->Run();
149 BackgroundSupervisor::onCancel()
157 BackgroundSupervisor::onWorkerUnitTick ()
162 if (theApp->getVerboseLogging())
163 *theApp->getLog() << "Units done: " << static_cast<int>(m_iUnitsDone) <<"\n";
166 wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_UNIT_TICK);
167 addTaskEvent.SetInt (m_iUnitsDone - 1);
168 addTaskEvent.SetClientData (this);
169 wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
173 BackgroundSupervisor::onWorkerDone (int iThread)
175 wxCriticalSection critsectDone;
176 critsectDone.Enter();
179 wxASSERT (m_iRunning >= 0);
182 if (theApp->getVerboseLogging()) {
184 msg.Printf("Background Supervisor: Thread finished. Remaining threads: %d\n", m_iRunning);
185 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
186 eventLog.SetString( msg );
187 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
191 critsectDone.Leave();
195 BackgroundSupervisor::onWorkerFail (int iThread, std::string strFailMessage)
198 wxCommandEvent eventLog( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
199 eventLog.SetString( strFailMessage.c_str() );
200 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
205 #endif // HAVE_WXTHREADS