r607: *** empty log message ***
[ctsim.git] / src / backgroundsupr.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          BackgroundSupr.cpp
5 **   Purpose:       Background Supervisor classes
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  February 2001
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id: backgroundsupr.cpp,v 1.13 2001/03/05 17:26:46 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #include "wx/wxprec.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/wx.h"
32 #endif
33
34 #include "ct.h"
35 #include "ctsim.h"
36 #include "docs.h"
37 #include "views.h"
38 #include "backgroundsupr.h"
39 #include "backgroundmgr.h"
40
41 #ifdef HAVE_WXTHREADS
42
43 ////////////////////////////////////////////////////////////////////////////
44 //
45 // Class BackgroundSupervisor -- An event handler run by a SupervisorThread
46 //
47 ////////////////////////////////////////////////////////////////////////////
48
49 IMPLEMENT_DYNAMIC_CLASS(BackgroundSupervisor, wxEvtHandler)
50 BEGIN_EVENT_TABLE(BackgroundSupervisor, BackgroundSupervisor)
51 END_EVENT_TABLE()
52
53
54
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)
59 {
60   m_iNumThreads = theApp->getNumberCPU();
61   //   ++m_iNumThreads;
62
63   m_vecpThreads.reserve (m_iNumThreads);
64   for (int iThread = 0; iThread < m_iNumThreads; iThread++)
65     m_vecpThreads[iThread] = NULL;
66
67 }
68
69 BackgroundSupervisor::~BackgroundSupervisor()
70 {
71   wxCommandEvent doneEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_REMOVE);
72   doneEvent.SetClientData (this);
73   wxPostEvent (theApp->getBackgroundManager(), doneEvent);
74
75   m_pDocument->removeBackgroundSupervisor (this);
76
77   delete m_pTimer;
78 }
79
80 void
81 BackgroundSupervisor::deleteWorkers()
82 {
83   wxCriticalSectionLocker lock (m_critsectThreads);
84   if (m_bWorkersDeleted)
85     return;
86
87   for (int i = 0; i < m_iNumThreads; i++) 
88     if (m_vecpThreads[i]) {
89       m_vecpThreads[i]->Delete(); // sends Destroy message to workers
90   }
91
92   while (m_iRunning > 0) {
93     m_pMyThread->Sleep(50);
94   }
95   m_iRunning = 0;
96   m_bWorkersDeleted = true;
97 }
98
99 bool
100 BackgroundSupervisor::start()
101 {
102   int iBaseUnits = m_iTotalUnits / m_iNumThreads;
103   int iExtraUnits = m_iTotalUnits % m_iNumThreads;
104   int iStartUnit = 0;
105   for (int iThread = 0; iThread < m_iNumThreads; iThread++) {
106     int iNumUnits = iBaseUnits;
107     if (iThread < iExtraUnits)
108       ++iNumUnits;
109     m_vecpThreads[iThread] = createWorker (iThread, iStartUnit, iNumUnits);
110     if (! m_vecpThreads[iThread]) {
111       m_bFail = true;
112       m_strFailMessage = "createWorker returned NULL [BackgroundSupervisor]";
113       break;
114     }
115     if (m_vecpThreads[iThread]->Create () != wxTHREAD_NO_ERROR) {
116       m_bFail = true;
117       m_strFailMessage = "Thread creation failed [BackgroundSupervisor]";
118       break;
119     }
120    m_vecpThreads[iThread]->SetPriority (40);
121    iStartUnit += iNumUnits;
122   }
123   if (m_bFail)
124     return false;
125
126   m_pTimer = new Timer;
127   
128   std::string strLabel (m_strProcessTitle);
129   strLabel += " ";
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);
136
137   m_pDocument->addBackgroundSupervisor (this);
138   
139   m_iRunning = m_iNumThreads;
140   m_iUnitsDone = 0;
141
142   for (int i = 0; i < m_iNumThreads; i++)
143     m_vecpThreads[i]->Run();
144     
145   return true;
146 }
147
148 void
149 BackgroundSupervisor::onCancel()
150 {
151   m_bCancelled = true;
152   m_bDone = true;
153 }
154
155
156 void
157 BackgroundSupervisor::onWorkerUnitTick ()
158 {
159     ++m_iUnitsDone;
160     
161 #ifdef DEBUG
162     if (theApp->getVerboseLogging())
163       *theApp->getLog() << "Units done: " << static_cast<int>(m_iUnitsDone) <<"\n";
164 #endif
165     
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);
170 }
171
172 void
173 BackgroundSupervisor::onWorkerDone (int iThread)
174 {
175         wxCriticalSection critsectDone;
176         critsectDone.Enter();
177
178   m_iRunning--;
179   wxASSERT (m_iRunning >= 0);
180
181 #ifdef DEBUG
182   if (theApp->getVerboseLogging()) {
183     wxString msg;
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
188   }
189 #endif
190
191   critsectDone.Leave();
192 }
193
194 void
195 BackgroundSupervisor::onWorkerFail (int iThread, std::string strFailMessage)
196 {
197   m_iRunning--;
198   wxCommandEvent eventLog( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
199   eventLog.SetString( strFailMessage.c_str() );
200   wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
201
202   onCancel();
203 }
204
205 #endif // HAVE_WXTHREADS