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