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.20 2002/10/24 20:09:55 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"
45 ////////////////////////////////////////////////////////////////////////////
47 // Class BackgroundSupervisor -- An event handler run by a SupervisorThread
49 ////////////////////////////////////////////////////////////////////////////
51 IMPLEMENT_DYNAMIC_CLASS(BackgroundSupervisor, wxEvtHandler)
52 BEGIN_EVENT_TABLE(BackgroundSupervisor, BackgroundSupervisor)
57 BackgroundSupervisor::BackgroundSupervisor (SupervisorThread* pMyThread, wxWindow* pParentFrame, BackgroundProcessingDocument* pDocument, const char* const pszProcessTitle, int iTotalUnits)
58 : wxEvtHandler(), m_pMyThread(pMyThread), m_pParentFrame(pParentFrame), m_pDocument(pDocument), m_strProcessTitle(pszProcessTitle),
59 m_iTotalUnits(iTotalUnits), m_iNumThreads(0), m_bDone(false), m_bFail(false), m_bCancelled(false), m_iRunning(0),
60 m_pTimer(NULL), m_bWorkersDeleted(false), m_bBackgroundManagerAdded(false)
62 m_iNumThreads = theApp->getNumberCPU();
65 m_vecpThreads.resize (m_iNumThreads);
66 for (int iThread = 0; iThread < m_iNumThreads; iThread++)
67 m_vecpThreads[iThread] = NULL;
71 BackgroundSupervisor::~BackgroundSupervisor()
73 m_pDocument->removeBackgroundSupervisor (this);
79 BackgroundSupervisor::deleteWorkers()
81 wxCriticalSectionLocker lock (m_critsectThreads);
82 if (m_bWorkersDeleted)
85 for (int i = 0; i < m_iNumThreads; i++)
87 m_vecpThreads[i]->Delete(); // send Destroy message to workers
90 wxCommandEvent doneEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_REMOVE);
91 doneEvent.SetClientData (this);
92 wxPostEvent (theApp->getBackgroundManager(), doneEvent);
95 while (m_iRunning > 0 || m_bBackgroundManagerAdded)
96 m_pMyThread->Sleep(50);
98 m_bWorkersDeleted = true;
102 BackgroundSupervisor::ackRemoveBackgroundManager()
104 m_bBackgroundManagerAdded = false;
108 BackgroundSupervisor::start()
110 int iBaseUnits = m_iTotalUnits / m_iNumThreads;
111 int iExtraUnits = m_iTotalUnits % m_iNumThreads;
113 for (int iThread = 0; iThread < m_iNumThreads; iThread++) {
114 int iNumUnits = iBaseUnits;
115 if (iThread < iExtraUnits)
117 m_vecpThreads[iThread] = createWorker (iThread, iStartUnit, iNumUnits);
118 if (! m_vecpThreads[iThread]) {
120 m_strFailMessage = "createWorker returned NULL [BackgroundSupervisor]";
123 if (m_vecpThreads[iThread]->Create () != wxTHREAD_NO_ERROR) {
125 m_strFailMessage = "Thread creation failed [BackgroundSupervisor]";
128 m_vecpThreads[iThread]->SetPriority (40);
129 iStartUnit += iNumUnits;
134 m_pTimer = new Timer;
136 std::string strLabel (m_strProcessTitle);
138 strLabel += m_pParentFrame->GetTitle();
141 wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_ADD);
142 addTaskEvent.SetString (strLabel.c_str());
143 addTaskEvent.SetInt (m_iTotalUnits);
144 addTaskEvent.SetClientData (this);
145 wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
148 m_pDocument->addBackgroundSupervisor (this);
149 m_bBackgroundManagerAdded = true;
151 m_iRunning = m_iNumThreads;
154 for (int i = 0; i < m_iNumThreads; i++)
155 m_vecpThreads[i]->Run();
161 BackgroundSupervisor::onCancel()
169 BackgroundSupervisor::onWorkerUnitTick ()
174 wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_UNIT_TICK);
175 addTaskEvent.SetInt (m_iUnitsDone - 1);
176 addTaskEvent.SetClientData (this);
177 wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
182 BackgroundSupervisor::onWorkerDone (int iThread)
184 wxCriticalSection critsectDone;
185 critsectDone.Enter();
190 if (theApp->getVerboseLogging()) {
192 msg.Printf("Background Supervisor: Thread finished. Remaining threads: %d\n", m_iRunning);
193 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
194 eventLog.SetString( msg );
195 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
199 critsectDone.Leave();
203 BackgroundSupervisor::onWorkerFail (int iThread, std::string strFailMessage)
206 wxCommandEvent eventLog( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
207 eventLog.SetString( strFailMessage.c_str() );
208 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
213 #endif // HAVE_WXTHREADS