Update copyright date; remove old CVS keyword
[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-2009 Kevin Rosenberg
11 **
12 **  This program is free software; you can redistribute it and/or modify
13 **  it under the terms of the GNU General Public License (version 2) as
14 **  published by the Free Software Foundation.
15 **
16 **  This program is distributed in the hope that it will be useful,
17 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 **  GNU General Public License for more details.
20 **
21 **  You should have received a copy of the GNU General Public License
22 **  along with this program; if not, write to the Free Software
23 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 ******************************************************************************/
25
26 #include "wx/wxprec.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/wx.h"
30 #endif
31
32 // pragma line required for Fedora 4 and wxWin 2.4.2
33 #pragma implementation "timer.h"
34
35 #include "ct.h"
36 #include "ctsim.h"
37 #include "docs.h"
38 #include "views.h"
39 #include "backgroundsupr.h"
40 #include "backgroundmgr.h"
41
42 #ifdef HAVE_WXTHREADS
43
44 #define USE_BKGMGR 1
45
46 ////////////////////////////////////////////////////////////////////////////
47 //
48 // Class BackgroundSupervisor -- An event handler run by a SupervisorThread
49 //
50 ////////////////////////////////////////////////////////////////////////////
51
52 IMPLEMENT_DYNAMIC_CLASS(BackgroundSupervisor, wxEvtHandler)
53 BEGIN_EVENT_TABLE(BackgroundSupervisor, BackgroundSupervisor)
54 END_EVENT_TABLE()
55
56
57
58 BackgroundSupervisor::BackgroundSupervisor (SupervisorThread* pMyThread, wxWindow* pParentFrame, BackgroundProcessingDocument* pDocument, wxChar const* pszProcessTitle, int iTotalUnits)
59     : wxEvtHandler(), m_pMyThread(pMyThread), m_pParentFrame(pParentFrame), m_pDocument(pDocument), m_strProcessTitle(pszProcessTitle),
60     m_iTotalUnits(iTotalUnits), m_iNumThreads(0), m_bDone(false), m_bFail(false), m_bCancelled(false), m_iRunning(0),
61     m_pTimer(NULL), m_bWorkersDeleted(false), m_bBackgroundManagerAdded(false)
62 {
63   m_iNumThreads = theApp->getNumberCPU();
64   //   ++m_iNumThreads;
65
66   m_vecpThreads.resize (m_iNumThreads);
67   for (int iThread = 0; iThread < m_iNumThreads; iThread++)
68     m_vecpThreads[iThread] = NULL;
69
70 }
71
72 BackgroundSupervisor::~BackgroundSupervisor()
73 {
74   m_pDocument->removeBackgroundSupervisor (this);
75
76   delete m_pTimer;
77 }
78
79 void
80 BackgroundSupervisor::deleteWorkers()
81 {
82   wxCriticalSectionLocker lock (m_critsectThreads);
83   if (m_bWorkersDeleted)
84     return;
85
86   for (int i = 0; i < m_iNumThreads; i++)
87     if (m_vecpThreads[i])
88       m_vecpThreads[i]->Delete(); // send Destroy message to workers
89
90 #ifdef USE_BKGMGR
91   wxCommandEvent doneEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_REMOVE);
92   doneEvent.SetClientData (this);
93   wxPostEvent (theApp->getBackgroundManager(), doneEvent);
94 #endif
95
96   while (m_iRunning > 0 || m_bBackgroundManagerAdded)
97     m_pMyThread->Sleep(50);
98
99   m_bWorkersDeleted = true;
100 }
101
102 void
103 BackgroundSupervisor::ackRemoveBackgroundManager()
104 {
105   m_bBackgroundManagerAdded = false;
106 }
107
108 bool
109 BackgroundSupervisor::start()
110 {
111   int iBaseUnits = m_iTotalUnits / m_iNumThreads;
112   int iExtraUnits = m_iTotalUnits % m_iNumThreads;
113   int iStartUnit = 0;
114   for (int iThread = 0; iThread < m_iNumThreads; iThread++) {
115     int iNumUnits = iBaseUnits;
116     if (iThread < iExtraUnits)
117       ++iNumUnits;
118     m_vecpThreads[iThread] = createWorker (iThread, iStartUnit, iNumUnits);
119     if (! m_vecpThreads[iThread]) {
120       m_bFail = true;
121       m_strFailMessage = _T("createWorker returned NULL [BackgroundSupervisor]");
122       break;
123     }
124     if (m_vecpThreads[iThread]->Create () != wxTHREAD_NO_ERROR) {
125       m_bFail = true;
126       m_strFailMessage = _T("Thread creation failed [BackgroundSupervisor]");
127       break;
128     }
129    m_vecpThreads[iThread]->SetPriority (40);
130    iStartUnit += iNumUnits;
131   }
132   if (m_bFail)
133     return false;
134
135   m_pTimer = new Timer;
136
137   wxString strLabel (m_strProcessTitle);
138   strLabel += _T(" ");
139   strLabel += dynamic_cast<wxFrame*>(m_pParentFrame)->GetTitle();
140
141 #ifdef USE_BKGMGR
142   wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_ADD);
143   addTaskEvent.SetString (strLabel);
144   addTaskEvent.SetInt (m_iTotalUnits);
145   addTaskEvent.SetClientData (this);
146   wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
147 #endif
148
149   m_pDocument->addBackgroundSupervisor (this);
150   m_bBackgroundManagerAdded = true;
151
152   m_iRunning = m_iNumThreads;
153   m_iUnitsDone = 0;
154
155   for (int i = 0; i < m_iNumThreads; i++)
156     m_vecpThreads[i]->Run();
157
158   return true;
159 }
160
161 void
162 BackgroundSupervisor::onCancel()
163 {
164   m_bCancelled = true;
165   m_bDone = true;
166 }
167
168
169 void
170 BackgroundSupervisor::onWorkerUnitTick ()
171 {
172     ++m_iUnitsDone;
173
174 #ifdef USE_BKGMGR
175     wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_UNIT_TICK);
176     addTaskEvent.SetInt (m_iUnitsDone - 1);
177     addTaskEvent.SetClientData (this);
178     wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
179 #endif
180 }
181
182 void
183 BackgroundSupervisor::onWorkerDone (int iThread)
184 {
185         wxCriticalSection critsectDone;
186         critsectDone.Enter();
187
188   m_iRunning--;
189
190 #ifdef DEBUG
191   if (theApp->getVerboseLogging()) {
192     wxString msg;
193     msg.Printf(_T("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, const wxString& strFailMessage)
205 {
206   m_iRunning--;
207   wxCommandEvent eventLog( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
208   eventLog.SetString( strFailMessage );
209   wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
210
211   onCancel();
212 }
213
214 #endif // HAVE_WXTHREADS