r624: no message
[ctsim.git] / src / backgroundsupr.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          backgroundsupr.h
5 **   Purpose:       Header file for background supervisors
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.h,v 1.11 2001/03/09 21:31:51 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 #ifndef _BACKGROUNDSUPR_H
29 #define _BACKGROUNDSUPR_H
30
31 #include <vector>
32 #include <wx/thread.h>
33 #include <wx/progdlg.h>
34 #include "timer.h"
35
36 // This thread creates a SupervisorTask event handler object
37 // The thread is detached and terminates when SupervisorTask terminates
38 class SupervisorThread : public wxThread {
39 private:
40
41 public:
42   SupervisorThread()
43     : wxThread(wxTHREAD_DETACHED)
44   {}
45
46 };
47
48
49 // Pure virtual class for BackgroundSupervisor that can communication with BackgroundManager via messages
50 class BackgroundWorkerThread;
51 class BackgroundProcessingDocument;
52
53 class BackgroundSupervisor : public wxEvtHandler {
54 private:
55   DECLARE_DYNAMIC_CLASS(BackgroundSupervisor)
56
57   SupervisorThread* m_pMyThread;
58   wxFrame* m_pParentFrame;
59   BackgroundProcessingDocument* m_pDocument;
60   const std::string m_strProcessTitle;
61   
62   const unsigned int m_iTotalUnits;
63   int m_iNumThreads;
64   volatile bool m_bDone;
65   volatile bool m_bFail;
66   std::string m_strFailMessage;
67   volatile bool m_bCancelled;
68   volatile int m_iRunning;
69   volatile unsigned int m_iUnitsDone;
70   Timer* m_pTimer;
71   volatile bool m_bWorkersDeleted;
72   volatile bool m_bBackgroundManagerAdded;
73
74   typedef std::vector<BackgroundWorkerThread*> ThreadContainer;
75   ThreadContainer m_vecpThreads;
76   wxCriticalSection m_critsectThreads;
77
78 public:
79   enum {
80     MSG_BACKGROUND_SUPERVISOR_ADD = 7500, // sends to BackgroundManager and Document
81     MSG_BACKGROUND_SUPERVISOR_REMOVE = 7501, // sends to BackgroundManager and Document
82     MSG_BACKGROUND_SUPERVISOR_UNIT_TICK = 7502,  // sends to BackgroundManager for progress bars
83     MSG_BACKGROUND_SUPERVISOR_CANCEL = 7503,   // *sent* to Supervisor to cancel process
84     MSG_DOCUMENT_ACK_REMOVE = 7504,
85
86     MSG_WORKER_THREAD_UNIT_TICK = 7505,
87     MSG_WORKER_THREAD_DONE = 7506,
88     MSG_WORKER_THREAD_FAIL = 7507,   // sent by workers when they fail
89   };
90
91   BackgroundSupervisor (SupervisorThread* pMyThread, wxFrame* pParentFrame, BackgroundProcessingDocument* pDocument, const char* const pszProcessTitle, 
92     int iTotalUnits);
93
94   BackgroundSupervisor ()
95     : wxEvtHandler(), m_iTotalUnits(0)
96   {}
97
98   virtual ~BackgroundSupervisor();
99
100   virtual BackgroundWorkerThread* createWorker (int iThread, int iStartUnit, int iNumUnits)
101   { return NULL; }
102
103   bool start();
104   virtual void onDone() {};
105   
106   virtual void onCancel();
107
108   virtual void onWorkerFail(int iThread, std::string strFailMessage);
109   virtual void onWorkerUnitTick();
110   virtual void onWorkerDone(int iThread);
111
112   void deleteWorkers();
113   void ackRemoveBackgroundManager();
114   bool workersDone() const { return m_iRunning == 0; }
115   bool workersDeleted() const { return m_bWorkersDeleted; }
116   bool isDone() const {return m_bDone;}
117   void setDone() { m_bDone = true; }
118   bool fail() const {return m_bFail;}
119   const std::string& getFailMessage() const { return m_strFailMessage; }
120   bool cancelled() const {return m_bCancelled;}
121
122   int getNumWorkers() const { return m_iNumThreads; }
123   double getTimerEnd() { return m_pTimer->timerEnd(); }
124
125   DECLARE_EVENT_TABLE()
126 };
127
128
129 class BackgroundWorkerThread : public wxThread {
130 protected:
131   BackgroundSupervisor* m_pSupervisor;
132   const int m_iThread;
133   const int m_iStartUnit;
134   const int m_iNumUnits;
135
136 public:
137   BackgroundWorkerThread (BackgroundSupervisor* pSupervisor, int iThread, int iStartUnit, int iNumUnits)
138     : wxThread (wxTHREAD_DETACHED), m_pSupervisor(pSupervisor), m_iThread(iThread), m_iStartUnit(iStartUnit), m_iNumUnits(iNumUnits)
139   {}
140 };
141
142 #endif  // _BACKGROUNDSUPR_H_