r11859: Canonicalize whitespace
[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$
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 // pragma line required for Fedora 4 and wxWin 2.4.2
35 #pragma implementation "timer.h"
36
37 #include "ct.h"
38 #include "ctsim.h"
39 #include "docs.h"
40 #include "views.h"
41 #include "backgroundsupr.h"
42 #include "backgroundmgr.h"
43
44 #ifdef HAVE_WXTHREADS
45
46 #define USE_BKGMGR 1
47
48 ////////////////////////////////////////////////////////////////////////////
49 //
50 // Class BackgroundSupervisor -- An event handler run by a SupervisorThread
51 //
52 ////////////////////////////////////////////////////////////////////////////
53
54 IMPLEMENT_DYNAMIC_CLASS(BackgroundSupervisor, wxEvtHandler)
55 BEGIN_EVENT_TABLE(BackgroundSupervisor, BackgroundSupervisor)
56 END_EVENT_TABLE()
57
58
59
60 BackgroundSupervisor::BackgroundSupervisor (SupervisorThread* pMyThread, wxWindow* pParentFrame, BackgroundProcessingDocument* pDocument, const char* 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)
64 {
65   m_iNumThreads = theApp->getNumberCPU();
66   //   ++m_iNumThreads;
67
68   m_vecpThreads.resize (m_iNumThreads);
69   for (int iThread = 0; iThread < m_iNumThreads; iThread++)
70     m_vecpThreads[iThread] = NULL;
71
72 }
73
74 BackgroundSupervisor::~BackgroundSupervisor()
75 {
76   m_pDocument->removeBackgroundSupervisor (this);
77
78   delete m_pTimer;
79 }
80
81 void
82 BackgroundSupervisor::deleteWorkers()
83 {
84   wxCriticalSectionLocker lock (m_critsectThreads);
85   if (m_bWorkersDeleted)
86     return;
87
88   for (int i = 0; i < m_iNumThreads; i++)
89     if (m_vecpThreads[i])
90       m_vecpThreads[i]->Delete(); // send Destroy message to workers
91
92 #ifdef USE_BKGMGR
93   wxCommandEvent doneEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_REMOVE);
94   doneEvent.SetClientData (this);
95   wxPostEvent (theApp->getBackgroundManager(), doneEvent);
96 #endif
97
98   while (m_iRunning > 0 || m_bBackgroundManagerAdded)
99     m_pMyThread->Sleep(50);
100
101   m_bWorkersDeleted = true;
102 }
103
104 void
105 BackgroundSupervisor::ackRemoveBackgroundManager()
106 {
107   m_bBackgroundManagerAdded = false;
108 }
109
110 bool
111 BackgroundSupervisor::start()
112 {
113   int iBaseUnits = m_iTotalUnits / m_iNumThreads;
114   int iExtraUnits = m_iTotalUnits % m_iNumThreads;
115   int iStartUnit = 0;
116   for (int iThread = 0; iThread < m_iNumThreads; iThread++) {
117     int iNumUnits = iBaseUnits;
118     if (iThread < iExtraUnits)
119       ++iNumUnits;
120     m_vecpThreads[iThread] = createWorker (iThread, iStartUnit, iNumUnits);
121     if (! m_vecpThreads[iThread]) {
122       m_bFail = true;
123       m_strFailMessage = "createWorker returned NULL [BackgroundSupervisor]";
124       break;
125     }
126     if (m_vecpThreads[iThread]->Create () != wxTHREAD_NO_ERROR) {
127       m_bFail = true;
128       m_strFailMessage = "Thread creation failed [BackgroundSupervisor]";
129       break;
130     }
131    m_vecpThreads[iThread]->SetPriority (40);
132    iStartUnit += iNumUnits;
133   }
134   if (m_bFail)
135     return false;
136
137   m_pTimer = new Timer;
138
139   std::string strLabel (m_strProcessTitle);
140   strLabel += " ";
141   strLabel += m_pParentFrame->GetTitle();
142
143 #ifdef USE_BKGMGR
144   wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_ADD);
145   addTaskEvent.SetString (strLabel.c_str());
146   addTaskEvent.SetInt (m_iTotalUnits);
147   addTaskEvent.SetClientData (this);
148   wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
149 #endif
150
151   m_pDocument->addBackgroundSupervisor (this);
152   m_bBackgroundManagerAdded = true;
153
154   m_iRunning = m_iNumThreads;
155   m_iUnitsDone = 0;
156
157   for (int i = 0; i < m_iNumThreads; i++)
158     m_vecpThreads[i]->Run();
159
160   return true;
161 }
162
163 void
164 BackgroundSupervisor::onCancel()
165 {
166   m_bCancelled = true;
167   m_bDone = true;
168 }
169
170
171 void
172 BackgroundSupervisor::onWorkerUnitTick ()
173 {
174     ++m_iUnitsDone;
175
176 #ifdef USE_BKGMGR
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);
181 #endif
182 }
183
184 void
185 BackgroundSupervisor::onWorkerDone (int iThread)
186 {
187         wxCriticalSection critsectDone;
188         critsectDone.Enter();
189
190   m_iRunning--;
191
192 #ifdef DEBUG
193   if (theApp->getVerboseLogging()) {
194     wxString msg;
195     msg.Printf("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
199   }
200 #endif
201
202   critsectDone.Leave();
203 }
204
205 void
206 BackgroundSupervisor::onWorkerFail (int iThread, std::string strFailMessage)
207 {
208   m_iRunning--;
209   wxCommandEvent eventLog( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
210   eventLog.SetString( strFailMessage.c_str() );
211   wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
212
213   onCancel();
214 }
215
216 #endif // HAVE_WXTHREADS