r604: no 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.12 2001/03/04 22:30:19 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, wxDocument* 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_bBackgroundTaskAdded(false), 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   if (m_bBackgroundTaskAdded) {
72     wxCommandEvent doneEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_REMOVE);
73     doneEvent.SetClientData (this);
74     wxPostEvent (theApp->getBackgroundManager(), doneEvent);
75     wxPostEvent (m_pDocument, doneEvent);
76   }
77
78   while (m_bBackgroundTaskAdded) {
79     m_pMyThread->Sleep(50);
80   }
81
82   delete m_pTimer;
83 }
84
85 void
86 BackgroundSupervisor::deleteWorkers()
87 {
88   wxCriticalSectionLocker lock (m_critsectThreads);
89   if (m_bWorkersDeleted)
90     return;
91
92   for (int i = 0; i < m_iNumThreads; i++) 
93     if (m_vecpThreads[i]) {
94       m_vecpThreads[i]->Delete(); // sends Destroy message to workers
95   }
96
97   while (m_iRunning > 0) {
98     m_pMyThread->Sleep(50);
99   }
100   m_iRunning = 0;
101   m_bWorkersDeleted = true;
102 }
103
104 bool
105 BackgroundSupervisor::start()
106 {
107   int iBaseUnits = m_iTotalUnits / m_iNumThreads;
108   int iExtraUnits = m_iTotalUnits % m_iNumThreads;
109   int iStartUnit = 0;
110   for (int iThread = 0; iThread < m_iNumThreads; iThread++) {
111     int iNumUnits = iBaseUnits;
112     if (iThread < iExtraUnits)
113       ++iNumUnits;
114     m_vecpThreads[iThread] = createWorker (iThread, iStartUnit, iNumUnits);
115     if (! m_vecpThreads[iThread]) {
116       m_bFail = true;
117       m_strFailMessage = "createWorker returned NULL [BackgroundSupervisor]";
118       break;
119     }
120     if (m_vecpThreads[iThread]->Create () != wxTHREAD_NO_ERROR) {
121       m_bFail = true;
122       m_strFailMessage = "Thread creation failed [BackgroundSupervisor]";
123       break;
124     }
125    m_vecpThreads[iThread]->SetPriority (40);
126    iStartUnit += iNumUnits;
127   }
128   if (m_bFail)
129     return false;
130
131   m_pTimer = new Timer;
132   
133   std::string strLabel (m_strProcessTitle);
134   strLabel += " ";
135   strLabel += m_pParentFrame->GetTitle();
136   wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_ADD);
137   addTaskEvent.SetString (strLabel.c_str());
138   addTaskEvent.SetInt (m_iTotalUnits);
139   addTaskEvent.SetClientData (this);
140   wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
141   wxPostEvent (m_pDocument, addTaskEvent);
142   m_bBackgroundTaskAdded = true;
143   
144   m_iRunning = m_iNumThreads;
145   m_iUnitsDone = 0;
146
147   for (int i = 0; i < m_iNumThreads; i++)
148     m_vecpThreads[i]->Run();
149     
150   return true;
151 }
152
153 void
154 BackgroundSupervisor::onCancel()
155 {
156   m_bCancelled = true;
157   m_bDone = true;
158 }
159
160 void
161 BackgroundSupervisor::onAckDocumentRemove()
162 {
163   m_bBackgroundTaskAdded = false;
164 }
165
166
167 void
168 BackgroundSupervisor::onWorkerUnitTick ()
169 {
170     ++m_iUnitsDone;
171     
172 #ifdef DEBUG
173     if (theApp->getVerboseLogging())
174       *theApp->getLog() << "Units done: " << static_cast<int>(m_iUnitsDone) <<"\n";
175 #endif
176     
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 }
182
183 void
184 BackgroundSupervisor::onWorkerDone (int iThread)
185 {
186         wxCriticalSection critsectDone;
187         critsectDone.Enter();
188
189   m_iRunning--;
190   wxASSERT (m_iRunning >= 0);
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