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-2009 Kevin Rosenberg
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License (version 2) as
14 ** published by the Free Software Foundation.
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ******************************************************************************/
26 #include "wx/wxprec.h"
32 // pragma line required for Fedora 4 and wxWin 2.4.2
33 #pragma implementation "timer.h"
39 #include "backgroundsupr.h"
40 #include "backgroundmgr.h"
46 ////////////////////////////////////////////////////////////////////////////
48 // Class BackgroundSupervisor -- An event handler run by a SupervisorThread
50 ////////////////////////////////////////////////////////////////////////////
52 IMPLEMENT_DYNAMIC_CLASS(BackgroundSupervisor, wxEvtHandler)
53 BEGIN_EVENT_TABLE(BackgroundSupervisor, BackgroundSupervisor)
58 BackgroundSupervisor::BackgroundSupervisor (SupervisorThread* pMyThread, wxWindow* pParentFrame, BackgroundProcessingDocument* pDocument, wxChar const* pszProcessTitle, int iTotalUnits)
59 : wxEvtHandler(), m_pMyThread(pMyThread), m_pParentFrame(pParentFrame), m_pDocument(pDocument), m_strProcessTitle(pszProcessTitle),
60 m_iTotalUnits(iTotalUnits), m_iNumThreads(0), m_bDone(false), m_bFail(false), m_bCancelled(false), m_iRunning(0),
61 m_pTimer(NULL), m_bWorkersDeleted(false), m_bBackgroundManagerAdded(false)
63 m_iNumThreads = theApp->getNumberCPU();
66 m_vecpThreads.resize (m_iNumThreads);
67 for (int iThread = 0; iThread < m_iNumThreads; iThread++)
68 m_vecpThreads[iThread] = NULL;
72 BackgroundSupervisor::~BackgroundSupervisor()
74 m_pDocument->removeBackgroundSupervisor (this);
80 BackgroundSupervisor::deleteWorkers()
82 wxCriticalSectionLocker lock (m_critsectThreads);
83 if (m_bWorkersDeleted)
86 for (int i = 0; i < m_iNumThreads; i++)
88 m_vecpThreads[i]->Delete(); // send Destroy message to workers
91 wxCommandEvent doneEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_REMOVE);
92 doneEvent.SetClientData (this);
93 wxPostEvent (theApp->getBackgroundManager(), doneEvent);
96 while (m_iRunning > 0 || m_bBackgroundManagerAdded)
97 m_pMyThread->Sleep(50);
99 m_bWorkersDeleted = true;
103 BackgroundSupervisor::ackRemoveBackgroundManager()
105 m_bBackgroundManagerAdded = false;
109 BackgroundSupervisor::start()
111 int iBaseUnits = m_iTotalUnits / m_iNumThreads;
112 int iExtraUnits = m_iTotalUnits % m_iNumThreads;
114 for (int iThread = 0; iThread < m_iNumThreads; iThread++) {
115 int iNumUnits = iBaseUnits;
116 if (iThread < iExtraUnits)
118 m_vecpThreads[iThread] = createWorker (iThread, iStartUnit, iNumUnits);
119 if (! m_vecpThreads[iThread]) {
121 m_strFailMessage = _T("createWorker returned NULL [BackgroundSupervisor]");
124 if (m_vecpThreads[iThread]->Create () != wxTHREAD_NO_ERROR) {
126 m_strFailMessage = _T("Thread creation failed [BackgroundSupervisor]");
129 m_vecpThreads[iThread]->SetPriority (40);
130 iStartUnit += iNumUnits;
135 m_pTimer = new Timer;
137 wxString strLabel (m_strProcessTitle);
139 strLabel += dynamic_cast<wxFrame*>(m_pParentFrame)->GetTitle();
142 wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_ADD);
143 addTaskEvent.SetString (strLabel);
144 addTaskEvent.SetInt (m_iTotalUnits);
145 addTaskEvent.SetClientData (this);
146 wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
149 m_pDocument->addBackgroundSupervisor (this);
150 m_bBackgroundManagerAdded = true;
152 m_iRunning = m_iNumThreads;
155 for (int i = 0; i < m_iNumThreads; i++)
156 m_vecpThreads[i]->Run();
162 BackgroundSupervisor::onCancel()
170 BackgroundSupervisor::onWorkerUnitTick ()
175 wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_UNIT_TICK);
176 addTaskEvent.SetInt (m_iUnitsDone - 1);
177 addTaskEvent.SetClientData (this);
178 wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
183 BackgroundSupervisor::onWorkerDone (int iThread)
185 wxCriticalSection critsectDone;
186 critsectDone.Enter();
191 if (theApp->getVerboseLogging()) {
193 msg.Printf(_T("Background Supervisor: Thread finished. Remaining threads: %d\n"), m_iRunning);
194 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
195 eventLog.SetString( msg );
196 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
200 critsectDone.Leave();
204 BackgroundSupervisor::onWorkerFail (int iThread, const wxString& strFailMessage)
207 wxCommandEvent eventLog( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
208 eventLog.SetString( strFailMessage );
209 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
214 #endif // HAVE_WXTHREADS