1 /*****************************************************************************
4 ** Name: backgroundsupr.h
5 ** Purpose: Header file for background supervisors
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: February 2001
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2009 Kevin Rosenberg
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.
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.
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 ******************************************************************************/
26 #ifndef _BACKGROUNDSUPR_H
27 #define _BACKGROUNDSUPR_H
30 #include <wx/thread.h>
31 #include <wx/progdlg.h>
34 // This thread creates a SupervisorTask event handler object
35 // The thread is detached and terminates when SupervisorTask terminates
36 class SupervisorThread : public wxThread {
41 : wxThread(wxTHREAD_DETACHED)
47 // Pure virtual class for BackgroundSupervisor that can communication with BackgroundManager via messages
48 class BackgroundWorkerThread;
49 class BackgroundProcessingDocument;
51 class BackgroundSupervisor : public wxEvtHandler {
53 DECLARE_DYNAMIC_CLASS(BackgroundSupervisor)
55 SupervisorThread* m_pMyThread;
56 wxWindow* m_pParentFrame;
57 BackgroundProcessingDocument* m_pDocument;
58 const wxString m_strProcessTitle;
60 const unsigned int m_iTotalUnits;
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;
69 volatile bool m_bWorkersDeleted;
70 volatile bool m_bBackgroundManagerAdded;
72 typedef std::vector<BackgroundWorkerThread*> ThreadContainer;
73 ThreadContainer m_vecpThreads;
74 wxCriticalSection m_critsectThreads;
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,
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
89 BackgroundSupervisor (SupervisorThread* pMyThread, wxWindow* pParentFrame, BackgroundProcessingDocument* pDocument, wxChar const* pszProcessTitle,
92 BackgroundSupervisor ()
93 : wxEvtHandler(), m_iTotalUnits(0)
96 virtual ~BackgroundSupervisor();
98 virtual BackgroundWorkerThread* createWorker (int iThread, int iStartUnit, int iNumUnits)
102 virtual void onDone() {};
104 virtual void onCancel();
106 virtual void onWorkerFail(int iThread, const wxString& strFailMessage);
107 virtual void onWorkerUnitTick();
108 virtual void onWorkerDone(int iThread);
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;}
120 int getNumWorkers() const { return m_iNumThreads; }
121 double getTimerEnd() { return m_pTimer->timerEnd(); }
123 DECLARE_EVENT_TABLE()
127 class BackgroundWorkerThread : public wxThread {
129 BackgroundSupervisor* m_pSupervisor;
131 const int m_iStartUnit;
132 const int m_iNumUnits;
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)
140 #endif // _BACKGROUNDSUPR_H_