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
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"
34 // pragma line required for Fedora 4 and wxWin 2.4.2
35 #pragma implementation "timer.h"
41 #include "backgroundsupr.h"
42 #include "backgroundmgr.h"
48 ////////////////////////////////////////////////////////////////////////////
50 // Class BackgroundSupervisor -- An event handler run by a SupervisorThread
52 ////////////////////////////////////////////////////////////////////////////
54 IMPLEMENT_DYNAMIC_CLASS(BackgroundSupervisor, wxEvtHandler)
55 BEGIN_EVENT_TABLE(BackgroundSupervisor, BackgroundSupervisor)
60 BackgroundSupervisor::BackgroundSupervisor (SupervisorThread* pMyThread, wxWindow* pParentFrame, BackgroundProcessingDocument* pDocument, wxChar const* pszProcessTitle, int iTotalUnits)
61 : wxEvtHandler(), m_pMyThread(pMyThread), m_pParentFrame(pParentFrame), m_pDocument(pDocument), m_strProcessTitle(pszProcessTitle),
62 m_iTotalUnits(iTotalUnits), m_iNumThreads(0), m_bDone(false), m_bFail(false), m_bCancelled(false), m_iRunning(0),
63 m_pTimer(NULL), m_bWorkersDeleted(false), m_bBackgroundManagerAdded(false)
65 m_iNumThreads = theApp->getNumberCPU();
68 m_vecpThreads.resize (m_iNumThreads);
69 for (int iThread = 0; iThread < m_iNumThreads; iThread++)
70 m_vecpThreads[iThread] = NULL;
74 BackgroundSupervisor::~BackgroundSupervisor()
76 m_pDocument->removeBackgroundSupervisor (this);
82 BackgroundSupervisor::deleteWorkers()
84 wxCriticalSectionLocker lock (m_critsectThreads);
85 if (m_bWorkersDeleted)
88 for (int i = 0; i < m_iNumThreads; i++)
90 m_vecpThreads[i]->Delete(); // send Destroy message to workers
93 wxCommandEvent doneEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_REMOVE);
94 doneEvent.SetClientData (this);
95 wxPostEvent (theApp->getBackgroundManager(), doneEvent);
98 while (m_iRunning > 0 || m_bBackgroundManagerAdded)
99 m_pMyThread->Sleep(50);
101 m_bWorkersDeleted = true;
105 BackgroundSupervisor::ackRemoveBackgroundManager()
107 m_bBackgroundManagerAdded = false;
111 BackgroundSupervisor::start()
113 int iBaseUnits = m_iTotalUnits / m_iNumThreads;
114 int iExtraUnits = m_iTotalUnits % m_iNumThreads;
116 for (int iThread = 0; iThread < m_iNumThreads; iThread++) {
117 int iNumUnits = iBaseUnits;
118 if (iThread < iExtraUnits)
120 m_vecpThreads[iThread] = createWorker (iThread, iStartUnit, iNumUnits);
121 if (! m_vecpThreads[iThread]) {
123 m_strFailMessage = _T("createWorker returned NULL [BackgroundSupervisor]");
126 if (m_vecpThreads[iThread]->Create () != wxTHREAD_NO_ERROR) {
128 m_strFailMessage = _T("Thread creation failed [BackgroundSupervisor]");
131 m_vecpThreads[iThread]->SetPriority (40);
132 iStartUnit += iNumUnits;
137 m_pTimer = new Timer;
139 wxString strLabel (m_strProcessTitle);
141 strLabel += dynamic_cast<wxFrame*>(m_pParentFrame)->GetTitle();
144 wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_ADD);
145 addTaskEvent.SetString (strLabel);
146 addTaskEvent.SetInt (m_iTotalUnits);
147 addTaskEvent.SetClientData (this);
148 wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
151 m_pDocument->addBackgroundSupervisor (this);
152 m_bBackgroundManagerAdded = true;
154 m_iRunning = m_iNumThreads;
157 for (int i = 0; i < m_iNumThreads; i++)
158 m_vecpThreads[i]->Run();
164 BackgroundSupervisor::onCancel()
172 BackgroundSupervisor::onWorkerUnitTick ()
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);
185 BackgroundSupervisor::onWorkerDone (int iThread)
187 wxCriticalSection critsectDone;
188 critsectDone.Enter();
193 if (theApp->getVerboseLogging()) {
195 msg.Printf(_T("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, const wxString& strFailMessage)
209 wxCommandEvent eventLog( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
210 eventLog.SetString( strFailMessage );
211 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
216 #endif // HAVE_WXTHREADS