r609: *** 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.15 2001/03/05 20:29:23 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 // #define USE_BKGMGR 1
44
45 ////////////////////////////////////////////////////////////////////////////
46 //
47 // Class BackgroundSupervisor -- An event handler run by a SupervisorThread
48 //
49 ////////////////////////////////////////////////////////////////////////////
50
51 IMPLEMENT_DYNAMIC_CLASS(BackgroundSupervisor, wxEvtHandler)
52 BEGIN_EVENT_TABLE(BackgroundSupervisor, BackgroundSupervisor)
53 END_EVENT_TABLE()
54
55
56
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)
61 {
62   m_iNumThreads = theApp->getNumberCPU();
63   //   ++m_iNumThreads;
64
65   m_vecpThreads.reserve (m_iNumThreads);
66   for (int iThread = 0; iThread < m_iNumThreads; iThread++)
67     m_vecpThreads[iThread] = NULL;
68
69 }
70
71 BackgroundSupervisor::~BackgroundSupervisor()
72 {
73 #ifdef USE_BKGMGR
74   wxCommandEvent doneEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_REMOVE);
75   doneEvent.SetClientData (this);
76   wxPostEvent (theApp->getBackgroundManager(), doneEvent);
77 #endif
78
79   m_pDocument->removeBackgroundSupervisor (this);
80
81   delete m_pTimer;
82 }
83
84 void
85 BackgroundSupervisor::deleteWorkers()
86 {
87   wxCriticalSectionLocker lock (m_critsectThreads);
88   if (m_bWorkersDeleted)
89     return;
90
91   for (int i = 0; i < m_iNumThreads; i++) 
92     if (m_vecpThreads[i]) {
93       m_vecpThreads[i]->Delete(); // sends Destroy message to workers
94   }
95
96   while (m_iRunning > 0) {
97     m_pMyThread->Sleep(50);
98   }
99   m_iRunning = 0;
100   m_bWorkersDeleted = true;
101 }
102
103 bool
104 BackgroundSupervisor::start()
105 {
106   int iBaseUnits = m_iTotalUnits / m_iNumThreads;
107   int iExtraUnits = m_iTotalUnits % m_iNumThreads;
108   int iStartUnit = 0;
109   for (int iThread = 0; iThread < m_iNumThreads; iThread++) {
110     int iNumUnits = iBaseUnits;
111     if (iThread < iExtraUnits)
112       ++iNumUnits;
113     m_vecpThreads[iThread] = createWorker (iThread, iStartUnit, iNumUnits);
114     if (! m_vecpThreads[iThread]) {
115       m_bFail = true;
116       m_strFailMessage = "createWorker returned NULL [BackgroundSupervisor]";
117       break;
118     }
119     if (m_vecpThreads[iThread]->Create () != wxTHREAD_NO_ERROR) {
120       m_bFail = true;
121       m_strFailMessage = "Thread creation failed [BackgroundSupervisor]";
122       break;
123     }
124    m_vecpThreads[iThread]->SetPriority (40);
125    iStartUnit += iNumUnits;
126   }
127   if (m_bFail)
128     return false;
129
130   m_pTimer = new Timer;
131   
132   std::string strLabel (m_strProcessTitle);
133   strLabel += " ";
134   strLabel += m_pParentFrame->GetTitle();
135
136 #ifdef USE_BKGMGR
137   wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_ADD);
138   addTaskEvent.SetString (strLabel.c_str());
139   addTaskEvent.SetInt (m_iTotalUnits);
140   addTaskEvent.SetClientData (this);
141   wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
142 #endif
143
144   m_pDocument->addBackgroundSupervisor (this);
145   
146   m_iRunning = m_iNumThreads;
147   m_iUnitsDone = 0;
148
149   for (int i = 0; i < m_iNumThreads; i++)
150     m_vecpThreads[i]->Run();
151     
152   return true;
153 }
154
155 void
156 BackgroundSupervisor::onCancel()
157 {
158   m_bCancelled = true;
159   m_bDone = true;
160 }
161
162
163 void
164 BackgroundSupervisor::onWorkerUnitTick ()
165 {
166     ++m_iUnitsDone;
167     
168 #ifdef DEBUG
169     if (theApp->getVerboseLogging())
170       *theApp->getLog() << "Units done: " << static_cast<int>(m_iUnitsDone) <<"\n";
171 #endif
172     
173 #ifdef USE_BKGMGR
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);
178 #endif
179 }
180
181 void
182 BackgroundSupervisor::onWorkerDone (int iThread)
183 {
184         wxCriticalSection critsectDone;
185         critsectDone.Enter();
186
187   m_iRunning--;
188   wxASSERT (m_iRunning >= 0);
189
190 #ifdef DEBUG
191   if (theApp->getVerboseLogging()) {
192     wxString msg;
193     msg.Printf("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
197   }
198 #endif
199
200   critsectDone.Leave();
201 }
202
203 void
204 BackgroundSupervisor::onWorkerFail (int iThread, std::string strFailMessage)
205 {
206   m_iRunning--;
207   wxCommandEvent eventLog( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
208   eventLog.SetString( strFailMessage.c_str() );
209   wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
210
211   onCancel();
212 }
213
214 #endif // HAVE_WXTHREADS