r577: no message
[ctsim.git] / src / threadrecon.cpp
index 03d4d39d37987dd4bbc18277f0c2d6f07b65d614..0fd94b293460292533316dd82abbdab4ad0b17eb 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2001 Kevin Rosenberg
 **
-**  $Id: threadrecon.cpp,v 1.6 2001/02/23 18:56:56 kevin Exp $
+**  $Id: threadrecon.cpp,v 1.7 2001/02/23 21:58:32 kevin Exp $
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
@@ -45,7 +45,7 @@
 
 IMPLEMENT_DYNAMIC_CLASS(ReconstructorSupervisor, BackgroundSupervisor)
 BEGIN_EVENT_TABLE(ReconstructorSupervisor, BackgroundSupervisor)
-EVT_MENU(RECONSTRUCTION_THREAD_EVENT, ReconstructorSupervisor::OnThreadEvent)
+EVT_MENU(MSG_BACKGROUND_SUPERVISOR_CANCEL, ReconstructorSupervisor::OnCancel)
 EVT_MENU(MSG_WORKER_THREAD_FAIL, ReconstructorSupervisor::OnWorkerFail)
 EVT_MENU(MSG_WORKER_THREAD_DONE, ReconstructorSupervisor::OnWorkerDone)
 EVT_MENU(MSG_WORKER_THREAD_UNIT_TICK, ReconstructorSupervisor::OnWorkerUnitTick)
@@ -56,13 +56,13 @@ ReconstructorSupervisor::ReconstructorSupervisor (ProjectionFileView* pProjView,
                                               int iImageNX, int iImageNY, const char* pszFilterName, double dFilterParam, const char* pszFilterMethod, 
                                               int iZeropad, const char* pszFilterGenerationName, const char* pszInterpName, int iInterpParam,
                                               const char* pszBackprojectName, const char* const pszLabel)
-                                              : m_pProjView(pProjView), m_pDialogProgress(NULL), m_pGauge(NULL), m_bFail(false), m_iNumThreads(0), m_iImageNX(iImageNX), 
+                                              : m_pDialogProgress(NULL), m_bFail(false), m_iNumThreads(0), m_iImageNX(iImageNX), 
                                               m_iImageNY(iImageNY), m_strLabel(pszLabel), m_pTimer(NULL), m_bCancelled(false), m_bCancelling(false), 
                                               BackgroundSupervisor()
 {
   m_iNumThreads = theApp->getNumberCPU();
   //  ++m_iNumThreads;
-  m_iTotalViews = m_pProjView->GetDocument()->getProjections().nView();
+  m_iTotalViews = pProjView->GetDocument()->getProjections().nView();
   int iBaseViews = m_iTotalViews / m_iNumThreads;
   int iExtraViews = m_iTotalViews % m_iNumThreads;
   
@@ -83,7 +83,16 @@ ReconstructorSupervisor::ReconstructorSupervisor (ProjectionFileView* pProjView,
       break;
     }
   }
-  
+  m_pProjView = pProjView;
+  m_pProjDoc = pProjView->GetDocument();
+}
+
+// Static function
+void 
+BackgroundSupervisor::cancelSupervisor (BackgroundSupervisor* pSupervisor)
+{
+  wxCommandEvent cancelEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_CANCEL);
+  wxPostEvent (pSupervisor, cancelEvent);
 }
 
 
@@ -93,13 +102,17 @@ ReconstructorSupervisor::start()
   if (m_bFail)
     return false;
   
-  m_pProjView->GetDocument()->addReconstructor (this);
   if (! theApp->getUseBackgroundTasks())
     m_pDialogProgress = new wxProgressDialog (_T("Filtered Backprojection"), _T("Reconstruction Progress"), m_iTotalViews, m_pProjView->getFrame(), wxPD_CAN_ABORT | wxPD_AUTO_HIDE);
   else {
     std::string strLabel ("Reconstructing ");
     strLabel += m_pProjView->GetFrame()->GetTitle();
-    m_pGauge = theApp->getBackgroundManager()->addTask (this, m_iTotalViews, strLabel.c_str());
+    wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_ADD);
+    addTaskEvent.SetString (strLabel.c_str());
+    addTaskEvent.SetInt (m_iTotalViews);
+    addTaskEvent.SetClientData (this);
+    wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
+    wxPostEvent (m_pProjDoc, addTaskEvent);
   }
   
   m_iRunning = m_iNumThreads;
