r7061: initial property settings
[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 #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, wxWindow* 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), m_bBackgroundManagerAdded(false)
61 {
62   m_iNumThreads = theApp->getNumberCPU();
63   //   ++m_iNumThreads;
64
65   m_vecpThreads.resize (m_iNumThreads);
66   for (int iThread = 0; iThread < m_iNumThreads; iThread++)
67     m_vecpThreads[iThread] = NULL;
68
69 }
70
71 BackgroundSupervisor::~BackgroundSupervisor()
72 {
73   m_pDocument->removeBackgroundSupervisor (this);
74
75   delete m_pTimer;
76 }
77
78 void
79 BackgroundSupervisor::deleteWorkers()
80 {
81   wxCriticalSectionLocker lock (m_critsectThreads);
82   if (m_bWorkersDeleted)
83     return;
84
85   for (int i = 0; i < m_iNumThreads; i++) 
86     if (m_vecpThreads[i]) 
87       m_vecpThreads[i]->Delete(); // send Destroy message to workers
88
89 #ifdef USE_BKGMGR
90   wxCommandEvent doneEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_REMOVE);
91   doneEvent.SetClientData (this);
92   wxPostEvent (theApp->getBackgroundManager(), doneEvent);
93 #endif
94
95   while (m_iRunning > 0 || m_bBackgroundManagerAdded) 
96     m_pMyThread->Sleep(50);
97   
98   m_bWorkersDeleted = true;
99 }
100
101 void
102 BackgroundSupervisor::ackRemoveBackgroundManager()
103 {
104   m_bBackgroundManagerAdded = false;
105 }
106
107 bool
108 BackgroundSupervisor::start()
109 {
110   int iBaseUnits = m_iTotalUnits / m_iNumThreads;
111   int iExtraUnits = m_iTotalUnits % m_iNumThreads;
112   int iStartUnit = 0;
113   for (int iThread = 0; iThread < m_iNumThreads; iThread++) {
114     int iNumUnits = iBaseUnits;
115     if (iThread < iExtraUnits)
116       ++iNumUnits;
117     m_vecpThreads[iThread] = createWorker (iThread, iStartUnit, iNumUnits);
118     if (! m_vecpThreads[iThread]) {
119       m_bFail = true;
120       m_strFailMessage = "createWorker returned NULL [BackgroundSupervisor]";
121       break;
122     }
123     if (m_vecpThreads[iThread]->Create () != wxTHREAD_NO_ERROR) {
124       m_bFail = true;
125       m_strFailMessage = "Thread creation failed [BackgroundSupervisor]";
126       break;
127     }
128    m_vecpThreads[iThread]->SetPriority (40);
129    iStartUnit += iNumUnits;
130   }
131   if (m_bFail)
132     return false;
133
134   m_pTimer = new Timer;
135   
136   std::string strLabel (m_strProcessTitle);
137   strLabel += " ";
138   strLabel += m_pParentFrame->GetTitle();
139
140 #ifdef USE_BKGMGR
141   wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_ADD);
142   addTaskEvent.SetString (strLabel.c_str());
143   addTaskEvent.SetInt (m_iTotalUnits);
144   addTaskEvent.SetClientData (this);
145   wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
146 #endif
147
148   m_pDocument->addBackgroundSupervisor (this);
149   m_bBackgroundManagerAdded = true;
150
151   m_iRunning = m_iNumThreads;
152   m_iUnitsDone = 0;
153
154   for (int i = 0; i < m_iNumThreads; i++)
155     m_vecpThreads[i]->Run();
156     
157   return true;
158 }
159
160 void
161 BackgroundSupervisor::onCancel()
162 {
163   m_bCancelled = true;
164   m_bDone = true;
165 }
166
167
168 void
169 BackgroundSupervisor::onWorkerUnitTick ()
170 {
171     ++m_iUnitsDone;
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
189 #ifdef DEBUG
190   if (theApp->getVerboseLogging()) {
191     wxString msg;
192     msg.Printf("Background Supervisor: Thread finished. Remaining threads: %d\n", m_iRunning);  
193     wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
194     eventLog.SetString( msg );
195     wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
196   }
197 #endif
198
199   critsectDone.Leave();
200 }
201
202 void
203 BackgroundSupervisor::onWorkerFail (int iThread, std::string strFailMessage)
204 {
205   m_iRunning--;
206   wxCommandEvent eventLog( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
207   eventLog.SetString( strFailMessage.c_str() );
208   wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
209
210   onCancel();
211 }
212
213 #endif // HAVE_WXTHREADS