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.17 2001/03/09 02:40:17 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, wxFrame* 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)
62 m_iNumThreads = theApp->getNumberCPU();
65 m_vecpThreads.reserve (m_iNumThreads);
66 for (int iThread = 0; iThread < m_iNumThreads; iThread++)
67 m_vecpThreads[iThread] = NULL;
71 BackgroundSupervisor::~BackgroundSupervisor()
74 wxCommandEvent doneEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_REMOVE);
75 doneEvent.SetClientData (this);
76 wxPostEvent (theApp->getBackgroundManager(), doneEvent);
79 m_pDocument->removeBackgroundSupervisor (this);
85 BackgroundSupervisor::deleteWorkers()
87 wxCriticalSectionLocker lock (m_critsectThreads);
88 if (m_bWorkersDeleted)
91 for (int i = 0; i < m_iNumThreads; i++)
93 m_vecpThreads[i]->Delete(); // send Destroy message to workers
95 while (m_iRunning > 0)
96 m_pMyThread->Sleep(50);
98 m_bWorkersDeleted = true;
102 BackgroundSupervisor::start()
104 int iBaseUnits = m_iTotalUnits / m_iNumThreads;
105 int iExtraUnits = m_iTotalUnits % m_iNumThreads;
107 for (int iThread = 0; iThread < m_iNumThreads; iThread++) {
108 int iNumUnits = iBaseUnits;
109 if (iThread < iExtraUnits)
111 m_vecpThreads[iThread] = createWorker (iThread, iStartUnit, iNumUnits);
112 if (! m_vecpThreads[iThread]) {
114 m_strFailMessage = "createWorker returned NULL [BackgroundSupervisor]";
117 if (m_vecpThreads[iThread]->Create () != wxTHREAD_NO_ERROR) {
119 m_strFailMessage = "Thread creation failed [BackgroundSupervisor]";
122 m_vecpThreads[iThread]->SetPriority (40);
123 iStartUnit += iNumUnits;
128 m_pTimer = new Timer;
130 std::string strLabel (m_strProcessTitle);
132 strLabel += m_pParentFrame->GetTitle();
135 wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_ADD);
136 addTaskEvent.SetString (strLabel.c_str());
137 addTaskEvent.SetInt (m_iTotalUnits);
138 addTaskEvent.SetClientData (this);
139 wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
142 m_pDocument->addBackgroundSupervisor (this);
144 m_iRunning = m_iNumThreads;
147 for (int i = 0; i < m_iNumThreads; i++)
148 m_vecpThreads[i]->Run();
154 BackgroundSupervisor::onCancel()
162 BackgroundSupervisor::onWorkerUnitTick ()
167 wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_UNIT_TICK);
168 addTaskEvent.SetInt (m_iUnitsDone - 1);
169 addTaskEvent.SetClientData (this);
170 wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
175 BackgroundSupervisor::onWorkerDone (int iThread)
177 wxCriticalSection critsectDone;
178 critsectDone.Enter();
183 if (theApp->getVerboseLogging()) {
185 msg.Printf("Background Supervisor: Thread finished. Remaining threads: %d\n", m_iRunning);
186 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
187 eventLog.SetString( msg );
188 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
192 critsectDone.Leave();
196 BackgroundSupervisor::onWorkerFail (int iThread, std::string strFailMessage)
199 wxCommandEvent eventLog( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
200 eventLog.SetString( strFailMessage.c_str() );
201 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
206 #endif // HAVE_WXTHREADS