@@ -109,15 +122,12 @@ ReconstructorSupervisor::start()
   // starting all threads
   for (int i = 0; i < m_iNumThreads; i++)
     m_vecpThread[i]->Run();
-  
-  if (m_bCancelled)
-    return false;
-  
+    
   return true;
 }
 
 void
-ReconstructorSupervisor::cancel()
+ReconstructorSupervisor::OnCancel(wxCommandEvent& event)
 {
   if (isDone() || m_bCancelled)
     return;
@@ -138,23 +148,25 @@ ReconstructorSupervisor::cleanUp()
       m_vecpThread[i]->Pause();
       m_vecpThread[i]->Delete();
     }
-  m_critsectThreadContainer.Leave();
     
   for (i = 0; i < m_iNumThreads; i++) {
       delete m_vecpChildImageFile[i];
       m_vecpChildImageFile[i] = NULL;
     }    
+  m_critsectThreadContainer.Leave();
     
     m_iNumThreads = 0;
     m_iRunning = 0;
     delete m_pDialogProgress;
     delete m_pTimer;
     m_pDialogProgress = NULL;
-    m_pGauge = NULL;
     setDone();
-    m_pProjView->GetDocument()->removeReconstructor (this);
-    theApp->getBackgroundManager()->taskDone (this);
-    
+
+    wxCommandEvent doneEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_REMOVE);
+    doneEvent.SetClientData (this);
+    wxPostEvent (theApp->getBackgroundManager(), doneEvent);
+    wxPostEvent (m_pProjDoc, doneEvent);
+
     cleanSection.Leave();
 }
 
@@ -164,7 +176,7 @@ ReconstructorSupervisor::onDone()
   wxCriticalSection doneSection;
   doneSection.Enter();
   
-  m_pProjView->GetDocument()->removeReconstructor (this);
+
   ImageFileDocument* pReconDoc = theApp->newImageDoc();
   if (! pReconDoc) {
     sys_error (ERR_SEVERE, "Unable to create image file");
@@ -202,32 +214,32 @@ ReconstructorSupervisor::OnWorkerUnitTick (wxCommandEvent& event)
     
     if (m_pDialogProgress)
       m_bCancelling = ! m_pDialogProgress->Update (m_iViewsDone - 1);
-    else if (m_pGauge) {
-      m_pGauge->SetValue (m_iViewsDone - 1); 
-      m_bCancelling = theApp->getBackgroundManager()->isCancelling(this);
-    }
-    if (! isDone() && m_bCancelling) {
-      cancel();
-      return;
+    else {
+      wxCommandEvent addTaskEvent (wxEVT_COMMAND_MENU_SELECTED, MSG_BACKGROUND_SUPERVISOR_UNIT_TICK);
+      addTaskEvent.SetInt (m_iViewsDone - 1);
+      addTaskEvent.SetClientData (this);
+      wxPostEvent (theApp->getBackgroundManager(), addTaskEvent);
     }
 }
 
 void
 ReconstructorSupervisor::OnWorkerDone (wxCommandEvent& event)
 {
-  if (isDone())
-    return;
-
   m_iRunning--;
+  wxASSERT (m_iRunning >= 0);
   m_critsectThreadContainer.Enter();
   m_vecpThread[event.GetInt()] = NULL;
   m_critsectThreadContainer.Leave();
-  wxString msg;
-  msg.Printf("Reconstructor Supervisor: Thread finished. Remaining threads: %d\n", m_iRunning);  
-  wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
-  eventLog.SetString( msg );
-  wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
 
+#ifdef DEBUG
+  if (theApp->getVerboseLogging()) {
+    wxString msg;
+    msg.Printf("Reconstructor Supervisor: Thread finished. Remaining threads: %d\n", m_iRunning);  
+    wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
+    eventLog.SetString( msg );
+    wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
+  }
+#endif
   if (m_iRunning <= 0) {
     onDone();
     return;
@@ -252,15 +264,6 @@ ReconstructorSupervisor::OnWorkerFail (wxCommandEvent& event)
 }
 
 
-void
-ReconstructorSupervisor::OnThreadEvent (wxCommandEvent& event)
-{
-      wxString msg;
-      msg.Printf("Reconstructor Supervisor: Received event #%d\n", event.GetInt());  
-      wxCommandEvent eventLog( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
-      eventLog.SetString( msg );
-      wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
-}
 
 ImageFile*
 ReconstructorSupervisor::getImageFile()