r623: 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.17 2001/03/09 02:40:17 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(); // send Destroy message to workers
94
95   while (m_iRunning > 0) 
96     m_pMyThread->Sleep(50);
97   
98   m_bWorkersDeleted = true;
99 }
100
101 bool
102 BackgroundSupervisor::start()
103 {
104   int iBaseUnits = m_iTotalUnits / m_iNumThreads;
105   int iExtraUnits = m_iTotalUnits % m_iNumThreads;
106   int iStartUnit = 0;
107   for (int iThread = 0; iThread < m_iNumThreads; iThread++) {
108     int iNumUnits = iBaseUnits;
109     if (iThread < iExtraUnits)
110       ++iNumUnits;
111     m_vecpThreads[iThread] = createWorker (iThread, iStartUnit, iNumUnits);
112     if (! m_vecpThreads[iThread]) {
113       m_bFail = true;
114       m_strFailMessage = "createWorker returned NULL [BackgroundSupervisor]";
115       break;
116     }
117     if (m_vecpThreads[iThread]->Create () != wxTHREAD_NO_ERROR) {
118       m_bFail = true;
119       m_strFailMessage = "Thread creation failed [BackgroundSupervisor]";
120       break;
121     }
122    m_vecpThreads[iThread]->SetPriority (40);
123    iStartUnit += iNumUnits;
124   }
125   if (m_bFail)
126     return false;
127
128   m_pTimer = new Timer;
129   
130   std::string strLabel (m_strProcessTitle);
131   strLabel += " ";
132   strLabel += m_pParentFrame->GetTitle();
133
134 #ifdef USE_BKGMGR
135   wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_ADD);
136   addTaskEvent.SetString (strLabel.c_str());
137   addTaskEvent.SetInt (m_iTotalUnits);
138   addTaskEvent.SetClientData (this);
139   wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
140 #endif
141
142   m_pDocument->addBackgroundSupervisor (this);
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
161 void
162 BackgroundSupervisor::onWorkerUnitTick ()
163 {
164     ++m_iUnitsDone;
165     
166 #ifdef USE_BKGMGR
167     wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_UNIT_TICK);
168     addTaskEvent.SetInt (m_iUnitsDone - 1);
169     addTaskEvent.SetClientData (this);
170     wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
171 #endif
172 }
173
174 void
175 BackgroundSupervisor::onWorkerDone (int iThread)
176 {
177         wxCriticalSection critsectDone;
178         critsectDone.Enter();
179
180   m_iRunning--;
181
182 #ifdef DEBUG
183   if (theApp->getVerboseLogging()) {
184     wxString msg;
185     msg.Printf("Background Supervisor: Thread finished. Remaining threads: %d\n", m_iRunning);  
186     wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
187     eventLog.SetString( msg );
188     wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
189   }
190 #endif
191
192   critsectDone.Leave();
193 }
194
195 void
196 BackgroundSupervisor::onWorkerFail (int iThread, std::string strFailMessage)
197 {
198   m_iRunning--;
199   wxCommandEvent eventLog( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
200   eventLog.SetString( strFailMessage.c_str() );
201   wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
202
203   onCancel();
204 }
205
206 #endif // HAVE_WXTHREADS